ML Math Essentials
🎯 Learning Objectives
- Review linear algebra concepts essential for ML
- Understand gradients and the chain rule for optimization
- Apply MLE and MAP estimation
- Master gradient descent variants (batch, SGD, mini-batch)
- Recognize convex vs non-convex optimization
1. Linear Algebra for ML
- Vector: A column of numbers x ∈ Rⁿ. Represents a data point with n features.
- Matrix: X ∈ Rᵐ×ⁿ — m samples, n features. Each row is a data point.
- Dot product: a·b = ∑aᵢbᵢ = |a||b|cosθ. Measures similarity.
- Matrix multiplication: (AB)ᵢⱼ = ∑ₖ AᵢₖBₖⱼ. AB ≠ BA in general.
1.1 Key Concepts
For a square matrix A, if Av = λv for some non-zero vector v, then:
- λ is an eigenvalue and v is the corresponding eigenvector
- To find: solve det(A − λI) = 0 for λ, then (A − λI)v = 0 for v
- ML usage: PCA finds eigenvectors of the covariance matrix. The eigenvector with the largest eigenvalue is the direction of maximum variance.
Q: Find the eigenvalues of A = [[4, 2], [1, 3]].
A: det(A − λI) = (4−λ)(3−λ) − 2×1 = λ² − 7λ + 10 = 0
λ = (7 ± √(49−40))/2 = (7 ± 3)/2. So λ&sub1; = 5, λ&sub2; = 2.
A symmetric matrix A is positive definite if xᵀAx > 0 for all non-zero x, equivalently all eigenvalues are positive. The covariance matrix is always positive semi-definite.
2. Calculus for ML
For a function f(w&sub1;, w&sub2;, ..., wₙ), the gradient is the vector of partial derivatives:
∇f = [∂f/∂w&sub1;, ∂f/∂w&sub2;, ..., ∂f/∂wₙ]ᵀ
The gradient points in the direction of steepest ascent. To minimise f, move in the direction of −∇f.
If y = f(g(x)), then dy/dx = f'(g(x)) × g'(x).
In ML: Backpropagation in neural networks uses the chain rule to compute gradients layer by layer:
∂L/∂w = (∂L/∂a) × (∂a/∂z) × (∂z/∂w)
Q: Compute the gradient of f(w&sub1;, w&sub2;) = w&sub1;² + 3w&sub1;w&sub2; + 2w&sub2;² at (1, 2).
A:
- ∂f/∂w&sub1; = 2w&sub1; + 3w&sub2; = 2(1) + 3(2) = 8
- ∂f/∂w&sub2; = 3w&sub1; + 4w&sub2; = 3(1) + 4(2) = 11
∇f(1,2) = [8, 11]
3. Probability for ML
P(A|B) = P(B|A) × P(A) / P(B)
- P(A|B): Posterior
- P(B|A): Likelihood
- P(A): Prior
- P(B): Evidence (normalizing constant)
3.1 Maximum Likelihood Estimation (MLE)
Find the parameter θ that maximises the likelihood of the observed data:
θ_MLE = argmax_θ P(D|θ) = argmax_θ ∑ log P(xᵢ|θ)
For a Gaussian: MLE of μ = sample mean, MLE of σ² = sample variance (biased).
Q: Coin flips: 7 heads, 3 tails. What is the MLE estimate of P(heads)?
A: For Bernoulli, MLE of p = (number of heads)/(total flips) = 7/10 = 0.7
3.2 Maximum A Posteriori (MAP)
Incorporates a prior distribution on θ:
θ_MAP = argmax_θ P(θ|D) = argmax_θ [log P(D|θ) + log P(θ)]
- MAP with Gaussian prior on weights is equivalent to L2 regularization (Ridge)
- MAP with Laplace prior on weights is equivalent to L1 regularization (Lasso)
- MAP with uniform prior reduces to MLE
4. Optimization & Gradient Descent
Input: learning rate η, initial weights w
Repeat until convergence:
1. Compute gradient g = (1/n) ∑ ∇L(w; x_i, y_i) over ALL n samples
2. Update: w ← w − η × g
Output: Optimized weights w
| Variant | Batch Size | Pro | Con |
|---|---|---|---|
| Batch GD | All n samples | Stable convergence, true gradient | Slow for large datasets; memory intensive |
| SGD | 1 sample | Fast updates, can escape local minima | Noisy; high variance in updates |
| Mini-batch GD | B samples (32–512) | Balance: fast yet stable, GPU-friendly | Extra hyperparameter (batch size) |
Q: f(w) = w² − 4w + 5. Starting at w=0, learning rate η=0.1. What is w after one step?
A: f'(w) = 2w − 4. At w=0: f'(0) = −4. Update: w = 0 − 0.1×(−4) = 0.4
After two steps: f'(0.4) = 2(0.4)−4 = −3.2. w = 0.4 − 0.1(−3.2) = 0.72
(True minimum is at w = 2.)
4.1 Convexity
A function f is convex if for all x, y and λ ∈ [0,1]:
f(λx + (1−λ)y) ≤ λf(x) + (1−λ)f(y)
- Convex functions have a single global minimum (no local minima)
- GD on convex functions is guaranteed to find the global minimum
- Convex in ML: MSE loss for linear regression, logistic loss
- Non-convex in ML: Neural network loss functions (many local minima)
4.2 Learning Rate
- Too large: Overshoots the minimum; may diverge
- Too small: Very slow convergence; may get stuck
- Learning rate schedules: Start large and decay (step decay, exponential decay, cosine annealing)
- Adaptive methods: Adam, RMSProp, AdaGrad automatically adjust per-parameter learning rates
5. Essential Formulas Summary
| Concept | Formula | ML Application |
|---|---|---|
| Dot product | a·b = ∑aᵢbᵢ | Similarity, linear models |
| Matrix inverse | A⁻¹A = I | Normal equation: w*=(XᵀX)⁻¹Xᵀy |
| Eigendecomposition | Av = λv | PCA |
| Gradient | ∇f = [∂f/∂wᵢ] | Gradient descent |
| Chain rule | dy/dx = (dy/du)(du/dx) | Backpropagation |
| Bayes' theorem | P(A|B)=P(B|A)P(A)/P(B) | Naive Bayes, MAP |
| MLE | argmax ∑log P(xᵢ|θ) | Parameter estimation |
| GD update | w ← w − η∇L | All iterative training |