pulsatrix 1.1.0
Loading...
Searching...
No Matches
pca.hpp File Reference

Principal component analysis of a layer's representations (INT-5): 2D and 3D views, directions to steer along, and fewer dimensions for what comes after. More...

#include <cstdint>
#include <vector>
#include "pulsatrix/tensor.hpp"
Include dependency graph for pca.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  pulsatrix::Pca
 A fitted PCA. More...
 

Namespaces

namespace  pulsatrix
 

Functions

Pca pulsatrix::FitPca (const Tensor &x, int64_t components, bool center=true)
 Fits the components directions of most variance.
 
Tensor pulsatrix::PcaProject (const Pca &pca, const Tensor &x, bool whiten=false)
 Each row's coordinates on the components: (x - mean) components^T, shape (n, components), on x's backend.
 
Tensor pulsatrix::PcaReconstruct (const Pca &pca, const Tensor &scores)
 The rows that coordinates map back to: mean + scores components, shape (n, width). Projecting and reconstructing keeps only what the components hold.
 
std::vector< float > pulsatrix::PcaComponent (const Pca &pca, int64_t index)
 Component index as a unit vector of floats, for SteeringHook(). One standard deviation along it is sqrt(explained_variance[index]), a natural unit for the coefficient.
 
double pulsatrix::SubspaceOverlap (const Pca &a, const Pca &b, int64_t k)
 How much two PCAs' first k components span the same space: the mean squared cosine of the principal angles between them, |A_k B_k^T|_F^2 / k. 1 when the spans are the same (whatever the order or rotation inside them), 0 when they are orthogonal, and about k / width for unrelated random subspaces.
 
double pulsatrix::PcaSplitHalfOverlap (const Tensor &x, int64_t k, uint64_t seed=0)
 Fits k components on each half of the rows, split at random, and returns their SubspaceOverlap(). Near 1 means the k-dimensional picture is a property of the data; well below 1 means it depends on which inputs happened to be drawn.
 

Detailed Description

Principal component analysis of a layer's representations (INT-5): 2D and 3D views, directions to steer along, and fewer dimensions for what comes after.

FitPca() finds the directions along which a set of inputs' representations vary most. PcaProject() places inputs on them, for a scatter plot (viz/projection.hpp) or as input to methods that want fewer dimensions. PcaComponent() gives a direction as a steering vector.

It matches scikit-learn's PCA (the full solver): the data are centered, variances divide by n - 1, and each component's sign makes its largest-magnitude entry positive, so the same data always give the same picture. Everything is in double precision on the host, through the covariance (width x width) or the Gram matrix (n x n), whichever is smaller.

Two cautions. Components whose variances are close can swap or rotate between two samples of the same data, so a 2D picture isn't always stable: PcaSplitHalfOverlap() checks. And the largest directions of variance aren't necessarily the ones the model uses: a direction found here is a hypothesis to test, for example by steering along it.