Skip to main content

Crate rmt

Crate rmt 

Source
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

DistributionMatrix TypeDensity
marchenko_pastur_densityWishart (X^T X)Bounded support
wigner_semicircle_densitySymmetric randomSemicircle
Tracy–Widom (edge; not implemented here)Largest eigenvalueSkewed

§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

  1. Finite size effects: MP/semicircle are asymptotic. Small n deviates.
  2. Not centered: MP assumes zero-mean data. Center your features.
  3. Correlated features: MP assumes independence. Correlated data has different spectrum.
  4. Ratio out of range: MP needs p/n in (0, infinity). Tracy-Widom for edge.
  5. 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.