PCA (Principal Component Analysis) › Formula
Machine Learning from ScratchAll chapters 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:
Here is the mean (average) of . 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 and weight . Do they vary together? Covariance measures this:
If tall people tend to be heavier, then when is positive (taller than average), 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 of them, you need to track all pairwise relationships. The covariance matrix does exactly this:
Here is your data matrix with rows (observations) and 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:
This matrix is symmetric because . 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 (length 1). When you project your data onto , you get . The variance of this projection is:
This compact formula says: the variance in direction equals , where is the covariance matrix. To find the direction of maximum variance, we need to maximize subject to the constraint that has unit length ().
Why the constraint? Without it, we could make the variance arbitrarily large just by making 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:
The term enforces our constraint. Taking the derivative with respect to and setting it to zero:
Simplifying by dividing by 2:
This is the eigenvalue equation. The solutions are the eigenvectors of , and the values 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:
Here is a matrix whose columns are the eigenvectors of . Each column is a principal component (a direction of variance). The matrix contains your data in the new coordinate system. Each column of 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 -th component explains:
If the first eigenvalue is and the sum of all eigenvalues is , then the first principal component explains 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 components, you capture the maximum possible variance among all sets of orthogonal directions. No other choice of perpendicular directions would capture more variance.
Optimality for reconstruction: When you project your data onto components and reconstruct it back in the original space, PCA minimizes the mean squared reconstruction error. Among all possible -dimensional linear projections, PCA provides the best approximation to the original data.