Skip to content
VidiMaster it, module by module
Module 5/Applications

Principal Component Analysis

PCA is this module's capstone: it takes a cloud of high-dimensional data and finds the few directions that matter. Center the features into a matrix AA, form the covariance matrix C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T}, and — because CC is symmetric — use the Spectral Theorem (or the SVD) to diagonalize it, C=SDSTC=SDS^{\mathsf T}. The orthonormal eigenvectors are the principal components; their eigenvalues λk\lambda_k are the variances captured along each one. Keeping the components with the largest eigenvalues compresses the data with minimal loss — turning a point cloud in R75\mathbb R^{75} into a picture in R2\mathbb R^2.

Before you start — give these a try

Attempting first primes your brain for the lesson — even if you miss. Nothing is graded or saved; it's just a warm-up.

Two features are recorded for n=3n=3 subjects; with subjects as columns the raw data is M=[534654]M=\begin{bmatrix}5&3&4\\6&5&4\end{bmatrix}. Center each feature and compute the covariance matrix C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T} (divide by n−1n-1). Enter CC as a 2×22\times2 matrix.

For the covariance matrix C=[10.50.51]C=\begin{bmatrix}1&0.5\\0.5&1\end{bmatrix}, list its eigenvalues — the variances carried by the two principal components — in decreasing order.

What you’ll be able to do

  • Build the data matrix of a PCA — objects in the columns, features in the rows — then center each feature (and, when scales differ, divide by its standard deviation) to obtain the normalized matrix AA (Remark 15.9, steps 1–2).
  • Form the covariance matrix C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T} and read its entries as sample variances (diagonal) and covariances (off-diagonal), knowing it is symmetric and positive semidefinite (Definition 15.7, §15.5).
  • Apply the Spectral Theorem (Theorem 9.1) to diagonalize C=SDSTC=SDS^{\mathsf T}, identify the principal components as the orthonormal eigenvectors (columns of SS), and explain why they are determined only up to sign.
  • Interpret each eigenvalue λk\lambda_k as the variance along its principal component, compute the total variance tr⁡(C)=∑kλk\operatorname{tr}(C)=\sum_k\lambda_k and the proportion of variance explained λk/∑jλj\lambda_k/\sum_j\lambda_j, and see that the change of basis A′=STAA'=S^{\mathsf T}A makes the new features uncorrelated (C′=DC'=D).
  • Perform dimensionality reduction by keeping the components with the largest eigenvalues and discarding near-zero ones (redundant features), and recognize the equivalent SVD route A=UΣVTA=U\Sigma V^{\mathsf T} with λk=σk2/(n−1)\lambda_k=\sigma_k^2/(n-1) (Theorem 9.3).

In your course

· MATH2015 · Linear Algebra & Probability
§15.6 Application: Principal Component Analysis§15.5 Covariance and Correlation
  • Definition 15.7Covariance
    Cov⁡(X,Y)=E[(X−μX)(Y−μY)]\operatorname{Cov}(X,Y)=\mathbb E[(X-\mu_X)(Y-\mu_Y)], with Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X,X)=\operatorname{Var}(X); assembling all pairwise sample covariances of the centered features gives the covariance matrix C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T}.
  • Proposition 15.17Properties of covariance (bilinearity)
    Covariance is symmetric and bilinear: Cov⁡(X,Y)=Cov⁡(Y,X)\operatorname{Cov}(X,Y)=\operatorname{Cov}(Y,X) and Cov⁡ ⁣(∑iaiXi,∑jbjYj)=∑i,jaibjCov⁡(Xi,Yj)\operatorname{Cov}\!\big(\sum_i a_iX_i,\sum_j b_jY_j\big)=\sum_{i,j}a_ib_j\operatorname{Cov}(X_i,Y_j) — the reason CC is symmetric (Remark 15.7 notes the analogy with inner products).
  • Remark 15.8The key step of PCA
    The central mathematical step is a change of basis that makes the features uncorrelated; one then focuses on the principal components to understand the data's structure.
  • Remark 15.9The PCA procedure
    The seven-step recipe: build the feature matrix, normalize to AA, form C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T}, eigen-decompose (sorted eigenvalues, unit eigenvectors as columns of an orthogonal SS), transform A′=STAA'=S^{\mathsf T}A, keep the leading components, and interpret.
  • Theorem 9.1Spectral Theorem
    A real symmetric matrix has an orthonormal basis of eigenvectors: C=SDSTC=SDS^{\mathsf T} with SS orthogonal and DD diagonal. This is what makes the covariance matrix orthogonally diagonalizable in step 4, and §15.6 cites it explicitly.
  • Theorem 9.3Singular value decomposition
    A=UΣVTA=U\Sigma V^{\mathsf T}; for centered AA the left singular vectors are the principal components and λk=σk2/(n−1)\lambda_k=\sigma_k^2/(n-1), giving PCA without forming CC.
This lesson isolates the linear-algebra core of §15.6. The course presents PCA inside the probability chapter: it defines covariance in §15.5 (Definition 15.7), builds the covariance matrix C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T} in §15.6, and summarizes the seven-step procedure in Remark 15.9. The decisive step — diagonalizing the symmetric CC — rests on this module's Spectral Theorem (Theorem 9.1), which §15.6 cites explicitly; the equivalent, numerically preferable SVD route (Theorem 9.3) is added here from this module (the notes derive PCA via the Spectral Theorem, not the SVD). Because principal components are eigenvectors, they are defined only up to sign (and rotation within equal-eigenvalue subspaces), so no principal-component vector is auto-graded — only sign-independent quantities are checked: the covariance matrix, eigenvalues/variances, total variance tr⁡(C)=∑kλk\operatorname{tr}(C)=\sum_k\lambda_k, proportions of variance explained, and counts of components. All covariance matrices and eigenvalues were recomputed with numpy (np.cov with the n−1n-1 convention, and eigh); the OCR'd numerals and glyphs in the source were not trusted — indeed the notes' stated eigenvalues 1.90, 1.19, 0.0011.90,\ 1.19,\ 0.001 do not sum to the trace 33, so the middle value should read ≈1.10\approx 1.10.
1

Centering (and scaling) the data: the matrix $A$

PCA starts from raw measurements and strips away the parts that carry no shape information. Arrange the data as a matrix MM whose columns are the objects (subjects, samples) and whose rows are the features (Remark 15.9, step 1). The first normalization step is centering: from each feature (row) subtract its sample mean, si′=si−μi1,μi=1n∑ksik,s_i'=s_i-\mu_i\mathbf 1,\qquad \mu_i=\tfrac1n\sum_{k}s_{ik}, so every row of the resulting matrix has mean 00. If the features live on very different scales (heights in cm versus dimensionless ratios, say), also divide each centered row by its sample standard deviation σi\sigma_i, giving rows of unit variance — the standardized version, which makes CC a correlation matrix. The normalized data is the matrix AA. Centering is not optional: covariance is measured about the mean, and skipping it folds the mean vector into the first principal component and corrupts every eigenvalue.

2

The covariance matrix $C=\tfrac{1}{n-1}AA^{\mathsf T}$

To see how features move together we build the covariance matrix C=1n−1AAT∈Rd×dC=\frac{1}{n-1}AA^{\mathsf T}\in\mathbb R^{d\times d} for dd features (Remark 15.9, step 3). Its entry Cij=1n−1∑kaikajkC_{ij}=\frac{1}{n-1}\sum_k a_{ik}a_{jk} is exactly the sample covariance Cov⁡(si,sj)\operatorname{Cov}(s_i,s_j) of features ii and jj (Definition 15.7, §15.5), and the diagonal entries CiiC_{ii} are the feature variances. Dividing by n−1n-1 rather than nn is the unbiased sample convention used in the course. Two structural facts drive everything that follows. First, CC is symmetric, CT=CC^{\mathsf T}=C, because Cov⁡(si,sj)=Cov⁡(sj,si)\operatorname{Cov}(s_i,s_j)=\operatorname{Cov}(s_j,s_i) — covariance is symmetric and bilinear, behaving like an inner product on centered features (Proposition 15.17, Remark 15.7). Second, C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T} is positive semidefinite, so all its eigenvalues are ≥0\ge 0 — reassuring, since they will turn out to be variances. A large ∣Cij∣|C_{ij}| signals redundancy between two features; a near-zero one means they are linearly unrelated.

3

Diagonalizing $C$: the principal components

The whole point of PCA is to change coordinates so the features become uncorrelated, i.e. so the covariance matrix becomes diagonal (Remark 15.8). Because CC is real and symmetric, the Spectral Theorem (Theorem 9.1) guarantees an orthonormal basis of eigenvectors and an orthogonal matrix SS — its columns the eigenvectors — with C=SDST,D=diag⁡(λ1,…,λd).C=SDS^{\mathsf T},\qquad D=\operatorname{diag}(\lambda_1,\dots,\lambda_d). Rewrite the data in the new basis via A′=STAA'=S^{\mathsf T}A (Remark 15.9, step 5; here S−1=STS^{-1}=S^{\mathsf T} since SS is orthogonal). The covariance matrix of the new features is C′=1n−1A′A′T=STCS=D,C'=\frac{1}{n-1}A'A'^{\mathsf T}=S^{\mathsf T}CS=D, which is diagonal: every covariance is now 00. The principal components are these orthonormal eigenvectors of CC (the columns of SS), sorted so that λ1≥λ2≥⋯≥λd\lambda_1\ge\lambda_2\ge\cdots\ge\lambda_d. They are orthogonal by construction but not unique: an eigenvector is defined only up to sign (and, within a repeated-eigenvalue subspace, up to rotation), so u\mathbf u and −u-\mathbf u are equally valid principal components.

4

Eigenvalues as variance; dimensionality reduction (and the SVD route)

In the diagonalized covariance matrix DD, the eigenvalue λk\lambda_k is the variance of the data along the kk-th principal component. This gives a clean budget for information: the total variance is tr⁡(C)=∑iCii=∑kλk,\operatorname{tr}(C)=\sum_i C_{ii}=\sum_k\lambda_k, and the proportion of variance explained by component kk is λk/∑jλj\lambda_k/\sum_j\lambda_j. Dimensionality reduction keeps the components with the largest eigenvalues and discards the smallest: if λr,…,λd\lambda_r,\dots,\lambda_d are tiny, the matching new features barely vary across subjects and carry almost no information, so projecting onto the top components (the first rows of A′A') loses very little (Remark 15.9, steps 6–7). A near-zero eigenvalue flags a redundant feature — in the course's example the covariance matrix of height, weight and BMI has an eigenvalue ≈0\approx 0 because BMI is determined by the other two. The same principal directions come directly from the singular value decomposition (Theorem 9.3): if A=UΣVTA=U\Sigma V^{\mathsf T}, the left singular vectors (columns of UU) are the principal components and λk=σk2/(n−1)\lambda_k=\sigma_k^2/(n-1). Working from the SVD of AA avoids forming CC explicitly and is the numerically preferred route.

Definition 15.7 — Covariance (and the covariance matrix)

For random variables X,YX,Y, Cov⁡(X,Y)=E[(X−μX)(Y−μY)]\operatorname{Cov}(X,Y)=\mathbb E[(X-\mu_X)(Y-\mu_Y)], and Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X,X)=\operatorname{Var}(X). For centered sample data stored in A∈Rd×nA\in\mathbb R^{d\times n} (features ×\times objects), the covariance matrix is C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T}, whose (i,j)(i,j) entry is the sample covariance of features ii and jj.

Intuition. Covariance measures whether two features rise and fall together: positive when they move the same way, negative when opposite, zero when linearly unrelated (§15.5). Packing all pairwise covariances into one matrix CC makes the relationships a single algebraic object. Because Cov⁡\operatorname{Cov} is symmetric and bilinear — it behaves like an inner product on centered features (Remark 15.7) — CC is symmetric; and since C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T}, it is positive semidefinite, so its eigenvalues are real and ≥0\ge 0.
Theorem 9.1 — Spectral Theorem

Every real symmetric matrix CC has an orthonormal basis of eigenvectors with real eigenvalues; equivalently there is an orthogonal matrix SS (STS=IS^{\mathsf T}S=I) and a diagonal D=diag⁡(λ1,…,λd)D=\operatorname{diag}(\lambda_1,\dots,\lambda_d) with C=SDSTC=SDS^{\mathsf T}.

Intuition. This is the engine of PCA. The covariance matrix CC is symmetric, so it is orthogonally diagonalizable — its eigenvectors can be chosen mutually perpendicular and of unit length. Adopting them as a new coordinate system (via the orthogonal SS) turns CC into the diagonal DD: in the new basis C′=STCS=DC'=S^{\mathsf T}CS=D, so all covariances vanish and the features become uncorrelated. The diagonal entries left behind are the eigenvalues, which are exactly the variances of the new features.
Remark 15.9 — The PCA procedure

(1) Build the feature matrix MM (objects in columns, features in rows). (2) Normalize: subtract each row's mean, and if the row variances differ greatly divide by the row's standard deviation, giving AA. (3) Compute C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T}. (4) Find the eigenvalues and eigenvectors of CC; sort the eigenvalues decreasingly and normalize the eigenvectors to length 11, placing them in the columns of the orthogonal matrix SS. (5) Transform: A′=STAA'=S^{\mathsf T}A. (6) From the eigenvalue sizes decide how many leading components to keep — the principal components are the first rows of A′A'. (7) Interpret them.

Intuition. Steps 1–3 turn raw data into the symmetric covariance matrix; steps 4–5 apply the Spectral Theorem to rotate into the uncorrelated eigenbasis; steps 6–7 are where eigenvalue size guides truncation and a human reads meaning into the surviving axes. The only genuinely linear-algebraic choices — which eigenvectors, and in what order — are fixed by sorting the eigenvalues, with each eigenvector's sign left free.
Theorem 9.3 — Singular value decomposition

Every matrix A∈Rd×nA\in\mathbb R^{d\times n} factors as A=UΣVTA=U\Sigma V^{\mathsf T} with U,VU,V orthogonal and Σ\Sigma diagonal carrying the nonnegative singular values σ1≥σ2≥⋯≥0\sigma_1\ge\sigma_2\ge\cdots\ge 0.

Intuition. The SVD delivers PCA without ever forming CC. For centered AA, AAT=UΣ2UTAA^{\mathsf T}=U\Sigma^2U^{\mathsf T}, so the left singular vectors (columns of UU) are the eigenvectors of C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T} — the principal components — and the variances are λk=σk2/(n−1)\lambda_k=\sigma_k^2/(n-1). Computing the SVD of AA directly is more numerically stable than squaring the data into CC first, which is why practical PCA uses it; it ties the application to this module's two capstone theorems at once.

Worked examples

Example 1

A tiny study records two features for n=3n=3 subjects. With subjects as columns, the raw data matrix is M=[534654]M=\begin{bmatrix}5&3&4\\6&5&4\end{bmatrix} (row 1 == feature s1s_1, row 2 == feature s2s_2). Center each feature and compute the covariance matrix C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T} (sample convention, divide by n−1n-1).

  1. 1

    Row means (Remark 15.9, step 2): μ1=5+3+43=4\mu_1=\tfrac{5+3+4}{3}=4 and μ2=6+5+43=5\mu_2=\tfrac{6+5+4}{3}=5.

  2. 2

    Subtract each row mean to center: A=[1−1010−1]A=\begin{bmatrix}1&-1&0\\1&0&-1\end{bmatrix}; each row now has mean 00.

  3. 3

    Form AATAA^{\mathsf T}: the (1,1)(1,1) entry is 12+(−1)2+02=21^2+(-1)^2+0^2=2, the (2,2)(2,2) entry is 12+02+(−1)2=21^2+0^2+(-1)^2=2, and the off-diagonal is 1⋅1+(−1)⋅0+0⋅(−1)=11\cdot1+(-1)\cdot0+0\cdot(-1)=1, so AAT=[2112]AA^{\mathsf T}=\begin{bmatrix}2&1\\1&2\end{bmatrix}.

  4. 4

    Divide by n−1=2n-1=2: C=12[2112]=[10.50.51]C=\tfrac12\begin{bmatrix}2&1\\1&2\end{bmatrix}=\begin{bmatrix}1&0.5\\0.5&1\end{bmatrix}. The diagonal gives the sample variances (11 each) and the off-diagonal the covariance Cov⁡(s1,s2)=0.5\operatorname{Cov}(s_1,s_2)=0.5 (Definition 15.7).

Answer. C=[10.50.51]C=\begin{bmatrix}1&0.5\\0.5&1\end{bmatrix}: both features have variance 11 and are positively correlated, with covariance 0.50.5.
Example 2

Continuing with C=[10.50.51]C=\begin{bmatrix}1&0.5\\0.5&1\end{bmatrix}, use the Spectral Theorem to find the variances carried by the principal components, the total variance, and the proportion of variance explained by the first principal component. What are the principal components themselves?

Example 3

Three features are measured on n=4n=4 subjects; the already-centered data matrix is A=[33−3−33−33−3600−6]A=\begin{bmatrix}3&3&-3&-3\\3&-3&3&-3\\6&0&0&-6\end{bmatrix}, where row 3 is exactly row 1 ++ row 2. Compute C=1n−1AATC=\frac{1}{n-1}AA^{\mathsf T}, find its eigenvalues, and decide how many principal components to keep to capture at least 90%90\% of the variance.