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
pub mod measure;
pub mod pitman_yor_process;
pub mod stick_breaking_process;
pub use measure::*;
pub use pitman_yor_process::*;
pub use stick_breaking_process::*;
use super::BaselineMeasure;
use crate::{Distribution, DistributionError, RandomVariable};
use std::{fmt::Debug, marker::PhantomData};
#[derive(Clone, Debug)]
pub struct DirichletProcess<G0, T>
where
G0: Distribution<Value = T, Condition = ()>,
T: RandomVariable,
{
phantom: PhantomData<(G0, T)>,
}
#[derive(thiserror::Error, Debug)]
pub enum DirichletProcessError {
#[error("'α' must be positibe")]
AlphaMustBePositive,
#[error("Unknown error")]
Unknown,
}
impl<G0, T> DirichletProcess<G0, T>
where
G0: Distribution<Value = T, Condition = ()>,
T: RandomVariable,
{
pub fn new() -> Self {
Self {
phantom: PhantomData,
}
}
}
#[derive(Clone, Debug)]
pub struct DirichletProcessParams<G0, T>
where
G0: Distribution<Value = T, Condition = ()>,
T: RandomVariable,
{
alpha: f64,
g0: BaselineMeasure<G0, T>,
}
impl<G0, T> DirichletProcessParams<G0, T>
where
G0: Distribution<Value = T, Condition = ()>,
T: RandomVariable,
{
pub fn new(alpha: f64, g0: BaselineMeasure<G0, T>) -> Result<Self, DistributionError> {
if alpha <= 0.0 {
return Err(DistributionError::InvalidParameters(
DirichletProcessError::AlphaMustBePositive.into(),
));
}
Ok(Self { alpha, g0 })
}
pub fn alpha(&self) -> f64 {
self.alpha
}
pub fn g0(&self) -> &BaselineMeasure<G0, T> {
&self.g0
}
}