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
use crate::{Categorical, CategoricalParams, DistributionError, SampleableDistribution};
use crate::{DependentJoint, Distribution, IndependentJoint, RandomVariable};
use opensrdk_linear_algebra::*;
use rand::prelude::*;
use std::hash::Hash;
use std::{
    fmt::Debug,
    ops::{BitAnd, Mul},
};

#[derive(Clone, Debug)]
pub struct ContinuousSamplesDistribution<T>
where
    T: RandomVariable,
{
    samples: Vec<T>,
}

#[derive(thiserror::Error, Debug)]
pub enum ContinuousSamplesError {
    #[error("Samples are empty")]
    SamplesAreEmpty,
    #[error("TransformVec info mismatch")]
    TransformVecInfoMismatch,
}

impl<T> ContinuousSamplesDistribution<T>
where
    T: RandomVariable,
{
    pub fn new(samples: Vec<T>) -> Self {
        Self { samples }
    }

    pub fn samples(&self) -> &Vec<T> {
        &self.samples
    }

    pub fn samples_mut(&mut self) -> &mut Vec<T> {
        &mut self.samples
    }

    pub fn sum(&self) -> Result<T, DistributionError> {
        let n = self.samples.len();
        if n == 0 {
            return Err(DistributionError::Others(
                ContinuousSamplesError::SamplesAreEmpty.into(),
            ));
        }
        let (sum, info) = self.samples[0].clone().transform_vec();
        let mut sum = sum.col_mat();
        for i in 1..n {
            let (v, info_i) = self.samples[i].clone().transform_vec();
            if info != info_i {
                return Err(DistributionError::Others(
                    ContinuousSamplesError::TransformVecInfoMismatch.into(),
                ));
            }
            sum = sum + v.col_mat();
        }

        T::restore(sum.elems(), &info)
    }

    pub fn mean(&mut self) -> Result<T, DistributionError> {
        let (sum, info) = self.sum().unwrap().transform_vec();
        let elems = sum
            .iter()
            .map(|elem| elem / self.samples.len() as f64)
            .collect::<Vec<f64>>();
        T::restore(&elems, &info)
    }
}

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

    fn p_kernel(&self, x: &Self::Value, _: &Self::Condition) -> Result<f64, DistributionError> {
        let eq_num = &self
            .samples
            .iter()
            .map(|sample| -> f64 {
                if sample == x {
                    1.0
                } else {
                    0.0
                }
            })
            .sum::<f64>();
        Ok(eq_num / self.samples.len() as f64)
    }
}

impl<T, Rhs, TRhs> Mul<Rhs> for ContinuousSamplesDistribution<T>
where
    T: RandomVariable + Eq + Hash,
    Rhs: Distribution<Value = TRhs, Condition = ()>,
    TRhs: RandomVariable,
{
    type Output = IndependentJoint<Self, Rhs, T, TRhs, ()>;

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

impl<T, Rhs, URhs> BitAnd<Rhs> for ContinuousSamplesDistribution<T>
where
    T: RandomVariable + Eq + Hash,
    Rhs: Distribution<Value = (), Condition = URhs>,
    URhs: RandomVariable,
{
    type Output = DependentJoint<Self, Rhs, T, (), URhs>;

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

impl<T> SampleableDistribution for ContinuousSamplesDistribution<T>
where
    T: RandomVariable + PartialEq,
{
    fn sample(
        &self,
        _theta: &Self::Condition,
        rng: &mut dyn RngCore,
    ) -> Result<Self::Value, DistributionError> {
        let pi = vec![1.0 / self.samples.len() as f64; self.samples.len()];
        let params = CategoricalParams::new(pi)?;
        let sampled = Categorical.sample(&params, rng)?;
        Ok(self.samples[sampled].clone())
    }
}