pub struct Crp { /* private fields */ }
Expand description
Chinese Restaurant Process, a distribution over partitions.
§Example
use::rv::prelude::*;
let mut rng = rand::thread_rng();
let crp = Crp::new(1.0, 10).expect("Invalid parameters");
let partition = crp.draw(&mut rng);
assert_eq!(partition.len(), 10);
Implementations§
source§impl Crp
impl Crp
sourcepub fn new(alpha: f64, n: usize) -> Result<Self, CrpError>
pub fn new(alpha: f64, n: usize) -> Result<Self, CrpError>
Create an empty Crp
with parameter alpha
§Arguments
- alpha: Discount parameter in (0, Infinity)
- n: the number of items in the partition
sourcepub fn new_unchecked(alpha: f64, n: usize) -> Self
pub fn new_unchecked(alpha: f64, n: usize) -> Self
Create a new Crp without checking whether the parametes are valid.
sourcepub fn alpha(&self) -> f64
pub fn alpha(&self) -> f64
Get the discount parameter, alpha
.
§Example
let crp = Crp::new(1.0, 12).unwrap();
assert_eq!(crp.alpha(), 1.0);
sourcepub fn set_alpha(&mut self, alpha: f64) -> Result<(), CrpError>
pub fn set_alpha(&mut self, alpha: f64) -> Result<(), CrpError>
Set the value of alpha
§Example
let mut crp = Crp::new(1.1, 20).unwrap();
assert_eq!(crp.alpha(), 1.1);
crp.set_alpha(2.3).unwrap();
assert_eq!(crp.alpha(), 2.3);
Will error for invalid parameters
assert!(crp.set_alpha(0.5).is_ok());
assert!(crp.set_alpha(0.0).is_err());
assert!(crp.set_alpha(-1.0).is_err());
assert!(crp.set_alpha(std::f64::INFINITY).is_err());
assert!(crp.set_alpha(std::f64::NEG_INFINITY).is_err());
assert!(crp.set_alpha(std::f64::NAN).is_err());
sourcepub fn set_alpha_unchecked(&mut self, alpha: f64)
pub fn set_alpha_unchecked(&mut self, alpha: f64)
Set the value of alpha without input validation
sourcepub fn n(&self) -> usize
pub fn n(&self) -> usize
Get the number of entries in the partition, n
.
§Example
let crp = Crp::new(1.0, 12).unwrap();
assert_eq!(crp.n(), 12);
sourcepub fn set_n(&mut self, n: usize) -> Result<(), CrpError>
pub fn set_n(&mut self, n: usize) -> Result<(), CrpError>
Set the value of n
§Example
let mut crp = Crp::new(1.1, 20).unwrap();
assert_eq!(crp.n(), 20);
crp.set_n(11).unwrap();
assert_eq!(crp.n(), 11);
Will error for invalid parameters
assert!(crp.set_n(5).is_ok());
assert!(crp.set_n(1).is_ok());
assert!(crp.set_n(0).is_err());
sourcepub fn set_n_unchecked(&mut self, n: usize)
pub fn set_n_unchecked(&mut self, n: usize)
Set the value of alpha without input validation
Trait Implementations§
source§impl<'de> Deserialize<'de> for Crp
impl<'de> Deserialize<'de> for Crp
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for Crp
Auto Trait Implementations§
impl Freeze for Crp
impl RefUnwindSafe for Crp
impl Send for Crp
impl Sync for Crp
impl Unpin for Crp
impl UnwindSafe for Crp
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moresource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self
is actually part of its subset T
(and can be converted to it).source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.