Tutorials › Multivariate Statistics (Graduate) › A Worked Sample PCA with Three Variables

Population and Sample PCA · Tutorial 492 of 975

A Worked Sample PCA with Three Variables

Work through a centered three-variable dataset from sample covariance to principal component scores and a rank-two reconstruction, checking each calculation along the way.

Intermediate 10 min read

What You'll Learn

  • Form the sample covariance matrix of a centered three-variable dataset using the sample divisor.
  • Verify ordered eigenpairs for a sample covariance matrix.
  • Calculate observation scores by projecting onto the sample principal directions.
  • Relate the sample covariance of the scores to the eigenvalues.
  • Reconstruct observations using retained principal directions.
  • Check reconstruction error against the variance in the omitted component.

From Centered Data to Sample PCA

A sample PCA calculation connects several objects that are easy to confuse: the sample covariance matrix, its eigenvectors, the scores of individual observations, and the reconstructed data. Keeping their roles distinct makes the calculation auditable. In this tutorial, each observation has three variables, the observations have already been centered, and PCA uses the ordinary sample covariance matrix.

Let \(X\) be an \(n \times 3\) data matrix whose \(i\)th row is the centered observation \(x_i^T\). Its sample covariance matrix is \(S=X^T X/(n-1)\). If the columns of \(V\) are orthonormal eigenvectors of \(S\), ordered from largest eigenvalue to smallest, the principal component scores form the matrix \(T=XV\). The score in component \(j\) for observation \(i\) is \(t_{ij}=x_i^T v_j\).

Definition: For centered observations \(x_1,\ldots,x_n\) in three variables, the sample PCA directions are orthonormal eigenvectors \(v_1,v_2,v_3\) of \(S=(1/(n-1))\sum_{i=1}^n x_i x_i^T\), ordered by their eigenvalues \(\lambda_1\geq\lambda_2\geq\lambda_3\geq 0\). The sample score of observation \(i\) on direction \(v_j\) is \(t_{ij}=v_j^T x_i\).

The divisor \(n-1\) is used because this is the sample covariance matrix. Replacing it by another positive common divisor would multiply every eigenvalue by the same positive number and leave the eigenvectors unchanged; here we retain the sample divisor so that the eigenvalues have the usual sample-variance interpretation.

Worked Example: Forming the Sample Covariance Matrix

Consider four centered observations on three variables. The data matrix is

$$ X= \begin{pmatrix} \frac{5}{2\sqrt{2}} & \frac{1}{2\sqrt{2}} & \frac12\\ \frac{1}{2\sqrt{2}} & \frac{5}{2\sqrt{2}} & -\frac12\\ -\frac{1}{2\sqrt{2}} & -\frac{5}{2\sqrt{2}} & -\frac12\\ -\frac{5}{2\sqrt{2}} & -\frac{1}{2\sqrt{2}} & \frac12 \end{pmatrix}. $$

Each column sums to zero: for the first column the numerators are \(5+1-1-5=0\), for the second they are \(1+5-5-1=0\), and for the third they are \(1-1-1+1=0\). Thus the displayed data are centered. Since \(n=4\), the sample covariance divisor is \(n-1=3\).

For the first diagonal entry, the sum of squares in column one is \(25/8+1/8+1/8+25/8=52/8=13/2\). Hence \(S_{11}=(13/2)/3=13/6\). The second column has the same sum of squares, so \(S_{22}=13/6\). The third column has sum of squares \(1/4+1/4+1/4+1/4=1\), giving \(S_{33}=1/3\).

For the covariance between the first two variables, the four rowwise products are \(5/8,5/8,5/8,5/8\). Their sum is \(5/2\), so \(S_{12}=S_{21}=(5/2)/3=5/6\). For the first and third variables, the rowwise products are \(5/(4\sqrt{2}),-1/(4\sqrt{2}),1/(4\sqrt{2}),-5/(4\sqrt{2})\); they sum to zero. The products for the second and third variables also sum to zero: they are \(1/(4\sqrt{2}),-5/(4\sqrt{2}),5/(4\sqrt{2}),-1/(4\sqrt{2})\). Therefore

$$ S=\frac{1}{3}X^T X = \begin{pmatrix} 13/6 & 5/6 & 0\\ 5/6 & 13/6 & 0\\ 0 & 0 & 1/3 \end{pmatrix}. $$

This is a symmetric matrix, as a sample covariance matrix must be. Its trace is \(13/6+13/6+1/3=14/3\), a useful check for the eigenvalue calculation that follows.

Eigenpairs and Observation Scores

The first two coordinates of \(S\) form a symmetric two-by-two block. Its diagonal entries are \(13/6\) and its off-diagonal entries are \(5/6\). The vector \((1,1)^T\) is an eigenvector of that block with eigenvalue \(13/6+5/6=3\); the vector \((1,-1)^T\) has eigenvalue \(13/6-5/6=4/3\). The third coordinate is already an eigenvector with eigenvalue \(1/3\). Normalizing the first two vectors gives the ordered eigenpairs

$$ \lambda_1=3,\quad v_1=\frac{1}{\sqrt{2}}(1,1,0)^T;\qquad \lambda_2=\frac{4}{3},\quad v_2=\frac{1}{\sqrt{2}}(1,-1,0)^T;\qquad \lambda_3=\frac{1}{3},\quad v_3=(0,0,1)^T. $$

The vectors have unit length and are mutually orthogonal. Multiplication verifies the eigenvalue equations. For example, the first coordinate of \(Sv_1\) is \((13/6+5/6)/\sqrt{2}=3/\sqrt{2}\), and its second coordinate is the same, so \(Sv_1=3v_1\). For \(v_2\), those coordinates are \((13/6-5/6)/\sqrt{2}=4/(3\sqrt{2})\) and its negative, so \(Sv_2=(4/3)v_2\). Finally, \(Sv_3=(1/3)v_3\). The eigenvalues are nonnegative and sum to \(14/3\), agreeing with the trace check.

Worked Example: Calculating the Three Scores

Use the centered data matrix above and the three ordered eigenvectors just verified. The score matrix is \(T=XV\), where \(V=(v_1\ v_2\ v_3)\). For the first observation \(x_1=(5/(2\sqrt{2}),1/(2\sqrt{2}),1/2)^T\), the scores are

$$ v_1^T x_1=\frac{1}{\sqrt{2}}\left(\frac{5}{2\sqrt{2}}+\frac{1}{2\sqrt{2}}\right)=\frac32,\qquad v_2^T x_1=\frac{1}{\sqrt{2}}\left(\frac{5}{2\sqrt{2}}-\frac{1}{2\sqrt{2}}\right)=1,\qquad v_3^T x_1=\frac12. $$

The same projections for each row give

$$ T=XV= \begin{pmatrix} 3/2 & 1 & 1/2\\ 3/2 & -1 & -1/2\\ -3/2 & 1 & -1/2\\ -3/2 & -1 & 1/2 \end{pmatrix}. $$

For instance, row two has first score \((1/(2\sqrt{2})+5/(2\sqrt{2}))/\sqrt{2}=3/2\), second score \((1/(2\sqrt{2})-5/(2\sqrt{2}))/\sqrt{2}=-1\), and third score \(-1/2\). Row three gives \((-3/2,1,-1/2)\), and row four gives \((-3/2,-1,1/2)\). Each score column sums to zero, consistent with the centered observations.

Why the Score Variances Equal the Eigenvalues

Theorem: Suppose \(X\) is a centered \(n \times 3\) data matrix, \(S=X^TX/(n-1)\), and \(V\) is an orthogonal matrix whose columns are eigenvectors of \(S\), with \(V^T S V=\operatorname{diag}(\lambda_1,\lambda_2,\lambda_3)\). For \(T=XV\), the sample covariance matrix of the score columns is \(\operatorname{diag}(\lambda_1,\lambda_2,\lambda_3)\).

Proof. The score matrix is centered because the columns of \(X\) sum to zero and \(V\) is fixed: each column of \(T=XV\) is a linear combination of centered columns. Its sample covariance matrix is therefore \(T^TT/(n-1)\). Substitute \(T=XV\) and use the definition of \(S\):

$$ \frac{T^T T}{n-1} =\frac{(XV)^T(XV)}{n-1} =V^T\left(\frac{X^T X}{n-1}\right)V =V^T S V =\operatorname{diag}(\lambda_1,\lambda_2,\lambda_3). $$

Thus the score columns have sample variances \(\lambda_1,\lambda_2,\lambda_3\), and their sample covariances with one another are zero. In this example, the sums of squared score values are \(9\), \(4\), and \(1\). Dividing by \(n-1=3\) gives \(3\), \(4/3\), and \(1/3\), precisely the eigenvalues. The trace check also has a score-based interpretation: total sample variance is the sum of the three score variances, \(3+4/3+1/3=14/3\). \(\square\)

Retaining Two Components

Keeping the first two components means projecting each centered observation onto the span of \(v_1\) and \(v_2\). Write \(V_2=(v_1\ v_2)\). The reconstructed data matrix is \(\widehat{X}_2=XV_2V_2^T\); in score form, its rows are the first two scores multiplied back by their directions. This calculation retains the part of each observation along the first two directions and omits its part along \(v_3\).

Worked Example: Reconstructing with Two Components

For the first observation, the retained scores are \(3/2\) and \(1\). Its two-component reconstruction is

$$ \widehat{x}_{1,2} =\frac32v_1+1v_2 =\frac32\frac{(1,1,0)^T}{\sqrt{2}}+\frac{(1,-1,0)^T}{\sqrt{2}} =\left(\frac{5}{2\sqrt{2}},\frac{1}{2\sqrt{2}},0\right)^T. $$

The original third coordinate was \(1/2\), so the residual is \(x_1-\widehat{x}_{1,2}=(0,0,1/2)^T\). Applying the same calculation to all four rows gives

$$ \widehat{X}_2= \begin{pmatrix} 5/(2\sqrt{2}) & 1/(2\sqrt{2}) & 0\\ 1/(2\sqrt{2}) & 5/(2\sqrt{2}) & 0\\ -1/(2\sqrt{2}) & -5/(2\sqrt{2}) & 0\\ -5/(2\sqrt{2}) & -1/(2\sqrt{2}) & 0 \end{pmatrix}. $$

The omitted residuals are \((0,0,1/2)^T\), \((0,0,-1/2)^T\), \((0,0,-1/2)^T\), and \((0,0,1/2)^T\). Their squared lengths sum to \(4(1/4)=1\). Equivalently, the squared Frobenius norm of \(X-\widehat{X}_2\) is \(1\). The omitted component has sample variance \(\lambda_3=1/3\); multiplying by \(n-1=3\) gives the same total squared error, \(3(1/3)=1\).

The retained variance is \(\lambda_1+\lambda_2=3+4/3=13/3\), while total variance is \(14/3\). Thus the fraction of total sample variance associated with the retained components is \((13/3)/(14/3)=13/14\).

Result: If \(V\) is a full orthonormal eigenvector matrix for \(S\), with ordered eigenvalues \(\lambda_1\geq\lambda_2\geq\lambda_3\), then retaining the first \(k\) directions gives squared reconstruction error \(\lVert X-XV_kV_k^T\rVert_F^2=(n-1)\sum_{j=k+1}^3\lambda_j\), for \(k=1\) or \(2\).

To verify the error identity, note that \(X=XV V^T\), because \(VV^T=I\). The residual after retaining \(k\) directions is consequently

$$ X-XV_kV_k^T=\sum_{j=k+1}^3 (Xv_j)v_j^T. $$

The matrices \((Xv_j)v_j^T\) for distinct \(j\) are orthogonal under the Frobenius inner product: for \(j\ne \ell\), their inner product is \(\operatorname{tr}(v_j (Xv_j)^T Xv_\ell v_\ell^T) =(v_j^Tv_\ell)((Xv_j)^TXv_\ell)=0\), since \(v_j^Tv_\ell=0\). The squared Frobenius norm of each term is \(\lVert Xv_j\rVert^2\lVert v_j\rVert^2=\lVert Xv_j\rVert^2\), since \(v_j\) has unit length. Also, \(\lVert Xv_j\rVert^2=v_j^T X^TXv_j=(n-1)v_j^TSv_j=(n-1)\lambda_j\). Adding these squared norms for the omitted directions proves the identity. \(\square\)

Common Mistakes and Practical Checks

A common mistake is to treat an eigenvalue as the sum of squared scores rather than their sample variance. In this example, the first score column has sum of squares \(9\), but its sample variance is \(9/(4-1)=3\), which is the eigenvalue. The divisor matters when interpreting eigenvalues as sample variances, even though it does not change the sample directions.

Another mistake is to reconstruct using the score values alone. Scores are coordinates in the principal-direction system; to return to the original three-variable coordinates, multiply each retained score by its eigenvector and add the resulting vectors. For two components, the first observation is reconstructed as \((3/2)v_1+1v_2\), not as the three-number vector \((3/2,1,0)\).

Several checks catch arithmetic errors: the covariance matrix should be symmetric; the eigenvectors should have unit length and be mutually orthogonal; the eigenvalues should sum to the trace of \(S\); and the total squared reconstruction error should match the omitted eigenvalues multiplied by \(n-1\). These checks do not replace the calculation, but they make it much easier to locate an inconsistent entry or divisor.

Check Your Understanding

Use the definitions and calculation above to answer the following questions.

  1. Why is the sample covariance divisor \(3\) for this dataset?
  2. Verify directly that \(v_2=(1,-1,0)^T/\sqrt{2}\) has unit length and is an eigenvector of \(S\) with eigenvalue \(4/3\).
  3. What is the sample variance of the third score column, and how can it be checked from its four entries?
  4. For reconstruction with only the first component, what is the squared Frobenius reconstruction error according to the omitted eigenvalues?
  5. Explain why the first observation's two-component reconstruction has a zero third coordinate, although the original third coordinate is \(1/2\).
Clinical Biostats