ML Math Essentials

GATE Relevance: High

🎯 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

Vectors and Matrices
  • 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

Eigenvalues and Eigenvectors

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.
GATE Numerical: Eigenvalues

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.

Positive Definite Matrix

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

Gradient

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.

Chain Rule

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)

GATE Numerical: Gradient Computation

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

Bayes' Theorem

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)

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).

GATE Numerical: MLE

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)

MAP Estimation

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

Batch 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
VariantBatch SizeProCon
Batch GDAll n samplesStable convergence, true gradientSlow for large datasets; memory intensive
SGD1 sampleFast updates, can escape local minimaNoisy; high variance in updates
Mini-batch GDB samples (32–512)Balance: fast yet stable, GPU-friendlyExtra hyperparameter (batch size)
GATE Numerical: Gradient Descent Step

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

Convex Functions

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

Choosing the 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

ConceptFormulaML Application
Dot producta·b = ∑aᵢbᵢSimilarity, linear models
Matrix inverseA⁻¹A = INormal equation: w*=(XᵀX)⁻¹Xᵀy
EigendecompositionAv = λvPCA
Gradient∇f = [∂f/∂wᵢ]Gradient descent
Chain ruledy/dx = (dy/du)(du/dx)Backpropagation
Bayes' theoremP(A|B)=P(B|A)P(A)/P(B)Naive Bayes, MAP
MLEargmax ∑log P(xᵢ|θ)Parameter estimation
GD updatew ← w − η∇LAll iterative training