|
| 1 | +use approx::assert_abs_diff_eq; |
| 2 | +use ndarray::prelude::*; |
| 3 | +use ndarray_rand::{rand_distr::StandardNormal, RandomExt}; |
| 4 | +use proptest::prelude::*; |
| 5 | + |
| 6 | +use ndarray_linalg_rs::lobpcg::*; |
| 7 | +use rand::SeedableRng; |
| 8 | +use rand_xoshiro::Xoshiro256Plus; |
| 9 | + |
| 10 | +mod common; |
| 11 | + |
| 12 | +/// Eigenvalue structure in high dimensions |
| 13 | +/// |
| 14 | +/// This test checks that the eigenvalues are following the Marchensko-Pastur law. The data is |
| 15 | +/// standard uniformly distributed (i.e. E(x) = 0, E^2(x) = 1) and we have twice the amount of |
| 16 | +/// data when compared to features. The probability density of the eigenvalues should then follow |
| 17 | +/// a special densitiy function, described by the Marchenko-Pastur law. |
| 18 | +/// |
| 19 | +/// See also https://en.wikipedia.org/wiki/Marchenko%E2%80%93Pastur_distribution |
| 20 | +#[test] |
| 21 | +fn test_marchenko_pastur() { |
| 22 | + // create random number generator |
| 23 | + let mut rng = Xoshiro256Plus::seed_from_u64(3); |
| 24 | + |
| 25 | + // generate normal distribution random data with N >> p |
| 26 | + let data = Array2::random_using((1000, 500), StandardNormal, &mut rng) / 1000f64.sqrt(); |
| 27 | + |
| 28 | + let res = TruncatedSvd::new_with_rng(data, Order::Largest, Xoshiro256Plus::seed_from_u64(42)) |
| 29 | + .precision(1e-3) |
| 30 | + .decompose(500) |
| 31 | + .unwrap(); |
| 32 | + |
| 33 | + let sv = res.values().mapv(|x: f64| x * x); |
| 34 | + |
| 35 | + // we have created a random spectrum and can apply the Marchenko-Pastur law |
| 36 | + // with variance 1 and p/n = 0.5 |
| 37 | + let (a, b) = ( |
| 38 | + 1. * (1. - 0.5f64.sqrt()).powf(2.0), |
| 39 | + 1. * (1. + 0.5f64.sqrt()).powf(2.0), |
| 40 | + ); |
| 41 | + |
| 42 | + // check that the spectrum has correct boundaries |
| 43 | + assert_abs_diff_eq!(b, sv[0], epsilon = 0.1); |
| 44 | + assert_abs_diff_eq!(a, sv[sv.len() - 1], epsilon = 0.1); |
| 45 | + |
| 46 | + // estimate density empirical and compare with Marchenko-Pastur law |
| 47 | + let mut i = 0; |
| 48 | + 'outer: for th in Array1::linspace(0.1f64, 2.8, 28).slice(s![..;-1]) { |
| 49 | + let mut count = 0; |
| 50 | + while sv[i] >= *th { |
| 51 | + count += 1; |
| 52 | + i += 1; |
| 53 | + |
| 54 | + if i == sv.len() { |
| 55 | + break 'outer; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + let x = th + 0.05; |
| 60 | + let mp_law = ((b - x) * (x - a)).sqrt() / std::f64::consts::PI / x; |
| 61 | + let empirical = count as f64 / 500. / ((2.8 - 0.1) / 28.); |
| 62 | + |
| 63 | + assert_abs_diff_eq!(mp_law, empirical, epsilon = 0.05); |
| 64 | + } |
| 65 | +} |
0 commit comments