Linear Algebra (source: Gilbert Strang notes) · 1 / 37

Prologue: Data Compression, PCA & Residue Analysis (IITM MLT L5)

Notes on L5: Representation learning part 3 | data compression, PCA & residue analysis from the IIT Madras BS programme (Machine Learning Techniques). The video transcript was not available, so these notes follow the lecture's title and the standard content of this lecture; ordering and examples may differ from the video. The three questions after it are the ones that led into Strang's course.

1. Recap from Parts 1–2

Setup. You have data points \(x_1, \dots, x_n \in \mathbb{R}^d\). Assume the data is centered, so \(\frac{1}{n}\sum_i x_i = 0\).

Representing a point with a line. Take a unit vector \(w\) with \(\|w\| = 1\). The projection of \(x_i\) onto the line through \(w\) is \((x_i^\top w)\,w\). So each point is stored as a single scalar coefficient \(x_i^\top w\), plus the shared vector \(w\).

Choosing the best line. Pick the \(w\) that minimizes the average reconstruction error:

\[\min_{\|w\|=1} \frac{1}{n}\sum_{i=1}^n \|x_i - (x_i^\top w)w\|^2\]

Equivalent form. Expand the error for one point:

\[\|x_i - (x_i^\top w)w\|^2 = \|x_i\|^2 - (x_i^\top w)^2\]
The \(\|x_i\|^2\) term does not depend on \(w\). So minimizing the error is the same as solving
\[\max_{\|w\|=1} \; w^\top C w, \qquad C = \frac{1}{n}\sum_i x_i x_i^\top\]
where \(C\) is the covariance matrix of the centered data.

2. Residue analysis

Residues after the first line. Let \(w_1\) be the best line. The residue of each point is what the line fails to capture:

\[r_i = x_i - (x_i^\top w_1)\,w_1\]

Key property: residues are orthogonal to \(w_1\).

\[w_1^\top r_i = x_i^\top w_1 - (x_i^\top w_1)\|w_1\|^2 = 0\]
So the residues live entirely in the \((d-1)\)-dimensional subspace orthogonal to \(w_1\).

Repeating the procedure. Treat the residues \(\{r_i\}\) as a new dataset and find the best line for them. Call it \(w_2\). Since every \(r_i \perp w_1\), the optimal \(w_2\) is also orthogonal to \(w_1\). Compute new residues and continue.

Result after \(d\) rounds. You get an orthonormal set \(w_1, \dots, w_d\). Each point is represented exactly:

\[x_i = \sum_{k=1}^d (x_i^\top w_k)\,w_k\]
This is the original data written in a new orthonormal basis, ordered by how much of the data each direction explains.

3. Data compression

Stopping early. Suppose that after \(k\) rounds the residues are approximately zero. That means the data essentially lies in a \(k\)-dimensional subspace.

Storage cost.

  • Original data: \(n \cdot d\) numbers.
  • Compressed data: \(k \cdot d\) numbers for the basis vectors, plus \(n \cdot k\) numbers for the coefficients. Total \(k(d + n)\).

The savings are large when \(k \ll d\).

The tradeoff. A smaller \(k\) gives more compression but a larger reconstruction error. The residue norms after step \(k\) measure exactly what is lost.

4. Connection to PCA (eigenvectors)

First direction. Use a Lagrangian on \(\max_w w^\top C w\) subject to \(w^\top w = 1\). Setting the gradient to zero gives

\[Cw = \lambda w\]
At such a \(w\), the objective equals \(w^\top C w = \lambda\). So \(w_1\) is the eigenvector of \(C\) with the largest eigenvalue \(\lambda_1\).

Later directions (deflation). The covariance matrix of the residues is

\[C_r = (I - w_1 w_1^\top)\, C\, (I - w_1 w_1^\top) = C - \lambda_1 w_1 w_1^\top\]
This matrix has the same eigenvectors as \(C\), except that \(w_1\)'s eigenvalue becomes 0. So the best line for the residues is the eigenvector with the second-largest eigenvalue. By induction, \(w_k\) is the eigenvector with the \(k\)-th largest eigenvalue.

Conclusion. The greedy residue procedure is exactly Principal Component Analysis (PCA). The vectors \(w_k\) are the principal components, which are the eigenvectors of the covariance matrix sorted by eigenvalue.

5. Interpreting the eigenvalues

Eigenvalue as variance. For each component,

\[\lambda_k = w_k^\top C w_k = \frac{1}{n}\sum_i (x_i^\top w_k)^2\]
This is the variance of the data along \(w_k\). So PCA can be read two equivalent ways: the directions of maximum variance, or the directions of minimum reconstruction error.

Error after keeping \(k\) components.

\[\text{Error}(k) = \frac{1}{n}\sum_i \|x_i - \hat{x}_i\|^2 = \sum_{j=k+1}^{d} \lambda_j\]
The reconstruction error is the sum of the eigenvalues you dropped.

Choosing \(k\). Keep the smallest \(k\) such that the fraction of variance explained is high enough:

\[\frac{\sum_{j=1}^k \lambda_j}{\sum_{j=1}^d \lambda_j} \geq 0.95\]
The threshold, here 0.95, is chosen by you.

6. PCA algorithm summary

  1. Center the data by subtracting the mean from every point.
  2. Compute the covariance matrix \(C = \frac{1}{n}\sum_i x_i x_i^\top\).
  3. Find the eigenvalues and eigenvectors of \(C\), and sort them so \(\lambda_1 \geq \dots \geq \lambda_d\).
  4. Choose \(k\) using the variance-explained criterion.
  5. Represent each point as \(\big(x_i^\top w_1, \dots, x_i^\top w_k\big) \in \mathbb{R}^k\).

Key takeaways

  • Residues are orthogonal to earlier directions. Finding lines on residues one after another produces an orthonormal basis.
  • The greedy procedure equals the eigen-decomposition of \(C\). Greedy residue-fitting is PCA.
  • Eigenvalues measure variance. The eigenvalue of each direction is the variance it captures, and the dropped eigenvalues add up to the compression error.
  • Compression works when variance is concentrated. If a few directions hold most of the variance, the data can be compressed with little loss.

Q1. Why is the optimal \(w\) an eigenvector?

Your question How is the solution of this equation, (xᵀw)w, the eigenvector?

A quick correction first: \((x_i^\top w)\,w\) is not the eigenvector. It is the projection of \(x_i\) onto the line. What turns out to be an eigenvector is the optimal direction \(w\) that defines that line. Here is the chain of reasoning.

Step 1: Write the reconstruction error for one point

The point \(x_i\) is approximated by its projection \((x_i^\top w)w\). Using \(\|w\|=1\):

\[\|x_i - (x_i^\top w)w\|^2 = x_i^\top x_i - 2(x_i^\top w)^2 + (x_i^\top w)^2 \underbrace{w^\top w}_{=1} = \|x_i\|^2 - (x_i^\top w)^2\]

Step 2: Average over all points

\[\frac{1}{n}\sum_i \|x_i - (x_i^\top w)w\|^2 = \underbrace{\frac{1}{n}\sum_i \|x_i\|^2}_{\text{constant}} - \frac{1}{n}\sum_i (x_i^\top w)^2\]

The first term doesn't depend on \(w\). So minimizing the error is the same as maximizing \(\frac{1}{n}\sum_i (x_i^\top w)^2\).

Step 3: Rewrite the objective using the covariance matrix

Since \((x_i^\top w)^2 = (w^\top x_i)(x_i^\top w) = w^\top (x_i x_i^\top) w\),

\[\frac{1}{n}\sum_i (x_i^\top w)^2 = w^\top \Big(\frac{1}{n}\sum_i x_i x_i^\top\Big) w = w^\top C w\]

The problem is now:

\[\max_{w} \; w^\top C w \quad \text{subject to} \quad w^\top w = 1\]

Step 4: Solve with a Lagrange multiplier

The Lagrangian is

\[L(w, \lambda) = w^\top C w - \lambda (w^\top w - 1)\]

Take the gradient with respect to \(w\). Since \(C\) is symmetric, \(\nabla_w (w^\top C w) = 2Cw\):

\[\nabla_w L = 2Cw - 2\lambda w = 0 \;\;\Longrightarrow\;\; \boxed{Cw = \lambda w}\]

This is the eigenvector equation. Any stationary point \(w\) must be an eigenvector of \(C\).

Step 5: Find which eigenvector is the best one

Plug an eigenvector back into the objective:

\[w^\top C w = w^\top (\lambda w) = \lambda \, w^\top w = \lambda\]

The objective value at an eigenvector equals its eigenvalue. To maximize, pick the eigenvector with the largest eigenvalue \(\lambda_1\).

Alternative check without calculus

\(C\) is symmetric and positive semidefinite. So it has an orthonormal eigenbasis \(u_1,\dots,u_d\) with eigenvalues \(\lambda_1 \ge \dots \ge \lambda_d \ge 0\).

Write any unit vector as \(w = \sum_j a_j u_j\) with \(\sum_j a_j^2 = 1\). Then

\[w^\top C w = \sum_j \lambda_j a_j^2 \;\le\; \lambda_1 \sum_j a_j^2 = \lambda_1\]

Equality holds when \(a_1 = 1\) and all other \(a_j = 0\), that is, when \(w = u_1\). This shows directly that the top eigenvector is the global maximizer, not just a stationary point.

Summary

Object What it is
\(w\) (optimal) Top eigenvector of \(C\)
\(x_i^\top w\) Scalar coefficient stored for point \(i\)
\((x_i^\top w)\,w\) Reconstruction of \(x_i\) on the line (a vector, not an eigenvector)
\(\lambda_1 = w^\top C w\) Variance of the data along \(w\)

Q2. PCA in the multivariable setup

Your question Can you explain PCA in the multivariable setup?

I'll take "multivariable setup" to mean projecting onto a \(k\)-dimensional subspace all at once, instead of finding one line at a time. This is the general form of PCA, and the one-line case is just \(k=1\).

1. Setup

You have centered data \(x_1,\dots,x_n \in \mathbb{R}^d\). Stack them as rows of a matrix \(X \in \mathbb{R}^{n\times d}\). The covariance matrix is

\[C = \frac{1}{n}\sum_i x_i x_i^\top = \frac{1}{n}X^\top X \quad (d\times d)\]

You want a \(k\)-dimensional subspace (\(k < d\)) that best represents the data.

2. Representing a subspace

Describe the subspace by a matrix \(W = [w_1 \cdots w_k] \in \mathbb{R}^{d\times k}\) with orthonormal columns:

\[W^\top W = I_k\]

For each point:

  • The coefficients (the compressed code) are \(z_i = W^\top x_i \in \mathbb{R}^k\). These are the \(k\) dot products \(x_i^\top w_j\).
  • The reconstruction is \(\hat{x}_i = W z_i = WW^\top x_i\).

\(P = WW^\top\) is the orthogonal projection matrix onto the subspace. It satisfies \(P^2 = P\) and \(P^\top = P\).

For \(k=1\), this reduces to \((x_i^\top w)\,w\), which is exactly what you had before.

3. Reconstruction error

The error for one point is

\[\|x_i - P x_i\|^2 = x_i^\top (I-P)^\top (I-P) x_i = x_i^\top (I-P)\, x_i = \|x_i\|^2 - x_i^\top WW^\top x_i\]

This uses the fact that \(I-P\) is also a projection, so \((I-P)^\top(I-P) = I-P\).

The term \(x_i^\top WW^\top x_i\) equals \(\|W^\top x_i\|^2\). Averaging over all points:

\[\frac{1}{n}\sum_i \|x_i - \hat{x}_i\|^2 = \text{const} - \frac{1}{n}\sum_i \|W^\top x_i\|^2\]

4. The trace objective

Rewrite the second term using the trace trick, \(a^\top a = \operatorname{tr}(aa^\top)\):

\[\frac{1}{n}\sum_i \|W^\top x_i\|^2 = \frac{1}{n}\sum_i \operatorname{tr}\!\big(W^\top x_i x_i^\top W\big) = \operatorname{tr}\!\big(W^\top C W\big) = \sum_{j=1}^k w_j^\top C w_j\]

So PCA in the multivariable form is the problem

\[\boxed{\max_{W \in \mathbb{R}^{d\times k}} \operatorname{tr}(W^\top C W) \quad \text{s.t.} \quad W^\top W = I_k}\]

Interpretation: maximize the total variance captured across \(k\) orthonormal directions.

5. Solving it

Lagrangian. The constraint \(W^\top W = I_k\) is a \(k \times k\) symmetric matrix equation, so the multiplier is a symmetric matrix \(\Lambda\):

\[L(W,\Lambda) = \operatorname{tr}(W^\top C W) - \operatorname{tr}\!\big(\Lambda(W^\top W - I)\big)\]

Stationarity. Using \(\nabla_W \operatorname{tr}(W^\top C W) = 2CW\):

\[CW = W\Lambda\]

Diagonalizing. \(\Lambda\) is symmetric, so \(\Lambda = Q D Q^\top\) with \(Q\) orthogonal. Set \(\tilde W = WQ\). Then

\[C\tilde W = \tilde W D\]

Each column of \(\tilde W\) is an eigenvector of \(C\). So the optimal subspace is spanned by \(k\) eigenvectors of \(C\).

Which eigenvectors. At such a solution, \(\operatorname{tr}(W^\top C W) = \operatorname{tr}(\Lambda) = \) the sum of the chosen eigenvalues. To maximize this, choose the eigenvectors of the \(k\) largest eigenvalues:

\[\max \operatorname{tr}(W^\top C W) = \lambda_1 + \cdots + \lambda_k\]

This result is Ky Fan's theorem. It is the \(k\)-dimensional version of the bound \(w^\top C w \le \lambda_1\) from the one-line case.

6. Subtle point: the subspace is unique, the basis is not

If \(W\) is optimal, then \(WR\) is also optimal for any \(k\times k\) orthogonal matrix \(R\):

\[\operatorname{tr}(R^\top W^\top C W R) = \operatorname{tr}(W^\top C W)\]

The objective only "sees" the projection matrix \(WW^\top\), and \((WR)(WR)^\top = WW^\top\). So:

  • The joint optimization determines only the subspace.
  • Choosing the eigenvectors themselves as the basis is a convention. In that basis, the coordinates \(z_i\) are uncorrelated, because \(W^\top C W = \operatorname{diag}(\lambda_1,\dots,\lambda_k)\).
  • The greedy residue method from the lecture lands on exactly this eigenvector basis.

The subspace is unique as long as \(\lambda_k > \lambda_{k+1}\).

7. Error and choosing \(k\)

The minimum reconstruction error is the sum of the dropped eigenvalues:

\[\min \frac{1}{n}\sum_i\|x_i - \hat x_i\|^2 = \operatorname{tr}(C) - \sum_{j=1}^k \lambda_j = \sum_{j=k+1}^d \lambda_j\]

Pick \(k\) so that the explained variance \(\dfrac{\sum_{j\le k}\lambda_j}{\sum_{j\le d}\lambda_j}\) reaches your threshold.

8. Matrix form with SVD

Take the SVD of the data matrix, \(X = U\Sigma V^\top\). Then

\[C = \tfrac{1}{n}X^\top X = V\,\tfrac{\Sigma^2}{n}\,V^\top\]
  • The principal directions are the columns of \(V\).
  • The eigenvalues are \(\lambda_j = \sigma_j^2/n\).
  • The compressed data is \(Z = XW = XV_k = U_k\Sigma_k\), an \(n\times k\) matrix.
  • The reconstruction \(\hat X = U_k \Sigma_k V_k^\top\) is the best rank-\(k\) approximation of \(X\) in Frobenius norm (Eckart–Young theorem).

In practice, PCA is computed through the SVD rather than by forming \(C\). This is more numerically stable.

Summary

One line (\(k=1\)) Subspace (general \(k\))
\(w\), with \(\Vert w\Vert =1\) \(W\), with \(W^\top W = I_k\)
\((x^\top w)\,w\) \(WW^\top x\)
\(\max w^\top C w\) \(\max \operatorname{tr}(W^\top C W)\)
\(Cw = \lambda w\) \(CW = W\Lambda\)
Optimum \(\lambda_1\) Optimum \(\lambda_1+\dots+\lambda_k\)
Error \(\sum_{j\ge 2}\lambda_j\) Error \(\sum_{j>k}\lambda_j\)

If by "multivariable" you meant something else, such as PCA on multivariate Gaussian data or the probabilistic view of PCA, tell me and I'll cover that instead.


Q3. Why eigenvectors of the covariance matrix?

Your question Why are these solutions coming out to be the eigenvectors of the covariance matrix?

There are three ways to see it. Each answers "why" at a different level: why the covariance matrix shows up, why the optimum must be an eigenvector, and why it is the top one.

1. Why the covariance matrix appears at all

The objective is the average of \((x_i^\top w)^2\). Squaring a dot product gives

\[(x_i^\top w)^2 = w^\top (x_i x_i^\top)\, w\]

Averaging over points just averages the matrices \(x_i x_i^\top\), which produces \(C\). So the reconstruction error depends on the data only through its second moments.

This means two datasets with the same \(C\) get the same principal components. PCA cannot see anything beyond \(C\). Since the problem lives entirely in \(C\), the answer has to be expressed in terms of \(C\)'s own structure, which is its eigenvectors.

2. Why the optimum must be an eigenvector: the "no improving rotation" argument

Suppose \(w\) is optimal, and think about what \(Cw\) means.

Gradient direction. The gradient of \(w^\top C w\) is \(2Cw\). So \(Cw\) points in the direction that increases the variance fastest.

What you are allowed to move. You must stay on the unit sphere. So you can only move \(w\) in directions perpendicular to \(w\). Moving along \(w\) itself just rescales \(w\), which the constraint forbids.

Condition at the optimum. At a maximum, no allowed move can increase the variance. So \(Cw\) must have zero component perpendicular to \(w\):

\[Cw - (w^\top C w)\,w = 0\]

Conclusion. \(Cw\) points along \(w\), which means \(Cw = \lambda w\). Being an eigenvector is precisely the condition "no small rotation of the line increases the captured variance." The Lagrange multiplier equation is a formal way of writing this same statement.

Geometric meaning. \(Cw\) is the covariance between the data and its projection on \(w\):

\[Cw = \tfrac{1}{n}\sum_i x_i\,(x_i^\top w)\]

It shows which way the data "pulls" when you project onto \(w\). If \(Cw\) had a sideways component, the data would be correlated with a direction perpendicular to \(w\). Tilting \(w\) toward that direction would capture more variance. Eigenvectors are exactly the directions with no sideways pull.

3. Why it is the top eigenvector: in the eigenbasis the problem decouples

\(C\) is symmetric, so it has orthonormal eigenvectors \(u_1,\dots,u_d\). In that basis, \(C\) is diagonal: there are no cross terms.

Write \(w = \sum_j a_j u_j\) with \(\sum_j a_j^2 = 1\). Then

\[w^\top C w = \sum_j \lambda_j a_j^2\]

The weights \(a_j^2\) are non-negative and sum to 1. So the variance along \(w\) is a weighted average of the eigenvalues.

A weighted average is largest when all the weight sits on the largest value. So you set \(a_1 = 1\), which gives \(w = u_1\). Any mixing with other eigenvectors averages in smaller eigenvalues and lowers the variance.

Picture. The level sets of \(w^\top C w\) are ellipsoids whose axes point along the eigenvectors. The eigenvalues set how far the data spreads along each axis. The best single line is the longest axis.

For \(k\) directions. The same logic applies. With orthonormal \(w_1,\dots,w_k\), each eigenvalue \(\lambda_j\) gets a total weight between 0 and 1, and the total weight across all eigenvalues is exactly \(k\). The sum is maximized by putting full weight on the \(k\) largest eigenvalues.

In one line

The objective is a quadratic form in \(C\). Eigenvectors are the directions where \(C\) has no cross-coupling, so at an eigenvector no rotation can capture more variance. Variance along any direction is a weighted average of the eigenvalues, so the best direction is the one with the largest eigenvalue.