pub struct Crp { /* private fields */ }Expand description
Chinese Restaurant Process, a distribution over partitions.
§Example
use::rv::prelude::*;
let mut rng = rand::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 parameters are valid.
use rv::dist::Crp;
let crp = Crp::new_unchecked(3.0, 10);
assert_eq!(crp.alpha(), 3.0);
assert_eq!(crp.n(), 10);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(f64::INFINITY).is_err());
assert!(crp.set_alpha(f64::NEG_INFINITY).is_err());
assert!(crp.set_alpha(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
Source§impl HasDensity<Partition> for Crp
impl HasDensity<Partition> for Crp
Source§impl Parameterized for Crp
impl Parameterized for Crp
type Parameters = CrpParameters
fn emit_params(&self) -> Self::Parameters
fn from_params(params: Self::Parameters) -> Self
fn map_params(&self, f: impl Fn(Self::Parameters) -> Self::Parameters) -> Self
Source§impl Sampleable<Partition> for Crp
impl Sampleable<Partition> for Crp
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§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.