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
use crate::{Distribution, RandomVariable, SampleableDistribution};

#[derive(Clone, Debug)]
pub struct Degenerate<T>
where
    T: RandomVariable + PartialEq,
{
    value: T,
}

impl<T> Degenerate<T>
where
    T: RandomVariable + PartialEq,
{
    pub fn new(value: T) -> Self {
        Self { value }
    }
}

impl<T> Distribution for Degenerate<T>
where
    T: RandomVariable + PartialEq,
{
    type Value = T;
    type Condition = ();

    fn p_kernel(
        &self,
        x: &Self::Value,
        _theta: &Self::Condition,
    ) -> Result<f64, crate::DistributionError> {
        if self.value.eq(x) {
            Ok(1.0)
        } else {
            Ok(0.0)
        }
    }
}

impl<T> SampleableDistribution for Degenerate<T>
where
    T: RandomVariable + PartialEq,
{
    fn sample(
        &self,
        _theta: &Self::Condition,
        _rng: &mut dyn rand::RngCore,
    ) -> Result<Self::Value, crate::DistributionError> {
        Ok(self.value.clone())
    }
}