Optimization and Principal Component Analysis
Part of Math Camp: An Introduction to Mathematics and Statistics with Applications in Economics.
Main Stam4Econ site | View this section in the book | Author: Fan Wang | DOI: 10.5281/zenodo.24696926
Date: 2026-11-30 | HTML | PDF | QMD
1 Optimization and Principal Component Analysis
1.1 The centered variance covariance matrix
Given a matrix of data, \(\widehat{X}\), what happens we multiply the transpose of the matrix with itself? Let:
\[ X = \begin{bmatrix} x_{11} & x_{12} \\ x_{21} & x_{22} \\ x_{31} & x_{32} \end{bmatrix} \]
Then we have:
\[ X^T X = \begin{bmatrix} x_{11} & x_{21} & x_{31} \\ x_{12} & x_{22} & x_{32} \end{bmatrix} \begin{bmatrix} x_{11} & x_{12} \\ x_{21} & x_{22} \\ x_{31} & x_{32} \end{bmatrix} = \begin{bmatrix} x_{11}^2 + x_{21}^2 + x_{31}^2 & x_{11}x_{12} + x_{21}x_{22} + x_{31}x_{32} \\ x_{12}x_{11} + x_{22}x_{21} + x_{32}x_{31} & x_{12}^2 + x_{22}^2 + x_{32}^2 \end{bmatrix} \]
Each column of \(X\) can be viewed as a variable. The diagonal entries of \(X^TX\) are sums of squares, and the off-diagonal entries are cross-products. For a centered \(n\)-observation data matrix, \(X^TX/(n-1)\) is the sample covariance matrix; without centering, the cross-products also reflect the column means.
1.2 Do we always have to demean the data before PCA?
In standard PCA, the answer is essentially yes: we usually center each variable first. The reason is that PCA is designed to find directions of greatest variation around the mean, not directions of where the data happen to sit in the ambient space. If we do not subtract the mean, a large overall shift in the data can dominate the first principal component even when there is little spread around that shifted center.
Suppose the raw data matrix is \(X\) (\(m\) observations by \(p\) variables), and let \(\bar{x}\) be the \(p \times 1\) vector of column means. The centered data matrix is
\[ \tilde{X} = X - \mathbf{1}\bar{x}^T, \]
where \(\mathbf{1}\) is the \(m \times 1\) vector of ones. Then the sample covariance matrix among the \(p\) variables is
\[ \Sigma = \frac{1}{m-1} \tilde{X}^T\tilde{X}. \]
This is the matrix whose eigenvectors define the principal directions. If we skip centering, the object we optimize is instead based on \(X^T X\), which includes the mean level of the variables. In that case, the first principal component may simply point in the direction of the overall mean rather than the direction of maximum variation around the mean.
So the key idea is:
- PCA on centered data captures variation around the center of the cloud of points.
- PCA on raw data can be dominated by the location of the cloud rather than its shape.
This is why demeaning is not a cosmetic step in classical PCA; it is essential for the usual covariance-based interpretation. That said, there are special settings where centering is not needed or is not the right choice. For example, if all variables are already centered, or if the application is specifically about absolute level rather than deviations from the mean, then the centering step may be unnecessary. But for the standard PCA formulation used in statistics and data analysis, demeaning is the normal and essential step.
1.3 The principal component analysis idea
Each individual has some vector of attributes, \(x_i \in R^p\), and we have \(m\) individuals. We stack these as rows of a data matrix \(X\) that is \(m\) by \(p\) (observations by variables):
\[ X = \begin{bmatrix} — & x_1^T & — \\ — & x_2^T & — \\ & \vdots & \\ — & x_m^T & — \end{bmatrix} \]
We can think of each individual as a point in \(R^p\), in another word, the individual is defined by a \(p\)-dimensional vector of characteristics. But potentially this is a lot of information if \(p\) is large. How do we summarize this person? Perhaps we can find a lower dimensional representation of the person.
Specifically, we can think about this as looking for a basis vector. When we project an individual’s vector of data onto this basis, we can capture partly what this individual’s attributes are through the “shadow” they cast. Because we are projecting to a specific basis vector, so the direction of this “shadow” is the direction that the basis vector is pointing in and known, what is to be determined is just the length of the shadow along the direction of the basis vector.
For example, given the vector \(x_i = [x_1, x_2]^T\), and a basis vector \(b = [b_1, b_2]^T\), the projection of \(x_i\) onto \(b\) is given by \(p^{x_i}_b = \gamma^{x_i}_b b\), where \(\gamma^{x_i}_b\) is a scalar that determines how long the “shadow” is. The key fact is that the residual vector, \(x_i - p^{x_i}_b\), is perpendicular to the direction of the basis vector \(b\). That is,
\[ (x_i - \gamma^{x_i}_b b) \cdot b = 0. \]
Expanding the dot product gives
\[ x_i \cdot b - \gamma^{x_i}_b (b \cdot b) = 0, \]
so
\[ \gamma^{x_i}_b \|b\|^2 = x_i \cdot b, \]
and therefore, as long as \(b \neq 0\),
\[ \gamma^{x_i}_b = \frac{x_i \cdot b}{\|b\|^2}. \]
This is exactly the scalar that scales the unit direction \(b\) to the orthogonal projection of \(x_i\) onto the line spanned by \(b\). For each row of matrix \(X\), we can cast onto the same basis and compute their respective \(\gamma^{x_j}_b\) shadow lengths.
Principal component analysis (PCA) searches over unit directions on the sphere in \(\mathbb{R}^p\) and finds the direction onto which the data vectors have the greatest aggregate squared projection. A direction orthogonal to all data vectors would yield zero projections and provide no information. Finding the direction with the largest aggregate projection is an optimization problem.
1.4 The principal component analysis optimization problem
We need to normalize the basis vector, because the shadow lengths proportions of the basis. We normalize the basis vector to be a unit vector, i.e., \(\|b_a\| = 1\).
Considering all \(i\), we have:
\[ \sum_{i=1}^{m} (x_i \cdot b_a)^2 = \sum_{i=1}^{m} b_a^T x_i x_i^T b_a = b_a^T \left( \sum_{i=1}^{m} x_i x_i^T \right) b_a \]
This is the squared sum
So we set up a constrained optimization problem, where we maximize the above expression subject to the constraint that \(b_a\) is a unit vector, i.e., \(\|b_a\| = 1\). This is equivalent to maximizing the expression subject to the constraint that \(b_a^T b_a = 1\).
We can solve this using the method of Lagrange multipliers. The Lagrangian function is given by:
\[ \mathcal{L}(b_a, \lambda) = b_a^T \left( \sum_{i=1}^{m} x_i x_i^T \right) b_a - \lambda (b_a^T b_a - 1) \]
But we can get rid of the summation term by noting that, with \(X\) stacked by rows,
\[ \sum_{i=1}^{m} x_i x_i^T = X^T X \]
So the fully matrix based lagrangian function is:
\[ \mathcal{L}(b_a, \lambda) = b_a^T X^T X b_a - \lambda (b_a^T b_a - 1) \]
where \(X^T X\) is the (unnormalized) Gram / sum-of-squares matrix among the \(p\) attributes; after demeaning the columns of \(X\), it is proportional to the sample covariance matrix.
Taking the derivative with respect to \(b_a\) and setting it equal to zero, we have:
\[ \frac{\partial \mathcal{L}(b_a, \lambda)}{\partial b_a} = 2X^T X b_a - 2\lambda b_a = 0 \]
To see this carefully, write the Lagrangian as
\[ \mathcal{L}(b_a, \lambda) = b_a^T A b_a - \lambda (b_a^T b_a - 1) \]
And we have:
\[ \frac{\partial \mathcal{L}}{\partial b_a} = 2X^T X b_a - 2\lambda b_a. \]
Setting this equal to zero gives the first-order condition, which is that the optimal constrained basis vector, \(b_a^{\ast}\) (along with \(\lambda^{\ast}\)) must satisfy:
\[ X^T X b_a^{\ast} = A b_a^{\ast} = \lambda^{\ast} b_a^{\ast} \]
This is the eigenvalue-eigenvector equation, where \(b_a^{\ast}\) is an eigenvector of the matrix \(X^T X=A\), and \(\lambda^{\ast}\) is the corresponding eigenvalue. But there is a problem, as we demonstrated earlier, there are potentially multiple eigenvector-eigenvalue pairs, all of which would satisfy the optimality condition above, so how do we pick the right one?
We proceed to recall the objective of the optimization problem as well as the constraint that the \(b_a\) is a unit-norm basis vector. For a unit-norm eigenvector \(b_a\) with eigenvalue \(\lambda\), working with the just derived optimality condition, we (1) multiply the Equation above by \(b_a^T\) on the left and on the right at the same time, (2) move the position of \(\lambda\) because it is a scalar value, (3) realize that \(b_a^T b_a = 1\) by unit-norm constraint, and arrive the result that:
\[ (b_a^{\ast})^T X^T X b_a^{\ast} = (b_a^{\ast})^T \lambda^{\ast} b_a^{\ast} = \lambda^{\ast} (b_a^{\ast})^T b_a^{\ast} = \lambda^{\ast} 1 = \lambda^{\ast}. \]
The derivation above tells us that among the candidate \((b_a^{\ast}, \lambda^{\ast})\) that satisfy the optimality condition, the pair where the \(\lambda^{\ast}\) is the largest (the pair with the largest eigenvalue) will maximize the objective function. So we select among the candidates the one with the largest eigenvalue.
This makes intuitive sense, our objective here is to find the basis vector over which when we project all “individual” data vectors, the most information is captured. If the eigenvalue is zero, is there is no information captured, larger eigenvalue means that more information is captured through the “reflections” of the multiple-dimensional-individual-specific information onto this single directional basis vector.
1.5 From the first to the second and third principal components
The first principal component solves
\[ \max_{\|b\|=1} b^T A b, \]
where \(A = X^T X\) or, after centering, \(A = \tilde{X}^T\tilde{X}\). The solution is the eigenvector \(v_1\) associated with the largest eigenvalue \(\lambda_1\).
The second principal component is obtained by solving the same problem again, but now with the new restriction that the direction must be orthogonal to the first one:
\[ \max_{\|b\|=1,\; b^T v_1 = 0} b^T A b. \]
This is equivalent to maximizing the quadratic form over the subspace orthogonal to \(v_1\). Because the matrix \(A\) is symmetric, its eigenvectors are orthogonal, and the best direction in that orthogonal subspace is the next eigenvector \(v_2\), associated with the second largest eigenvalue \(\lambda_2\).
Similarly, the third principal component solves
\[ \max_{\|b\|=1,\; b^T v_1 = 0,\; b^T v_2 = 0} b^T A b. \]
This is the same optimization problem restricted to the subspace orthogonal to both \(v_1\) and \(v_2\). The optimal direction is the eigenvector \(v_3\) corresponding to the third largest eigenvalue \(\lambda_3\).
In general, the \(k\)th principal component is the unit vector \(v_k\) solving
\[ \max_{\|b\|=1,\; b^T v_j = 0 \text{ for } j=1,\ldots,k-1} b^T A b, \]
and the solution is the eigenvector associated with the \(k\)th largest eigenvalue of \(A\).
This is the sequence of principal components: each one captures the largest remaining variance, subject to being orthogonal to the earlier components.
Furthermore, we have a very easy way to summarize the total variance we are capturing after each and all iterations, it is simply the sum of the sequence of eigenvalues:
\[ \lambda_1 + \lambda_2 + \cdots + \lambda_r. \]
From each these basis-vector that we find, we project individual-specific-vector-of-data onto each success data-vectors, with the earlier projection capturing more and subsequent ones capturing less of the information embedded in the original data, and with incremental dimension capturing orthogonal information. The relative importance of each projection depends on the rate of value reduction in the sequence of \(\lambda_k\) values.
1.6 PCA, stars in space and reflection along radius of earth
To gain intuition for what we are doing with each successively and additionally constrained optimization problem: Consider space, each star has a coordinate in 3D, consider the center of earth as origin, and consider the surface of the earth to mark the direction and unit-length of all basis-vector in 3D normalized by the radius of earth. Principal component analysis is doing the following:
- Consider a sample dataset of stars. \(X\) is the matrix of star positions with rows as stars (observations) and columns as spatial coordinates (variables). For any point on the surface of the earth, draw a line from the center to that point, and extend this line in both directions infinitely.
- Look at the “reflection” of each star on this infinite line in space, which is measured by finding the closest point along this line to the star, the point (measured from the origin of earth) is now the projection of the star onto or point-on-earth-surface-specific basis vector. We look at the relative length of this projection vs the basis vector for all stars, that creates a new array/vector of individual-star-specific data points, for all the stars in the sample. Note that the value for each star in this vector can be negative or positive, depending on whether its closest point to the line is in the direction of the basis vector, or in the opposite direction.
- We look through all points on earth, and measure the squared sum (aggregating each time over all stars’ reflections) of these relative length of projection from star to the basis vector, and we find the basis-vector that maximizes the squared sum relative length of projection. It turns out the basis vector that solves this problem is the eigenvector of the \(X^T X\) Gram matrix of coordinates with the highest eigenvalue.
- After having found the first eigenvector that maximizes the squared sum of the relative lengths of the projections, we then look for the second eigenvector. This second eigenvector must be orthogonal to the first one and maximizes the squared sum of the relative lengths of the projections in the remaining subspace. This is equivalent to looking at a circle around the earth’s origin where all points in the circle are orthogonal to the first basis vector that we found (an arrow right through the center of the circle). Note that we are constrained to stay on the surface of earth, so there is only one circle of points where the values are orthogonal to the first basis vector we found, where the projection of all the points along the circle to this first basis vector is the null (zero) vector.