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]

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]

Formats the value using the given formatter.

impl Clone for Categorical
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl PartialEq for Categorical
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Sample<f64> for Categorical
[src]

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]

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]

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]

Calculates the cumulative distribution function for the categorical distribution at x

Panics

If x < 0.0 or x > k where k is the number of categories (i.e. the length of the prob_mass slice passed to the constructor)

Formula

sum(p_j) from 0..x

where p_j is the probability mass for the jth category

impl InverseCDF<f64> for Categorical
[src]

Calculates the inverse cumulative distribution function for the categorical distribution at x

Panics

If x <= 0.0 or x >= 1.0

Formula

i

where i is the first index such that x < f(i) and f(x) is defined as p_x + f(x - 1) and f(0) = p_0 where p_x is the xth probability mass

impl Min<u64> for Categorical
[src]

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]

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]

Returns the mean of the categorical distribution

Formula

sum(j * p_j) for j in 0..k-1

where p_j is the jth probability mass and k is the number of categories

impl Variance<f64> for Categorical
[src]

Returns the variance of the categorical distribution

Formula

sum(p_j * (j - μ)^2) for j in 0..k-1

where p_j is the jth probability mass, k is the number of categories, and μ is the mean

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 jth probability mass, k is the number of categories, and μ is the mean

impl Median<f64> for Categorical
[src]

Returns the median of the categorical distribution

Formula

CDF^-1(0.5)

impl Discrete<u64, f64> for Categorical
[src]

Calculates the probability mass function for the categorical distribution at x

Panics

If x >= k where k is the number of categories

Formula

p_x

Calculates the log probability mass function for the categorical distribution at x