free-probability 0.1.0

Free probability theory computations with random matrices
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Moment computation and free cumulant ↔ moment transforms.
//!
//! The fundamental identity linking moments `m_n` and free cumulants `κ_n`:
//!
//! ```text
//! m_n = Σ_{π ∈ NC(n)} Π_{B ∈ π} κ_{|B|}
//! ```
//!
//! where `NC(n)` is the set of non-crossing partitions of `{1, …, n}`.
//! This module implements the recursive inversion/transform.

use crate::FP_MAX_ORDER;

/// Compute the k-th raw moment E[X^k] of an empirical distribution.
///
/// # Arguments
///
/// * `dist` — Empirical distribution with sample points
/// * `k` — Moment order (1 = mean, 2 = variance + mean², etc.)
pub fn compute_moment(dist: &crate::EmpiricalDist, k: usize) -> f64 {
    if dist.points.is_empty() {
        return 0.0;
    }
    let n = dist.points.len() as f64;
    let sum: f64 = dist.points.iter().map(|&x| x.powi(k as i32)).sum();
    sum / n
}

/// Compute moments up to order `max_k`.
///
/// `output[0]` receives m₁, `output[1]` receives m₂, …, `output[max_k-1]` receives m₍ₘₐₓ_ₖ₎.
pub fn compute_moments(dist: &crate::EmpiricalDist, max_k: usize, output: &mut [f64]) {
    for k in 1..=max_k {
        if k > output.len() {
            break;
        }
        output[k - 1] = compute_moment(dist, k);
    }
}

/// Convert a moment sequence to free cumulants (moment → cumulant).
///
/// `moments[i]` = m_{i+1}, `cumulants[i]` = κ_{i+1}.
///
/// Implements the recursive formula:
/// ```text
/// κ_n = m_n - Σ_{s=1}^{n-1} κ_s · [z^{n-s}] M̃(z)^s
/// ```
/// where `M̃(z) = 1 + m₁ z + m₂ z² + …`.
pub fn moment_to_cumulant(moments: &[f64], cumulants: &mut [f64]) {
    let n = moments.len().min(cumulants.len());
    if n == 0 {
        return;
    }

    // Extended moment array: ext[0] = m₀ = 1, ext[i] = m_i for i ≥ 1
    let mut ext = vec![0.0_f64; n + 1];
    ext[0] = 1.0;
    for i in 0..n {
        ext[i + 1] = moments[i];
    }

    let mut series = vec![0.0_f64; n + 1];    // current M̃(z)^s
    let mut new_series = vec![0.0_f64; n + 1];

    cumulants[0] = moments[0]; // κ₁ = m₁

    for nn in 2..=n {
        // series = M̃(z)^1
        for j in 0..nn {
            series[j] = ext[j];
        }

        let mut val = moments[nn - 1]; // m_nn

        for s in 1..=nn - 1 {
            let t_val = if nn >= s { series[nn - s] } else { 0.0 };
            val -= cumulants[s - 1] * t_val;

            // Update series to M̃(z)^{s+1}
            if s < nn - 1 {
                new_series.fill(0.0);
                for j in 0..nn {
                    for i in 0..=j {
                        new_series[j] += series[i] * ext[j - i];
                    }
                }
                series.copy_from_slice(&new_series);
            }
        }

        cumulants[nn - 1] = val;
    }
}

/// Convert free cumulants to moments (inverse transform).
///
/// `cumulants[i]` = κ_{i+1}, `moments[i]` = m_{i+1}.
pub fn cumulant_to_moment(cumulants: &[f64], moments: &mut [f64]) {
    let n = cumulants.len().min(moments.len());
    if n == 0 {
        return;
    }

    // Extended moment array
    let mut ext = vec![0.0_f64; n + 1];
    ext[0] = 1.0;

    let mut series = vec![0.0_f64; n + 1];
    let mut new_series = vec![0.0_f64; n + 1];

    for nn in 1..=n {
        if nn == 1 {
            moments[0] = cumulants[0];
            ext[1] = moments[0];
            continue;
        }

        // series = M̃(z)^1
        for j in 0..nn {
            series[j] = ext[j];
        }

        let mut val = cumulants[nn - 1]; // κ_nn

        for s in 1..=nn - 1 {
            let t_val = if nn >= s { series[nn - s] } else { 0.0 };
            val += cumulants[s - 1] * t_val;

            if s < nn - 1 {
                new_series.fill(0.0);
                for j in 0..nn {
                    for i in 0..=j {
                        new_series[j] += series[i] * ext[j - i];
                    }
                }
                series.copy_from_slice(&new_series);
            }
        }

        moments[nn - 1] = val;
        ext[nn] = val;
    }
}

/// Validate that a moment sequence is positive semi-definite (Hankel test).
///
/// Returns `true` if all leading principal minors of the Hankel moment matrix
/// are non-negative (allowing a tolerance of 1e-10).
pub fn validate_moments(moments: &[f64]) -> bool {
    let n = moments.len();
    if n == 0 {
        return true;
    }

    // Extended moment array
    let mut ext = vec![0.0_f64; n + 1];
    ext[0] = 1.0;
    for i in 0..n {
        ext[i + 1] = moments[i];
    }

    let ext_len = n + 1;
    let max_k = (ext_len / 2 + 1).min(FP_MAX_ORDER / 2);

    for k in 1..=max_k {
        if 2 * k - 2 >= ext_len {
            break;
        }

        // Build k×k Hankel matrix
        let size = k.min(FP_MAX_ORDER / 2);
        if size == 0 {
            continue;
        }

        let mut det = 1.0_f64;
        let mut mat = vec![vec![0.0_f64; size]; size];

        for i in 0..size {
            for j in 0..size {
                mat[i][j] = if i + j < ext_len { ext[i + j] } else { 0.0 };
            }
        }

        // Gaussian elimination (in-place, determinant tracking)
        for i in 0..size {
            let mut pivot = mat[i][i];
            if pivot.abs() < 1e-12 {
                let mut found = false;
                for j in (i + 1)..size {
                    if mat[j][i].abs() > 1e-12 {
                        mat.swap(i, j);
                        pivot = mat[i][i];
                        found = true;
                        break;
                    }
                }
                if !found {
                    det = 0.0;
                    break;
                }
            }
            det *= pivot;
            for j in (i + 1)..size {
                let factor = mat[j][i] / pivot;
                for l in i..size {
                    mat[j][l] -= factor * mat[i][l];
                }
            }
        }

        if det < -1e-10 {
            return false;
        }
    }

    true
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::EmpiricalDist;

    #[test]
    fn test_uniform_moments() {
        let n = 10000_usize;
        let points: Vec<f64> = (0..n).map(|i| (i as f64 + 0.5) / n as f64).collect();
        let dist = EmpiricalDist {
            support_min: 0.0,
            support_max: 1.0,
            points: &points,
        };

        let mut m = vec![0.0_f64; 6];
        compute_moments(&dist, 6, &mut m);

        assert!((m[0] - 0.5).abs() < 0.01, "m1 = {}", m[0]);
        assert!((m[1] - 1.0 / 3.0).abs() < 0.01, "m2 = {}", m[1]);
        assert!((m[2] - 0.25).abs() < 0.01, "m3 = {}", m[2]);
        assert!((m[3] - 0.2).abs() < 0.01, "m4 = {}", m[3]);
        assert!((m[4] - 1.0 / 6.0).abs() < 0.01, "m5 = {}", m[4]);
    }

    #[test]
    fn test_moment_cumulant_roundtrip() {
        let moments_in = vec![2.0, 7.0, 28.0, 127.0, 626.0];
        let n = moments_in.len();
        let mut cumulants = vec![0.0_f64; n];
        let mut moments_out = vec![0.0_f64; n];

        moment_to_cumulant(&moments_in, &mut cumulants);
        cumulant_to_moment(&cumulants, &mut moments_out);

        for i in 0..n {
            assert!(
                (moments_out[i] - moments_in[i]).abs() < 1e-8,
                "i={}: expected {}, got {}",
                i,
                moments_in[i],
                moments_out[i]
            );
        }
    }

    #[test]
    fn test_gaussian_moment_cumulant_roundtrip() {
        // Standard normal: m1=0, m2=1, m3=0, m4=3, m5=0, m6=15
        let moments = vec![0.0, 1.0, 0.0, 3.0, 0.0, 15.0];
        let n = moments.len();
        let mut cumulants = vec![0.0_f64; n];
        let mut moments_rt = vec![0.0_f64; n];

        moment_to_cumulant(&moments, &mut cumulants);
        cumulant_to_moment(&cumulants, &mut moments_rt);

        for i in 0..n {
            assert!(
                (moments_rt[i] - moments[i]).abs() < 1e-8,
                "i={}: expected {}, got {}",
                i,
                moments[i],
                moments_rt[i]
            );
        }
    }

    #[test]
    fn test_degenerate_point_mass() {
        // Point mass at c=3: m_k = 3^k
        let c = 3.0;
        let moments = vec![c, c * c, c * c * c, c * c * c * c];
        let n = moments.len();
        let mut cumulants = vec![0.0_f64; n];

        moment_to_cumulant(&moments, &mut cumulants);

        // Free cumulants of point mass: κ₁ = c, κ_n = 0 for n ≥ 2
        assert!((cumulants[0] - c).abs() < 1e-10, "κ1 = {}", cumulants[0]);
        assert!(
            cumulants[1].abs() < 1e-8,
            "κ2 = {} (should be 0)",
            cumulants[1]
        );
        assert!(
            cumulants[2].abs() < 1e-8,
            "κ3 = {} (should be 0)",
            cumulants[2]
        );
        assert!(
            cumulants[3].abs() < 1e-8,
            "κ4 = {} (should be 0)",
            cumulants[3]
        );
    }

    #[test]
    fn test_single_point_distribution() {
        let points = vec![5.0];
        let dist = EmpiricalDist {
            support_min: 5.0,
            support_max: 5.0,
            points: &points,
        };

        let mut m = vec![0.0_f64; 3];
        compute_moments(&dist, 3, &mut m);

        assert!((m[0] - 5.0).abs() < 1e-12);
        assert!((m[1] - 25.0).abs() < 1e-12);
        assert!((m[2] - 125.0).abs() < 1e-12);
    }

    #[test]
    fn test_two_point_distribution() {
        let points = vec![0.0, 2.0];
        let dist = EmpiricalDist {
            support_min: 0.0,
            support_max: 2.0,
            points: &points,
        };

        let mut m = vec![0.0_f64; 4];
        compute_moments(&dist, 4, &mut m);

        assert!((m[0] - 1.0).abs() < 1e-12);
        assert!((m[1] - 2.0).abs() < 1e-12);
        assert!((m[2] - 4.0).abs() < 1e-12);
        assert!((m[3] - 8.0).abs() < 1e-12);

        // Roundtrip
        let mut cumulants = vec![0.0_f64; 4];
        let mut m_rt = vec![0.0_f64; 4];
        moment_to_cumulant(&m, &mut cumulants);
        cumulant_to_moment(&cumulants, &mut m_rt);

        for i in 0..4 {
            assert!((m_rt[i] - m[i]).abs() < 1e-8, "i={}: {} vs {}", i, m_rt[i], m[i]);
        }
    }

    #[test]
    fn test_validate_valid_moments() {
        // Gaussian(0,1): 0, 1, 0, 3
        let moments = vec![0.0, 1.0, 0.0, 3.0];
        assert!(validate_moments(&moments), "valid Gaussian rejected");
    }

    #[test]
    fn test_validate_invalid_moments() {
        // Impossible: m₂ < m₁² → variance < 0
        let moments = vec![5.0, 1.0];
        assert!(!validate_moments(&moments), "invalid moments accepted");
    }

    #[test]
    fn test_validate_uniform_moments() {
        // Uniform[0,1]
        let moments = vec![0.5, 1.0 / 3.0, 0.25, 0.2];
        assert!(validate_moments(&moments), "valid uniform rejected");
    }

    #[test]
    fn test_large_order_roundtrip() {
        // Test with higher-order moments (order 10)
        let mut moments_in = Vec::new();
        for k in 1..=10 {
            moments_in.push(2.0_f64.powi(k as i32));
        }
        let n = moments_in.len();
        let mut cumulants = vec![0.0_f64; n];
        let mut moments_out = vec![0.0_f64; n];

        moment_to_cumulant(&moments_in, &mut cumulants);
        cumulant_to_moment(&cumulants, &mut moments_out);

        for i in 0..n {
            assert!(
                (moments_out[i] - moments_in[i]).abs() < 1e-6,
                "i={}: expected {}, got {}",
                i,
                moments_in[i],
                moments_out[i]
            );
        }
    }

    #[test]
    fn test_bernoulli_moments() {
        // Bernoulli(0.5): X ∈ {0, 1}, E[X] = 0.5, E[X²] = 0.5, ..., E[X^k] = 0.5
        let points: Vec<f64> = vec![0.0, 1.0];
        let dist = EmpiricalDist {
            support_min: 0.0,
            support_max: 1.0,
            points: &points,
        };

        let mut m = vec![0.0_f64; 5];
        compute_moments(&dist, 5, &mut m);

        for k in 0..5 {
            assert!((m[k] - 0.5).abs() < 1e-12, "m{} = {}", k + 1, m[k]);
        }
    }
}