Struct statrs::distribution::Categorical
[−]
[src]
pub struct Categorical { /* fields omitted */ }
Implements the Categorical distribution, also known as the generalized Bernoulli or discrete distribution
Examples
use statrs::distribution::{Categorical, Discrete}; use statrs::statistics::Mean; use statrs::prec; let n = Categorical::new(&[0.0, 1.0, 2.0]).unwrap(); assert!(prec::almost_eq(n.mean(), 5.0 / 3.0, 1e-15)); assert_eq!(n.pmf(1), 1.0 / 3.0);
Methods
impl Categorical
[src]
fn new(prob_mass: &[f64]) -> Result<Categorical>
Constructs a new categorical distribution
with the probabilities masses defined by prob_mass
Errors
Returns an error if prob_mass
is empty, the sum of
the elements in prob_mass
is 0, or any element is less than
0 or is f64::NAN
Note
The elements in prob_mass
do not need to be normalized
Examples
use statrs::distribution::Categorical; let mut result = Categorical::new(&[0.0, 1.0, 2.0]); assert!(result.is_ok()); result = Categorical::new(&[0.0, -1.0, 2.0]); assert!(result.is_err());
Trait Implementations
impl Debug for Categorical
[src]
impl Clone for Categorical
[src]
fn clone(&self) -> Categorical
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl PartialEq for Categorical
[src]
fn eq(&self, __arg_0: &Categorical) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Categorical) -> bool
This method tests for !=
.
impl Sample<f64> for Categorical
[src]
fn sample<R: Rng>(&mut self, r: &mut R) -> f64
Generate a random sample from a categorical
distribution using r
as the source of randomness.
Refer here for implementation details
impl IndependentSample<f64> for Categorical
[src]
fn ind_sample<R: Rng>(&self, r: &mut R) -> f64
Generate a random independent sample from a categorical
distribution using r
as the source of randomness.
Refer here for implementation details
impl Distribution<f64> for Categorical
[src]
fn sample<R: Rng>(&self, r: &mut R) -> f64
Generate a random sample from the categorical distribution
using r
as the source of randomness
Examples
use rand::StdRng; use statrs::distribution::{Categorical, Distribution}; let mut r = rand::StdRng::new().unwrap(); let n = Categorical::new(&[1.0, 2.0, 3.0]).unwrap(); print!("{}", n.sample::<StdRng>(&mut r));
impl Univariate<u64, f64> for Categorical
[src]
impl InverseCDF<f64> for Categorical
[src]
fn inverse_cdf(&self, x: f64) -> f64
impl Min<u64> for Categorical
[src]
fn min(&self) -> u64
Returns the minimum value in the domain of the categorical distribution representable by a 64-bit integer
Formula
0
impl Max<u64> for Categorical
[src]
fn max(&self) -> u64
Returns the maximum value in the domain of the categorical distribution representable by a 64-bit integer
Formula
n
impl Mean<f64> for Categorical
[src]
fn mean(&self) -> f64
Returns the mean of the categorical distribution
Formula
sum(j * p_j) for j in 0..k-1
where p_j
is the j
th probability mass and k
is the number
of categories
impl Variance<f64> for Categorical
[src]
fn variance(&self) -> f64
Returns the variance of the categorical distribution
Formula
sum(p_j * (j - μ)^2) for j in 0..k-1
where p_j
is the j
th probability mass, k
is the number
of categories, and μ
is the mean
fn std_dev(&self) -> f64
Returns the standard deviation of the categorical distribution
Formula
sqrt(sum(p_j * (j - μ)^2)) for j in 0..k-1
where p_j
is the j
th probability mass, k
is the number
of categories, and μ
is the mean