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
//! 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::Array1;
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>>;
/// 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"))
}
}
/// 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)
fn ppf(&self, p: F) -> StatsResult<F> {
Err(crate::error::StatsError::NotImplementedError(
"PPF not implemented for this discrete distribution".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)
}
}