use rv::data::CategoricalDatum;
use rv::prelude::*;
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)]
enum Color {
Red = 0,
Blue = 1,
Green = 2,
}
impl CategoricalDatum for Color {
fn into_usize(&self) -> usize {
*self as usize
}
fn from_usize(n: usize) -> Self {
match n {
0 => Color::Red,
1 => Color::Blue,
2 => Color::Green,
_ => panic!("Cannot convert {} to Color", n),
}
}
}
fn main() {
let mut rng = rand::thread_rng();
let ctgrl = Categorical::new(&vec![0.25, 0.25, 0.5]).unwrap();
let p = ctgrl.pmf(&Color::Red);
println!("p({:?}) = {}", Color::Red, p);
let xs: Vec<Color> = ctgrl.sample(10, &mut rng);
println!("xs: {:?}", xs);
}