Struct statrs::distribution::Dirichlet
[−]
[src]
pub struct Dirichlet { /* fields omitted */ }
Implements the Dirichlet distribution
Examples
use statrs::distribution::{Dirichlet, Continuous}; use statrs::statistics::Mean; let n = Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap(); assert_eq!(n.mean(), [1.0 / 6.0, 1.0 / 3.0, 0.5]); assert_eq!(n.pdf(&[0.33333, 0.33333, 0.33333]), 2.222155556222205);
Methods
impl Dirichlet
[src]
fn new(alpha: &[f64]) -> Result<Dirichlet>
Constructs a new dirichlet distribution with the given concenctration parameters (alpha)
Errors
Returns an error if any element x
in alpha exist
such that x < = 0.0
or x
is NaN
, or if the length of alpha is
less than 2
Examples
use statrs::distribution::Dirichlet; let alpha_ok = [1.0, 2.0, 3.0]; let mut result = Dirichlet::new(&alpha_ok); assert!(result.is_ok()); let alpha_err = [0.0]; result = Dirichlet::new(&alpha_err); assert!(result.is_err());
fn new_with_param(alpha: f64, n: usize) -> Result<Dirichlet>
Constructs a new dirichlet distribution with the given
concenctration parameter (alpha) repeated n
times
Errors
Returns an error if alpha < = 0.0
or alpha
is NaN
,
or if n < 2
Examples
use statrs::distribution::Dirichlet; let mut result = Dirichlet::new_with_param(1.0, 3); assert!(result.is_ok()); result = Dirichlet::new_with_param(0.0, 1); assert!(result.is_err());
fn alpha(&self) -> &[f64]
Returns the concentration parameters of the dirichlet distribution as a slice
Examples
use statrs::distribution::Dirichlet; let n = Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap(); assert_eq!(n.alpha(), [1.0, 2.0, 3.0]);
Trait Implementations
impl Debug for Dirichlet
[src]
impl Clone for Dirichlet
[src]
fn clone(&self) -> Dirichlet
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 Dirichlet
[src]
fn eq(&self, __arg_0: &Dirichlet) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Dirichlet) -> bool
This method tests for !=
.
impl Sample<Vec<f64>> for Dirichlet
[src]
fn sample<R: Rng>(&mut self, r: &mut R) -> Vec<f64>
Generate random samples from a dirichlet
distribution using r
as the source of randomness.
Refer here for implementation details
impl IndependentSample<Vec<f64>> for Dirichlet
[src]
fn ind_sample<R: Rng>(&self, r: &mut R) -> Vec<f64>
Generate random independent samples from a dirichlet
distribution using r
as the source of randomness.
Refer here for implementation details
impl Distribution<Vec<f64>> for Dirichlet
[src]
fn sample<R: Rng>(&self, r: &mut R) -> Vec<f64>
Generate random samples from the dirichlet distribution
using r
as the source of randomness
Examples
use rand::StdRng; use statrs::distribution::{Dirichlet, Distribution}; let mut r = rand::StdRng::new().unwrap(); let n = Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap(); print!("{:?}", n.sample::<StdRng>(&mut r));
impl Mean<Vec<f64>> for Dirichlet
[src]
fn mean(&self) -> Vec<f64>
Returns the means of the dirichlet distribution
Formula
α_i / α_0
for the i
th element where α_i
is the i
th concentration parameter
and α_0
is the sum of all concentration parameters
impl Variance<Vec<f64>> for Dirichlet
[src]
fn variance(&self) -> Vec<f64>
Returns the variances of the dirichlet distribution
Formula
(α_i * (α_0 - α_i)) / (α_0^2 * (α_0 + 1))
for the i
th element where α_i
is the i
th concentration parameter
and α_0
is the sum of all concentration parameters
fn std_dev(&self) -> Vec<f64>
Returns the standard deviation of the dirichlet distribution
Formula
sqrt((α_i * (α_0 - α_i)) / (α_0^2 * (α_0 + 1)))
for the i
th element where α_i
is the i
th concentration parameter
and α_0
is the sum of all concentration parameters
impl Entropy<f64> for Dirichlet
[src]
fn entropy(&self) -> f64
Returns the entropy of the dirichlet distribution
Formula
ln(B(α)) - (K - α_0)ψ(α_0) - Σ((α_i - 1)ψ(α_i))
where
B(α) = Π(Γ(α_i)) / Γ(Σ(α_i))
α_0
is the sum of all concentration parameters,
K
is the number of concentration parameters, ψ
is the digamma function, α_i
is the i
th concentration parameter, and Σ
is the sum from 1
to K
impl<'a> Continuous<&'a [f64], f64> for Dirichlet
[src]
fn pdf(&self, x: &[f64]) -> f64
Calculates the probabiliy density function for the dirichlet distribution
with given x
's corresponding to the concentration parameters for this
distribution
Panics
If any element in x
is not in (0, 1)
, the elements in x
do not sum to
1
with a tolerance of 1e-4
, or if x
is not the same length as the vector of
concentration parameters for this distribution
Formula
(1 / B(α)) * Π(x_i^(α_i - 1))
where
B(α) = Π(Γ(α_i)) / Γ(Σ(α_i))
α
is the vector of concentration parameters, α_i
is the i
th
concentration parameter, x_i
is the i
th argument corresponding to
the i
th concentration parameter, Γ
is the gamma function,
Π
is the product from 1
to K
, Σ
is the sum from 1
to K
,
and K
is the number of concentration parameters
fn ln_pdf(&self, x: &[f64]) -> f64
Calculates the log probabiliy density function for the dirichlet distribution
with given x
's corresponding to the concentration parameters for this
distribution
Panics
If any element in x
is not in (0, 1)
, the elements in x
do not sum to
1
with a tolerance of 1e-4
, or if x
is not the same length as the vector of
concentration parameters for this distribution
Formula
ln((1 / B(α)) * Π(x_i^(α_i - 1)))
where
B(α) = Π(Γ(α_i)) / Γ(Σ(α_i))
α
is the vector of concentration parameters, α_i
is the i
th
concentration parameter, x_i
is the i
th argument corresponding to
the i
th concentration parameter, Γ
is the gamma function,
Π
is the product from 1
to K
, Σ
is the sum from 1
to K
,
and K
is the number of concentration parameters