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
use crate::{DistributionError, RandomVariable};
#[derive(Clone, Debug)]
pub struct CategoricalParams {
p: Vec<f64>,
}
impl CategoricalParams {
pub fn new(p: Vec<f64>) -> Result<Self, DistributionError> {
Ok(Self { p })
}
pub fn p(&self) -> &Vec<f64> {
&self.p
}
}
impl RandomVariable for CategoricalParams {
type RestoreInfo = usize;
fn transform_vec(&self) -> (Vec<f64>, Self::RestoreInfo) {
(self.p.clone(), self.p.len())
}
fn len(&self) -> usize {
self.p.len()
}
fn restore(v: &[f64], info: &Self::RestoreInfo) -> Result<Self, DistributionError> {
if v.len() != *info {
return Err(DistributionError::InvalidRestoreVector);
}
CategoricalParams::new(v.to_vec())
}
}