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