Equations · Pearson (1901); Hotelling (1933)
Principal component analysis (PCA), explained term by term
PCA finds the directions along which centered data vary the most. The first principal component is the unit direction of largest variance; each next one is the largest-variance direction orthogonal to those before. They are the eigenvectors of the covariance matrix, and projecting onto the first reduces the data to dimensions while keeping as much variance as possible.
Term by term
The data matrix, one row per example, centered: each column's mean has been subtracted. Forgetting to center is the classic PCA bug.
The sample covariance matrix of the examples: , symmetric.
The unit vector that maximizes , the variance of the data projected onto . The unit-length constraint stops from growing without bound.
The eigendecomposition of : the columns of are the principal directions, and the diagonal of holds the variances along them, .
The projected data: each example's coordinates along the first components. is the first columns of . The same directions come from the SVD of : they are its right singular vectors, with .
Shapes
- , , , .
A small worked example
Centered points : , so , with eigenvalues 5 and 0. The first component is ; the projections are , with variance 5. One dimension keeps 100% of the variance, because the points lie on a line.
In code
import numpy as np
def pca(X, k):
Xc = X - X.mean(axis=0) # center first
U, S, Vt = np.linalg.svd(Xc, full_matrices=False)
var = S**2 / (len(X) - 1) # eigenvalues of the covariance
return Xc @ Vt[:k].T, var[:k] # Z (n, k), variancesEvery 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
- Normal equation (linear regression)
- Singular value decomposition (SVD)