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
use super::DirichletProcessError;
use crate::{Beta, BetaParams, DistributionError};
use crate::{DependentJoint, Distribution, IndependentJoint, RandomVariable};
use rand::prelude::*;
use std::{ops::BitAnd, ops::Mul};

/// # Stick breaking process
/// https://papers.ssrn.com/sol3/papers.cfm?abstract_id=945330
#[derive(Clone, Debug)]
pub struct StickBreakingProcess;

impl Distribution for StickBreakingProcess {
    type Value = Vec<f64>;
    type Condition = StickBreakingProcessParams;

    fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
        let mut accumulated_w = 0.0;
        let mut accumulated_p = 1.0;
        let beta_params = BetaParams::new(1.0, theta.alpha)?;

        for &wi in x.iter() {
            let vi = wi / (1.0 - accumulated_w);

            accumulated_p *= Beta.p_kernel(&vi, &beta_params)?;
            accumulated_w += wi;
        }

        Ok(accumulated_p)
    }

    // fn sample(
    //     &self,
    //     theta: &Self::Condition,
    //     rng: &mut dyn RngCore,
    // ) -> Result<Self::Value, DistributionError> {
    //     rng.gen_range(0..1);
    //     todo!("{:?}", theta);
    // }
}

#[derive(Clone, Debug)]
pub struct StickBreakingProcessParams {
    alpha: f64,
}

impl StickBreakingProcessParams {
    pub fn new(alpha: f64) -> Result<Self, DistributionError> {
        if alpha <= 0.0 {
            return Err(DistributionError::InvalidParameters(
                DirichletProcessError::AlphaMustBePositive.into(),
            ));
        }
        Ok(Self { alpha })
    }
}

impl RandomVariable for StickBreakingProcessParams {
    type RestoreInfo = ();

    fn transform_vec(&self) -> (Vec<f64>, Self::RestoreInfo) {
        todo!()
    }

    fn len(&self) -> usize {
        todo!()
    }

    fn restore(v: &[f64], info: &Self::RestoreInfo) -> Result<Self, DistributionError> {
        todo!()
    }
}

impl<Rhs, TRhs> Mul<Rhs> for StickBreakingProcess
where
    Rhs: Distribution<Value = TRhs, Condition = StickBreakingProcessParams>,
    TRhs: RandomVariable,
{
    type Output = IndependentJoint<Self, Rhs, Vec<f64>, TRhs, StickBreakingProcessParams>;

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

impl<Rhs, URhs> BitAnd<Rhs> for StickBreakingProcess
where
    Rhs: Distribution<Value = StickBreakingProcessParams, Condition = URhs>,
    URhs: RandomVariable,
{
    type Output = DependentJoint<Self, Rhs, Vec<f64>, StickBreakingProcessParams, URhs>;

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