Principal component analysis “backwards”: how much variance of the data is explained by a given linear combination of the variables?

I have carried out a principal components analysis of six variables $A$, $B$, $C$, $D$, $E$ and $F$. If I understand correctly, unrotated PC1 tells me what linear combination of these variables describes/explains the most variance in the data and PC2 tells me what linear combination of these variables describes the next most variance in the data and so on.

I'm just curious -- is there any way of doing this "backwards"? Let's say I choose some linear combination of these variables -- e.g. $A+2B+5C$, could I work out how much variance in the data this describes?

• Strictly, PC2 is the linear combination orthogonal to PC1 which describes the next most variance in the data. – Henry Mar 22 '11 at 15:54
• Are you trying to estimate $Var(A+2B+5C)$? – vqv Mar 24 '11 at 14:50
• All nice answers (three +1s). I'm curious about people's opinion about whether the formulated problem is solvable via latent variable approaches (SEM/LVM), if we consider one or more latent variables "a linear combination of the variables". – Aleksandr Blekh Jan 28 '15 at 9:44
• @Aleksandr, my answer is actually directly at odds with the other two. I edited my answer to clarify the disagreement (and plan to edit it further to spell out the math). Imagine a dataset with two standardized identical variables $X=Y$. How much variance is described by $X$? Two other solutions give $50\%$. I argue that the correct answer is $100\%$. – amoeba Jan 28 '15 at 9:59
• @amoeba: Despite still struggling to understand the material completely, I understand that your answer is different. When I said "all nice answers", I implied that I like the level of the answers per se, not their correctness. I find that it has an educational value to people like me, who are in their self-education quest in rough-terrain country, called Statistics :-). Hope it makes sense. – Aleksandr Blekh Jan 28 '15 at 10:08

If we start with the premise that all variables have been centred (standard practice in PCA), then the total variance in the data is just the sum of squares:

$$T=\sum_{i}(A_{i}^{2}+B_{i}^{2}+C_{i}^{2}+D_{i}^{2}+E_{i}^{2}+F_{i}^{2})$$

This is equal to the trace of the covariance matrix of the variables, which equals the sum of the eigenvalues of the covariance matrix. This is the same quantity that PCA speaks of in terms of "explaining the data" - i.e. you want your PCs to explain the greatest proportion of the diagonal elements of the covariance matrix. Now if we make this an objective function for a set of predicted values like so:

$$S=\sum_{i}\left(\left[A_{i}-\hat{A}_{i}\right]^{2}+\dots+\left[F_{i}-\hat{F}_{i}\right]^{2}\right)$$

Then the first principal component minimises $S$ among all rank 1 fitted values $(\hat{A}_{i},\dots,\hat{F}_{i})$. So it would seem like the appropriate quantity you are after is $$P=1-\frac{S}{T}$$ To use your example $A+2B+5C$, we need to turn this equation into rank 1 predictions. First you need to normalise the weights to have sum of squares 1. So we replace $(1,2,5,0,0,0)$ (sum of squares $30$) with $\left(\frac{1}{\sqrt{30}},\frac{2}{\sqrt{30}},\frac{5}{\sqrt{30}},0,0,0\right)$. Next we "score" each observation according to the normalised weights:

$$Z_{i}=\frac{1}{\sqrt{30}}A_{i}+\frac{2}{\sqrt{30}}B_{i}+\frac{5}{\sqrt{30}}C_{i}$$

Then we multiply the scores by the weight vector to get our rank 1 prediction.

$$\begin{pmatrix} \hat{A}_{i} \\ \hat{B}_{i} \\ \hat{C}_{i} \\ \hat{D}_{i} \\ \hat{E}_{i} \\ \hat{F}_{i}\end{pmatrix} =Z_{i}\times\begin{pmatrix} \frac{1}{\sqrt{30}} \\ \frac{2}{\sqrt{30}} \\ \frac{5}{\sqrt{30}} \\ 0 \\ 0 \\ 0\end{pmatrix}$$

Then we plug these estimates into $S$ calculate $P$. You can also put this into matrix norm notation, which may suggest a different generalisation. If we set $O$ as the $N\times q$ matrix of observed values of the variables ($q=6$ in your case), and $E$ as a corresponding matrix of predictions. We can define the proportion of variance explained as:

$$\frac{||O||_{2}^{2}-||O-E||_{2}^{2}}{||O||_{2}^{2}}$$

Where $||.||_{2}$ is the Frobenius matrix norm. So you could "generalise" this to be some other kind of matrix norm, and you will get a difference measure of "variation explained", although it won't be "variance" per se unless it is sum of squares.

• This is a reasonable approach, but your expression can be greatly simplified and shown to be equal to the sum of squares of $Z_i$ divided by the total sum of squares $T$. Also, I think this is not the best way to interpret the question; see my answer for an alternative approach that I argue makes more sense (in particular, see my example figure there). – amoeba Jan 28 '15 at 0:57
• Think about it like that. Imagine a dataset with two standardized identical variables $X=Y$. How much variance is described by $X$? Your calculation gives $50\%$. I argue that the correct answer is $100\%$. – amoeba Jan 28 '15 at 9:48
• @amoeba - if $X=Y$ then the first PC is $(\frac {1}{\sqrt {2}},\frac {1}{\sqrt {2}})$ - this makes rank $1$ scores of $z_i=\frac {x_i+y_i}{\sqrt {2}}=x_i\sqrt {2}$ (assuming $x_i=y_i$). This gives rank $1$ predictions of $\hat {x}_i=x_i$, and similarly $\hat {y}_i=y_i$. Hence you get $O-E=0$ and $S =0$. Hence you get 100% as your intuition suggests. – probabilityislogic Jan 31 '15 at 12:46
• Hey, yes, sure, the 1st PC explains 100% variance, but that's not what I meant. What I meant is that $X=Y$, but the question is how much variance is described by $X$, i.e. by $(1,0)$ vector? What does your formula say then? – amoeba Jan 31 '15 at 14:23
• @amoeba - this says 50%, but note that the $(1,0)$ vector says that the best rank $1$ predictor for $(x_i, y_i)$ is given as $\hat {x}_i= x_i$ and $\hat {y}_i=0$ (noting that $z_i=x_i$ under your choice of vector). This is not an optimal prediction, which is why you don't get 100%. You need to predict both $X$ and $Y$ in this set-up. – probabilityislogic Jan 31 '15 at 23:20

Let's say I choose some linear combination of these variables -- e.g. $A+2B+5C$, could I work out how much variance in the data this describes?

This question can be understood in two different ways, leading to two different answers.

A linear combination corresponds to a vector, which in your example is $[1, 2, 5, 0, 0, 0]$. This vector, in turn, defines an axis in the 6D space of the original variables. What you are asking is, how much variance does projection on this axis "describe"? The answer is given via the notion of "reconstruction" of original data from this projection, and measuring the reconstruction error (see Wikipedia on Fraction of variance unexplained). Turns out, this reconstruction can be reasonably done in two different ways, yielding two different answers.

Approach #1

Let $\newcommand{\S}{\boldsymbol \Sigma} \newcommand{\w}{\mathbf w} \newcommand{\v}{\mathbf v}\newcommand{\X}{\mathbf X} \X$ be the centered dataset ($n$ rows correspond to samples, $d$ columns correspond to variables), let $\S$ be its covariance matrix, and let $\w$ be a unit vector from $\mathbb R^d$. The total variance of the dataset is the sum of all $d$ variances, i.e. the trace of the covariance matrix: $T = \mathrm{tr}(\S)$. The question is: what proportion of $T$ does $\w$ describe? The two answers given by @todddeluca and @probabilityislogic are both equivalent to the following: compute projection $\X \w$, compute its variance and divide by $T$: $$R^2_\mathrm{first} = \frac{\mathrm{Var}(\X \w)}{T} = \frac{\w^\top \S \w}{\mathrm{tr}(\S)}.$$

This might not be immediately obvious, because e.g. @probabilityislogic suggests to consider the reconstruction $\X \w \w^\top$ and then to compute $$\frac{\|\X\|^2 - \|\X-\X \w \w^\top\|^2}{\|\X\|^2},$$ but with a little algebra this can be shown to be an equivalent expression.

Approach #2

Okay. Now consider a following example: $\X$ is a $d=2$ dataset with covariance matrix $$\S = \left(\begin{array}{c}1&0.99\\0.99&1\end{array}\right)$$ and $\mathbf w = (\begin{array}{}1&0\end{array})^\top$ is simply an $x$ vector: The total variance is $T=2$. The variance of the projection onto $\w$ (shown in red dots) is equal to $1$. So according to the above logic, the explained variance is equal to $1/2$. And in some sense it is: red dots ("reconstruction") are far away from the corresponding blue dots, so a lot of the variance is "lost".

On the other hand, the two variables have $0.99$ correlation and so are almost identical; saying that one of them describes only $50\%$ of the total variance is weird, because each of them contains "almost all the information" about the second one. We can formalize it as follows: given projection $\X\w$, find a best possible reconstruction $\X\w\v^\top$ with $\v$ not necessarily the same as $\w$, and then compute the reconstruction error and plug it into the expression for the proportion of explained variance: $$R^2_\mathrm{second}=\frac{\|\X\|^2 - \|\X-\X \w \v^\top\|^2}{\|\X\|^2},$$ where $\v$ is chosen such that $\|\X-\X \w \v^\top\|^2$ is minimal (i.e. $R^2$ is maximal). This is exactly equivalent to computing $R^2$ of multivariate regression predicting original dataset $\X$ from the $1$-dimensional projection $\X\w$.

It is a matter of straightforward algebra to use regression solution for $\v$ to find that the whole expression simplifies to $$R^2_\mathrm{second}=\frac{\|\S \w\|^2}{\w^\top \S \w \cdot \mathrm{tr}(\S)}.$$ In the example above this is equal to $0.9901$, which seems reasonable.

Note that if (and only if) $\w$ is one of the eigenvectors of $\S$, i.e. one of the principal axes, with eigenvalue $\lambda$ (so that $\S \w = \lambda \w$), then both approaches to compute $R^2$ coincide and reduce to the familiar PCA expression $$R^2_\mathrm{PCA} = R^2_\mathrm{first} = R^2_\mathrm{second} = \lambda/\mathrm{tr}(\S) = \lambda/\sum \lambda_i.$$

PS. See my answer here for an application of the derived formula to the special case of $\w$ being one of the basis vectors: Variance of the data explained by a single variable.

Appendix. Derivation of the formula for $R^2_\mathrm{second}$

Finding $\v$ minimizing the reconstruction $\|\X-\X \w \v^\top\|^2$ is a regression problem (with $\X \w$ as univariate predictor and $\X$ as multivariate response). Its solution is given by $$\v^\top = \left((\X \w)^\top (\X \w)\right)^{-1}(\X \w)^\top \X = (\w^\top \S \w)^{-1} \w^\top \S.$$

Next, the $R^2$ formula can be simplified as $$R^2=\frac{\|\X\|^2 - \|\X-\X \w \v^\top\|^2}{\|\X\|^2} = \frac{\|\X \w \v^\top\|^2}{\|\X\|^2}$$ due to the Pythagoras theorem, because the hat matrix in regression is an orthogonal projection (but it is also easy to show directly).

Plugging now the equation for $\v$, we obtain for the numerator: $$\|\X \w \v^\top\|^2 = \mathrm{tr}\left(\X \w \v^\top (\X \w \v^\top)^\top\right) = \mathrm{tr}(\X\w\w^\top\S\S\w\w^\top\X^\top)/(\w^\top\S\w)^2=\mathrm{tr}(\w^\top\S\S\w)/(\w^\top\S\w) = \|\S\w\|^2 / (\w^\top\S\w).$$

The denominator is equal to $\|\X\|^2 = \mathrm{tr}(\S)$ resulting in the formula given above.

• I think this is an answer to a different question. For example, it not the case that that optimising your $R^2$ wrt $w$ will give the first PC as the unique answer (in those cases where it is unique). The fact that $(1,0)$ and $\frac {1}{\sqrt {2}}(1,1)$ both give 100% when $X=Y$ is evidence enough. Your proposed method seems to assume that the "normalised" objective function for PCA will always understate the variance explained (yours isn't a normalised PCA objective function as it normalises by the quantity being optimised in PCA). – probabilityislogic Feb 1 '15 at 3:26
• I agree that our answers are to different questions, but it's not clear to me which one OP had in mind. Also, note that my interpretation is not something very weird: it's a standard regression approach: when we say that $x$ explains so and so much variance in $y$, we compute reconstruction error of $\|y-xb\|$ with an optimal $b$, not just $\|y-x\|$. Here is another argument: if all $n$ variables are standardized, then in your approach each one explains $1/n$ amount of variance. This is not very informative: some variables can be much more predictive than others! My approach reflects that. – amoeba Feb 1 '15 at 13:29
• @amoeba (+1) Great answer, it's really helpful! Would you know any reference that tackles this issue? Thanks! – PierreE Apr 6 '16 at 0:24
• @PierreE Thanks. No, I don't think I have any reference for that. – amoeba Apr 6 '16 at 7:29

Let the total variance, $T$, in a data set of vectors be the sum of squared errors (SSE) between the vectors in the data set and the mean vector of the data set, $$T = \sum_{i} (x_i-\bar{x}) \cdot (x_i-\bar{x})$$ where $\bar{x}$ is the mean vector of the data set, $x_i$ is the ith vector in the data set, and $\cdot$ is the dot product of two vectors. Said another way, the total variance is the SSE between each $x_i$ and its predicted value, $f(x_i)$, when we set $f(x_i)=\bar{x}$.

Now let the predictor of $x_i$, $f(x_i)$, be the projection of vector $x_i$ onto a unit vector $c$.

$$f_c(x_i) = (c \cdot x_i)c$$

Then the $SSE$ for a given $c$ is $$SSE_c = \sum_i (x_i - f_c(x_i)) \cdot (x_i - f_c(x_i))$$

I think that if you choose $c$ to minimize $SSE_c$, then $c$ is the first principal component.

If instead you choose $c$ to be the normalized version of the vector $(1, 2, 5, ...)$, then $T-SSE_c$ is the variance in the data described by using $c$ as a predictor.

• This is a reasonable approach, but I think this is not the best way to interpret the question; see my answer for an alternative approach that I argue makes more sense (in particular, see my example figure there). – amoeba Jan 28 '15 at 0:58
• Think about it like that. Imagine a dataset with two standardized identical variables $X=Y$. How much variance is described by $X$? Your calculation gives $50\%$. I argue that the correct answer is $100\%$. – amoeba Jan 28 '15 at 9:48