PCA (Principal Component Analysis) › Formula

Michael BrenndoerferJuly 21, 20255 min read
Machine Learning from ScratchAll chapters
Part 6 of 9Explainability
Chapter 48PCA (Principal Component Analysis) › Formula
Reading section 3 of 9 · Formula · 5 minSection 3 of 9 · 5 min

Book contents

Section 3 of 9 · 5 min read

PCA (Principal Component Analysis) › FormulaOne section · Sections in this chapter3 / 9 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Now let's build the mathematics of PCA step by step, starting from basic concepts you already know and working toward the full algorithm. The journey from variance to principal components is more natural than you might expect.

Starting with Variance

Think about a single variable, like the heights of people in a dataset. How spread out are the values? Variance answers this question:

Var(x)=1n−1∑i=1n(xi−xˉ)2\text{Var}(x) = \frac{1}{n-1} \sum_{i=1}^{n} (x_i - \bar{x})^2

Here xˉ\bar{x} is the mean (average) of xx. For each data point, we calculate how far it is from the mean, square that distance, and average all the squared distances. The squaring ensures that deviations above and below the mean both contribute positively to the total spread.

Now consider two variables, height xx and weight yy. Do they vary together? Covariance measures this:

Cov(x,y)=1n−1∑i=1n(xi−xˉ)(yi−yˉ)\text{Cov}(x,y) = \frac{1}{n-1} \sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})

If tall people tend to be heavier, then when xi−xˉx_i - \bar{x} is positive (taller than average), yi−yˉy_i - \bar{y} tends to be positive too (heavier than average). Multiplying these together gives positive products, so the covariance is positive. If the variables move in opposite directions, covariance is negative. If they're unrelated, positive and negative products cancel out, giving covariance near zero.

The Covariance Matrix

When you have many variables, say pp of them, you need to track all pairwise relationships. The covariance matrix C\mathbf{C} does exactly this:

C=1n−1XTX\mathbf{C} = \frac{1}{n-1} \mathbf{X}^T \mathbf{X}

Here X\mathbf{X} is your data matrix with nn rows (observations) and pp columns (variables). We assume you've already mean-centered the data, meaning you subtracted the mean from each column so each variable has mean zero.

What does this matrix contain? The diagonal elements are the variances of each individual variable. The off-diagonal elements are the covariances between pairs of variables. So if you have three variables, your covariance matrix looks like:

C=[Var(x1)Cov(x1,x2)Cov(x1,x3)Cov(x2,x1)Var(x2)Cov(x2,x3)Cov(x3,x1)Cov(x3,x2)Var(x3)]\mathbf{C} = \begin{bmatrix} \text{Var}(x_1) & \text{Cov}(x_1,x_2) & \text{Cov}(x_1,x_3) \\ \text{Cov}(x_2,x_1) & \text{Var}(x_2) & \text{Cov}(x_2,x_3) \\ \text{Cov}(x_3,x_1) & \text{Cov}(x_3,x_2) & \text{Var}(x_3) \end{bmatrix}

This matrix is symmetric because Cov(xi,xj)=Cov(xj,xi)\text{Cov}(x_i,x_j) = \text{Cov}(x_j,x_i). The covariance matrix captures all the linear relationships in your data in one compact object.

Finding Principal Components

PCA answers a specific question: in which direction does the data vary the most?

Imagine drawing a line through your data cloud. You can project each point onto this line by dropping a perpendicular from the point to the line. The projected points form a one-dimensional dataset on the line. Some lines will give you spread-out projections (high variance), others will give you bunched-up projections (low variance). PCA finds the line that maximizes this variance.

Mathematically, a direction is a unit vector w\mathbf{w} (length 1). When you project your data X\mathbf{X} onto w\mathbf{w}, you get Xw\mathbf{X}\mathbf{w}. The variance of this projection is:

Var(Xw)=wTCw\text{Var}(\mathbf{X}\mathbf{w}) = \mathbf{w}^T \mathbf{C} \mathbf{w}

This compact formula says: the variance in direction w\mathbf{w} equals wTCw\mathbf{w}^T \mathbf{C} \mathbf{w}, where C\mathbf{C} is the covariance matrix. To find the direction of maximum variance, we need to maximize wTCw\mathbf{w}^T \mathbf{C} \mathbf{w} subject to the constraint that w\mathbf{w} has unit length (wTw=1\mathbf{w}^T \mathbf{w} = 1).

Why the constraint? Without it, we could make the variance arbitrarily large just by making w\mathbf{w} longer. Requiring unit length keeps the problem well-defined.

This is a constrained optimization problem. We solve it using Lagrange multipliers, a technique you may remember from calculus. We form the Lagrangian:

L=wTCw−λ(wTw−1)\mathcal{L} = \mathbf{w}^T \mathbf{C} \mathbf{w} - \lambda(\mathbf{w}^T \mathbf{w} - 1)

The term λ(wTw−1)\lambda(\mathbf{w}^T \mathbf{w} - 1) enforces our constraint. Taking the derivative with respect to w\mathbf{w} and setting it to zero:

∂L∂w=2Cw−2λw=0\frac{\partial \mathcal{L}}{\partial \mathbf{w}} = 2\mathbf{C}\mathbf{w} - 2\lambda\mathbf{w} = 0

Simplifying by dividing by 2:

Cw=λw\mathbf{C}\mathbf{w} = \lambda\mathbf{w}

This is the eigenvalue equation. The solutions w\mathbf{w} are the eigenvectors of C\mathbf{C}, and the values λ\lambda are the eigenvalues. What does this mean? The principal components are eigenvectors of the covariance matrix. The variance along each principal component is the corresponding eigenvalue. Larger eigenvalues mean more variance in that direction.

The Complete PCA Transformation

Once you've found all the eigenvectors, you can transform your data into the new coordinate system:

Y=XW\mathbf{Y} = \mathbf{X}\mathbf{W}

Here W\mathbf{W} is a matrix whose columns are the eigenvectors of C\mathbf{C}. Each column is a principal component (a direction of variance). The matrix Y\mathbf{Y} contains your data in the new coordinate system. Each column of Y\mathbf{Y} is a principal component score, telling you the position of each observation along that component.

The eigenvalues tell you how much variance each component captures. To find what proportion of total variance the kk-th component explains:

λk∑j=1pλj\frac{\lambda_k}{\sum_{j=1}^{p} \lambda_j}

If the first eigenvalue is λ1=5\lambda_1 = 5 and the sum of all eigenvalues is 1010, then the first principal component explains 5/10=50%5/10 = 50\% of the total variance. This helps you decide how many components to keep. If the first three components explain 95% of variance, you might drop the rest.

Mathematical Properties

PCA has several mathematical properties worth knowing:

Orthogonality: Principal components are perpendicular (orthogonal) to each other. This means they capture different, non-overlapping patterns in the data. Each component adds new information rather than repeating what previous components captured.

Variance preservation: The total variance in the original data equals the sum of all eigenvalues. When you keep the first kk components, you capture the maximum possible variance among all sets of kk orthogonal directions. No other choice of kk perpendicular directions would capture more variance.

Optimality for reconstruction: When you project your data onto kk components and reconstruct it back in the original space, PCA minimizes the mean squared reconstruction error. Among all possible kk-dimensional linear projections, PCA provides the best approximation to the original data.