ES98E · Course contents

Part A · Lectures 5–8

7. Latent geometry: from compression to generation

An unlabelled collection of sensor traces has no target column to predict. Yet some traces vary together, some are unusual, and the apparent dimension may greatly exceed the number of underlying modes. Learning can now mean finding coordinates, groups or a probability distribution rather than an input–output map.

In supervised learning, pairs (xi,yi)(x_i,y_i) constrain a conditional prediction, such as p(y∣x)p(y\mid x). In unsupervised learning, observations xix_i alone supply the structure to be learned. Clustering, dimensionality reduction, density modelling and anomaly detection are different tasks within this setting. Not every unsupervised method maximises a marginal likelihood: ordinary PCA and kk-means have geometric objectives, although probabilistic relatives exist.

In this chapter: PCA, reconstruction, probabilistic PCA, nonlinear latent models.

Which direction loses the least information?

Let x1,…,xn∈RDx_1,\ldots,x_n\in\mathbb R^D, xˉ=n−1∑ixi\bar x=n^{-1}\sum_i x_i, and use the covariance convention

S=1n∑i=1n(xi−xˉ)(xi−xˉ)T.S=\frac1n\sum_{i=1}^n(x_i-\bar x)(x_i-\bar x)^T.

This is the empirical covariance with divisor nn, not the unbiased population-covariance estimator with divisor n−1n-1. For a unit vector uu, the centred projected coordinates uT(xi−xˉ)u^T(x_i-\bar x) have empirical variance uTSuu^TSu. To retain as much variance as possible in one coordinate, maximise this quadratic form subject to uTu=1u^Tu=1.

A Lagrange multiplier gives Su=λuSu=\lambda u. The spectral theorem orders the eigenvalues λ1≥⋯≥λD≥0\lambda_1\ge\cdots\ge\lambda_D\ge0, and the maximum is λ1\lambda_1 at a leading eigenvector. Requiring later directions to be orthogonal gives the remaining eigenvectors. Their signs are arbitrary; equal eigenvalues allow rotations within a tied eigenspace. The resulting principal subspace is unique only when the retained and discarded eigenvalues are separated.

This construction makes a geometric claim: a direction with a wide spread of projected data is more useful for compression than one with little spread. It does not claim that high variance is scientifically important. Feature scaling changes SS, and a large-variance nuisance can dominate a small but predictive signal.

Rotate the candidate axis before accepting the leading eigenvector. Every retained coordinate grows at the expense of a perpendicular residual. The variation curve below records exactly the same operation as the geometric projection, and makes the maximum at the principal direction visible.

Turn the projection until it keeps the variation

retained variance = uᵀSu · retained + discarded = tr(S)

-332.3-2.3coordinate x₁coordinate x₂ (equal units)018050axis angle (degrees)retained variance

retained = 2.000discarded = 2.000; total = 4.0

Rotate one unit direction through a centred cloud with leading principal variance 3. Each point slides onto its projection; dashed segments show what was discarded. The lower curve tracks the same retained variance as a function of angle. At 45° the retained variance is maximal. Both geometric axes have equal units, and total empirical variance is conserved. ↗

Maximum variance is minimum squared reconstruction error

Collect the first M<DM<D orthonormal eigenvectors into UMU_M. A point is encoded and reconstructed by

zi=UMT(xi−xˉ),x~i=xˉ+UMzi.z_i=U_M^T(x_i-\bar x),\qquad \tilde x_i=\bar x+U_Mz_i.

The discarded residual is perpendicular to the retained subspace. Pythagoras gives

1n∑i∥xi−x~i∥2=tr⁡(S)−tr⁡(UMTSUM)=∑j>Mλj.\frac1n\sum_i\|x_i-\tilde x_i\|^2 =\operatorname{tr}(S)-\operatorname{tr}(U_M^TSU_M) =\sum_{j>M}\lambda_j.

Thus PCA minimises mean squared reconstruction error among rank-MM orthogonal projections. Total squared error is nn times this sum; the normalisation must not be lost. For a data matrix XX with uncentred rows xiTx_i^T, the matrix formulas are

Xc=X−1xˉT,Z=XcUM,X~=ZUMT+1xˉT.X_c=X-\mathbf1\bar x^T,\qquad Z=X_cU_M,\qquad \tilde X=ZU_M^T+\mathbf1\bar x^T.

A thin SVD Xc=Vdiag⁡(sj)UTX_c=V\operatorname{diag}(s_j)U^T computes the principal directions without forming SS; λj=sj2/n\lambda_j=s_j^2/n. The mean and basis fitted on training data are reused on test data, rather than recomputed jointly with it.

Compare the retained and discarded directions for S=(2112)S=\begin{pmatrix}2&1\\1&2\end{pmatrix}. Keeping the diagonal direction retains variance three and discards variance one. The cloud is a deterministic teaching construction with exactly this empirical covariance, not a random sample from a Gaussian.

A coordinate and the information it discards
Keep the long axis, discard its normalx₁x₂retained variance = 3mean squared residual = 1Equal units on both axes
Centred two-dimensional points, their projections onto u = (1,1)/√2, and perpendicular residuals. The retained empirical variance is 3; mean squared reconstruction error is 1. Dashed segments are discarded components. Both axes have equal units. ↗

PCA supports visualisation, compressed storage, decorrelation and reconstruction-based anomaly scores. Compression must also store the mean and basis. Decorrelation is not independence except in special distributions such as a joint Gaussian. An unusual point may have small reconstruction error while lying far along the principal subspace; anomaly detection should consider both retained coordinates and discarded residuals. Linear projection cannot unfold a curved manifold. Kernel PCA changes the feature geometry, while nonlinear decoders learn an explicit generative map.

A subspace is not yet a distribution

Ordinary PCA says how to project an observed point, but not how frequently different points should occur. A latent-variable model supplies a generative mechanism:

z∼p(z),x∼pθ(x∣z),pθ(x)=∫pθ(x∣z)p(z) dz.z\sim p(z),\qquad x\sim p_\theta(x\mid z),\qquad p_\theta(x)=\int p_\theta(x\mid z)p(z)\,dz.

Probabilistic PCA chooses

z∼N(0,IM),x=μ+Wz+ε,ε∼N(0,σ2ID),z\sim\mathcal N(0,I_M),\qquad x=\mu+Wz+\varepsilon,\qquad \varepsilon\sim\mathcal N(0,\sigma^2I_D),

with independent zz and ε\varepsilon. Its marginal is N(μ,WWT+σ2ID)\mathcal N(\mu,WW^T+\sigma^2I_D). Latent variation accounts for the dominant directions; isotropic noise fills the rest. For σ2>0\sigma^2>0 the distribution has full-dimensional support, so the data do not literally lie on a plane.

Under the usual interior solution, maximum likelihood gives

μ^=xˉ,σ^2=1D−M∑j=M+1Dλj,W^=UM(ΛM−σ^2I)1/2R,\hat\mu=\bar x,\qquad \hat\sigma^2=\frac1{D-M}\sum_{j=M+1}^D\lambda_j, \qquad \hat W=U_M(\Lambda_M-\hat\sigma^2I)^{1/2}R,

where RR is any orthogonal M×MM\times M matrix. Latent rotations are not identifiable. The covariance fits leading eigenvalues individually and replaces all discarded eigenvalues by their average; this explains the noise estimate. Boundary cases with zero noise can be singular and need separate treatment. These formulas and their likelihood interpretation come from Tipping and Bishop's probabilistic PCA.

Conditioning is also analytic. With B=WTW+σ2IB=W^TW+\sigma^2I,

p(z∣x)=N ⁣(B−1WT(x−μ), σ2B−1).p(z\mid x)=\mathcal N\!\left(B^{-1}W^T(x-\mu),\ \sigma^2B^{-1}\right).

As noise tends to zero with WW full column rank, decoding the posterior mean approaches the orthogonal PCA reconstruction. The latent coordinates can remain scaled and rotated relative to ordinary PCA scores; the full probability model becomes singular in that limit. “Recovers PCA” refers to the principal subspace and reconstruction, not identical coordinates and a nonsingular density.

Generation now has an unambiguous recipe: independently draw z∗∼N(0,I)z^*\sim\mathcal N(0,I) and ε∗∼N(0,σ2I)\varepsilon^*\sim\mathcal N(0,\sigma^2I), then return μ+Wz∗+ε∗\mu+Wz^*+\varepsilon^*. Omitting noise generates only the low-rank component. The source describes MNIST1D samples with thirty components; without its data we cannot assess their resemblance. A single Gaussian can reproduce covariance while still mixing distinct digit classes into implausible samples.

Bending the generative map

Replace μ+Wz\mu+Wz by a network decoder gθ(z)g_\theta(z):

z∼N(0,I),x∣z∼N(gθ(z),σ2I).z\sim\mathcal N(0,I),\qquad x\mid z\sim\mathcal N(g_\theta(z),\sigma^2I).

The mean map can represent curved structure and sometimes use fewer latent dimensions for a given reconstruction error. But nonlinearity generally removes Gaussian conjugacy: the posterior and marginal integral are usually intractable. This is not true for every nonlinear model, and flexibility does not guarantee better generation, identifiable latent factors or the ability to represent arbitrary topology with a chosen continuous decoder.

Keep a collection of latent draws fixed while bending their decoder. The resulting motion separates three objects that can otherwise be conflated: the latent prior, the decoder mean and observation noise. Nonlinearity changes the map into data space; it need not change the prior at all.

Bend a latent line into a distribution

z ∼ N(0,1) · x = (z, b(z² − 1)) + ε

-335-1.5decoded x₁decoded x₂The same latent draws z

curvature b = 0.00noise σ = 0.08

Keep 48 latent Gaussian draws and independent noise innovations fixed while bending a polynomial decoder. Small ink marks show latent values; teal points show decoded means and amber offsets show observation noise. The decoder becomes nonlinear without changing the prior. This construction illustrates generation, not a trained VAE or identifiable physical factors. ↗

For clustering, a Gaussian mixture instead uses a discrete latent class and a distribution within each class. kk-means can be viewed through a limiting hard-assignment construction, but centres alone are not a normalised generative distribution. GANs, flows and diffusion models offer other generative formulations; they need not use the Gaussian decoder likelihood above or the same training objective.

A nonlinear decoder trained only with a reconstruction objective is not yet a variational autoencoder. We still need a distribution over latent codes, a way to infer them from observations, and an objective relating that inference to data probability. The final chapter builds those pieces and makes their approximation error explicit.