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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
//! Statistical distribution traits
//!
//! This module defines the core traits for statistical distributions,
//! including standard distributions and specialized circular distributions.
use crate::error::StatsResult;
use scirs2_core::ndarray::{Array, Array1, ArrayD, ArrayView1, IxDyn};
use scirs2_core::numeric::Float;
/// Base trait for all statistical distributions
pub trait Distribution<F: Float> {
/// Mean (expected value) of the distribution
fn mean(&self) -> F;
/// Variance of the distribution
fn var(&self) -> F;
/// Standard deviation of the distribution
fn std(&self) -> F;
/// Generate random samples from the distribution
///
/// # Arguments
///
/// * `size` - Number of samples to generate
///
/// # Returns
///
/// An array of random samples from the distribution
fn rvs(&self, size: usize) -> StatsResult<Array1<F>>;
/// Generate random samples with a user-specified output shape.
///
/// This is the SciPy-style ergonomic where `dist.rvs_array(&[8, 6])` returns
/// an `8 × 6` array of samples drawn IID from this distribution. The default
/// implementation draws `prod(shape)` scalar samples through `rvs(...)` and
/// reshapes them into a dynamically-dimensioned array.
///
/// # Arguments
///
/// * `shape` - Desired output shape. An empty shape produces a 0-D scalar array.
///
/// # Returns
///
/// A dynamic-rank array of IID samples shaped exactly as requested.
///
/// # Errors
///
/// Returns an error if the underlying `rvs` call fails or if the requested
/// shape's element count overflows `usize`.
fn rvs_array(&self, shape: &[usize]) -> StatsResult<ArrayD<F>> {
// Compute total element count, guarding against multiplication overflow.
let mut total: usize = 1;
for &dim in shape {
total = total.checked_mul(dim).ok_or_else(|| {
crate::error::StatsError::InvalidArgument(
"Requested rvs_array shape overflows usize".to_string(),
)
})?;
}
// Empty shape ⇒ 0-D array containing a single sample.
// Zero total ⇒ build an empty array with the requested shape (e.g. shape=[3, 0]).
if total == 0 {
return Array::from_shape_vec(IxDyn(shape), Vec::<F>::new()).map_err(|e| {
crate::error::StatsError::DimensionMismatch(format!(
"Failed to construct empty rvs_array with the requested shape: {e}"
))
});
}
let flat = self.rvs(total)?;
Array::from_shape_vec(IxDyn(shape), flat.to_vec()).map_err(|e| {
crate::error::StatsError::DimensionMismatch(format!(
"Failed to reshape rvs samples into requested shape: {e}"
))
})
}
/// Entropy of the distribution
fn entropy(&self) -> F;
}
/// Trait for continuous distributions
pub trait ContinuousDistribution<F: Float>: Distribution<F> {
/// Probability density function (PDF)
///
/// # Arguments
///
/// * `x` - Point at which to evaluate the PDF
///
/// # Returns
///
/// The probability density at x
fn pdf(&self, x: F) -> F;
/// Cumulative distribution function (CDF)
///
/// # Arguments
///
/// * `x` - Point at which to evaluate the CDF
///
/// # Returns
///
/// The cumulative probability up to x
fn cdf(&self, x: F) -> F;
/// Percent point function (inverse CDF)
///
/// # Arguments
///
/// * `q` - Quantile (probability) in [0, 1]
///
/// # Returns
///
/// The value x such that CDF(x) = q
fn ppf(&self, q: F) -> StatsResult<F> {
// Default implementation using binary search
// This can be overridden for distributions with analytical ppf
if q < F::zero() || q > F::one() {
return Err(crate::error::StatsError::InvalidArgument(
"Quantile must be in [0, 1]".to_string(),
));
}
// Use binary search to find the inverse
let mut low = F::from(-10.0).expect("Failed to convert constant to float");
let mut high = F::from(10.0).expect("Failed to convert constant to float");
let eps = F::from(1e-12).expect("Failed to convert constant to float");
// Find a reasonable search range
while self.cdf(low) > q {
low = low * F::from(2.0).expect("Failed to convert constant to float");
}
while self.cdf(high) < q {
high = high * F::from(2.0).expect("Failed to convert constant to float");
}
// Binary search
for _ in 0..100 {
let mid = (low + high) / F::from(2.0).expect("Failed to convert constant to float");
let cdf_mid = self.cdf(mid);
if (cdf_mid - q).abs() < eps {
return Ok(mid);
}
if cdf_mid < q {
low = mid;
} else {
high = mid;
}
}
Ok((low + high) / F::from(2.0).expect("Failed to convert constant to float"))
}
/// Vectorised PDF: evaluate `pdf` at every element of `x`.
///
/// SciPy-style ergonomic — accepts a 1-D `ArrayView1` and returns an owned
/// `Array1` with the same length. The default implementation broadcasts
/// the scalar `pdf` via `mapv`. Distributions that can compute the PDF
/// faster on a batch are free to override this method.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_stats::distributions::Normal;
/// use scirs2_stats::traits::ContinuousDistribution;
///
/// let n = Normal::new(0.0_f64, 1.0).expect("normal");
/// let xs = array![-1.0, 0.0, 1.0];
/// let pdfs = n.pdf_array(&xs.view());
/// assert_eq!(pdfs.len(), 3);
/// ```
fn pdf_array(&self, x: &ArrayView1<F>) -> Array1<F> {
x.mapv(|xi| self.pdf(xi))
}
/// Vectorised CDF: evaluate `cdf` at every element of `x`.
///
/// SciPy-style ergonomic. The default implementation broadcasts the scalar
/// `cdf` via `mapv`; distributions with a fast batch CDF should override.
fn cdf_array(&self, x: &ArrayView1<F>) -> Array1<F> {
x.mapv(|xi| self.cdf(xi))
}
/// Vectorised PPF: evaluate `ppf` at every element of `q`.
///
/// Returns an error on the first element whose `ppf` call fails (typically
/// out-of-range quantiles). On success the output array has the same length
/// as the input view.
fn ppf_array(&self, q: &ArrayView1<F>) -> StatsResult<Array1<F>> {
let mut out = Array1::<F>::zeros(q.len());
for (i, &qi) in q.iter().enumerate() {
out[i] = self.ppf(qi)?;
}
Ok(out)
}
}
/// Trait for discrete distributions
pub trait DiscreteDistribution<F: Float>: Distribution<F> {
/// Probability mass function (PMF)
///
/// # Arguments
///
/// * `k` - Point at which to evaluate the PMF
///
/// # Returns
///
/// The probability mass at k
fn pmf(&self, k: F) -> F;
/// Cumulative distribution function (CDF)
///
/// # Arguments
///
/// * `k` - Point at which to evaluate the CDF
///
/// # Returns
///
/// The cumulative probability up to k
fn cdf(&self, k: F) -> F;
/// Support of the distribution (range of possible values)
fn support(&self) -> (Option<F>, Option<F>) {
(None, None) // Default: unbounded support
}
/// Percent point function (inverse CDF)
///
/// Returns the smallest integer `k` in the support such that `CDF(k) >= p`.
/// This default implementation uses exponential expansion + binary search on
/// the CDF, so it works for any discrete distribution that provides `cdf()`,
/// `mean()`, `std()`, and `support()`.
///
/// Distributions with a closed-form quantile function should override this.
fn ppf(&self, p: F) -> StatsResult<F> {
// --- Validate input ---
if p < F::zero() || p > F::one() {
return Err(crate::error::StatsError::DomainError(
"Probability must be in [0, 1]".to_string(),
));
}
// Determine support bounds (integer-valued).
let (sup_lo, sup_hi) = self.support();
let lo_bound: i64 = match sup_lo {
Some(v) => {
let raw = v.to_f64().unwrap_or(0.0);
raw.ceil() as i64
}
None => i64::MIN / 2,
};
let hi_bound: i64 = match sup_hi {
Some(v) => {
let raw = v.to_f64().unwrap_or(f64::MAX);
raw.floor() as i64
}
None => i64::MAX / 2,
};
// p == 0 → return support minimum.
if p <= F::zero() {
let lo_f = F::from(lo_bound.max(0)).ok_or_else(|| {
crate::error::StatsError::ComputationError(
"Cannot convert support lower bound to F".to_string(),
)
})?;
return Ok(lo_f);
}
// p == 1 → return support maximum (or +∞ for infinite support).
if p >= F::one() {
return match sup_hi {
Some(v) => Ok(v),
None => Ok(F::infinity()),
};
}
// --- Derive initial search range from mean ± 10·std ---
let mean_f64 = self.mean().to_f64().unwrap_or(0.0);
let std_f64 = self.std().to_f64().unwrap_or(1.0).max(1.0);
let mut lo: i64 = ((mean_f64 - 10.0 * std_f64).floor() as i64).max(lo_bound);
let mut hi: i64 = ((mean_f64 + 10.0 * std_f64).ceil() as i64).min(hi_bound);
// Clamp lo so it is at least support minimum.
lo = lo.max(lo_bound);
// Ensure hi > lo.
if hi <= lo {
hi = lo + 1;
}
// --- Expand lo leftward until CDF(lo) can be < p ---
// (For distributions starting at 0 this loop usually doesn't run.)
{
let mut step: i64 = (std_f64 * 10.0).ceil() as i64 + 1;
loop {
let lo_f = F::from(lo).ok_or_else(|| {
crate::error::StatsError::ComputationError(
"Overflow expanding lower bound in ppf".to_string(),
)
})?;
if self.cdf(lo_f) < p || lo <= lo_bound {
break;
}
lo = (lo - step).max(lo_bound);
step = step.saturating_mul(2);
}
lo = lo.max(lo_bound);
}
// --- Expand hi rightward until CDF(hi) >= p ---
{
let cap: i64 = 1_000_000_000i64.min(hi_bound);
let mut step: i64 = (std_f64 * 10.0).ceil() as i64 + 1;
let mut iters = 0usize;
loop {
let hi_f = F::from(hi).ok_or_else(|| {
crate::error::StatsError::ComputationError(
"Overflow expanding upper bound in ppf".to_string(),
)
})?;
if self.cdf(hi_f) >= p || hi >= cap {
break;
}
hi = (hi + step).min(cap);
step = step.saturating_mul(2);
iters += 1;
if iters > 100 {
break;
}
}
}
// --- Binary search: smallest integer k in [lo, hi] with CDF(k) >= p ---
// Invariant: CDF(hi) >= p (if not, hi is the best we have).
let mut left: i64 = lo;
let mut right: i64 = hi;
// Check if the right bound actually satisfies the condition.
let right_f = F::from(right).ok_or_else(|| {
crate::error::StatsError::ComputationError(
"Overflow converting hi bound in ppf".to_string(),
)
})?;
if self.cdf(right_f) < p {
// Support is exhausted; return hi.
return Ok(right_f);
}
while right - left > 1 {
let mid: i64 = left + (right - left) / 2;
let mid_f = F::from(mid).ok_or_else(|| {
crate::error::StatsError::ComputationError(
"Overflow converting mid in ppf binary search".to_string(),
)
})?;
if self.cdf(mid_f) >= p {
right = mid;
} else {
left = mid;
}
}
// After the loop, `right` satisfies cdf(right) >= p.
// Check if `left` also satisfies cdf(left) >= p — if so it is the
// *smaller* integer and is the correct answer.
let left_f = F::from(left).ok_or_else(|| {
crate::error::StatsError::ComputationError(
"Overflow converting left in ppf".to_string(),
)
})?;
if self.cdf(left_f) >= p {
return Ok(left_f);
}
F::from(right).ok_or_else(|| {
crate::error::StatsError::ComputationError(
"Overflow converting result in ppf".to_string(),
)
})
}
/// Log probability mass function
fn logpmf(&self, x: F) -> F {
self.pmf(x).ln()
}
}
/// Trait for circular distributions (distributions on the unit circle)
pub trait CircularDistribution<F: Float>: Distribution<F> {
/// Probability density function for circular distributions
///
/// # Arguments
///
/// * `x` - Angle in radians
///
/// # Returns
///
/// The probability density at angle x
fn pdf(&self, x: F) -> F;
/// Cumulative distribution function for circular distributions
///
/// # Arguments
///
/// * `x` - Angle in radians
///
/// # Returns
///
/// The cumulative probability up to angle x
fn cdf(&self, x: F) -> F;
/// Generate a single random sample
///
/// # Returns
///
/// A single random sample from the distribution
fn rvs_single(&self) -> StatsResult<F>;
/// Circular mean (mean direction)
///
/// # Returns
///
/// The mean direction in radians
fn circular_mean(&self) -> F;
/// Circular variance
///
/// # Returns
///
/// The circular variance (1 - mean resultant length)
fn circular_variance(&self) -> F;
/// Circular standard deviation
///
/// # Returns
///
/// The circular standard deviation in radians
fn circular_std(&self) -> F;
/// Mean resultant length
///
/// # Returns
///
/// The mean resultant length (measure of concentration)
fn mean_resultant_length(&self) -> F;
/// Concentration parameter
///
/// # Returns
///
/// The concentration parameter of the distribution
fn concentration(&self) -> F;
}
/// Trait for multivariate distributions
pub trait MultivariateDistribution<F: Float> {
/// Probability density function for multivariate distributions
///
/// # Arguments
///
/// * `x` - Point at which to evaluate the PDF
///
/// # Returns
///
/// The probability density at x
fn pdf(&self, x: &Array1<F>) -> F;
/// Generate random samples from the multivariate distribution
///
/// # Arguments
///
/// * `size` - Number of samples to generate
///
/// # Returns
///
/// A matrix where each row is a sample
fn rvs(&self, size: usize) -> StatsResult<scirs2_core::ndarray::Array2<F>>;
/// Mean vector of the distribution
fn mean(&self) -> Array1<F>;
/// Covariance matrix of the distribution
fn cov(&self) -> scirs2_core::ndarray::Array2<F>;
/// Dimensionality of the distribution
fn dim(&self) -> usize;
/// Log probability density function for multivariate distributions
fn logpdf(&self, x: &Array1<F>) -> F {
self.pdf(x).ln()
}
/// Generate a single random sample from the multivariate distribution
fn rvs_single(&self) -> StatsResult<Vec<F>> {
let samples = self.rvs(1)?;
Ok(samples.row(0).to_vec())
}
}
/// Trait for distributions that support fitting to data
pub trait Fittable<F: Float> {
/// Fit the distribution to observed data
///
/// # Arguments
///
/// * `data` - Observed data points
///
/// # Returns
///
/// A fitted distribution instance
fn fit(data: &Array1<F>) -> StatsResult<Self>
where
Self: Sized;
/// Maximum likelihood estimation of parameters
///
/// # Arguments
///
/// * `data` - Observed data points
///
/// # Returns
///
/// A tuple of estimated parameters
fn mle(data: &Array1<F>) -> StatsResult<Vec<F>>;
}
/// Trait for distributions that can be truncated
pub trait Truncatable<F: Float>: Distribution<F> {
/// Create a truncated version of the distribution
///
/// # Arguments
///
/// * `lower` - Lower bound (None for no lower bound)
/// * `upper` - Upper bound (None for no upper bound)
///
/// # Returns
///
/// A truncated version of the distribution
fn truncate(&self, lower: Option<F>, upper: Option<F>)
-> StatsResult<Box<dyn Distribution<F>>>;
}
/// Trait for continuous distributions that support CDF-related functions
pub trait ContinuousCDF<F: Float>: ContinuousDistribution<F> {
/// Survival function (1 - CDF)
///
/// # Arguments
///
/// * `x` - Point at which to evaluate the survival function
///
/// # Returns
///
/// The survival probability at x (1 - CDF(x))
fn sf(&self, x: F) -> F {
F::one() - self.cdf(x)
}
/// Hazard function (PDF / (1 - CDF))
///
/// # Arguments
///
/// * `x` - Point at which to evaluate the hazard function
///
/// # Returns
///
/// The hazard rate at x
fn hazard(&self, x: F) -> F {
let sf_val = self.sf(x);
if sf_val == F::zero() {
F::infinity()
} else {
self.pdf(x) / sf_val
}
}
/// Cumulative hazard function (-ln(survival function))
///
/// # Arguments
///
/// * `x` - Point at which to evaluate the cumulative hazard function
///
/// # Returns
///
/// The cumulative hazard at x (-ln(1 - CDF(x)))
fn cumhazard(&self, x: F) -> F {
let sf_val = self.sf(x);
if sf_val <= F::zero() {
F::infinity()
} else {
-sf_val.ln()
}
}
/// Inverse survival function (PPF(1 - q))
///
/// # Arguments
///
/// * `q` - Probability in [0, 1]
///
/// # Returns
///
/// The value x such that SF(x) = q (equivalent to PPF(1 - q))
fn isf(&self, q: F) -> StatsResult<F> {
if q < F::zero() || q > F::one() {
return Err(crate::error::StatsError::InvalidArgument(
"Probability must be in [0, 1]".to_string(),
));
}
self.ppf(F::one() - q)
}
}
/// Trait for discrete distributions that support CDF-related functions
pub trait DiscreteCDF<F: Float>: DiscreteDistribution<F> {
/// Survival function (1 - CDF)
///
/// # Arguments
///
/// * `k` - Point at which to evaluate the survival function
///
/// # Returns
///
/// The survival probability at k (1 - CDF(k))
fn sf(&self, k: F) -> F {
F::one() - self.cdf(k)
}
/// Hazard function (PMF / (1 - CDF))
///
/// # Arguments
///
/// * `k` - Point at which to evaluate the hazard function
///
/// # Returns
///
/// The hazard rate at k
fn hazard(&self, k: F) -> F {
let sf_val = self.sf(k);
if sf_val == F::zero() {
F::infinity()
} else {
self.pmf(k) / sf_val
}
}
/// Cumulative hazard function (-ln(survival function))
///
/// # Arguments
///
/// * `k` - Point at which to evaluate the cumulative hazard function
///
/// # Returns
///
/// The cumulative hazard at k (-ln(1 - CDF(k)))
fn cumhazard(&self, k: F) -> F {
let sf_val = self.sf(k);
if sf_val <= F::zero() {
F::infinity()
} else {
-sf_val.ln()
}
}
/// Inverse survival function (PPF(1 - q))
///
/// # Arguments
///
/// * `q` - Probability in [0, 1]
///
/// # Returns
///
/// The value k such that SF(k) = q (equivalent to PPF(1 - q))
fn isf(&self, q: F) -> StatsResult<F> {
if q < F::zero() || q > F::one() {
return Err(crate::error::StatsError::InvalidArgument(
"Probability must be in [0, 1]".to_string(),
));
}
self.ppf(F::one() - q)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::StatsResult;
use scirs2_core::ndarray::Array1;
use scirs2_core::numeric::Float;
// -----------------------------------------------------------------------
// Test fixture: Bernoulli(p) implemented via the default ppf
// -----------------------------------------------------------------------
struct TestBernoulli {
p: f64,
}
impl Distribution<f64> for TestBernoulli {
fn mean(&self) -> f64 {
self.p
}
fn var(&self) -> f64 {
self.p * (1.0 - self.p)
}
fn std(&self) -> f64 {
self.var().sqrt()
}
fn rvs(&self, size: usize) -> StatsResult<Array1<f64>> {
Ok(Array1::zeros(size))
}
fn entropy(&self) -> f64 {
0.0
}
}
impl DiscreteDistribution<f64> for TestBernoulli {
fn pmf(&self, k: f64) -> f64 {
if (k - 0.0).abs() < 1e-9 {
1.0 - self.p
} else if (k - 1.0).abs() < 1e-9 {
self.p
} else {
0.0
}
}
fn cdf(&self, k: f64) -> f64 {
if k < 0.0 {
0.0
} else if k < 1.0 {
1.0 - self.p
} else {
1.0
}
}
fn support(&self) -> (Option<f64>, Option<f64>) {
(Some(0.0), Some(1.0))
}
// ppf deliberately NOT overridden → exercises the default
}
// -----------------------------------------------------------------------
// Test fixture: Poisson-like (mu) via default ppf
// -----------------------------------------------------------------------
struct TestPoisson {
mu: f64,
}
impl Distribution<f64> for TestPoisson {
fn mean(&self) -> f64 {
self.mu
}
fn var(&self) -> f64 {
self.mu
}
fn std(&self) -> f64 {
self.mu.sqrt()
}
fn rvs(&self, size: usize) -> StatsResult<Array1<f64>> {
Ok(Array1::zeros(size))
}
fn entropy(&self) -> f64 {
0.0
}
}
impl DiscreteDistribution<f64> for TestPoisson {
fn pmf(&self, k: f64) -> f64 {
if k < 0.0 || (k - k.floor()).abs() > 1e-9 {
return 0.0;
}
let ki = k.round() as u64;
let mut log_pmf = -(self.mu) + (ki as f64) * self.mu.ln();
for i in 1..=ki {
log_pmf -= (i as f64).ln();
}
log_pmf.exp()
}
fn cdf(&self, k: f64) -> f64 {
if k < 0.0 {
return 0.0;
}
let ki = k.floor() as u64;
(0..=ki).map(|i| self.pmf(i as f64)).sum::<f64>()
}
fn support(&self) -> (Option<f64>, Option<f64>) {
(Some(0.0), None)
}
// ppf deliberately NOT overridden
}
// -----------------------------------------------------------------------
// Test fixture: Geometric(p) (# failures before first success, starting at 0)
// CDF(k) = 1 - (1-p)^(k+1)
// -----------------------------------------------------------------------
struct TestGeometric {
p: f64,
}
impl Distribution<f64> for TestGeometric {
fn mean(&self) -> f64 {
(1.0 - self.p) / self.p
}
fn var(&self) -> f64 {
(1.0 - self.p) / (self.p * self.p)
}
fn std(&self) -> f64 {
self.var().sqrt()
}
fn rvs(&self, size: usize) -> StatsResult<Array1<f64>> {
Ok(Array1::zeros(size))
}
fn entropy(&self) -> f64 {
0.0
}
}
impl DiscreteDistribution<f64> for TestGeometric {
fn pmf(&self, k: f64) -> f64 {
if k < 0.0 || (k - k.floor()).abs() > 1e-9 {
return 0.0;
}
let ki = k.round() as u64;
self.p * (1.0 - self.p).powi(ki as i32)
}
fn cdf(&self, k: f64) -> f64 {
if k < 0.0 {
return 0.0;
}
let ki = k.floor() as u64;
1.0 - (1.0 - self.p).powi(ki as i32 + 1)
}
fn support(&self) -> (Option<f64>, Option<f64>) {
(Some(0.0), None)
}
// ppf deliberately NOT overridden
}
// -----------------------------------------------------------------------
// Helper: verify cdf(ppf(p)) >= p (fundamental discrete PPF invariant)
// Also verifies ppf(p) is the SMALLEST such k.
// -----------------------------------------------------------------------
fn check_ppf_invariant<D: DiscreteDistribution<f64>>(dist: &D, p: f64) {
let k = dist
.ppf(p)
.unwrap_or_else(|e| panic!("ppf({p}) failed: {e}"));
let cdf_k = dist.cdf(k);
assert!(
cdf_k >= p - 1e-12,
"CDF({k}) = {cdf_k} < p = {p}: invariant cdf(ppf(p)) >= p violated"
);
// Verify that k-1 does NOT satisfy the condition (k is smallest).
if k >= 1.0 {
let cdf_km1 = dist.cdf(k - 1.0);
assert!(
cdf_km1 < p + 1e-12,
"CDF({}) = {} >= p = {p}: k={k} is not the SMALLEST such integer",
k - 1.0,
cdf_km1
);
}
}
// -----------------------------------------------------------------------
// Bernoulli(p=0.3) tests
// -----------------------------------------------------------------------
#[test]
fn test_default_ppf_bernoulli_invariants() {
let b = TestBernoulli { p: 0.3 };
for &p in &[0.1f64, 0.25, 0.5, 0.7, 0.9] {
check_ppf_invariant(&b, p);
}
}
#[test]
fn test_default_ppf_bernoulli_known_values() {
let b = TestBernoulli { p: 0.3 };
// CDF(0) = 0.7, so ppf(p) = 0 for p <= 0.7 and 1 for p > 0.7
assert_eq!(b.ppf(0.5).expect("ppf(0.5)"), 0.0);
assert_eq!(b.ppf(0.7).expect("ppf(0.7)"), 0.0);
assert_eq!(b.ppf(0.8).expect("ppf(0.8)"), 1.0);
assert_eq!(b.ppf(1.0).expect("ppf(1.0)"), 1.0);
}
#[test]
fn test_default_ppf_bernoulli_p0_returns_support_lo() {
let b = TestBernoulli { p: 0.7 };
// p == 0 → support minimum = 0
assert_eq!(b.ppf(0.0).expect("ppf(0.0)"), 0.0);
}
#[test]
fn test_default_ppf_bernoulli_p1_returns_support_hi() {
let b = TestBernoulli { p: 0.7 };
// p == 1 → support maximum = 1
assert_eq!(b.ppf(1.0).expect("ppf(1.0)"), 1.0);
}
#[test]
fn test_default_ppf_bernoulli_out_of_range() {
let b = TestBernoulli { p: 0.5 };
assert!(b.ppf(-0.1).is_err());
assert!(b.ppf(1.1).is_err());
}
// -----------------------------------------------------------------------
// Poisson(mu=3) tests
// -----------------------------------------------------------------------
#[test]
fn test_default_ppf_poisson_invariants() {
let po = TestPoisson { mu: 3.0 };
for &p in &[0.1f64, 0.25, 0.5, 0.75, 0.9] {
check_ppf_invariant(&po, p);
}
}
#[test]
fn test_default_ppf_poisson_median() {
let po = TestPoisson { mu: 3.0 };
// Median of Poisson(3) is 3
let med = po.ppf(0.5).expect("ppf(0.5)");
assert!(
(med - 3.0).abs() <= 1.0,
"Expected median near 3, got {med}"
);
}
#[test]
fn test_default_ppf_poisson_p0_returns_zero() {
let po = TestPoisson { mu: 5.0 };
assert_eq!(po.ppf(0.0).expect("ppf(0.0)"), 0.0);
}
#[test]
fn test_default_ppf_poisson_p1_returns_infinity() {
let po = TestPoisson { mu: 5.0 };
// Poisson has no finite upper bound → +∞
let v = po.ppf(1.0).expect("ppf(1.0)");
assert!(v.is_infinite() && v > 0.0, "Expected +∞, got {v}");
}
#[test]
fn test_default_ppf_poisson_large_mu_invariants() {
let po = TestPoisson { mu: 50.0 };
for &p in &[0.1f64, 0.5, 0.9] {
check_ppf_invariant(&po, p);
}
}
// -----------------------------------------------------------------------
// Geometric(p=0.4) tests
// -----------------------------------------------------------------------
#[test]
fn test_default_ppf_geometric_invariants() {
let g = TestGeometric { p: 0.4 };
for &p in &[0.1f64, 0.25, 0.5, 0.75, 0.9] {
check_ppf_invariant(&g, p);
}
}
#[test]
fn test_default_ppf_geometric_p0_returns_zero() {
let g = TestGeometric { p: 0.3 };
assert_eq!(g.ppf(0.0).expect("ppf(0.0)"), 0.0);
}
#[test]
fn test_default_ppf_geometric_p1_returns_infinity() {
let g = TestGeometric { p: 0.3 };
let v = g.ppf(1.0).expect("ppf(1.0)");
assert!(v.is_infinite() && v > 0.0, "Expected +∞, got {v}");
}
#[test]
fn test_default_ppf_geometric_heavy_tail() {
// Small p → heavy tail, exercises exponential expansion of hi
let g = TestGeometric { p: 0.05 };
for &p in &[0.5f64, 0.9, 0.99] {
check_ppf_invariant(&g, p);
}
}
}