Mathematics for Machine Learning & Data Science
Modern machine learning is built directly on four mathematical pillars: multivariable chain rule (backpropagation), eigenvalues & SVD (dimensionality reduction and embeddings), convex optimization (gradient descent), and probability & information theory (cross-entropy loss and Bayesian inference).
Prerequisites
Uni BridgeUses Jacobians, Hessians, eigenvalues of symmetric matrices, and partial derivatives from University Bridge Chapters 3 & 4.
Core Algorithms
Interactive LabsHands-on intuition for backprop graphs, PCA variance maximization, learning rate stability $\eta$, and KL divergence.
Proofs & Theory
DerivationsComplete step-by-step proofs for Softmax-Cross-Entropy gradients, PCA variance optimality, step-size bound $\eta < 2/\lambda_{\max}$, and Gibbs' inequality.
2. Spectral Theorem → PCA & SVD: The covariance matrix $\Sigma = \frac{1}{n}X^T X$ is symmetric positive semi-definite — its top eigenvectors are the principal components!
3. Hessian Eigenvalues → Learning Rate $\eta$: The largest eigenvalue $\lambda_{\max}$ of the loss Hessian sets the strict speed limit $\eta < \frac{2}{\lambda_{\max}}$ before gradient descent explodes.
4-Chapter ML & Data Science Syllabus
Matrix derivatives, computational graphs, reverse-mode AD, Softmax + Cross-Entropy gradient proof, and the Backprop Graph Lab.
Covariance matrices, Singular Value Decomposition $A = U\Sigma V^T$, variance maximization proof, and the 2D PCA Projection Lab.
Convexity, Hessian conditioning, gradient descent, momentum, stability bound $\eta < 2/\lambda_{\max}$ proof, and the Gradient Descent Lab.
Bayes' theorem, Maximum Likelihood (MLE), Shannon entropy, KL divergence & Gibbs' inequality proof, and the KL & Bayes Lab.
Chapter 1: Matrix Calculus & Backpropagation
1.1 Essential Vector & Matrix Gradient Identities
1. Linear form: $\nabla_{\vec{x}} (\vec{a}^T \vec{x}) = \vec{a}$
2. Quadratic form: $\nabla_{\vec{x}} (\vec{x}^T A \vec{x}) = 2A\vec{x}$ (for general $M$: $(M + M^T)\vec{x}$)
3. Squared Euclidean norm: $\nabla_{\vec{w}} \|X\vec{w} - \vec{y}\|_2^2 = 2X^T(X\vec{w} - \vec{y})$
4. Elementwise activation $\vec{h} = \sigma(\vec{z})$: $\frac{\partial L}{\partial \vec{z}} = \frac{\partial L}{\partial \vec{h}} \odot \sigma'(\vec{z})$ (Hadamard product $\odot$).
1.2 Computational Graphs & Reverse-Mode Automatic Differentiation
• Forward pass: Compute and cache intermediate activations $\vec{z}_1, \dots, \vec{z}_K$ from inputs to scalar loss $L$.
• Backward pass: Initialize $\bar{L} = \frac{\partial L}{\partial L} = 1$. Propagate adjoints $\bar{u} := \frac{\partial L}{\partial u}$ backward using the chain rule: $$\frac{\partial L}{\partial u} = \sum_{v \in \text{children}(u)} \frac{\partial L}{\partial v} \frac{\partial v}{\partial u}$$ Reverse-mode AD computes gradients with respect to all parameters in a single backward sweep!
1.3 Softmax + Cross-Entropy Gradient (Full Proof)
• If $k = i$: by the quotient rule, $$\frac{\partial p_i}{\partial z_i} = \frac{e^{z_i} S - e^{z_i} e^{z_i}}{S^2} = p_i(1 - p_i)$$ • If $k \neq i$: $$\frac{\partial p_k}{\partial z_i} = \frac{0 - e^{z_k} e^{z_i}}{S^2} = -p_k p_i$$ Step 2 (Chain rule on Cross-Entropy $L = -\sum_{k=1}^C y_k \ln p_k$): $$\frac{\partial L}{\partial z_i} = -\sum_{k=1}^C \frac{y_k}{p_k} \frac{\partial p_k}{\partial z_i} = -\frac{y_i}{p_i}\big(p_i(1 - p_i)\big) - \sum_{k \neq i} \frac{y_k}{p_k}(-p_k p_i)$$ Step 3 (Simplify using $\sum_{k=1}^C y_k = 1$): $$\frac{\partial L}{\partial z_i} = -y_i + y_i p_i + p_i \sum_{k \neq i} y_k = -y_i + p_i \underbrace{\sum_{k=1}^C y_k}_{=\,1} = p_i - y_i \qquad \blacksquare$$
Interactive Backprop Lab: Computational Graph $L = \frac{1}{2}\big(\sigma(w x + b) - y\big)^2$
1.4 Exercises for Chapter 1
Exercise 1.1: Derive the Normal Equations for Ordinary Least Squares by setting $\nabla_{\vec{w}} \|X\vec{w} - \vec{y}\|_2^2 = \vec{0}$.
Exercise 1.2: A 3-class classifier outputs logits $\vec{z} = (2, 1, 0)^T$ for true label $\vec{y} = (0, 1, 0)^T$. Using $e^2 \approx 7.39, e^1 \approx 2.72, e^0 = 1$, compute $\nabla_{\vec{z}} L = \vec{p} - \vec{y}$.
Chapter 2: SVD & Principal Component Analysis (PCA)
2.1 Centered Data & the Sample Covariance Matrix
2.2 Singular Value Decomposition (SVD)
2.3 Why the Top Eigenvector Maximizes Projected Variance (Proof)
Step 3 (Evaluate objective at eigenvector): For $\Sigma \vec{u} = \lambda \vec{u}$ with $\|\vec{u}\|_2 = 1$: $$\operatorname{Var}(z) = \vec{u}^T (\Sigma \vec{u}) = \vec{u}^T (\lambda \vec{u}) = \lambda (\vec{u}^T \vec{u}) = \lambda$$ Therefore, the variance is maximized when $\lambda = \lambda_1$ (the largest eigenvalue) and $\vec{u} = \vec{v}_1$. The fraction of total variance explained is $\frac{\lambda_1}{\sum_{j=1}^d \lambda_j}$. $\blacksquare$
Interactive 2D PCA Lab: Rotate Projection Axis $\vec{u}(\theta)$ & Watch Variance Peak
2.4 Exercises for Chapter 2
Exercise 2.1: A 4-dimensional dataset has covariance eigenvalues $\lambda_1 = 6.4,\ \lambda_2 = 2.6,\ \lambda_3 = 0.7,\ \lambda_4 = 0.3$. How much total variance is retained if we project onto the top $k = 2$ principal components?
Exercise 2.2: A $1000 \times 1000$ grayscale image matrix ($1{,}000{,}000$ numbers) is approximated by a rank-$k$ SVD ($k = 20$). How many numbers are stored in $U_k, \Sigma_k, V_k^T$?
Chapter 3: Convex Optimization & Gradient Descent
3.1 Convex Sets, Convex Functions & Global Optimality
3.2 Gradient Descent, Stochastic GD & Momentum
• Gradient Descent with Momentum ($\beta \in [0, 1)$): $$\vec{v}_{t+1} = \beta \vec{v}_t + \nabla L(\vec{w}_t), \qquad \vec{w}_{t+1} = \vec{w}_t - \eta \vec{v}_{t+1}$$ Momentum damps oscillations across steep ravines while accelerating progress along shallow valleys.
3.3 Learning Rate Stability Bound $\eta < \frac{2}{\lambda_{\max}}$ (Proof)
Interactive Gradient Descent Lab: $L(x, y) = \frac{1}{2}(x^2 + 6y^2)$ ($\lambda_{\min}=1, \lambda_{\max}=6 \Rightarrow \eta_{\text{crit}} = \frac{2}{6} \approx 0.333$)
3.4 Exercises for Chapter 3
Exercise 3.1: Consider $L(w_1, w_2) = 2w_1^2 + 10w_2^2$. Find its Hessian $H_L$, eigenvalues $\lambda_{\min}, \lambda_{\max}$, condition number $\kappa$, and maximum stable learning rate $\eta_{\max}$.
Exercise 3.2: Starting at $\vec{w}_0 = (3, 1)^T$ on $L(w_1, w_2) = 2w_1^2 + 10w_2^2$ with $\eta = 0.05$, compute $\vec{w}_1$.
Chapter 4: Probability, Entropy & Bayesian Inference
4.1 Bayesian Updating & Maximum Likelihood Estimation (MLE)
4.2 Shannon Entropy, Cross-Entropy & KL Divergence
• Shannon Entropy: $H(P) = -\sum_{i=1}^k p_i \ln p_i$ (or $\log_2$ for bits; measures average surprise/uncertainty).
• Cross-Entropy: $H(P, Q) = -\sum_{i=1}^k p_i \ln q_i$.
• Kullback-Leibler (KL) Divergence: $$D_{\text{KL}}(P \parallel Q) = \sum_{i=1}^k p_i \ln\!\left(\frac{p_i}{q_i}\right) = H(P, Q) - H(P)$$ Since $H(P)$ does not depend on model parameters, minimizing cross-entropy $H(P, Q_\theta)$ is identical to minimizing $D_{\text{KL}}(P \parallel Q_\theta)$!
4.3 Gibbs' Inequality: $D_{\text{KL}}(P \parallel Q) \ge 0$ (Full Proof)
Interactive KL Divergence Lab: True Distribution $P = (p, 1-p)$ vs. Model $Q = (q, 1-q)$
4.4 Exercises for Chapter 4
Exercise 4.1: A rare defect occurs in $1\,\%$ of chips ($P(D) = 0.01$). A test detects a true defect with $99\,\%$ sensitivity ($P(+ \mid D) = 0.99$) and has a $2\,\%$ false-positive rate ($P(+ \mid \neg D) = 0.02$). If a chip tests positive, what is $P(D \mid +)$?
Exercise 4.2: Compute the Shannon entropy in bits ($H_2(P) = -\sum p_i \log_2 p_i$) for a biased coin with $P = (\frac{3}{4}, \frac{1}{4})$ vs. a fair coin $U = (\frac{1}{2}, \frac{1}{2})$.
Mathematics for ML — Proof Index & Drill Trainer
| Chapter | Theorem / Derivation | Shortcut |
|---|---|---|
| Chapter 1 | Softmax + Cross-Entropy Gradient ($\nabla_{\vec{z}} L = \vec{p} - \vec{y}$) | |
| Chapter 2 | PCA Variance Maximization via Rayleigh Quotient & Lagrange Multipliers | |
| Chapter 3 | Gradient Descent Learning Rate Stability Bound ($0 < \eta < 2/\lambda_{\max}$) | |
| Chapter 4 | Gibbs' Inequality: Non-Negativity of KL Divergence ($D_{\text{KL}}(P \parallel Q) \ge 0$) |