Expand description
§rmt
Random Matrix Theory: eigenvalue distributions and spectral statistics.
§The Core Idea
When you have a large random matrix, its eigenvalues follow predictable distributions. This is surprising: randomness at the element level produces order at the spectral level.
§Key Distributions
| Distribution | Matrix Type | Density |
|---|---|---|
marchenko_pastur_density | Wishart (X^T X) | Bounded support |
wigner_semicircle_density | Symmetric random | Semicircle |
| Tracy–Widom (edge; not implemented here) | Largest eigenvalue | Skewed |
§Quick Start
use rmt::{marchenko_pastur_density, wigner_semicircle_density, sample_wishart};
use ndarray::Array2;
// Marchenko-Pastur: eigenvalue density of X^T X / n
let ratio = 0.5; // p/n (dimensions / samples)
let density = marchenko_pastur_density(1.5, ratio, 1.0);
// Wigner semicircle: eigenvalue density of symmetric matrix
let density = wigner_semicircle_density(0.5, 1.0);
// Sample a Wishart matrix
let (n, p) = (100, 50);
let wishart = sample_wishart(n, p);§Why RMT for ML?
- Covariance matrices: Sample covariance eigenvalues follow Marchenko-Pastur
- Neural networks: Weight matrix spectra reveal training dynamics
- PCA: Distinguish signal from noise eigenvalues
- Regularization: Set shrinkage based on spectral distribution
§The Marchenko-Pastur Law
For a matrix X (n samples, p features), the eigenvalues of X^T X / n cluster in [lambda_-, lambda_+] where:
lambda_+/- = sigma^2 (1 +/- sqrt(p/n))^2
Density: rho(lambda) = (1/(2*pi*sigma^2)) * sqrt((lambda_+ - lambda)(lambda - lambda_-)) / (gamma*lambda)When p/n -> 0, this converges to a point mass at sigma^2 (classical regime). When p/n > 0, eigenvalues spread (high-dimensional regime).
§The Wigner Semicircle
For a symmetric matrix with i.i.d. entries, eigenvalues follow:
rho(lambda) = (1/(2*pi*sigma^2)) * sqrt(4*sigma^2 - lambda^2) for |lambda| <= 2*sigma§What Can Go Wrong
- Finite size effects: MP/semicircle are asymptotic. Small n deviates.
- Not centered: MP assumes zero-mean data. Center your features.
- Correlated features: MP assumes independence. Correlated data has different spectrum.
- Ratio out of range: MP needs p/n in (0, infinity). Tracy-Widom for edge.
- Numerical eigendecomposition: For large matrices, use iterative methods.
§References
- Marchenko & Pastur (1967). “Distribution of eigenvalues for some sets of random matrices”
- Wigner (1955). “Characteristic vectors of bordered matrices with infinite dimensions”
- Johnstone (2001). “On the distribution of the largest eigenvalue in PCA”
Functions§
- effective_
dimension - Estimate effective dimensionality of an embedding matrix using the Marchenko-Pastur law.
- empirical_
spectral_ density - Empirical spectral density via histogram.
- level_
spacing_ ratios - Level spacing ratio for eigenvalue sequence.
- marchenko_
pastur_ density - Marchenko-Pastur density at point lambda.
- marchenko_
pastur_ support - Marchenko-Pastur support bounds [lambda_-, lambda_+].
- mean_
spacing_ ratio - Mean level spacing ratio.
- sample_
goe - Sample a GOE (Gaussian Orthogonal Ensemble) matrix.
- sample_
goe_ with - Sample a GOE (Gaussian Orthogonal Ensemble) matrix using the provided RNG for reproducibility.
- sample_
wishart - Sample a Wishart matrix: W = X^T X where X is n x p Gaussian.
- sample_
wishart_ with - Sample a Wishart matrix W = X^T X where X is n x p Gaussian, using the provided RNG for reproducibility.
- stieltjes_
transform - Stieltjes transform: m(z) = (1/n) sum 1/(lambda_i - z)
- wigner_
semicircle_ density - Wigner semicircle density at point lambda.