PCA (spec model) #
Principal Component Analysis is represented as a linear projection onto learned components, plus an explicit mean for centering.
The exact model operations are the transform and inverse transform. A separate reference helper below constructs a one-component approximation with power iteration; its name records that numerical limitation explicitly.
The model follows the usual centered linear projection used by PCA. The fitting helper is deliberately narrower: it approximates one leading component by power iteration.
References:
- Pearson (1901), "On Lines and Planes of Closest Fit to Systems of Points in Space". https://doi.org/10.1080/14786440109462720
- Hotelling (1933), "Analysis of a complex of statistical variables into principal components". https://doi.org/10.2307/2333955
Implementation status #
No API builder implements this model, and no theorem is proved about it. It is a reference definition only.
Parameters for PCA as a linear map plus centering.
We store:
components : outDim × inDim(rows are principal directions),mean : inDim(for centering),explainedVariance : outDim(eigenvalues for the selected components).
This matches the typical PCA API: you can transform to outDim coordinates and inverse back
to inDim.
- components : TorchLean.Tensor α [outDim, inDim]
Principal directions, one row for each output coordinate.
- mean : TorchLean.Tensor α [inDim]
Coordinate-wise sample mean subtracted before projection.
- explainedVariance : TorchLean.Tensor α [outDim]
Covariance eigenvalue associated with each selected component.
Instances For
Forward pass: center and project: y = components · (x - mean).
Instances For
Inverse transform: reconstruct x ≈ componentsᵀ · y + mean.
Instances For
VJP contribution for components: outer product dL/dy ⊗ (x - mean).
Instances For
VJP contribution for mean: dL/dmean = -componentsᵀ · dL/dy.
Instances For
VJP contribution for input: dL/dx = componentsᵀ · dL/dy.
Instances For
Gradients for a PCASpec projection.
- componentsGradient : TorchLean.Tensor α [outDim, inDim]
Gradient with respect to the component matrix.
- meanGradient : TorchLean.Tensor α [inDim]
Gradient with respect to the stored mean.
- inputGradient : TorchLean.Tensor α [inDim]
Gradient with respect to the projected input.
Instances For
Full backward pass for a PCA projection.
Instances For
Approximate one leading PCA component with deterministic multistart power iteration.
The fit centers the samples and uses covariance Xᵀ X / (n - 1). It runs iterations steps
from the normalized all-ones vector and every coordinate vector, then keeps the largest Rayleigh
quotient. The coordinate starts span the input space, so a dominant eigenspace cannot be orthogonal
to every start. Convergence still depends on the spectral gap and iteration count.
Scaling the covariance before iteration avoids squaring its original magnitude when normalizing
iterates. This does not prevent overflow while forming the covariance itself. Zero covariance
retains a unit direction and reports zero variance. The output has exactly one component; the
iteration cost is cubic in inDim, with inDim + 1 starts.
Instances For
Instances For
Apply a fitted PCA transform to a batch of samples.
Instances For
Reconstruction error: ||x - inverse(transform(x))||_2^2 (sum of squared coordinates).
PyTorch analogy: torch.sum((x - x_hat) ** 2).
Instances For
Cumulative explained variance, obtained by prefix-summing explainedVariance.