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
mod bernoulli;
mod beta;
mod binomial;
mod chi_squared;
mod discreteuniform;
mod exponential;
mod gamma;
mod gumbel;
mod multivariatenormal;
mod normal;
mod pareto;
mod poisson;
mod t;
mod uniform;
use crate::linalg::{Matrix, Vector};
pub trait Distribution: Send + Sync {
type Output;
fn sample(&self) -> Self::Output;
}
pub trait Distribution1D: Distribution<Output = f64> {
fn sample_n(&self, n: usize) -> Vector {
(0..n).map(|_| self.sample()).collect()
}
fn sample_matrix(&self, nrows: usize, ncols: usize) -> Matrix {
Matrix::new(self.sample_n(nrows * ncols), nrows as i32, ncols as i32)
}
fn update(&mut self, params: &[f64]);
}
pub trait DistributionND: Distribution<Output = Vector> {
fn get_dim(&self) -> usize;
fn sample_n(&self, n: usize) -> Matrix {
let mut data = Vector::with_capacity(n * self.get_dim());
for _ in 0..n {
data.extend(self.sample());
}
Matrix::new(data, n as i32, self.get_dim() as i32)
}
}
pub trait Mean {
type MeanType;
fn mean(&self) -> Self::MeanType;
}
pub trait Variance {
type VarianceType;
fn var(&self) -> Self::VarianceType;
}
pub trait Continuous {
type PDFType;
fn pdf(&self, x: Self::PDFType) -> f64;
fn ln_pdf(&self, x: Self::PDFType) -> f64 {
self.pdf(x).ln()
}
}
pub trait Discrete: Distribution1D {
fn pmf(&self, x: i64) -> f64;
}
pub use self::bernoulli::Bernoulli;
pub use self::beta::Beta;
pub use self::binomial::Binomial;
pub use self::chi_squared::ChiSquared;
pub use self::discreteuniform::DiscreteUniform;
pub use self::exponential::Exponential;
pub use self::gamma::Gamma;
pub use self::gumbel::Gumbel;
pub use self::multivariatenormal::*;
pub use self::normal::Normal;
pub use self::pareto::Pareto;
pub use self::poisson::Poisson;
pub use self::t::*;
pub use self::uniform::Uniform;