# PCA objective function: what is the connection between maximizing variance and minimizing error?

The PCA algorithm can be formulated in terms of the correlation matrix (assume the data $X$ has already been normalized and we are only considering projection onto the first PC). The objective function can be written as:

$$\max_w (Xw)^T(Xw)\; \: \text{s.t.} \: \:w^Tw = 1.$$

This is fine, and we use Lagrangian multipliers to solve it, i.e. rewriting it as:

$$\max_w [(Xw)^T(Xw) - \lambda w^Tw],$$

which is equivalent to

$$\max_w \frac{ (Xw)^T(Xw) }{w^Tw},$$

and hence (see here on Mathworld) seems to be equal to $$\max_w \sum_{i=1}^n \text{(distance from point x_i to line w)}^2.$$

But this is saying to maximize the distance between point and line, and from what I've read here, this is incorrect -- it should be $\min$, not $\max$. Where is my error?

Or, can someone show me the link between maximizing variance in projected space and minimizing distance between point and line?

• I think minimum distance is used to meet the criterion of orthogonality for the components. The points are projected into the PCs that are orthogonal to each other but in each successive component the remaining variance is maximized. Jul 12 '12 at 15:42
• Hint: What happens when you consider the smallest eigenvalue first, rather than the largest one?
– whuber
Jul 12 '12 at 15:45
• @whuber The smallest eigenvalue probably has the PC that is the solution to the final objective function. But this PC does not maximixe the original objective function. Jul 12 '12 at 17:06
• I'm not sure what you mean by "final" and "original" objective function, Cam. PCA is not (conceptually) an optimization program. Its output is a set of principal directions, not just one. It is an (interesting) mathematical theorem that these directions can be found by solving a sequence of constrained quadratic programs, but that's not basic to the concepts or the practice of PCA. I am only suggesting that, by focusing on the smallest eigenvalue rather than on the largest one, you can reconcile the two ideas of (1) minimizing distances and (2) taking an optimization view of PCA.
– whuber
Jul 12 '12 at 18:32
• That's okay - your answer was the non-mistake version of what I was trying to do. Feb 3 '15 at 13:22

Let $\newcommand{\X}{\mathbf X}\X$ be a centered data matrix with $n$ observations in rows. Let $\newcommand{\S}{\boldsymbol \Sigma}\S=\X^\top\X/(n-1)$ be its covariance matrix. Let $\newcommand{\w}{\mathbf w}\w$ be a unit vector specifying an axis in the variable space. We want $\w$ to be the first principal axis.

According to the first approach, first principal axis maximizes the variance of the projection $\X \w$ (variance of the first principal component). This variance is given by the $$\mathrm{Var}(\X\w)=\w^\top\X^\top \X \w/(n-1)=\w^\top\S\w.$$

According to the second approach, first principal axis minimizes the reconstruction error between $\X$ and its reconstruction $\X\w\w^\top$, i.e. the sum of squared distances between the original points and their projections onto $\w$. The square of the reconstruction error is given by \begin{align}\newcommand{\tr}{\mathrm{tr}} \|\X-\X\w\w^\top\|^2 &=\tr\left((\X-\X\w\w^\top)(\X-\X\w\w^\top)^\top\right) \\ &=\tr\left((\X-\X\w\w^\top)(\X^\top-\w\w^\top\X^\top)\right) \\ &=\tr(\X\X^\top)-2\tr(\X\w\w^\top\X^\top)+\tr(\X\w\w^\top\w\w^\top\X^\top) \\ &=\mathrm{const}-\tr(\X\w\w^\top\X^\top) \\ &=\mathrm{const}-\tr(\w^\top\X^\top\X\w) \\ &=\mathrm{const} - \mathrm{const} \cdot \w^\top \S \w. \end{align}

Notice the minus sign before the main term. Because of that, minimizing the reconstruction error amounts to maximizing $\w^\top \S \w$, which is the variance. So minimizing reconstruction error is equivalent to maximizing the variance; both formulations yield the same $\w$.

• Something I noticed, isn't ${w}^{T} \Sigma w$ a convex function (With respect to $w$ as $\Sigma$ is PSD? How come we try to maximize it?
– Royi
Dec 24 '16 at 10:15
• @amoeba can you explain how you go from tr() to const in the last step? Jan 6 '17 at 11:35
• @alberto What is inside the trace is a number (1x1 matrix); a trace of a number is this number itself, so the trace can be removed. The constant appears because $\Sigma$ is equal to $X^\top X/n$, so there is this $1/n$ factor. Jan 6 '17 at 16:00
• @Leullame The calculation will hold verbatim for $W$ if it is a matrix with orthonormal columns. You need $W^\top W = I$ to go from line #3 to line #4. If matrix $W$ has orthonormal columns, then indeed $xWW^\top$ will be a projection of $x$ onto the subspace spanned by the columns of $W$ (here $x$ is a row vector). Feb 18 '17 at 18:51
• @DanielLópez Well, we are looking for a 1-dimensional subspace minimizing reconstruction error. A 1-dimensional subspace can be defined by a unit-norm vector pointing into its direction, which is what $w$ is taken to be. It has unit norm by construction. Feb 22 '18 at 11:54