Struct rv::dist::Categorical[][src]

pub struct Categorical { /* fields omitted */ }

Categorical distribution over unordered values in [0, k).

Implementations

impl Categorical[src]

pub fn new(weights: &[f64]) -> Result<Self, CategoricalError>[src]

Construct a new Categorical distribution from weights

Arguments

  • weights: A vector describing the proportional likelihood of each outcome. The weights must all be positive, but do not need to sum to 1 because they will be normalized in the constructor.

Examples

let weights: Vec<f64> = vec![4.0, 2.0, 3.0, 1.0];
let cat = Categorical::new(&weights).unwrap();

assert!(cat.supports(&0_u8));
assert!(cat.supports(&3_u8));
assert!(!cat.supports(&4_u8));

assert::close(cat.pmf(&0_u8), 0.4, 1E-12);

pub fn from_ln_weights(ln_weights: Vec<f64>) -> Result<Self, CategoricalError>[src]

Build a Categorical distribution from normalized log weights

Arguments

  • ln_weights: A vector describing the proportional likelihood of each outcome in log space. sum(exp(ln_weights)) must be equal to 1.

Example

let ln_weights: Vec<f64> = vec![
    -2.3025850929940455,
    -1.6094379124341003,
    -1.2039728043259361,
    -0.916290731874155
];

let cat = Categorical::from_ln_weights(ln_weights).unwrap();

assert::close(cat.pmf(&0_u8), 0.1, 1E-12);
assert::close(cat.pmf(&1_u8), 0.2, 1E-12);
assert::close(cat.pmf(&2_u8), 0.3, 1E-12);
assert::close(cat.pmf(&3_u8), 0.4, 1E-12);

pub fn new_unchecked(ln_weights: Vec<f64>) -> Self[src]

Creates a new Categorical without checking whether the ln weights are valid.

pub fn uniform(k: usize) -> Self[src]

Creates a Categorical distribution over [0, k) with uniform weights

pub fn weights(&self) -> Vec<f64>[src]

Return the weights (exp(ln_weights))

pub fn k(&self) -> usize[src]

Get the number of possible outcomes

Example

let cat = Categorical::uniform(4);
assert_eq!(cat.k(), 4);

pub fn ln_weights(&self) -> &Vec<f64>[src]

Get a reference to the weights

Trait Implementations

impl<X: CategoricalDatum> Cdf<X> for Categorical[src]

impl Clone for Categorical[src]

impl<X: CategoricalDatum> ConjugatePrior<X, Categorical> for SymmetricDirichlet[src]

type Posterior = Dirichlet

Type of the posterior distribution

type LnMCache = f64

Type of the ln_m cache

type LnPpCache = (Vec<f64>, f64)

Type of the ln_pp cache

impl<X: CategoricalDatum> ConjugatePrior<X, Categorical> for Dirichlet[src]

type Posterior = Self

Type of the posterior distribution

type LnMCache = (f64, f64)

Type of the ln_m cache

type LnPpCache = (Vec<f64>, f64)

Type of the ln_pp cache

impl Debug for Categorical[src]

impl<X: CategoricalDatum> DiscreteDistr<X> for Categorical[src]

impl Display for Categorical[src]

impl Entropy for Categorical[src]

impl<X: CategoricalDatum> HasSuffStat<X> for Categorical[src]

impl KlDivergence for Categorical[src]

impl<X: CategoricalDatum> Mode<X> for Categorical[src]

impl PartialEq<Categorical> for Categorical[src]

impl PartialOrd<Categorical> for Categorical[src]

impl Rv<Categorical> for SymmetricDirichlet[src]

impl Rv<Categorical> for Dirichlet[src]

impl<X: CategoricalDatum> Rv<X> for Categorical[src]

impl StructuralPartialEq for Categorical[src]

impl<X: CategoricalDatum> Support<X> for Categorical[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<SS, SP> SupersetOf<SS> for SP where
    SS: SubsetOf<SP>, 

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,