Skip to main content

RL 001

· 25 min read

Reinforcement Learning​

  • Learn how to make a good sequence of decisions by interacting with the environment.

Overview of Reinforcement Learning

Characteristics of RL​

  • Trial and Error: Based learning approach.
  • Optimization: Find good sequences of actions or decisions.
  • Delayed Consequences/Rewards: Takes time to relize the actions or decisions are good or bad.
  • Exploration: Learn by making decisions or forming actions or through experiences. Trying new actions to discover their effects.
  • Exploitation: Choosing actions with the highest expected reward, based on current knowledge.
  • Generalization: Use previous experiences or knowledge to new or unseen situations effectively.

Framwork of RL​

  • OpenAI Gym
  • Torch RL
  • AWS DeepRacer

RL Math​

Probability​

  • Sample Space (Ω\Omega): The set of all possible outcomes of a random experiment.

Bonferroni's Inequality​

P(A∩B)≥P(A)+P(B)−1P(A \cap B) \geq P(A) + P(B) - 1 P(∩i=1nAi)≥∑i=1nP(Ai)−(n−1)P(\cap^{n}_{i=1} A_i) \geq \sum_{i=1}^{n} P(A_i) - (n-1)

  • Gives a lower bound on the intersection probability which is useful when this probability is difficult to compute directly.
  • It is useful when the probabilities of individual events are sufficiently large.

Boole's Inequality​

P(∪i=1nAi)≤∑i=1nP(Ai)P(\cup^{n}_{i=1} A_i) \leq \sum_{i=1}^{n} P(A_i)

for any sets A1,A2,...,AnA_1, A_2, ..., A_n.

  • It is useful when finding an upper bound for the probabilities of the union of events.

Bayes' Rule​

P(A∣B)=P(A∩B)P(B)P(A|B) = \frac{P(A \cap B)}{P(B)} P(A∩B)=P(B∣A)P(A)=P(A∣B)P(B)P(A\cap B) = P(B|A) P(A) = P(A|B) P(B) P(A∣B)P(B)=P(B∣A)P(A)P(A|B)P(B) = P(B|A)P(A) P(A∣B)=P(B∣A)P(A)P(B)P(A|B) = \frac{P(B|A) P(A)}{P(B)}

  • It allows us to compute the conditional probability P(A∣B)P(A|B) from the inverse conditional probability P(B∣A)P(B|A).
  • Let A1,A2,...,AnA_1, A_2, ..., A_n be a partition of the sample space SS. Then let BB be any subset of SS we have:

P(Ai∣B)=P(B∣Ai)P(Ai)∑j=1∞P(B∣Aj)P(Aj)P(A_i|B) = \frac{P(B|A_i) P(A_i)}{\sum_{j=1}^{\infty} P(B|A_j) P(A_j)}

Independent Events​

P(A∩B)=P(A)P(B)P(A \cap B) = P(A) P(B)

  • A family Ai:i∈IA_i :i \in I of events is independent if for every finite subset J⊆IJ \subseteq I we have: P(⋂i∈JAi)=∏i∈JP(Ai)P\left(\bigcap_{i \in J} A_i\right) = \prod_{i \in J} P(A_i)

  • The pair-wise independence of events does not imply their mutual independence.

Conditional Independence​

P(A∩B∣C)=P(A∣C)P(B∣C)P(A \cap B|C) = P(A|C) P(B|C)

  • where P(C)>0P(C) > 0.
  • or equivalently, P(A∣B∩C)=P(A∣C)P(A|B \cap C) = P(A|C) and P(B∣A∩C)=P(B∣C)P(B|A \cap C) = P(B|C).

Induced Probability Function​

PX(xi)=P({wj∈Ω:X(wj)=xi})P_X(x_i) = P\big(\{w_j \in \Omega : X(w_j) = x_i\}\big)

  • Ω={w1,w2,...,wm}\Omega = \{w_1, w_2, ..., w_m\} is the sample space.
  • XX is a random variable with range X={x1,x2,...,xn}X = \{x_1, x_2, ..., x_n\}.
  • The result set {wj∈Ω:X(wj)=xi}\{w_j \in \Omega : X(w_j) = x_i\} is the set of all outcomes in the sample space that map to the value xix_i under the random variable XX.

Cumulative Distribution Function (CDF)​

FX(x)=PX(X≤x)∀x∈R F_X(x) = P_X(X \leq x) \quad \forall x \in \mathbb{R}

  • FX(x)F_X(x) is a cdf   ⟺  \iff the following conditions hold:
    • Monotonicity: FX(x)F_X(x) is non-decreasing.
    • Limiting values: lim⁡x→−∞FX(x)=0\lim_{x \to -\infty} F_X(x) = 0 and lim⁡x→∞FX(x)=1\lim_{x \to \infty} F_X(x) = 1.
    • Right-Continuity: lim⁡y↓xFX(y)=FX(x)∀x∈R\lim_{y \downarrow x} F_X(y) = F_X(x) \quad \forall x \in \mathbb{R}.
F_X(t)

1.0 | ●────────────
| ↑ ↑ ↑
| 3.001 3.01 3.1
0.7 | ●─────────○
| ↑ ↑ ↑
| 2.001 2.01 2.1
0.3 | ●────────○
| ↑ ↑ ↑
| 1.001 1.01 1.1
0.0 |─────○
+-------------------------------------- t
1 2 3
x x x

Continuous & Discrete Random Variables​

  • if XX is continuous, then FX(x)F_X(x) is continuous and differentiable almost everywhere. The probability density function (pdf) is defined as: fX(x)=ddxFX(x)f_X(x) = \frac{d}{dx} F_X(x)
  • if XX is discrete, then FX(x)F_X(x) is a step function and the probability mass function (pmf) is defined as: pX(xi)=P(X=xi)p_X(x_i) = P(X = x_i)
1 ────────────────────────━━━━━━━━
╱
╱
╱
╱
0 ━━━━━━━━━━━━━━━───────────────── x
1 ─────────────────────────●━━━━━━
│
0.7 ─────────────●━━━━━━━━━○
│
0.3 ─────●━━━━━━━○
│
0 ━━━━━━━○──────────────────────── x
1 2 3
1 ──────────────────────────━━━━━━
╱
╱
0.7 ───────────●━━━━━━━
│
0.5 ───────────○
╱
╱
0 ━━━━━━━━──────────────────────── x
a

Probability Mass Function (PMF)​

fX(x)={(1−p)x−1p,x=1,2,3,…0,otherwisef_X(x)= \begin{cases} (1-p)^{x-1}p, & x=1,2,3,\ldots \\ 0, & \text{otherwise} \end{cases}
  • A discrete random variable XX is given by fX(x)=P(X=x)f_X(x) = P(X=x) for x∈Rx \in \mathbb{R}.
  • It represents the probability that the first success occurs exactly on the xx-th trial in a sequence of independent trials.

Probability Density Function (PDF)​

FX(x)=∫−∞xfX(t)dt∀x∈RF_X(x) = \int_{-\infty}^{x} f_X(t) dt \quad \forall x \in \mathbb{R}
  • A continuous random variable XX is given by fX(x)f_X(x) for x∈Rx \in \mathbb{R}.
  • The probability is calculated as the area under the probability density function over a specific interval.

Expectation​

E[X]=∑xP(X=x)if X is discreteE[X] = \sum x P(X=x) \quad \text{if } X \text{ is discrete}

E[X]=∫−∞∞xfX(x)dxif X is continuousE[X] = \int_{-\infty}^{\infty} x f_X(x) dx \quad \text{if } X \text{ is continuous}

  • Linearity: E[aX+bY+c]=aE[X]+bE[Y]+cE[aX + bY + c] = aE[X] + bE[Y] + c for any constants aa, bb, and cc.
  • Non-negativity: If X≥0X \geq 0 then E[X]≥0E[X] \geq 0 because it is a weighted average of non-negative values.
  • Monotonicity: If X≥YX \geq Y then E[X]≥E[Y]E[X] \geq E[Y] because it is a weighted average of values that are greater than or equal to the corresponding values of YY.
  • Boundedness: If a≤X≤ba \leq X \leq b then a≤E[X]≤ba \leq E[X] \leq b because it is a weighted average of values that are bounded by aa and bb.
a ●──────────────●──────────────● b
가능한 값들 평균
E[X]

Moments​

μn′=E[Xn]∀n∈N\mu'_n = E[X^n] \quad \forall n \in \mathbb{N}

  • The nthn^{th} central moment of XX is: μn=E[(X−E[X])n]=E(X−μ)n\mu_n = E[(X - E[X])^n] = E(X - \mu)^n

  • 1th central moment is the mean, μ1=E[X]\mu_1 = E[X].

  • 2th central moment is the variance, μ2=E[(X−E[X])2]=Var(X)\mu_2 = E[(X - E[X])^2] = \text{Var}(X).

    • It emphasizes the variability of the distribution.
    • Var(aX+b)=a2Var(X)\text{Var}(aX + b) = a^2 \text{Var}(X) for any constants aa and bb.
  • 3th central moment is the skewness, μ3=E[(X−E[X])3]\mu_3 = E[(X - E[X])^3].

    • It emphasizes the skewness of the distribution.
  • 4th central moment is the kurtosis, μ4=E[(X−E[X])4]\mu_4 = E[(X - E[X])^4].

    • It emphasizes the tail behavior and extreme values of the distribution.

Covariance​

cov(X, Y)=E[(X−E[X])(Y−E[Y])] \text{cov(X, Y)} = E[(X - E[X])(Y - E[Y])]

  • It measures how muc htwo random variables change together.
  • Negative Covariance
  • Near Zero Covariance
  • Positive Covariance
Y: 시험 점수

높음 | ●
| ● ●
| ●
| ●
낮음 | ●
+-------------------- X: 공부 시간
적음 많음

Cov(X,Y) > 0

Correlation​

p(X,Y)=cov(X, Y)Var(X)Var(Y) p(X, Y) = \frac{\text{cov(X, Y)}}{\sqrt{\text{Var}(X)\text{Var}(Y)}}

  • Individual variances must be non-zero.
  • p(X,Y)p(X, Y) lies in the range [−1,1][-1, 1].

Joint Distributions​

fX,Y:R2→[0,1],fX,Y(x,y)=P(X=x,Y=y)if X,Y are discretef_{X,Y}:\mathbb{R}^2\to[0,1], \\ f_{X,Y}(x,y) = P(X=x, Y=y) \quad \text{if } X, Y \text{ are discrete}

  • Joint Probability Mass Function (PMF) for discrete random variables XX and YY.
Y
1 ┤ ● 1/4 ● 1/4
│ (0,1) (1,1)
0 ┤ ● 1/4 ● 1/4
│ (0,0) (1,0)
└──────────────────── X
0 1

Marginal Distributions​

fX(x)=∑yfX,Y(x,y)fY(y)=∑xfX,Y(x,y)f_X(x) = \sum_{y} f_{X,Y}(x,y) \\ f_Y(y) = \sum_{x} f_{X,Y}(x,y)

  • Fixing one variable and summing over the other variable gives the marginal distribution of the fixed variable.
Y=0 Y=1 행의 합
X=0 0.10 0.20 0.30
X=1 0.30 0.40 0.70
─────────────────────────
열의 합 0.40 0.60 1.00
  • fX(0)=0.10+0.20=0.30f_X(0) = 0.10 + 0.20 = 0.30: sum of the first row.
  • fX(1)=0.30+0.40=0.70f_X(1) = 0.30 + 0.40 = 0.70: sum of the second row.
  • fY(0)=0.10+0.30=0.40f_Y(0) = 0.10 + 0.30 = 0.40: sum of the first column.
  • fY(1)=0.20+0.40=0.60f_Y(1) = 0.20 + 0.40 = 0.60: sum of the second column.
  • Marginalization: It sums or integrates over all possible values of an unwanted variable to obtain the distribution of the variable of interest.

Joint and Marginal Distributions

Conditional Distributions​

fX∣Y(x∣y)=P(X=x∣Y=y)=fX,Y(x,y)fY(y)if fY(y)>0f_{X|Y}(x|y) = P(X = x | Y = y) = \frac{f_{X,Y}(x,y)}{f_Y(y)} \quad \text{if } f_Y(y) > 0

  • It represents the probability distribution of XX given that YY has a specific value.
  • If fY(y)=0f_Y(y) = 0, then fX∣Y(x∣y)f_{X|Y}(x|y) is undefined.

Conditional Distribution

Bernoulli Distribution​

X={1,with probability p0,with probability 1−pX = \begin{cases} 1, & \text{with probability } p \\ 0, & \text{with probability } 1-p \end{cases}

  • E[X]=pE[X] = p
  • Var(X)=p(1−p)\text{Var}(X) = p(1-p)
  • The outcome of a Bernoulli trial is either a success (1) or a failure (0).

Binomial Distribution​

  • probability of success in a single trial: pp
  • probability of failure in a single trial: q=1−pq = 1 - p

P(X=x∣n,p)=(nx)pxqn−xwhere(nx)=n!x!(n−x)!,0≤x≤n P(X = x|n, p) = \binom{n}{x} p^x q^{n-x} \\ \text{where} \binom{n}{x} = \frac{n!}{x!(n-x)!}, \quad 0 \leq x \leq n

  • E[X]=npE[X] = np
  • Var(X)=npq=np(1−p)\text{Var}(X) = npq = np(1-p)
  • The binomial distribution describes the number of successes in a fixed number of independent Bernoulli trials.
  • X∼Binomial(10,0.7)X \sim \text{Binomial}(10, 0.7)
    • P(X=7)=(107)(0.7)7(0.3)3≈0.2668P(X = 7) = \binom{10}{7} (0.7)^7 (0.3)^3 \approx 0.2668

Geometric Distribution​

  • probability of success in a single trial: pp
  • probability of failure in a single trial: q=1−pq = 1 - p

P(X=x∣p)=(qx−1)pwhere x=1,2,3,...P(X = x|p) = (q^{x-1}) p \\ \text{where } x = 1, 2, 3, ...

  • E[X]=1pE[X] = \frac{1}{p}
  • Var(X)=qp2\text{Var}(X) = \frac{q}{p^2}
  • The geometric distribution describes the number of trials before the first success in a sequence of independent Bernoulli trials.
  • X∼Geometric(0.7)X \sim \text{Geometric}(0.7)
    • P(X=3)=(0.3)2(0.7)≈0.063P(X = 3) = (0.3)^2 (0.7) \approx 0.063

Uniform Distribution​

fX(x∣a,b)={1b−a,a≤x≤b0,otherwisef_X(x|a,b) = \begin{cases} \frac{1}{b-a}, & a \leq x \leq b \\ 0, & \text{otherwise} \end{cases}

  • E[X]=a+b2E[X] = \frac{a+b}{2}
  • Var(X)=(b−a)212\text{Var}(X) = \frac{(b-a)^2}{12}
  • The uniform distribution describes a continuous random variable that has an equal probability of taking any value within a specified range.
  • The probability depends on the length of the interval, not its location.

Normal Distribution​

fX(x)=12πσ2e−(x−μ)22σ2,−∞<x<∞f_X(x) = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}, \quad -\infty < x < \infty

X∼N(μ,σ2)X \sim N(\mu, \sigma^2)

  • Central Limit Theorem: the distribution of the sum (or average) of a large number of independent, identically distributed variables will be approximately normal, regardless of the underlying distribution.

Xˉ=1n∑i=1nXi≈N(μ,σ2n)\bar{X} = \frac{1}{n} \sum_{i=1}^{n} X_i \approx N\left(\mu, \frac{\sigma^2}{n}\right)

Multivariate Normal Distribution​

N(x∣μ,Σ)=1(2π)D∣Σ∣e−12(x−μ)TΣ−1(x−μ)N(x|\mu, \Sigma) = \frac{1}{\sqrt{(2\pi)^D |\Sigma|}} e^{-\frac{1}{2} (x-\mu)^T \Sigma^{-1} (x-\mu)}

  • μ\mu is the DD-dimensional mean vector.
  • Σ\Sigma is the D×DD \times D covariance matrix.
  • ∣Σ∣|\Sigma| is the determinant of the covariance matrix.

Beta Distribution​

f(x∣α,β)=Γ(α+β)Γ(α)Γ(β)xα−1(1−x)β−1,0<x<1f(x|\alpha, \beta) = \frac{\Gamma(\alpha + \beta)}{\Gamma(\alpha)\Gamma(\beta)} x^{\alpha - 1} (1 - x)^{\beta - 1}, \quad 0 < x < 1

  • Γ(n)=(n−1)!\Gamma(n) = (n-1)! is the gamma function.
  • E[X]=αα+βE[X] = \frac{\alpha}{\alpha + \beta}
  • Var(X)=αβ(α+β)2(α+β+1)\text{Var}(X) = \frac{\alpha \beta}{(\alpha + \beta)^2 (\alpha + \beta + 1)}
α < β α = β α > β

높이 높이 높이
│╲ │ ╭──╮ │ ╱
│ ╲___ │ ╭─╯ ╰─╮ │ ___╱
└──────── x └────────── x └──────── x
0 1 0 1 0 1

0 쪽 강조 가운데 강조 1 쪽 강조
Beta(8,2) Beta(80,20)

╭────╮ /\
╭─╯ ╰─╮ / \
─────╯ ╰─── ─────/────\─────
0.8 0.8

불확실성 큼 불확실성 작음
  • p∣data∼Beta(α+s,β+f)p | \text{data} \sim \text{Beta}(\alpha + s, \beta + f)
    • where new observations are ss successes and ff failures.
  • The Beta distribution is a distribution of success probabilities for a Bernoulli or Binomial distribution.
  • Application
    • Measuring uncertainty in the probability of success for a Bernoulli or Binomial distribution.
    • Conversion rate or click-through rate (CTR) in online advertising.
    • Defect rate in manufacturing.

Linear Algebra​

Axioms of Vector Spaces​

  • Commutative Law: u+v=v+u,∀u,v∈Vu + v = v + u, \quad \forall u, v \in V
  • Associative Law: (u+v)+w=u+(v+w),∀u,v,w∈V(u + v) + w = u + (v + w), \quad \forall u, v, w \in V
  • Additive Identity: ∃0∈V\exists 0 \in V such that v+0=v,∀v∈Vv + 0 = v, \quad \forall v \in V
  • Additive Inverse: ∀v∈V,∃−v∈V\forall v \in V, \exists -v \in V such that v+(−v)=0v + (-v) = 0
  • Distributive Law:
    • a(u+v)=au+av,∀a∈F,u,v∈Va(u + v) = au + av, \quad \forall a \in F, u, v \in V
    • (a+b)v=av+bv,∀a,b∈F,v∈V(a + b)v = av + bv, \quad \forall a, b \in F, v \in V
  • Associative Law: (ab)v=a(bv),∀a,b∈F,v∈V(ab)v = a(bv), \quad \forall a, b \in F, v \in V
  • Unitary Law: 1v=v,∀v∈V1v = v, \quad \forall v \in V

Subspace​

  • x+αy∈W,∀x,y∈W,∀α∈Rx + \alpha y \in W, \quad \forall x, y \in W, \forall \alpha \in \mathbb{R}

Norm​

f:Rn→Rf: \mathbb{R}^n \to \mathbb{R}

  • It outputs a real number that represents the length or size of a vector in a vector space.

x=(x1,x2,…,xn)↦f(x)=∥x∥x=(x_1,x_2,\ldots,x_n) \mapsto f(x)=\lVert x\rVert

  • Non-negativity: ∀x∈Rn,∥x∥≥0\forall x \in \mathbb{R}^n, \quad \lVert x\rVert \geq 0
  • Definiteness: f(x)=0  ⟺  x=0f(x) = 0 \iff x = 0
  • Homogeneity: ∀x∈Rn,∀t∈R,f(tx)=∣t∣f(x)\forall x \in \mathbb{R}^n, \quad \forall t \in \mathbb{R}, f(tx) = |t| f(x)
  • Triangle inequality: ∀x,y∈Rn,f(x+y)≤f(x)+f(y)\forall x,y \in \mathbb{R}^n, \quad f(x+y) \leq f(x) + f(y)
    • ∥x+y∥≤∥x∥+∥y∥\lVert x + y \rVert \leq \lVert x \rVert + \lVert y \rVert
  • A norm is non-negative, only the zero vector has a norm of zero, scaling a vector by two doubles its norm, and the direct distance cannot be greater than the detoured distance.

∥x∥p=(∑i=1n∣xi∣p)1p,p≥1\lVert x \rVert_p = (\sum_{i=1}^{n} |x_i|^p)^{\frac{1}{p}}, \quad p \geq 1

  • L1 norm: ∥x∥1=∑i=1n∣xi∣\lVert x \rVert_1 = \sum_{i=1}^{n} |x_i|
    • Manhattan distance.
  • L2 norm: ∥x∥2=∑i=1n∣xi∣2\lVert x \rVert_2 = \sqrt{\sum_{i=1}^{n} |x_i|^2}
    • Euclidean distance.
  • L-infinity norm: ∥x∥∞=max⁡i∣xi∣\lVert x \rVert_\infty = \max_i|x_i|
    • ∥(3,4)∥∞=max⁡{3,4}=4\lVert (3, 4) \rVert_\infty = \max\{3, 4\} = 4

∥A∥F=∑i=1m∑j=1n∣aij∣2=tr(ATA)\lVert A \rVert_F = \sqrt{\sum_{i=1}^{m} \sum_{j=1}^{n} |a_{ij}|^2} = \sqrt{\text{tr}(A^T A)}

  • Frobenius norm: A=[1234]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}
    • ∥A∥F=12+22+32+42=30\lVert A \rVert_F = \sqrt{1^2 + 2^2 + 3^2 + 4^2} = \sqrt{30}
    • It is the square root of the sum of the absolute squares of its elements.

Span​

span{x1,x2,...,xn}={α1x1+α2x2+...+αnxn:αn∈R}span\{x_1, x_2, ..., x_n\} = \{ \alpha_1 x_1 + \alpha_2 x_2 + ... + \alpha_n x_n : \alpha_n \in \mathbb{R}\}

  • A set of vertors X={x1,x2,...,xn}X = \{x_1, x_2, ..., x_n\} in a vector space VV is said to span VV if every vector in VV can be expressed as a linear combination of the vectors in XX.
  • Span is the entire region that can be reached by scaling and adding the given vectors.

Range​

R(A)={α1a1+α2a2+...+αnan:αn∈R}R(A) = \{\alpha_1 a_1 + \alpha_2 a_2 + ... + \alpha_n a_n : \alpha_n \in \mathbb{R}\}

  • The range of a matrix AA is the set of all possible linear combinations of its column vectors.
  • Same as the columnspace of the matrix AA.

Nullspace​

N(A)={x∈Rn:Ax=0}N(A) = \{x \in \mathbb{R}^n : Ax = 0\}

  • A set of all vectors that equal 0 when multiplied by the matrix AA.
  • The dimensionality of the nullspace is called the nullity of the matrix AA.

Linear Independence​

  • A set of vectors {v1,v2,...,vn}\{v_1, v_2, ..., v_n\} is said to be linearly independent if the only solution to the equation α1v1+α2v2+...+αnvn=0\alpha_1 v_1 + \alpha_2 v_2 + ... + \alpha_n v_n = 0 is α1=α2=...=αn=0\alpha_1 = \alpha_2 = ... = \alpha_n = 0.
  • Column rank: The maximum number of linearly independent column vectors in a matrix.
  • Row rank: The maximum number of linearly independent row vectors in a matrix.
  • If one row vector is a combination of other row vectors, then it is linearly dependent.
  • The number of rows that are removed due to redundancy is the Rank of the matrix.
  • Due to the redundancy, the number of directions which is vanishing is the Nullity of the matrix.

Rank​

Rank(A)=dim(R(A)),A∈Rm×nRank(A) = \text{dim}(R(A)), \quad A \in \mathbb{R}^{m \times n}

  • rank(A)≤min⁡(m,n),A∈Rm×nrank(A) \leq \min(m, n), \quad A \in \mathbb{R}^{m \times n}
    • if rank(A)=min(m,n)rank(A) = min(m, n), then AA is said to be full rank.
    • Rank cannot exceed the number of rows or columns in the matrix.
  • rank(A)=rank(AT),A∈Rm×nrank(A) = rank(A^T), \quad A \in \mathbb{R}^{m \times n}
    • The rank of a matrix is equal to the rank of its transpose.
    • The amount of independent information in the rows is equal to the amount of independent information in the columns.
  • rank(AB)≤min⁡(rank(A),rank(B)),A∈Rm×n,B∈Rn×prank(AB) \leq \min(rank(A), rank(B)), \quad A \in \mathbb{R}^{m \times n}, B \in \mathbb{R}^{n \times p}
    • The information content of the product of two matrices cannot exceed the information content of either matrix.
  • rank(A+B)≤rank(A)+rank(B),A,B∈Rm×nrank(A + B) \leq rank(A) + rank(B), \quad A, B \in \mathbb{R}^{m \times n}
    • The information content of the sum of two matrices cannot exceed the sum of the information content of each matrix.

Orthogonal Matrices​

U∈Rn×n is orthogonal   ⟺  viTvj={1if i=j0if i≠jU \in \mathbb{R}^{n \times n} \text{ is orthogonal } \iff v_i^T v_j = \begin{cases} 1 & \text{if } i = j \\ 0 & \text{if } i \neq j \end{cases}

  • UUT=UTU=InUU^T = U^TU = I_n
  • ∥Ux∥2=∥x∥2,∀x∈Rn\lVert Ux \rVert_2 = \lVert x \rVert_2, \quad \forall x \in \mathbb{R}^n
  • Orthonormal: A set of vectors is orthonormal if they are all unit vectors and orthogonal to each other.
  • Rotation: R(θ)=[cos⁡θ−sin⁡θsin⁡θcos⁡θ]R(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}
  • Reflection: R=[100−1]R = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}

Quadratic Form​

Q(x)=xTAx,A∈Rn×nQ(x) = x^T A x, \quad A \in \mathbb{R}^{n \times n}

  • The quadratic form xTAxx^TAx gives a scalar value that measures the cost, energy, or weighted magnitude of the vextor xx, according to the quadratic surface defined by the matrix AA.
    • Positive definite: Q(x)>0,∀x≠0Q(x) > 0, \quad \forall x \neq 0
    • Positive semi-definite: Q(x)≥0,∀x≠0Q(x) \geq 0, \quad \forall x \neq 0
    • Negative definite: Q(x)<0,∀x≠0Q(x) < 0, \quad \forall x \neq 0
    • Negative semi-definite: Q(x)≤0,∀x≠0Q(x) \leq 0, \quad \forall x \neq 0
    • Indefinite: Q(x)Q(x) can be positive or negative for different x≠0x \neq 0

Matrix definiteness

  • 행렬의 정부호성(양의 정부호, 양의 준정부호, 음의 정부호, 음의 준정부호, 부정정부호)

ATA=ATA  ⟹  Q(x)=xTATAx=(Ax)T(Ax)=∥Ax∥2≥0A^TA = A^TA \implies Q(x) = x^T A^T A x = (Ax)^T (Ax) = \lVert Ax \rVert^2 \geq 0

  • Always positive semi-definite because it is the square of the norm of the vector AxAx.

Eigenvalues & Eigenvectors​

λ∈R is an eigenvalue of A∈Rn×n  ⟺  Ax⃗=λx⃗,x⃗∈Rn,x⃗≠0\lambda \in \mathbb{R} \text{ is an eigenvalue of } A \in \mathbb{R}^{n \times n} \iff A\vec{x} = \lambda \vec{x} , \quad \vec{x} \in \mathbb{R}^n, \vec{x} \neq 0

  • An eigenvector is a nonzero vector that remains on the same line when transformed by a matrix.
  • Its eigenvalue tells us how much the vector is scaled and whether its direction is reversed.

det(A−λI)=0\text{det}(A - \lambda I) = 0

  • A=[4123]A = \begin{bmatrix} 4 & 1 \\ 2 & 3 \end{bmatrix}
  • A−λI=[4−λ123−λ]A - \lambda I = \begin{bmatrix} 4 - \lambda & 1 \\ 2 & 3 - \lambda \end{bmatrix}
  • det(A−λI)=(4−λ)(3−λ)−2=λ2−7λ+10=(λ−5)(λ−2)=0\text{det}(A - \lambda I) = (4 - \lambda)(3 - \lambda) - 2 = \lambda^2 - 7\lambda + 10 = (\lambda - 5)(\lambda - 2) = 0
  • λ1=5,λ2=2\lambda_1 = 5, \quad \lambda_2 = 2

tr(A)=∑i=1nλi\text{tr}(A) = \sum_{i=1}^{n} \lambda_i

  • The trace is the sum of the scaling factors along all eigenvector directions.

det(A)=∏i=1nλi\text{det}(A) = \prod_{i=1}^{n} \lambda_i

  • The determinant is the product of the eigenvalues, representing the overall signed scaling factor for area or volume.

rank(A)=number of non-zero eigenvalues of Arank(A) = \text{number of non-zero eigenvalues of } A

  • For a diagonalizable matrix, the rank equals the number of nonzero eigenvalues, counted with multiplicity.

λi(A−1)=1λi(A)\lambda_i (A^{-1}) = \frac{1}{\lambda_i(A)}

  • The eigenvalues of the inverse of a matrix are the reciprocals of the eigenvalues of the original matrix.

Ax⃗=λx⃗  ⟹  A−1Ax⃗=A−1λx⃗  ⟹  x⃗=λA−1x⃗  ⟹  A−1x⃗=1λx⃗ A \vec{x} = \lambda \vec{x} \\ \implies A^{-1} A \vec{x} = A^{-1} \lambda \vec{x} \\ \implies \vec{x} = \lambda A^{-1} \vec{x} \\ \implies A^{-1} \vec{x} = \frac{1}{\lambda} \vec{x}

Diagonalization​

S=[⋮⋮⋯⋮v1⃗v2⃗⋯vn⃗⋮⋮⋯⋮]S = \begin{bmatrix} \vdots & \vdots & \cdots & \vdots \\ \vec{v_1} & \vec{v_2} & \cdots & \vec{v_n} \\ \vdots & \vdots & \cdots & \vdots \end{bmatrix} AS=[⋮⋮⋯⋮λ1v1⃗λ2v2⃗⋯λnvn⃗⋮⋮⋯⋮]=[⋮⋮⋯⋮v1⃗v2⃗⋯vn⃗⋮⋮⋯⋮][λ10⋯00λ2⋯0⋮⋮⋱⋮00⋯λn]=SΛAS = \begin{bmatrix} \vdots & \vdots & \cdots & \vdots \\ \lambda_1\vec{v_1} & \lambda_2\vec{v_2} & \cdots & \lambda_n\vec{v_n} \\ \vdots & \vdots & \cdots & \vdots \end{bmatrix} \\ = \begin{bmatrix} \vdots & \vdots & \cdots & \vdots \\ \vec{v_1} & \vec{v_2} & \cdots & \vec{v_n} \\ \vdots & \vdots & \cdots & \vdots \end{bmatrix} \begin{bmatrix} \lambda_1 & 0 & \cdots & 0 \\ 0 & \lambda_2 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & \lambda_n \end{bmatrix} \\ = S\Lambda

  • S−1AS=ΛS^{-1}AS = \Lambda
    • AS=SΛAS = S\Lambda
    • S−1AS=S−1SΛ=IΛ=ΛS^{-1}AS = S^{-1}S\Lambda = \mathbb{I}\Lambda = \Lambda
    • A matrix AA that appears complicated in the original coordinate system becomes the diagonal matrix Λ\Lambda when expressed in the eigenvector coordinate system.
  • A=SΛS−1A = S\Lambda S^{-1}
    • In the eigenvector basis, the matrix AA becomes the diagonal matrix Λ\Lambda.

Symmetric Matrices​

  • A real symmetric matrix satisfies A=ATA=A^T.
  • All eigenvalues of a real symmetric matrix are real.
  • A real symmetric matrix has a complete set of orthonormal eigenvectors.
  • Therefore, the eigenvector matrix SS is orthogonal.
STS=I⟹S−1=STS^TS=\mathbb{I} \quad\Longrightarrow\quad S^{-1}=S^T
  • The diagonalization of a symmetric matrix is
A=SΛSTA=S\Lambda S^T
  • SS contains the orthonormal eigenvectors of AA.
  • Λ\Lambda contains the corresponding eigenvalues.
  • STS^T converts xx into the eigenvector basis.
  • Λ\Lambda scales each eigenvector direction by its corresponding eigenvalue.
  • SS converts the result back to the original basis.

Definiteness of Symmetric Matrices​

  • Using A=SΛSTA=S\Lambda S^T and y=STxy=S^Tx,
xTAx=xTSΛSTx=yTΛy=∑i=1nλiyi2x^TAx = x^TS\Lambda S^Tx = y^T\Lambda y = \sum_{i=1}^{n}\lambda_i y_i^2
  • yiy_i is the component of xx along the ii-th eigenvector.

  • Since yi2≥0y_i^2\geq0, the sign of xTAxx^TAx depends entirely on the signs of the eigenvalues.

  • If all eigenvalues are positive, AA is positive definite.

λi>0 for all i⟹xTAx>0 for all x≠0\lambda_i>0\text{ for all }i \quad\Longrightarrow\quad x^TAx>0\text{ for all }x\neq0
  • If all eigenvalues are non-negative, AA is positive semidefinite (PSD).
λi≥0 for all i⟹xTAx≥0\lambda_i\geq0\text{ for all }i \quad\Longrightarrow\quad x^TAx\geq0
  • If all eigenvalues are negative, AA is negative definite.
λi<0 for all i⟹xTAx<0 for all x≠0\lambda_i<0\text{ for all }i \quad\Longrightarrow\quad x^TAx<0\text{ for all }x\neq0
  • If all eigenvalues are non-positive, AA is negative semidefinite (NSD).
λi≤0 for all i⟹xTAx≤0\lambda_i\leq0\text{ for all }i \quad\Longrightarrow\quad x^TAx\leq0
  • If AA has both positive and negative eigenvalues, AA is indefinite.
  • The eigenvectors determine the principal directions of the quadratic surface.
  • The eigenvalues determine the curvature and sign along those directions.
  • The definiteness of a symmetric matrix is determined entirely by the signs of its eigenvalues.

Eigenvalues of a Positive Semidefinite Matrix​

  • If AA is positive semidefinite, then
xTAx≥0x^TAx\geq0
  • For an eigenvector x⃗≠0⃗\vec{x}\neq\vec{0} with eigenvalue λ\lambda,
Ax⃗=λx⃗A\vec{x}=\lambda\vec{x} x⃗ TAx⃗=x⃗ T(λx⃗)=λx⃗ Tx⃗=λ∥x⃗∥2≥0\vec{x}^{\,T}A\vec{x} = \vec{x}^{\,T}(\lambda\vec{x}) = \lambda\vec{x}^{\,T}\vec{x} = \lambda\lVert\vec{x}\rVert^2 \geq0
  • Since x⃗≠0⃗\vec{x}\neq\vec{0},
∥x⃗∥2>0\lVert\vec{x}\rVert^2>0
  • Therefore,
λ≥0\lambda\geq0
  • Hence, all eigenvalues of a positive semidefinite matrix are non-negative.
  • A zero eigenvalue means that the corresponding eigenvector direction is collapsed to the zero vector.

Singular Value Decomposition​

  • Diagonalization is generally defined for square matrices, while singular value decomposition can be applied to any rectangular or square matrix.
A=UΣVTA=U\Sigma V^T
  • For A∈Rm×nA\in\mathbb{R}^{m\times n},
U∈Rm×m,Σ∈Rm×n,V∈Rn×nU\in\mathbb{R}^{m\times m}, \qquad \Sigma\in\mathbb{R}^{m\times n}, \qquad V\in\mathbb{R}^{n\times n}
  • The columns of VV are the right singular vectors and represent orthonormal directions in the input space.
  • The columns of UU are the left singular vectors and represent orthonormal directions in the output space.
  • The diagonal entries of Σ\Sigma are the singular values.
σ1≥σ2≥⋯≥0\sigma_1\geq\sigma_2\geq\cdots\geq0
  • For each singular-vector pair,
Avi⃗=σiui⃗A\vec{v_i}=\sigma_i\vec{u_i}
  • vi⃗\vec{v_i} is an input direction.

  • σi\sigma_i is the scaling factor.

  • ui⃗\vec{u_i} is the corresponding output direction.

  • The transformation AA can be interpreted as three steps:

    • VTV^T expresses the input in the right singular-vector basis.
    • Σ\Sigma scales each direction by its singular value.
    • UU maps the scaled result into the output space.
  • UU and VV are orthogonal matrices.

UTU=I,VTV=IU^TU=\mathbb I, \qquad V^TV=\mathbb I
  • The singular vectors and singular values are related to the eigenvectors and eigenvalues of ATAA^TA and AATAA^T.
ATAvi⃗=σi2vi⃗A^TA\vec{v_i}=\sigma_i^2\vec{v_i} AATui⃗=σi2ui⃗AA^T\vec{u_i}=\sigma_i^2\vec{u_i}
  • The rank of AA equals the number of nonzero singular values.
  • SVD is commonly used for dimensionality reduction, data compression, noise reduction, pseudoinverses, and latent-factor analysis.

SVD

SVD Matrix Flow

x⏟n×1→VTVTx⏟n×1→ΣΣVTx⏟m×1→UUΣVTx⏟m×1=Ax⏟m×1\underbrace{x}_{n\times 1} \xrightarrow{V^T} \underbrace{V^Tx}_{n\times 1} \xrightarrow{\Sigma} \underbrace{\Sigma V^Tx}_{m\times 1} \xrightarrow{U} \underbrace{U\Sigma V^Tx}_{m\times 1} = \underbrace{Ax}_{m\times 1}
  • UU is such that the mm columns of UU are the eigenvectors of AATAA^T, known as the left singular vectors of AA.
  • VV is such that the nn columns of VV are the eigenvectors of ATAA^TA, known as the right singular vectors of AA.
  • Σ\Sigma is a rectangular diagonal matrix with each element being the square root of an eigenvalue of AATAA^T or ATAA^TA.
  • SVD allows us to construct a lower rank approximation of a rectangular matrix.
    • Choose only the top rr singular values in Σ\Sigma.
    • The corresponding columns in UU and rows in VTV^T are also selected.
  • VV represents the principal directions in the input space, UU shows where those directions are mapped in the output space, and Σ\Sigma shows how much each direction is scaled.

Gradient​

∇Af(A)=[∂f∂A11⋯∂f∂A1n⋮⋱⋮∂f∂Am1⋯∂f∂Amn]\nabla_A f(A) = \begin{bmatrix} \frac{\partial f}{\partial A_{11}} & \cdots & \frac{\partial f}{\partial A_{1n}} \\ \vdots & \ddots & \vdots \\ \frac{\partial f}{\partial A_{m1}} & \cdots & \frac{\partial f}{\partial A_{mn}} \end{bmatrix}
  • The gradient with respect to AA is a matrix that shows how the function f(A)f(A) changes with respect to each element of AA.
(∇Af(A))ij=∂f(A)∂Aij\left(\nabla_A f(A)\right)_{ij} = \frac{\partial f(A)}{\partial A_{ij}}
  • Each element of the gradient measures how sensitive f(A)f(A) is to a small change in the corresponding element AijA_{ij}.

  • If f(x1,x2)=x12+2x22f(x_1,x_2)=x_1^2+2x_2^2 then the gradient is

∇f(x)=[2x14x2].\nabla f(x) = \begin{bmatrix} 2x_1 \\ 4x_2 \end{bmatrix}.
  • The gradient points in the direction of the steepest increase of the function.
    • ∇f(x)\nabla f(x) is a direction of steepest increase.
    • −∇f(x)-\nabla f(x) is a direction of steepest decrease.
  • The magnitude of the gradient indicates how steeply the function increases.
  • To reduce the function value, optimization moves in the opposite direction of the gradient. This method is called gradient descent.
xnew=xold−η∇f(xold)x_{\mathrm{new}} = x_{\mathrm{old}} - \eta\nabla f(x_{\mathrm{old}})
  • η\eta is the learning rate, which controls the size of each update step.

Hessian​

f:Rn→Rf:\mathbb{R}^n\to\mathbb{R} H(x)=∇x2f(x)=[∂2f∂x12⋯∂2f∂x1∂xn⋮⋱⋮∂2f∂xn∂x1⋯∂2f∂xn2]H(x) = \nabla_x^2f(x) = \begin{bmatrix} \frac{\partial^2f}{\partial x_1^2} & \cdots & \frac{\partial^2f}{\partial x_1\partial x_n} \\ \vdots & \ddots & \vdots \\ \frac{\partial^2f}{\partial x_n\partial x_1} & \cdots & \frac{\partial^2f}{\partial x_n^2} \end{bmatrix}
  • A Hessian matrix is a square matrix containing all second-order partial derivatives of a scalar-valued function.

  • It describes the local curvature of the function.

  • If the second-order partial derivatives are continuous, then the Hessian is symmetric.

  • If

f(x1,x2)=x12+2x22,f(x_1,x_2)=x_1^2+2x_2^2,

then the gradient is

∇f(x)=[∂f∂x1∂f∂x2]=[2x14x2].\nabla f(x) = \begin{bmatrix} \frac{\partial f}{\partial x_1} \\ \frac{\partial f}{\partial x_2} \end{bmatrix} = \begin{bmatrix} 2x_1 \\ 4x_2 \end{bmatrix}.
  • The Hessian is obtained by differentiating each component of the gradient with respect to every input variable.
H(x)=[∂∂x1(∂f∂x1)∂∂x2(∂f∂x1)∂∂x1(∂f∂x2)∂∂x2(∂f∂x2)]H(x) = \begin{bmatrix} \frac{\partial}{\partial x_1} \left(\frac{\partial f}{\partial x_1}\right) & \frac{\partial}{\partial x_2} \left(\frac{\partial f}{\partial x_1}\right) \\ \frac{\partial}{\partial x_1} \left(\frac{\partial f}{\partial x_2}\right) & \frac{\partial}{\partial x_2} \left(\frac{\partial f}{\partial x_2}\right) \end{bmatrix} =[∂∂x1(2x1)∂∂x2(2x1)∂∂x1(4x2)∂∂x2(4x2)]=[2004].= \begin{bmatrix} \frac{\partial}{\partial x_1}(2x_1) & \frac{\partial}{\partial x_2}(2x_1) \\ \frac{\partial}{\partial x_1}(4x_2) & \frac{\partial}{\partial x_2}(4x_2) \end{bmatrix} = \begin{bmatrix} 2&0 \\ 0&4 \end{bmatrix}.
  • At a stationary point where ∇f(x)=0\nabla f(x)=0:

    • If H(x)≻0H(x)\succ0, then the point is a strict local minimum.
    • If H(x)≺0H(x)\prec0, then the point is a strict local maximum.
    • If H(x)H(x) has both positive and negative eigenvalues, then the point is a saddle point.
    • If H(x)H(x) is only positive semidefinite or negative semidefinite, the Hessian test may be inconclusive.
  • If H(x)⪰0H(x)\succeq0 for every xx, then ff is convex.

  • If H(x)⪯0H(x)\preceq0 for every xx, then ff is concave.

Differentiating a Linear Function​

  • For a constant vector b∈Rnb\in\mathbb{R}^n,
f(x)=bTx=∑i=1nbixif(x)=b^Tx = \sum_{i=1}^{n}b_ix_i
  • When differentiating with respect to xkx_k, every term except bkxkb_kx_k is treated as a constant.
∂f(x)∂xk=bk\frac{\partial f(x)}{\partial x_k} = b_k
  • Collecting all partial derivatives gives
∇xf(x)=[b1b2⋮bn]=b\nabla_x f(x) = \begin{bmatrix} b_1\\ b_2\\ \vdots\\ b_n \end{bmatrix} = b
  • Therefore,
∇x(bTx)=b\nabla_x(b^Tx)=b
  • Since f(x)=bTxf(x)=b^Tx is linear, its gradient is constant and does not depend on xx.

Differentiating a Quadratic Function​

  • For f(x)=xTAxf(x)=x^TAx, where A∈Rn×nA\in\mathbb{R}^{n\times n},
∇xf(x)=(A+AT)x\nabla_x f(x) = (A+A^T)x
  • The two terms appear because each variable can occur in both positions of the product xixjx_ix_j.

  • If AA is symmetric, then A=ATA=A^T, so

∇x(xTAx)=2Ax\boxed{ \nabla_x(x^TAx)=2Ax }
  • The ll-th component of the gradient is
∂f(x)∂xl=(2Ax)l=2∑i=1nAlixi\frac{\partial f(x)}{\partial x_l} = (2Ax)_l = 2\sum_{i=1}^{n}A_{li}x_i
  • Differentiating the ll-th gradient component with respect to xkx_k gives the (l,k)(l,k)-th entry of the Hessian.
Hlk=∂∂xk(∂f(x)∂xl)=∂∂xk(2∑i=1nAlixi)=2AlkH_{lk} = \frac{\partial}{\partial x_k} \left( \frac{\partial f(x)}{\partial x_l} \right) = \frac{\partial}{\partial x_k} \left( 2\sum_{i=1}^{n}A_{li}x_i \right) = 2A_{lk}
  • Therefore,
∇x2(xTAx)=2A\boxed{ \nabla_x^2(x^TAx)=2A }
  • The gradient depends on xx, while the Hessian is constant because f(x)=xTAxf(x)=x^TAx is a quadratic function.