numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Comprehensive tests for statistical distribution functions
//!
//! This test suite validates the PDF, CDF, and quantile functions for various
//! probability distributions against known values and mathematical properties.

use numrs2::stats::distributions::*;

const TOL: f64 = 1e-6;
const LOOSER_TOL: f64 = 1e-4;

// ============================================================================
// Beta Distribution Tests
// ============================================================================

#[test]
fn test_beta_pdf_basic() {
    // Beta(2, 3) PDF at x=0.5
    // Expected: B(2,3) = Γ(2)Γ(3)/Γ(5) = 1*2/24 = 1/12
    // PDF = x^(a-1) * (1-x)^(b-1) / B(a,b) = 0.5^1 * 0.5^2 / (1/12) = 0.125 * 12 = 1.5
    let pdf: f64 = beta_pdf(0.5, 2.0, 3.0).expect("beta_pdf should succeed");
    assert!((pdf - 1.5).abs() < TOL, "Beta(2,3) PDF at 0.5 = {}", pdf);
}

#[test]
fn test_beta_pdf_edge_cases() {
    // PDF at x=0 with a>1
    let pdf: f64 = beta_pdf(0.0, 2.0, 3.0).expect("beta_pdf should succeed");
    assert_eq!(pdf, 0.0, "Beta PDF at x=0 should be 0");

    // PDF at x=1 with b>1
    let pdf: f64 = beta_pdf(1.0, 2.0, 3.0).expect("beta_pdf should succeed");
    assert_eq!(pdf, 0.0, "Beta PDF at x=1 should be 0");

    // PDF outside [0,1]
    let pdf: f64 = beta_pdf(1.5, 2.0, 3.0).expect("beta_pdf should succeed");
    assert_eq!(pdf, 0.0, "Beta PDF outside [0,1] should be 0");
}

#[test]
fn test_beta_cdf_properties() {
    // CDF at 0 should be 0
    let cdf: f64 = beta_cdf(0.0, 2.0, 3.0).expect("beta_cdf should succeed");
    assert!((cdf - 0.0).abs() < TOL, "Beta CDF at 0 = {}", cdf);

    // CDF at 1 should be 1
    let cdf: f64 = beta_cdf(1.0, 2.0, 3.0).expect("beta_cdf should succeed");
    assert!((cdf - 1.0).abs() < TOL, "Beta CDF at 1 = {}", cdf);

    // CDF should be monotonically increasing
    let cdf1 = beta_cdf(0.3, 2.0, 3.0).expect("beta_cdf should succeed");
    let cdf2 = beta_cdf(0.5, 2.0, 3.0).expect("beta_cdf should succeed");
    let cdf3 = beta_cdf(0.7, 2.0, 3.0).expect("beta_cdf should succeed");
    assert!(cdf1 < cdf2 && cdf2 < cdf3, "Beta CDF should be increasing");
}

#[test]
fn test_beta_ppf_cdf_inverse() {
    // PPF should be inverse of CDF
    let x = 0.5;
    let cdf: f64 = beta_cdf(x, 2.0, 3.0).expect("beta_cdf should succeed");
    let ppf: f64 = beta_ppf(cdf, 2.0, 3.0).expect("beta_ppf should succeed");
    assert!(
        (ppf - x).abs() < LOOSER_TOL,
        "Beta PPF(CDF(x)) ≈ x, got {}",
        ppf
    );
}

#[test]
fn test_beta_logpdf_consistency() {
    // log PDF should equal log of PDF
    let x = 0.5;
    let a = 2.0;
    let b = 3.0;
    let pdf: f64 = beta_pdf(x, a, b).expect("beta_pdf should succeed");
    let logpdf = beta_logpdf(x, a, b).expect("beta_logpdf should succeed");
    assert!(
        (logpdf - pdf.ln()).abs() < TOL,
        "log PDF should match ln(PDF)"
    );
}

// ============================================================================
// Gamma Distribution Tests
// ============================================================================

#[test]
fn test_gamma_pdf_basic() {
    // Gamma(1, 1) is exponential distribution with rate 1
    // PDF at x=0 should be 1.0
    let pdf: f64 = gamma_pdf(0.0001, 1.0, 1.0).expect("gamma_pdf should succeed");
    assert!((pdf - 1.0).abs() < 0.01, "Gamma(1,1) PDF at x≈0 ≈ 1.0");
}

#[test]
fn test_gamma_pdf_edge_cases() {
    // PDF at x=0
    let pdf: f64 = gamma_pdf(0.0, 2.0, 1.0).expect("gamma_pdf should succeed");
    assert_eq!(pdf, 0.0, "Gamma PDF at x=0 should be 0");

    // PDF at negative x
    let pdf: f64 = gamma_pdf(-1.0, 2.0, 1.0).expect("gamma_pdf should succeed");
    assert_eq!(pdf, 0.0, "Gamma PDF at x<0 should be 0");
}

#[test]
fn test_gamma_cdf_properties() {
    // CDF at 0 should be 0
    let cdf: f64 = gamma_cdf(0.0, 2.0, 1.0).expect("gamma_cdf should succeed");
    assert!((cdf - 0.0).abs() < TOL, "Gamma CDF at 0 = {}", cdf);

    // CDF should be monotonically increasing
    let cdf1 = gamma_cdf(1.0, 2.0, 1.0).expect("gamma_cdf should succeed");
    let cdf2 = gamma_cdf(2.0, 2.0, 1.0).expect("gamma_cdf should succeed");
    let cdf3 = gamma_cdf(3.0, 2.0, 1.0).expect("gamma_cdf should succeed");
    assert!(cdf1 < cdf2 && cdf2 < cdf3, "Gamma CDF should be increasing");
}

#[test]
fn test_gamma_ppf_cdf_inverse() {
    // PPF should be inverse of CDF
    let x = 2.0;
    let cdf: f64 = gamma_cdf(x, 2.0, 1.0).expect("gamma_cdf should succeed");
    let ppf: f64 = gamma_ppf(cdf, 2.0, 1.0).expect("gamma_ppf should succeed");
    assert!(
        (ppf - x).abs() < LOOSER_TOL,
        "Gamma PPF(CDF(x)) ≈ x, got {}",
        ppf
    );
}

#[test]
fn test_gamma_logpdf_consistency() {
    // log PDF should equal log of PDF
    let x = 2.0;
    let shape = 2.0;
    let scale = 1.0;
    let pdf: f64 = gamma_pdf(x, shape, scale).expect("gamma_pdf should succeed");
    let logpdf = gamma_logpdf(x, shape, scale).expect("gamma_logpdf should succeed");
    assert!(
        (logpdf - pdf.ln()).abs() < TOL,
        "log PDF should match ln(PDF)"
    );
}

// ============================================================================
// Student's t-Distribution Tests
// ============================================================================

#[test]
fn test_student_t_pdf_basic() {
    // Student's t PDF at x=0 with any df should be symmetric
    let pdf: f64 = student_t_pdf(0.0, 10.0).expect("student_t_pdf should succeed");
    assert!(pdf > 0.0, "Student's t PDF at 0 should be positive");

    // PDF should be symmetric
    let pdf_pos: f64 = student_t_pdf(1.5, 10.0).expect("student_t_pdf should succeed");
    let pdf_neg: f64 = student_t_pdf(-1.5, 10.0).expect("student_t_pdf should succeed");
    assert!(
        (pdf_pos - pdf_neg).abs() < TOL,
        "Student's t PDF should be symmetric"
    );
}

#[test]
fn test_student_t_cdf_properties() {
    // CDF at 0 should be 0.5
    let cdf: f64 = student_t_cdf(0.0, 10.0).expect("student_t_cdf should succeed");
    assert!((cdf - 0.5).abs() < TOL, "Student's t CDF at 0 = {}", cdf);

    // CDF should be monotonically increasing
    let cdf1 = student_t_cdf(-1.0, 10.0).expect("student_t_cdf should succeed");
    let cdf2 = student_t_cdf(0.0, 10.0).expect("student_t_cdf should succeed");
    let cdf3 = student_t_cdf(1.0, 10.0).expect("student_t_cdf should succeed");
    assert!(
        cdf1 < cdf2 && cdf2 < cdf3,
        "Student's t CDF should be increasing"
    );
}

#[test]
fn test_student_t_ppf_cdf_inverse() {
    // PPF should be inverse of CDF
    let x = 1.5;
    let cdf: f64 = student_t_cdf(x, 10.0).expect("student_t_cdf should succeed");
    let ppf: f64 = student_t_ppf(cdf, 10.0).expect("student_t_ppf should succeed");
    assert!(
        (ppf - x).abs() < LOOSER_TOL,
        "Student's t PPF(CDF(x)) ≈ x, got {}",
        ppf
    );
}

#[test]
fn test_student_t_critical_values() {
    // Test known critical values for t-distribution
    // For df=10, two-tailed 95% critical value ≈ ±2.228
    let upper: f64 = student_t_ppf(0.975, 10.0).expect("student_t_ppf should succeed");
    assert!(
        (upper - 2.228).abs() < 0.01,
        "t(10) 97.5% quantile ≈ 2.228, got {}",
        upper
    );
}

// ============================================================================
// Cauchy Distribution Tests
// ============================================================================

#[test]
fn test_cauchy_pdf_basic() {
    // Standard Cauchy PDF at x=0 should be 1/π
    let pdf: f64 = cauchy_pdf(0.0, 0.0, 1.0).expect("cauchy_pdf should succeed");
    let expected = 1.0 / std::f64::consts::PI;
    assert!((pdf - expected).abs() < TOL, "Cauchy PDF at 0 = {}", pdf);

    // PDF should be symmetric
    let pdf_pos: f64 = cauchy_pdf(2.0, 0.0, 1.0).expect("cauchy_pdf should succeed");
    let pdf_neg: f64 = cauchy_pdf(-2.0, 0.0, 1.0).expect("cauchy_pdf should succeed");
    assert!(
        (pdf_pos - pdf_neg).abs() < TOL,
        "Cauchy PDF should be symmetric"
    );
}

#[test]
fn test_cauchy_cdf_properties() {
    // CDF at median (loc) should be 0.5
    let cdf: f64 = cauchy_cdf(0.0, 0.0, 1.0).expect("cauchy_cdf should succeed");
    assert!((cdf - 0.5).abs() < TOL, "Cauchy CDF at median = {}", cdf);

    // CDF should be monotonically increasing
    let cdf1 = cauchy_cdf(-2.0, 0.0, 1.0).expect("cauchy_cdf should succeed");
    let cdf2 = cauchy_cdf(0.0, 0.0, 1.0).expect("cauchy_cdf should succeed");
    let cdf3 = cauchy_cdf(2.0, 0.0, 1.0).expect("cauchy_cdf should succeed");
    assert!(
        cdf1 < cdf2 && cdf2 < cdf3,
        "Cauchy CDF should be increasing"
    );
}

#[test]
fn test_cauchy_ppf_cdf_inverse() {
    // PPF should be inverse of CDF
    let x = 1.5;
    let cdf: f64 = cauchy_cdf(x, 0.0, 1.0).expect("cauchy_cdf should succeed");
    let ppf: f64 = cauchy_ppf(cdf, 0.0, 1.0).expect("cauchy_ppf should succeed");
    assert!((ppf - x).abs() < TOL, "Cauchy PPF(CDF(x)) ≈ x, got {}", ppf);
}

// ============================================================================
// Laplace Distribution Tests
// ============================================================================

#[test]
fn test_laplace_pdf_basic() {
    // Standard Laplace PDF at x=0 (mean) should be 1/(2*scale) = 0.5
    let pdf: f64 = laplace_pdf(0.0, 0.0, 1.0).expect("laplace_pdf should succeed");
    assert!((pdf - 0.5).abs() < TOL, "Laplace PDF at mean = {}", pdf);

    // PDF should be symmetric
    let pdf_pos: f64 = laplace_pdf(1.5, 0.0, 1.0).expect("laplace_pdf should succeed");
    let pdf_neg: f64 = laplace_pdf(-1.5, 0.0, 1.0).expect("laplace_pdf should succeed");
    assert!(
        (pdf_pos - pdf_neg).abs() < TOL,
        "Laplace PDF should be symmetric"
    );
}

#[test]
fn test_laplace_cdf_properties() {
    // CDF at mean should be 0.5
    let cdf: f64 = laplace_cdf(0.0, 0.0, 1.0).expect("laplace_cdf should succeed");
    assert!((cdf - 0.5).abs() < TOL, "Laplace CDF at mean = {}", cdf);

    // CDF should be monotonically increasing
    let cdf1 = laplace_cdf(-2.0, 0.0, 1.0).expect("laplace_cdf should succeed");
    let cdf2 = laplace_cdf(0.0, 0.0, 1.0).expect("laplace_cdf should succeed");
    let cdf3 = laplace_cdf(2.0, 0.0, 1.0).expect("laplace_cdf should succeed");
    assert!(
        cdf1 < cdf2 && cdf2 < cdf3,
        "Laplace CDF should be increasing"
    );
}

#[test]
fn test_laplace_ppf_cdf_inverse() {
    // PPF should be inverse of CDF
    let x = 1.0;
    let cdf: f64 = laplace_cdf(x, 0.0, 1.0).expect("laplace_cdf should succeed");
    let ppf: f64 = laplace_ppf(cdf, 0.0, 1.0).expect("laplace_ppf should succeed");
    assert!(
        (ppf - x).abs() < TOL,
        "Laplace PPF(CDF(x)) ≈ x, got {}",
        ppf
    );
}

// ============================================================================
// Logistic Distribution Tests
// ============================================================================

#[test]
fn test_logistic_pdf_basic() {
    // Standard Logistic PDF at x=0 (mean) should be 1/4
    let pdf: f64 = logistic_pdf(0.0, 0.0, 1.0).expect("logistic_pdf should succeed");
    assert!(
        (pdf - 0.25_f64).abs() < TOL,
        "Logistic PDF at mean = {}",
        pdf
    );

    // PDF should be symmetric
    let pdf_pos: f64 = logistic_pdf(1.5, 0.0, 1.0).expect("logistic_pdf should succeed");
    let pdf_neg: f64 = logistic_pdf(-1.5, 0.0, 1.0).expect("logistic_pdf should succeed");
    assert!(
        (pdf_pos - pdf_neg).abs() < TOL,
        "Logistic PDF should be symmetric"
    );
}

#[test]
fn test_logistic_cdf_properties() {
    // CDF at mean should be 0.5
    let cdf: f64 = logistic_cdf(0.0, 0.0, 1.0).expect("logistic_cdf should succeed");
    assert!((cdf - 0.5).abs() < TOL, "Logistic CDF at mean = {}", cdf);

    // CDF should be monotonically increasing
    let cdf1 = logistic_cdf(-2.0, 0.0, 1.0).expect("logistic_cdf should succeed");
    let cdf2 = logistic_cdf(0.0, 0.0, 1.0).expect("logistic_cdf should succeed");
    let cdf3 = logistic_cdf(2.0, 0.0, 1.0).expect("logistic_cdf should succeed");
    assert!(
        cdf1 < cdf2 && cdf2 < cdf3,
        "Logistic CDF should be increasing"
    );
}

#[test]
fn test_logistic_ppf_cdf_inverse() {
    // PPF should be inverse of CDF
    let x = 1.0;
    let cdf: f64 = logistic_cdf(x, 0.0, 1.0).expect("logistic_cdf should succeed");
    let ppf: f64 = logistic_ppf(cdf, 0.0, 1.0).expect("logistic_ppf should succeed");
    assert!(
        (ppf - x).abs() < TOL,
        "Logistic PPF(CDF(x)) ≈ x, got {}",
        ppf
    );
}

// ============================================================================
// Pareto Distribution Tests
// ============================================================================

#[test]
fn test_pareto_pdf_basic() {
    // Pareto(2, 1) PDF at x=2
    // f(2; α=2, xₘ=1) = 2 * 1^2 / 2^3 = 2/8 = 0.25
    let pdf: f64 = pareto_pdf(2.0, 2.0, 1.0).expect("pareto_pdf should succeed");
    assert!((pdf - 0.25).abs() < TOL, "Pareto(2,1) PDF at x=2 = {}", pdf);

    // PDF below xm should be 0
    let pdf: f64 = pareto_pdf(0.5, 2.0, 1.0).expect("pareto_pdf should succeed");
    assert_eq!(pdf, 0.0, "Pareto PDF below xm should be 0");
}

#[test]
fn test_pareto_cdf_properties() {
    // CDF at xm should be 0
    let cdf: f64 = pareto_cdf(1.0, 2.0, 1.0).expect("pareto_cdf should succeed");
    assert!((cdf - 0.0).abs() < TOL, "Pareto CDF at xm = {}", cdf);

    // CDF at x=2 with α=2, xₘ=1
    // F(2; 2, 1) = 1 - (1/2)^2 = 0.75
    let cdf: f64 = pareto_cdf(2.0, 2.0, 1.0).expect("pareto_cdf should succeed");
    assert!((cdf - 0.75).abs() < TOL, "Pareto(2,1) CDF at x=2 = {}", cdf);

    // CDF should be monotonically increasing
    let cdf1 = pareto_cdf(1.5, 2.0, 1.0).expect("pareto_cdf should succeed");
    let cdf2 = pareto_cdf(2.0, 2.0, 1.0).expect("pareto_cdf should succeed");
    let cdf3 = pareto_cdf(3.0, 2.0, 1.0).expect("pareto_cdf should succeed");
    assert!(
        cdf1 < cdf2 && cdf2 < cdf3,
        "Pareto CDF should be increasing"
    );
}

#[test]
fn test_pareto_ppf_cdf_inverse() {
    // PPF should be inverse of CDF
    let x = 2.0;
    let cdf: f64 = pareto_cdf(x, 2.0, 1.0).expect("pareto_cdf should succeed");
    let ppf: f64 = pareto_ppf(cdf, 2.0, 1.0).expect("pareto_ppf should succeed");
    assert!((ppf - x).abs() < TOL, "Pareto PPF(CDF(x)) ≈ x, got {}", ppf);
}

// ============================================================================
// Extreme Value Tests
// ============================================================================

#[test]
fn test_numerical_stability() {
    // Test with extreme parameter values for numerical stability

    // Beta with very small parameters
    let pdf: f64 = beta_pdf(0.5, 0.1, 0.1).expect("beta_pdf should succeed");
    assert!(
        pdf.is_finite(),
        "Beta PDF should be finite for small parameters"
    );

    // Gamma with large shape parameter
    let pdf: f64 = gamma_pdf(10.0, 100.0, 1.0).expect("gamma_pdf should succeed");
    assert!(
        pdf.is_finite(),
        "Gamma PDF should be finite for large shape"
    );

    // Student's t with very small df
    let pdf: f64 = student_t_pdf(0.0, 1.0).expect("student_t_pdf should succeed");
    assert!(pdf.is_finite(), "Student's t PDF should be finite for df=1");

    // Student's t with very large df (should approach normal)
    let pdf: f64 = student_t_pdf(0.0, 1000.0).expect("student_t_pdf should succeed");
    assert!(
        pdf.is_finite(),
        "Student's t PDF should be finite for large df"
    );
}

#[test]
fn test_error_handling() {
    // Test invalid parameters

    // Beta with non-positive parameters
    assert!(
        beta_pdf(0.5, -1.0, 3.0).is_err(),
        "Beta PDF should reject negative alpha"
    );
    assert!(
        beta_pdf(0.5, 2.0, 0.0).is_err(),
        "Beta PDF should reject zero beta"
    );

    // Gamma with non-positive parameters
    assert!(
        gamma_pdf(1.0, 0.0, 1.0).is_err(),
        "Gamma PDF should reject zero shape"
    );
    assert!(
        gamma_pdf(1.0, 2.0, -1.0).is_err(),
        "Gamma PDF should reject negative scale"
    );

    // Student's t with non-positive df
    assert!(
        student_t_pdf(0.0, 0.0).is_err(),
        "Student's t PDF should reject zero df"
    );
    assert!(
        student_t_pdf(0.0, -5.0).is_err(),
        "Student's t PDF should reject negative df"
    );

    // Cauchy with non-positive scale
    assert!(
        cauchy_pdf(0.0, 0.0, 0.0).is_err(),
        "Cauchy PDF should reject zero scale"
    );

    // Laplace with non-positive scale
    assert!(
        laplace_pdf(0.0, 0.0, -1.0).is_err(),
        "Laplace PDF should reject negative scale"
    );

    // Logistic with non-positive scale
    assert!(
        logistic_pdf(0.0, 0.0, 0.0).is_err(),
        "Logistic PDF should reject zero scale"
    );

    // Pareto with non-positive parameters
    assert!(
        pareto_pdf(2.0, 0.0, 1.0).is_err(),
        "Pareto PDF should reject zero alpha"
    );
    assert!(
        pareto_pdf(2.0, 2.0, -1.0).is_err(),
        "Pareto PDF should reject negative xm"
    );

    // Probability out of range
    assert!(beta_ppf(1.5, 2.0, 3.0).is_err(), "PPF should reject p > 1");
    assert!(
        gamma_ppf(-0.1, 2.0, 1.0).is_err(),
        "PPF should reject p < 0"
    );
}

// ============================================================================
// Integration Tests
// ============================================================================

#[test]
fn test_pdf_cdf_consistency() {
    // Verify that integrating PDF gives CDF (numerically)
    // This is a basic sanity check using trapezoidal rule

    // Beta distribution
    let a = 2.0;
    let b = 3.0;
    let x = 0.6;
    let n = 1000;
    let dx = x / n as f64;
    let mut integral = 0.0;
    for i in 1..n {
        let xi = i as f64 * dx;
        let pdf_val = beta_pdf(xi, a, b).expect("beta_pdf should succeed");
        integral += pdf_val * dx;
    }
    let cdf: f64 = beta_cdf(x, a, b).expect("beta_cdf should succeed");
    assert!((integral - cdf).abs() < 0.01, "Integral of Beta PDF ≈ CDF");
}

#[test]
fn test_distribution_relationships() {
    // Test known relationships between distributions

    // Chi-square(2) is Gamma(1, 2)
    // (Not directly tested here, but worth noting)

    // Cauchy(0,1) is Student's t with df=1
    // Compare PDFs at multiple points
    for x in &[0.0, 0.5, 1.0, 2.0] {
        let cauchy: f64 = cauchy_pdf(*x, 0.0, 1.0).expect("cauchy_pdf should succeed");
        let t1: f64 = student_t_pdf(*x, 1.0).expect("student_t_pdf should succeed");
        let diff = (cauchy - t1).abs();
        assert!(
            diff < TOL,
            "Cauchy(0,1) ≈ t(1) at x={}: cauchy={}, t1={}, diff={}, TOL={}",
            x,
            cauchy,
            t1,
            diff,
            TOL
        );
    }
}