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
pub mod params;

pub use params::*;

use crate::*;
use rand::Rng;
use std::ops::{BitAnd, Mul};

#[derive(Clone, Debug)]
pub struct Bernoulli;

#[derive(thiserror::Error, Debug)]
pub enum BernoulliError {
    #[error("'p' must be probability.")]
    PMustBeProbability,
}

impl Distribution for Bernoulli {
    type Value = bool;
    type Condition = BernoulliParams;

    fn p_kernel(
        &self,
        _x: &Self::Value,
        theta: &Self::Condition,
    ) -> Result<f64, DistributionError> {
        Ok(theta.p())
    }
}

impl DiscreteDistribution for Bernoulli {}

impl<Rhs, TRhs> Mul<Rhs> for Bernoulli
where
    Rhs: Distribution<Value = TRhs, Condition = BernoulliParams>,
    TRhs: RandomVariable,
{
    type Output = IndependentJoint<Self, Rhs, bool, TRhs, BernoulliParams>;

    fn mul(self, rhs: Rhs) -> Self::Output {
        IndependentJoint::new(self, rhs)
    }
}

impl<Rhs, URhs> BitAnd<Rhs> for Bernoulli
where
    Rhs: Distribution<Value = BernoulliParams, Condition = URhs>,
    URhs: RandomVariable,
{
    type Output = DependentJoint<Self, Rhs, bool, BernoulliParams, URhs>;

    fn bitand(self, rhs: Rhs) -> Self::Output {
        DependentJoint::new(self, rhs)
    }
}

impl ConditionDifferentiableDistribution for Bernoulli {
    fn ln_diff_condition(
        &self,
        x: &Self::Value,
        theta: &Self::Condition,
    ) -> Result<Vec<f64>, DistributionError> {
        let p = theta.p();
        let x_f64 = if *x { 1.0 } else { 0.0 };
        let f_p = x_f64 / p - (1.0 - x_f64) / (1.0 - p);
        Ok(vec![f_p])
    }
}

impl SampleableDistribution for Bernoulli {
    fn sample(
        &self,
        theta: &Self::Condition,
        rng: &mut dyn rand::RngCore,
    ) -> Result<Self::Value, DistributionError> {
        let u = rng.gen_range(0.0..=1.0);
        Ok(u <= theta.p())
    }
}