Struct statrs::distribution::Dirichlet
source · [−]pub struct Dirichlet { /* private fields */ }
Expand description
Implements the Dirichlet distribution
Examples
use statrs::distribution::{Dirichlet, Continuous};
use statrs::statistics::Distribution;
use nalgebra::DVector;
use statrs::statistics::MeanN;
let n = Dirichlet::new(vec![1.0, 2.0, 3.0]).unwrap();
assert_eq!(n.mean().unwrap(), DVector::from_vec(vec![1.0 / 6.0, 1.0 / 3.0, 0.5]));
assert_eq!(n.pdf(&DVector::from_vec(vec![0.33333, 0.33333, 0.33333])), 2.222155556222205);
Implementations
sourceimpl Dirichlet
impl Dirichlet
sourcepub fn new(alpha: Vec<f64>) -> Result<Dirichlet>
pub fn new(alpha: Vec<f64>) -> Result<Dirichlet>
Constructs a new dirichlet distribution with the given concentration 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;
use nalgebra::DVector;
let alpha_ok = vec![1.0, 2.0, 3.0];
let mut result = Dirichlet::new(alpha_ok);
assert!(result.is_ok());
let alpha_err = vec![0.0];
result = Dirichlet::new(alpha_err);
assert!(result.is_err());
sourcepub fn new_with_param(alpha: f64, n: usize) -> Result<Dirichlet>
pub fn new_with_param(alpha: f64, n: usize) -> Result<Dirichlet>
Constructs a new dirichlet distribution with the given
concentration 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());
sourcepub fn alpha(&self) -> &DVector<f64>
pub fn alpha(&self) -> &DVector<f64>
Returns the concentration parameters of the dirichlet distribution as a slice
Examples
use statrs::distribution::Dirichlet;
use nalgebra::DVector;
let n = Dirichlet::new(vec![1.0, 2.0, 3.0]).unwrap();
assert_eq!(n.alpha(), &DVector::from_vec(vec![1.0, 2.0, 3.0]));
sourcepub fn entropy(&self) -> Option<f64>
pub fn entropy(&self) -> Option<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
Trait Implementations
sourceimpl<'a> Continuous<&'a Matrix<f64, Dynamic, Const<1_usize>, VecStorage<f64, Dynamic, Const<1_usize>>>, f64> for Dirichlet
impl<'a> Continuous<&'a Matrix<f64, Dynamic, Const<1_usize>, VecStorage<f64, Dynamic, Const<1_usize>>>, f64> for Dirichlet
sourcefn pdf(&self, x: &DVector<f64>) -> f64
fn pdf(&self, x: &DVector<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
sourcefn ln_pdf(&self, x: &DVector<f64>) -> f64
fn ln_pdf(&self, x: &DVector<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
sourceimpl Distribution<Matrix<f64, Dynamic, Const<1_usize>, VecStorage<f64, Dynamic, Const<1_usize>>>> for Dirichlet
impl Distribution<Matrix<f64, Dynamic, Const<1_usize>, VecStorage<f64, Dynamic, Const<1_usize>>>> for Dirichlet
sourcefn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> DVector<f64>
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> DVector<f64>
Generate a random value of T
, using rng
as the source of randomness.
sourcefn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T> where
R: Rng,
fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T> where
R: Rng,
Create an iterator that generates random values of T
, using rng
as
the source of randomness. Read more
sourceimpl MeanN<Matrix<f64, Dynamic, Const<1_usize>, VecStorage<f64, Dynamic, Const<1_usize>>>> for Dirichlet
impl MeanN<Matrix<f64, Dynamic, Const<1_usize>, VecStorage<f64, Dynamic, Const<1_usize>>>> for Dirichlet
sourceimpl VarianceN<Matrix<f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>> for Dirichlet
impl VarianceN<Matrix<f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>> for Dirichlet
impl StructuralPartialEq for Dirichlet
Auto Trait Implementations
impl RefUnwindSafe for Dirichlet
impl Send for Dirichlet
impl Sync for Dirichlet
impl Unpin for Dirichlet
impl UnwindSafe for Dirichlet
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
sourcepub fn to_subset(&self) -> Option<SS>
pub fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct self
from the equivalent element of its
superset. Read more
sourcepub fn is_in_subset(&self) -> bool
pub fn is_in_subset(&self) -> bool
Checks if self
is actually part of its subset T
(and can be converted to it).
sourcepub fn to_subset_unchecked(&self) -> SS
pub fn to_subset_unchecked(&self) -> SS
Use with care! Same as self.to_subset
but without any property checks. Always succeeds.
sourcepub fn from_subset(element: &SS) -> SP
pub fn from_subset(element: &SS) -> SP
The inclusion map: converts self
to the equivalent element of its superset.
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more