Skip to main content
© 2026 ePowerAI — free, open courseware. No login required.
CoursesContact
ePOWERAI
CoursesContact
Week 14: PCA, the Fourier Matrix and the FFT
Linear Algebra

Foundations

  1. 01Start Here: Python and the Math the Labs Use
  1. 02Week 1: Vectors and Linear Combinations
  2. 03Week 2: Linear Transformations and Matrices
  3. 04Week 3: Elimination and LU Factorization
  4. 05Week 4: Determinants
  5. 06Week 5: Vector Spaces, Independence, and Basis
  6. 07Week 6: The Four Fundamental Subspaces
  7. 08Week 7: Orthogonality and Projections
  8. 09Week 8: Least Squares and QR
  9. 10Week 9: Eigenvalues and Eigenvectors
  10. 11Week 10: Diagonalization and Markov Matrices
  11. 12Week 11: Differential Equations
  12. 13Week 12: Symmetric and Positive Definite Matrices
  13. 14Week 13: The SVD and Complex Matrices
  14. 15Week 14: PCA, the Fourier Matrix and the FFT
Week 14· Linear Algebra7 min read

Week 14: PCA, the Fourier Matrix and the FFT

Learning Outcomes
  • Perform PCA on a dataset using the SVD of the centred data matrix
  • Interpret principal components as directions of maximum variance, and singular values squared as variance
  • Explain why dropping principal components loses no more variance than dropping raw features
  • Construct the Fourier matrix and explain its role in the discrete Fourier transform
  • Explain how the FFT computes the DFT in O(nlog⁡n)O(n\log n)O(nlogn) instead of O(n2)O(n^2)O(n2)
Prerequisites

Background knowledge assumed:

  • The singular value decomposition
  • Complex/unitary matrices

Recommended: Review Week 13: The SVD and Complex Matrices before starting.

You have 240 measurements of three features, x, y and z, and want to keep two numbers each. Feature z has the smallest variance, 2.89 against 5.81 and 4.56, so you drop it. It throws away 21.8% of the variance. The cloud is thin, but not along any raw axis.

A cloud of 240 points in 3D, with its principal axes v₁, v₂ and v₃ drawn at two standard deviations (σ = 3, 2, 0.5), the PC1–PC2 plane and the raw axes x, y and z. The slider flattens the cloud onto the PC1–PC2 plane from t = 0 to 1, then onto the PC1 line from 1 to 2. t = 0. The full cloud. PC1, PC2 and PC3 carry 67.9%, 30.2% and 1.9% of the variance. The raw features x, y and z carry 43.8%, 34.4% and 21.8%.

t = 0. The full cloud. PC1, PC2 and PC3 carry 67.9%, 30.2% and 1.9% of the variance. The raw features x, y and z carry 43.8%, 34.4% and 21.8%.

The amber line is PC1, the solid line PC2 and the dashed line PC3, each drawn two standard deviations either side of the centre. Slide ttt from 000 to 111 and every point moves onto the PC1–PC2 plane, keeping 98.1% of the variance. From 111 to 222 the points move onto the PC1 line, which keeps 67.9%.

Principal components from the SVD#

PCAPrincipal Component Analysis is the statistical reading of Week 13's SVD. Collect mmm observations of nnn features into X∈Rm×nX \in \mathbb{R}^{m \times n}X∈Rm×n, rows as observations. Subtract each column's mean, so the cloud sits at the origin, and call the result X~\tilde{X}X~. Uncentred, the first direction is pulled toward the mean.

Take the SVD X~=UΣVT\tilde{X} = U\Sigma V^TX~=UΣVT. The principal components are the columns viv_ivi​ of VVV, ordered by decreasing σi\sigma_iσi​.

Why maximum variance. For a unit vector www, the variance of the data along www is

1m−1∥X~w∥2=wTCw,C=X~TX~m−1=V ΣTΣm−1 VT.\frac{1}{m-1}\|\tilde{X}w\|^2 = w^T C w, \qquad C = \frac{\tilde{X}^T\tilde{X}}{m-1} = V\,\frac{\Sigma^T\Sigma}{m-1}\,V^T.m−11​∥X~w∥2=wTCw,C=m−1X~TX~​=Vm−1ΣTΣ​VT.

CCC is the sample covariance. With y=VTwy = V^Twy=VTw, wTCw=∑iλiyi2≤λ1w^TCw = \sum_i \lambda_i y_i^2 \le \lambda_1wTCw=∑i​λi​yi2​≤λ1​, so it is largest at the top eigenvector v1v_1v1​, with value σ12/(m−1)\sigma_1^2/(m-1)σ12​/(m−1).

The scene's numbers#

The scene's cloud is built so that its covariance is exact:

C=136(2091101011016410010100104)=V(940.25)VT,C = \frac{1}{36}\begin{pmatrix} 209 & 110 & 10 \\ 110 & 164 & 100 \\ 10 & 100 & 104 \end{pmatrix} = V \begin{pmatrix} 9 & & \\ & 4 & \\ & & 0.25 \end{pmatrix} V^T,C=361​​20911010​110164100​10100104​​=V​9​4​0.25​​VT,

with Week 13's axes v1=(2,2,1)/3v_1 = (2, 2, 1)/3v1​=(2,2,1)/3, v2=(−2,1,2)/3v_2 = (-2, 1, 2)/3v2​=(−2,1,2)/3 and v3=(1,−2,2)/3v_3 = (1, -2, 2)/3v3​=(1,−2,2)/3. The total variance is the trace, and the trace does not change with the basis:

209+164+10436=13.25=9+4+0.25.\frac{209 + 164 + 104}{36} = 13.25 = 9 + 4 + 0.25.36209+164+104​=13.25=9+4+0.25.

So PC1, PC2 and PC3 carry 9/13.25=67.9%9/13.25 = 67.9\%9/13.25=67.9%, 30.2%30.2\%30.2% and 1.9%1.9\%1.9%. The diagonal of CCC gives the raw features 43.8%43.8\%43.8%, 34.4%34.4\%34.4% and 21.8%21.8\%21.8%.

Dropping a raw feature is not dropping a component. Each raw variance is a mix of all three eigenvalues. For zzz, C33=19(1⋅9+4⋅4+4⋅0.25)=2.89C_{33} = \tfrac{1}{9}(1 \cdot 9 + 4 \cdot 4 + 4 \cdot 0.25) = 2.89C33​=91​(1⋅9+4⋅4+4⋅0.25)=2.89, because zzz leans on v2v_2v2​ and v3v_3v3​ alike. No choice of two raw features keeps more than 78.2%78.2\%78.2%. The best plane keeps 98.1%98.1\%98.1%, and it is oblique.

Truncation is projection. Keeping kkk components replaces X~\tilde{X}X~ by X~k=UkΣkVkT\tilde{X}_k = U_k\Sigma_kV_k^TX~k​=Uk​Σk​VkT​, which is the slider at t=1t = 1t=1 for k=2k = 2k=2. Each point moves to x~−(x~⋅v3)v3\tilde{x} - (\tilde{x}\cdot v_3)v_3x~−(x~⋅v3​)v3​, and the share of variance lost is σ32/∑jσj2\sigma_3^2/\sum_j\sigma_j^2σ32​/∑j​σj2​. By Eckart–Young this is the best rank-kkk approximation in both the Frobenius and the spectral norm, so no other plane keeps more.

The Fourier matrix and the FFT#

Week 13 ended with unitary matrices. The most used one is the Fourier matrix. With ω=e2πi/n\omega = e^{2\pi i/n}ω=e2πi/n, Fjk=ωjkF_{jk} = \omega^{jk}Fjk​=ωjk for j,k=0,…,n−1j, k = 0, \ldots, n-1j,k=0,…,n−1, and x^=Fx\hat{x} = Fxx^=Fx is the discrete Fourier transform (DFTDiscrete Fourier Transform), x^j=∑kxkωjk\hat{x}_j = \sum_k x_k\omega^{jk}x^j​=∑k​xk​ωjk. NumPy and most libraries use the conjugate sign, F‾x\overline{F}xFx. For n=4n = 4n=4, ω=i\omega = iω=i and

F4=(11111i−1−i1−11−11−i−1i).F_4 = \begin{pmatrix} 1 & 1 & 1 & 1 \\ 1 & i & -1 & -i \\ 1 & -1 & 1 & -1 \\ 1 & -i & -1 & i \end{pmatrix}.F4​=​1111​1i−1−i​1−11−1​1−i−1i​​.

Its columns are orthogonal: FHF=nIF^HF = nIFHF=nI, so F/nF/\sqrt{n}F/n​ is unitary and x=1nFHx^x = \tfrac{1}{n}F^H\hat{x}x=n1​FHx^.

The product FxFxFx costs O(n2)O(n^2)O(n2). The FFTFast Fourier Transform splits xxx into its even and odd entries, transforms each half at size n/2n/2n/2 (with ω2\omega^2ω2), and combines them, for nnn a power of 2:

x^j=e^j+ωjo^j,x^j+n/2=e^j−ωjo^j,j<n/2.\hat{x}_j = \hat{e}_j + \omega^j\hat{o}_j, \qquad \hat{x}_{j+n/2} = \hat{e}_j - \omega^j\hat{o}_j, \qquad j < n/2.x^j​=e^j​+ωjo^j​,x^j+n/2​=e^j​−ωjo^j​,j<n/2.

The cost obeys T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n)T(n)=2T(n/2)+O(n), so T(n)=O(nlog⁡n)T(n) = O(n\log n)T(n)=O(nlogn): log⁡2n\log_2 nlog2​n levels of O(n)O(n)O(n) work.

Course recap#

  • A=LUA = LUA=LU (Weeks 3–4) turns Ax=bAx = bAx=b into two triangular solves.
  • A=QRA = QRA=QR (Weeks 7–8) solves least squares without the normal equations.
  • A=SΛS−1A = S\Lambda S^{-1}A=SΛS−1 and A=QΛQTA = Q\Lambda Q^TA=QΛQT (Weeks 9–12) give powers and quadratic forms.
  • A=UΣVTA = U\Sigma V^TA=UΣVT (Weeks 13–14) works for every matrix, and for centred data its VVV and σi\sigma_iσi​ are PCA.
  • F/nF/\sqrt{n}F/n​ is a unitary change of basis, made cheap by the FFT.

Knowledge Check#

Exercise · Fill in the blank

Before you move the slider, predict from the variances 9, 4 and 0.25: what percentage does the cloud keep at t = 1? Give one decimal, then check the caption.

Question 1 of 4

Set the slider to t = 2. Along which line do all the points lie?

The dashed PC3 line
The solid PC2 line
The raw z axis
The amber PC1 line

Browser lab: PCA of the scene's cloud#

Rebuild the scene's cloud, run PCA, and check the FFT.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt

V = np.array([[2, 2, 1], [-2, 1, 2], [1, -2, 2]]).T / 3   # columns v1, v2, v3
sigma = np.array([3.0, 2.0, 0.5])

rng = np.random.default_rng(14)
Z = rng.normal(size=(240, 3))
Z -= Z.mean(axis=0)
L = np.linalg.cholesky(np.cov(Z, rowvar=False))
Z = Z @ np.linalg.inv(L).T            # sample covariance exactly I
X = (Z * sigma) @ V.T                 # sample covariance exactly V diag(9, 4, 0.25) Vᵀ

Xc = X - X.mean(axis=0)
U, S, Vt = np.linalg.svd(Xc, full_matrices=False)
var = S**2 / (len(X) - 1)
total = var.sum()
print("PC variances  ", var.round(4), "shares", (var / total).round(3))
print("raw variances ", Xc.var(axis=0, ddof=1).round(4))
print("keep x, y      ", round(1 - Xc[:, 2].var(ddof=1) / total, 3))
print("keep PC1, PC2  ", round((var[0] + var[1]) / total, 3))
print("Vᵀ rows       ", Vt.round(3).tolist())

scores = Xc @ Vt.T
fig, axes = plt.subplots(1, 2, figsize=(10, 4.2))
axes[0].scatter(Xc[:, 0], Xc[:, 1], s=8)
axes[0].set_title("raw features x, y")
axes[1].scatter(scores[:, 0], scores[:, 1], s=8)
axes[1].set_title("scores on PC1, PC2")
for ax in axes:
    ax.set_aspect("equal")
plt.tight_layout()
plt.show()

n = 8
F = np.exp(2j * np.pi * np.outer(np.arange(n), np.arange(n)) / n)
x = rng.normal(size=n)
print("F^H F = nI:", np.allclose(F.conj().T @ F, n * np.eye(n)))
print("fft matches F̄x:", np.allclose(np.fft.fft(x), F.conj() @ x))

The PC variances print as 999, 444 and 0.250.250.25. Keeping xxx and yyy keeps 0.7820.7820.782, against 0.9810.9810.981. The printed rows are v1v_1v1​, v2v_2v2​ and v3v_3v3​, possibly with a sign flipped, as in Week 13. The FFT check matches F‾x\overline{F}xFx, NumPy's sign. Change sigma to [3, 2, 1] and predict the gap before you run it.

Key takeaways#

PCA is the SVD of the centred data: the components are the viv_ivi​, and the variance along viv_ivi​ is σi2/(m−1)\sigma_i^2/(m-1)σi2​/(m−1). A thin cloud need not be thin along a raw axis: in the scene, dropping zzz keeps 78.2% and dropping PC3 keeps 98.1%. The Fourier matrix is unitary up to n\sqrt{n}n​, and the FFT applies it in O(nlog⁡n)O(n\log n)O(nlogn).

Further reading#

  • Gilbert Strang, Introduction to Linear Algebra (6th ed., Wellesley-Cambridge Press, 2023). Covers PCA through the SVD, and the Fourier matrix and the FFT.
  • Lloyd N. Trefethen & David Bau, Numerical Linear Algebra (SIAM, 1997). The SVD and the Eckart–Young theorem behind PCA as low-rank approximation.
  • Steven L. Brunton & J. Nathan Kutz, Data-Driven Science and Engineering (Cambridge University Press, 2019). PCA for dimensionality reduction and the FFT for signal processing.

Reading is free. PDF and ePub unlock with the newsletter. Join once and every lesson opens in this browser.

← Previous
Week 13: The SVD and Complex Matrices
On this page
  • Principal components from the SVD
  • The scene's numbers
  • The Fourier matrix and the FFT
  • Course recap
  • Knowledge Check
  • Browser lab: PCA of the scene's cloud
  • Key takeaways
  • Further reading