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
use crate::{
ConditionDifferentiableDistribution, DependentJoint, Distribution, IndependentJoint,
NormalParams, RandomVariable, SampleableDistribution, ValueDifferentiableDistribution,
};
use crate::{DistributionError, NormalError};
use rand::prelude::*;
use rand_distr::Normal as RandNormal;
use std::{ops::BitAnd, ops::Mul};
#[derive(Clone, Debug)]
pub struct Normal;
impl Distribution for Normal {
type Value = f64;
type Condition = NormalParams;
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let mu = theta.mu();
let sigma = theta.sigma();
Ok((-(x - mu).powi(2) / (2.0 * sigma.powi(2))).exp())
}
}
impl<Rhs, TRhs> Mul<Rhs> for Normal
where
Rhs: Distribution<Value = TRhs, Condition = NormalParams>,
TRhs: RandomVariable,
{
type Output = IndependentJoint<Self, Rhs, f64, TRhs, NormalParams>;
fn mul(self, rhs: Rhs) -> Self::Output {
IndependentJoint::new(self, rhs)
}
}
impl<Rhs, URhs> BitAnd<Rhs> for Normal
where
Rhs: Distribution<Value = NormalParams, Condition = URhs>,
URhs: RandomVariable,
{
type Output = DependentJoint<Self, Rhs, f64, NormalParams, URhs>;
fn bitand(self, rhs: Rhs) -> Self::Output {
DependentJoint::new(self, rhs)
}
}
impl SampleableDistribution for Normal {
fn sample(
&self,
theta: &Self::Condition,
rng: &mut dyn RngCore,
) -> Result<Self::Value, DistributionError> {
let mu = theta.mu();
let sigma = theta.sigma();
let normal = match RandNormal::new(mu, sigma) {
Ok(n) => n,
Err(_) => {
return Err(DistributionError::InvalidParameters(
NormalError::SigmaMustBePositive.into(),
))
}
};
Ok(rng.sample(normal))
}
}
impl ValueDifferentiableDistribution for Normal {
fn ln_diff_value(
&self,
x: &Self::Value,
theta: &Self::Condition,
) -> Result<Vec<f64>, DistributionError> {
let sigma = theta.sigma();
let mu = theta.mu();
let f_x = -(x - mu) / sigma.powi(2);
Ok(vec![f_x])
}
}
impl ConditionDifferentiableDistribution for Normal {
fn ln_diff_condition(
&self,
x: &Self::Value,
theta: &Self::Condition,
) -> Result<Vec<f64>, DistributionError> {
let sigma = theta.sigma();
let mu = theta.mu();
let f_mu = (x - mu) / sigma.powi(2);
let f_sigma = (x - mu).powi(2) / sigma.powi(3);
Ok(vec![f_mu, f_sigma])
}
}
#[cfg(test)]
mod tests {
use crate::{
ConditionDifferentiableDistribution, Distribution, Normal, NormalParams,
SampleableDistribution, ValueDifferentiableDistribution,
};
use rand::prelude::*;
#[test]
fn it_works() {
let n = Normal;
let mut rng = StdRng::from_seed([1; 32]);
let mu = 2.0;
let sigma = 3.0;
let x = n
.sample(&NormalParams::new(mu, sigma).unwrap(), &mut rng)
.unwrap();
println!("{}", x);
}
#[test]
fn it_works2() {
let n = Normal;
let mu = 2.0;
let sigma = 3.0;
let x = 0.5;
let f = n.ln_diff_value(&x, &NormalParams::new(mu, sigma).unwrap());
println!("{:#?}", f);
}
#[test]
fn it_works_3() {
let n = Normal;
let mu = 2.0;
let sigma = 3.0;
let x = 0.5;
let f = n.ln_diff_condition(&x, &NormalParams::new(mu, sigma).unwrap());
println!("{:#?}", f);
}
}