Equations · Standard statistics; least squares goes back to Legendre (1805) and Gauss (1809)
The normal equation for linear regression, explained
The closed-form answer to linear regression: the coefficients that minimize the sum of squared errors . It comes from setting that loss's gradient to zero, which gives the "normal equations" .
Term by term
The design matrix: one row per example, one column per feature. A column of ones is usually included so the first coefficient is the intercept.
The targets, one per example.
A small square matrix () of feature inner products. It is invertible when the columns of are linearly independent.
The matrix inverse. In code, solve the linear system rather than forming the inverse: it is faster and more accurate.
The estimated coefficients; the hat marks an estimate. ML papers often call them or .
Shapes
- , , , , .
A small worked example
Fit a line to . With an intercept column, , , . Solving gives : the line , which fits these points exactly.
In code
import numpy as np
X = np.array([[1, 0], [1, 1], [1, 2]], dtype=float)
y = np.array([1, 3, 5], dtype=float)
beta = np.linalg.solve(X.T @ X, X.T @ y) # [1. 2.]
beta, *_ = np.linalg.lstsq(X, y, rcond=None) # more stable: avoids forming X^T XEvery symbol in it
Other equations
- Scaled dot-product attention
- Softmax
- Cross-entropy loss
- Adam update
- Layer normalization
- RMSNorm
- The ELBO
- KL divergence
- Backpropagation through one layer
- LoRA
- GELU
- Dropout
- Batch normalization
- Rotary position embedding (RoPE)
- Multi-head attention
- PPO clipped objective
- DPO loss
- Policy gradient (REINFORCE)
- Bellman equation
- Q-learning update
- InfoNCE contrastive loss
- Diffusion forward process
- DDPM simplified loss
- VAE reparameterization trick
- GAN minimax objective
- AdamW
- Cosine learning-rate schedule
- Perplexity
- Logistic regression
- Singular value decomposition (SVD)
- Principal component analysis (PCA)