VivaPrep
← Jaber Notes

Jaber Notes · 1 of 16

Math Foundations

Linear algebra, calculus, probability, statistics, information theory.

The mathematical machinery under everything else: vectors and matrices, SVD and eigendecomposition, gradients and the chain rule, distributions, MLE/MAP, and entropy/KL. Derivations, not just definitions.

Visual reference

Eigenvector intuition

vAv = λv
Transforming vector v by matrix A gives Av — still pointing along the same line, just stretched by factor λ. That unchanged direction is the eigenvector.

Bayes' theorem as a tree

1000 peopledisease (1%) — 10healthy (99%) — 990test + : ~10test − : ~0test + (false alarm): ~10test − : ~980≈10 real positives vs ≈10 false alarms → only ~50% of "+" results are real
Even with a 99%-accurate test, a 1% base rate means most positive results come from the huge healthy group's false alarms, not the small sick group — the base-rate fallacy.
Everything below is math you will use directly when reading papers, implementing algorithms, and understanding why things work.

Table of contents

  1. Linear algebra
  2. Calculus and analysis
  3. Probability theory
  4. Statistics
  5. Information theory

1. Linear algebra

1.1 Vectors

A column vector xRn\mathbf{x} \in \mathbb{R}^n is a list of nn real numbers. Row vector = x\mathbf{x}^\top.

Inner product (dot product):

x,y=xy=i=1nxiyi.\langle \mathbf{x}, \mathbf{y} \rangle = \mathbf{x}^\top \mathbf{y} = \sum_{i=1}^n x_i y_i.

Euclidean (L2) norm:

x2=xx=i=1nxi2.\|\mathbf{x}\|_2 = \sqrt{\mathbf{x}^\top \mathbf{x}} = \sqrt{\sum_{i=1}^n x_i^2}.

L1 norm: x1=xi\|\mathbf{x}\|_1 = \sum |x_i|. Induces sparsity in optimization.

L∞ norm: x=maxixi\|\mathbf{x}\|_\infty = \max_i |x_i|.

Cauchy–Schwarz inequality: xyxy|\mathbf{x}^\top \mathbf{y}| \leq \|\mathbf{x}\|\,\|\mathbf{y}\|. Equality iff x=cy\mathbf{x} = c\mathbf{y}. This is the foundation of cosine similarity.

Cosine similarity: cosθ=xyxy[1,1]\cos\theta = \frac{\mathbf{x}^\top \mathbf{y}}{\|\mathbf{x}\|\,\|\mathbf{y}\|} \in [-1,1].

1.2 Matrices

A matrix ARm×n\mathbf{A} \in \mathbb{R}^{m \times n} has mm rows and nn columns.

Transpose: (A)ij=Aji(\mathbf{A}^\top)_{ij} = A_{ji}. Matrix product: (AB)ik=jAijBjk(\mathbf{AB})_{ik} = \sum_j A_{ij} B_{jk} (row-times-column). Properties: (AB)=BA(\mathbf{AB})^\top = \mathbf{B}^\top \mathbf{A}^\top,   (AB)C=A(BC)\;(\mathbf{AB})\mathbf{C} = \mathbf{A}(\mathbf{BC}).

Symmetric matrix: A=A\mathbf{A} = \mathbf{A}^\top. Covariance matrices are always symmetric.

Trace: tr(A)=iAii\operatorname{tr}(\mathbf{A}) = \sum_i A_{ii}. Cyclic: tr(ABC)=tr(CAB)\operatorname{tr}(\mathbf{ABC}) = \operatorname{tr}(\mathbf{CAB}).

Determinant: scalar that captures volume scaling. det(AB)=det(A)det(B)\det(\mathbf{AB}) = \det(\mathbf{A})\det(\mathbf{B}).

Rank: dimension of column space (= row space). Full rank ⇒ invertible (for square matrices).

Frobenius norm: AF=ijAij2=tr(AA)\|\mathbf{A}\|_F = \sqrt{\sum_{ij} A_{ij}^2} = \sqrt{\operatorname{tr}(\mathbf{A}^\top\mathbf{A})}.

1.3 Systems of linear equations

Ax=b\mathbf{Ax} = \mathbf{b}: if A\mathbf{A} is square and invertible, unique solution x=A1b\mathbf{x} = \mathbf{A}^{-1}\mathbf{b}. In practice, use Gaussian elimination or numpy.linalg.solve (never explicitly invert).

Overdetermined (m>nm > n): no exact solution in general → least-squares minxAxb2\min_\mathbf{x}\|\mathbf{Ax}-\mathbf{b}\|^2 → normal equations AAx=Ab\mathbf{A}^\top\mathbf{Ax} = \mathbf{A}^\top\mathbf{b}.

1.4 Eigendecomposition

For square ARn×n\mathbf{A} \in \mathbb{R}^{n\times n}:

Av=λv\mathbf{A}\mathbf{v} = \lambda \mathbf{v}

λ\lambda is an eigenvalue and v0\mathbf{v} \neq \mathbf{0} is its eigenvector.

Spectral theorem: a real symmetric matrix has all real eigenvalues and an orthonormal basis of eigenvectors:

A=QΛQ,QQ=I,\mathbf{A} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top, \quad \mathbf{Q}^\top\mathbf{Q}=\mathbf{I},

where Λ=diag(λ1,,λn)\boldsymbol{\Lambda} = \operatorname{diag}(\lambda_1,\ldots,\lambda_n).

Positive semidefinite (PSD): all λi0\lambda_i \geq 0. Equivalent: xAx0  x\mathbf{x}^\top\mathbf{A}\mathbf{x} \geq 0\; \forall \mathbf{x}. Covariance matrices are always PSD.

Use in ML: PCA uses eigendecomposition of the sample covariance to find principal directions.

1.5 Singular value decomposition (SVD)

For any ARm×n\mathbf{A} \in \mathbb{R}^{m \times n}:

A=UΣV,\mathbf{A} = \mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top,

where:

  • URm×m\mathbf{U} \in \mathbb{R}^{m\times m}: left singular vectors (columns orthonormal).
  • ΣRm×n\boldsymbol{\Sigma} \in \mathbb{R}^{m\times n}: diagonal with singular values σ1σ20\sigma_1 \geq \sigma_2 \geq \cdots \geq 0.
  • VRn×n\mathbf{V} \in \mathbb{R}^{n\times n}: right singular vectors (columns orthonormal).

Thin SVD: keep only r=rank(A)r = \operatorname{rank}(\mathbf{A}) nonzero singular values. A=i=1rσiuivi\mathbf{A} = \sum_{i=1}^r \sigma_i \mathbf{u}_i \mathbf{v}_i^\top (outer product expansion).

Rank-kk approximation: truncate to first kk singular values — best rank-kk approximation in Frobenius and spectral norm (Eckart–Young theorem).

Connections:

  • Singular values of A\mathbf{A} = square roots of eigenvalues of AA\mathbf{A}^\top\mathbf{A}.
  • Right singular vectors = eigenvectors of AA\mathbf{A}^\top\mathbf{A} (used in PCA).
  • SVD is the computational workhorse behind PCA, matrix factorization in recommenders, pseudoinverse.

Pseudoinverse: A+=VΣ+U\mathbf{A}^+ = \mathbf{V}\boldsymbol{\Sigma}^+\mathbf{U}^\top where Σ+\Sigma^+ inverts nonzero entries. Gives minimum-norm least-squares solution when A\mathbf{A} is rank-deficient.

1.6 Matrix calculus (gradients)

Gradient of a scalar w.r.t. a vector wRn\mathbf{w} \in \mathbb{R}^n:

wf(w)=[f/w1f/wn].\nabla_\mathbf{w} f(\mathbf{w}) = \begin{bmatrix} \partial f/\partial w_1 \\ \vdots \\ \partial f/\partial w_n \end{bmatrix}.

Key identities (denominator layout, a\mathbf{a} constant):

ExpressionGradient w.r.t. w\mathbf{w}
aw\mathbf{a}^\top\mathbf{w}a\mathbf{a}
wAw\mathbf{w}^\top\mathbf{A}\mathbf{w}(A+A)w(\mathbf{A}+\mathbf{A}^\top)\mathbf{w} (= 2Aw2\mathbf{A}\mathbf{w} if symmetric)
Awb2\|\mathbf{Aw}-\mathbf{b}\|^22A(Awb)2\mathbf{A}^\top(\mathbf{Aw}-\mathbf{b})
w2\|\mathbf{w}\|^22w2\mathbf{w}

Hessian: matrix of second partials Hij=2f/wiwj\mathbf{H}_{ij} = \partial^2 f / \partial w_i \partial w_j. PSD Hessian everywhere ⇒ convex function.


2. Calculus and analysis

2.1 Derivative rules recap

Chain rule (scalar): (f(g(x)))=f(g(x))g(x)(f(g(x)))' = f'(g(x)) \cdot g'(x).

Chain rule (vector): if z=f(y)\mathbf{z} = f(\mathbf{y}) and y=g(x)\mathbf{y} = g(\mathbf{x}), then:

zx=zyyx(Jacobian multiplication).\frac{\partial \mathbf{z}}{\partial \mathbf{x}} = \frac{\partial \mathbf{z}}{\partial \mathbf{y}} \cdot \frac{\partial \mathbf{y}}{\partial \mathbf{x}} \quad (\text{Jacobian multiplication}).

This is the backbone of backpropagation.

Product rule: (fg)=fg+fg(fg)' = f'g + fg'. Quotient rule: (f/g)=(fgfg)/g2(f/g)' = (f'g - fg')/g^2.

2.2 Taylor expansion

f(x+δ)f(x)+f(x)δ+12f(x)δ2+f(x + \delta) \approx f(x) + f'(x)\delta + \frac{1}{2}f''(x)\delta^2 + \cdots

Multivariate version:

f(w+δ)f(w)+f(w)δ+12δH(w)δ.f(\mathbf{w} + \boldsymbol{\delta}) \approx f(\mathbf{w}) + \nabla f(\mathbf{w})^\top \boldsymbol{\delta} + \frac{1}{2}\boldsymbol{\delta}^\top \mathbf{H}(\mathbf{w})\boldsymbol{\delta}.

Used in Newton's method (second-order optimization) and understanding convergence of gradient descent.

2.3 Convexity

A set CC is convex if x,yC,  t[0,1]:  tx+(1t)yC\forall \mathbf{x},\mathbf{y} \in C,\; t\in[0,1]:\; t\mathbf{x}+(1-t)\mathbf{y}\in C.

A function f:CRf: C \to \mathbb{R} is convex if:

f(tx+(1t)y)tf(x)+(1t)f(y).f(t\mathbf{x}+(1-t)\mathbf{y}) \leq t f(\mathbf{x}) + (1-t) f(\mathbf{y}).

Equivalently: H(w)0\mathbf{H}(\mathbf{w}) \succeq 0 (PSD Hessian) everywhere.

Why it matters: any local minimum of a convex function is a global minimum.

Strictly convex: H0\mathbf{H} \succ 0 (positive definite) ⇒ unique global minimum.

Examples in ML:

  • MSE loss (linear regression): convex (Hessian = 2nXX\frac{2}{n}\mathbf{X}^\top\mathbf{X}, PSD).
  • Cross-entropy (logistic regression): convex in w\mathbf{w}.
  • Ridge objective: strictly convex.
  • Neural network losses: non-convex in general.

2.4 Lagrangian optimization and KKT conditions

Constrained optimization: minf(w)\min f(\mathbf{w}) s.t. gi(w)0g_i(\mathbf{w}) \leq 0, hj(w)=0h_j(\mathbf{w}) = 0.

Lagrangian: L(w,α,ν)=f(w)+iαigi(w)+jνjhj(w)\mathcal{L}(\mathbf{w}, \boldsymbol{\alpha}, \boldsymbol{\nu}) = f(\mathbf{w}) + \sum_i \alpha_i g_i(\mathbf{w}) + \sum_j \nu_j h_j(\mathbf{w}).

KKT conditions (necessary at optimum under constraint qualifications):

  1. Stationarity: wL=0\nabla_\mathbf{w} \mathcal{L} = \mathbf{0}.
  2. Primal feasibility: gi(w)0g_i(\mathbf{w}^\star) \leq 0.
  3. Dual feasibility: αi0\alpha_i \geq 0.
  4. Complementary slackness: αigi(w)=0\alpha_i g_i(\mathbf{w}^\star) = 0.

SVM is derived directly from KKT conditions on the margin-maximization problem.


3. Probability theory

3.1 Foundations

Sample space Ω\Omega: set of all outcomes. Event AΩA \subseteq \Omega. Probability measure PP: P(Ω)=1P(\Omega)=1, P(A)0P(A) \geq 0, countably additive.

Conditional probability: P(AB)=P(AB)/P(B)P(A|B) = P(A \cap B)/P(B).

Independence: P(AB)=P(A)P(B)P(A \cap B) = P(A)P(B).

Bayes' theorem:

P(AB)=P(BA)P(A)P(B).\boxed{P(A|B) = \frac{P(B|A)\,P(A)}{P(B)}}.

In ML: P(hypothesisdata)P(datahypothesis)P(hypothesis)P(\text{hypothesis}|\text{data}) \propto P(\text{data}|\text{hypothesis})\cdot P(\text{hypothesis}) — this is the foundation of Bayesian learning.

3.2 Random variables and distributions

PMF (discrete): P(X=x)P(X=x). PDF (continuous): f(x)=dF/dxf(x) = dF/dx. CDF: F(x)=P(Xx)F(x) = P(X \leq x).

Expectation: E[X]=xP(X=x)\mathbb{E}[X] = \sum x P(X=x) or xf(x)dx\int x f(x)dx. Variance: Var(X)=E[(Xμ)2]=E[X2](E[X])2\text{Var}(X) = \mathbb{E}[(X-\mu)^2] = \mathbb{E}[X^2] - (\mathbb{E}[X])^2. Standard deviation: σ=Var(X)\sigma = \sqrt{\text{Var}(X)}. Covariance: Cov(X,Y)=E[(XμX)(YμY)]\text{Cov}(X,Y) = \mathbb{E}[(X-\mu_X)(Y-\mu_Y)].

Linearity of expectation: E[aX+bY]=aE[X]+bE[Y]\mathbb{E}[aX+bY] = a\mathbb{E}[X]+b\mathbb{E}[Y] (always, no independence needed).

3.3 Key distributions in ML

Bernoulli: X{0,1}X \in \{0,1\}, P(X=1)=pP(X=1)=p. Mean = pp, Var = p(1p)p(1-p). Binary classification labels.

Binomial Bin(n,p)\text{Bin}(n,p): number of successes in nn Bernoulli trials.

Categorical / Multinoulli: P(X=k)=pkP(X=k) = p_k, pk=1\sum p_k = 1. Multi-class labels; output of softmax.

Gaussian N(μ,σ2)\mathcal{N}(\mu,\sigma^2):

f(x)=12πσ2exp ⁣((xμ)22σ2).f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\left(-\frac{(x-\mu)^2}{2\sigma^2}\right).

Properties: symmetric, unimodal, characterized by mean and variance, 68-95-99.7 rule.

Multivariate Gaussian N(μ,Σ)\mathcal{N}(\boldsymbol{\mu},\boldsymbol{\Sigma}):

f(x)=1(2π)d/2Σ1/2exp ⁣(12(xμ)Σ1(xμ)).f(\mathbf{x}) = \frac{1}{(2\pi)^{d/2}|\boldsymbol{\Sigma}|^{1/2}} \exp\!\left(-\frac{1}{2}(\mathbf{x}-\boldsymbol{\mu})^\top \boldsymbol{\Sigma}^{-1}(\mathbf{x}-\boldsymbol{\mu})\right).

Used in GMMs, Gaussian processes, LDA.

Uniform: constant density. Used in random initialization.

Exponential: f(x)=λeλxf(x) = \lambda e^{-\lambda x}, x0x \geq 0. Memoryless property.

Beta Beta(α,β)\text{Beta}(\alpha,\beta): distribution over [0,1][0,1]; conjugate prior to Bernoulli/Binomial.

Dirichlet Dir(α)\text{Dir}(\boldsymbol{\alpha}): distribution over probability simplices; conjugate prior to Categorical.

3.4 Maximum likelihood estimation (MLE)

Given i.i.d. data {xi}i=1n\{x_i\}_{i=1}^n and a parameterized model p(xθ)p(x|\theta), find θ\theta to maximize the likelihood:

L(θ)=i=1np(xiθ).\mathcal{L}(\theta) = \prod_{i=1}^n p(x_i|\theta).

Equivalently maximize the log-likelihood (avoids underflow, same argmax):

(θ)=i=1nlogp(xiθ).\ell(\theta) = \sum_{i=1}^n \log p(x_i|\theta).

Example — Gaussian MLE: (μ,σ2)=n2log(2πσ2)12σ2i(xiμ)2\ell(\mu,\sigma^2) = -\frac{n}{2}\log(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum_i(x_i-\mu)^2. Setting /μ=0\partial\ell/\partial\mu = 0: μ^=xˉ\hat{\mu} = \bar{x}. Setting /σ2=0\partial\ell/\partial\sigma^2 = 0: σ^2=1n(xixˉ)2\hat{\sigma}^2 = \frac{1}{n}\sum(x_i-\bar{x})^2 (biased).

Example — Bernoulli MLE: p^=xin\hat{p} = \frac{\sum x_i}{n} (fraction of 1s).

MLE and OLS connection: If yi=xiw+εiy_i = \mathbf{x}_i^\top\mathbf{w} + \varepsilon_i with εiN(0,σ2)\varepsilon_i \sim \mathcal{N}(0,\sigma^2), then MLE for w\mathbf{w} = minimize (yixiw)2\sum(y_i - \mathbf{x}_i^\top\mathbf{w})^2 = OLS.

3.5 Maximum a posteriori (MAP)

Incorporate a prior p(θ)p(\theta) via Bayes:

θMAP=argmaxθlogp(θD)posterior=argmaxθ[logp(Dθ)+logp(θ)].\theta_\text{MAP} = \arg\max_\theta \underbrace{\log p(\theta|\mathcal{D})}_{\text{posterior}} = \arg\max_\theta \left[ \log p(\mathcal{D}|\theta) + \log p(\theta) \right].

MAP vs regularization: Gaussian prior θN(0,1λI)\theta \sim \mathcal{N}(\mathbf{0},\frac{1}{\lambda}\mathbf{I}) gives MAP = Ridge regression. Laplace prior gives MAP = Lasso.

3.6 Law of large numbers and central limit theorem

LLN: XˉnPE[X]\bar{X}_n \xrightarrow{P} \mathbb{E}[X] as nn\to\infty. Basis for Monte Carlo.

CLT: n(Xˉnμ)dN(0,σ2)\sqrt{n}(\bar{X}_n - \mu) \xrightarrow{d} \mathcal{N}(0,\sigma^2). Why Gaussian distribution appears everywhere.


4. Statistics

4.1 Estimators and their properties

An estimator θ^(D)\hat{\theta}(\mathcal{D}) is a function of the data.

Bias: Bias(θ^)=E[θ^]θ\text{Bias}(\hat\theta) = \mathbb{E}[\hat\theta] - \theta^\star. Zero bias = unbiased. Variance: Var(θ^)=E[(θ^E[θ^])2]\text{Var}(\hat\theta) = \mathbb{E}[(\hat\theta - \mathbb{E}[\hat\theta])^2]. Mean squared error: MSE(θ^)=Bias2(θ^)+Var(θ^)\text{MSE}(\hat\theta) = \text{Bias}^2(\hat\theta) + \text{Var}(\hat\theta).

This is the bias-variance decomposition of an estimator's error — mirrors the ML bias-variance tradeoff exactly.

Consistency: θ^nPθ\hat\theta_n \xrightarrow{P} \theta^\star as nn \to \infty.

Efficiency: estimator with minimum variance among unbiased estimators (Cramér-Rao bound).

4.2 Hypothesis testing

Framework: null hypothesis H0H_0, alternative H1H_1. Test statistic TT, reject H0H_0 if T>tαT > t_\alpha.

p-value: probability of observing data at least as extreme as seen, *assuming H0H_0 true*. Small p-value = evidence against H0H_0.

Type I error (false positive): reject H0H_0 when it is true. Controlled at level α\alpha. Type II error (false negative): fail to reject H0H_0 when it is false. Related to power.

t-test, chi-squared test, ANOVA: standard tests for means, proportions, group comparisons.

4.3 Confidence intervals

A 95%95\% CI [L,U][L,U] means: if we repeated the experiment many times, 95%95\% of such intervals would contain the true θ\theta^\star. For large nn (CLT):

Xˉn±1.96σ^n.\bar{X}_n \pm 1.96 \cdot \frac{\hat\sigma}{\sqrt{n}}.

4.4 Experimental design

Randomized controlled trial (A/B test): gold standard for causal inference. Random assignment breaks confounding.

Confounding: a variable ZZ affects both the treatment TT and outcome YY, creating spurious correlation. Models can detect correlation but cannot automatically infer causation from observational data.

Stratification: ensure both groups have similar distributions of known confounders. Used in stratified k-fold CV.


5. Information theory

5.1 Entropy

Shannon entropy of a discrete distribution pp:

H(p)=kpklogpk.H(p) = -\sum_k p_k \log p_k.

Measures uncertainty / minimum expected bits to encode a sample. Max entropy = uniform distribution. H=0H=0 when distribution is deterministic.

5.2 Cross-entropy and KL divergence

Cross-entropy between true pp and model distribution qq:

H(p,q)=kpklogqk.H(p, q) = -\sum_k p_k \log q_k.

Used as classification loss: true labels are pp (one-hot), model output is qq (probabilities).

KL divergence (Kullback–Leibler):

DKL(pq)=kpklogpkqk=H(p,q)H(p).D_\text{KL}(p \| q) = \sum_k p_k \log \frac{p_k}{q_k} = H(p,q) - H(p).

Always 0\geq 0 (Gibbs' inequality); equals 0 iff p=qp = q. Not symmetric.

Minimizing cross-entropy ≡ minimizing KL divergence (since H(p)H(p) is fixed w.r.t. model parameters).

5.3 Mutual information

I(X;Y)=DKL(p(X,Y)p(X)p(Y))=H(X)+H(Y)H(X,Y).I(X;Y) = D_\text{KL}(p(X,Y) \| p(X)p(Y)) = H(X) + H(Y) - H(X,Y).

Measures statistical dependence (= 0 iff independent). Used in feature selection, information bottleneck.

5.4 Gini impurity (decision trees)

G=1kpk2.G = 1 - \sum_k p_k^2.

Alternative impurity to entropy (computationally faster, similar results). The CART algorithm minimizes weighted Gini after each split.


*File: notes/01_math_foundations.md — next: notes/02_learning_theory.md*