pub struct Dirichlet<D>{ /* private fields */ }
nalgebra
only.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§
Source§impl Dirichlet<Dyn>
impl Dirichlet<Dyn>
Sourcepub fn new(alpha: Vec<f64>) -> Result<Self, DirichletError>
pub fn new(alpha: Vec<f64>) -> Result<Self, DirichletError>
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<Self, DirichletError>
pub fn new_with_param(alpha: f64, n: usize) -> Result<Self, DirichletError>
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());
Source§impl<D> Dirichlet<D>
impl<D> Dirichlet<D>
Sourcepub fn new_from_nalgebra(alpha: OVector<f64, D>) -> Result<Self, DirichletError>
pub fn new_from_nalgebra(alpha: OVector<f64, D>) -> Result<Self, DirichletError>
Constructs a new distribution with the given vector for alpha
Does not clone the vector it takes ownership of
§Error
Returns an error if vector has length less than 2 or if any element of alpha is NOT finite positive
Sourcepub fn alpha(&self) -> &OVector<f64, D>
pub fn alpha(&self) -> &OVector<f64, D>
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§
Source§impl<D> Continuous<&Matrix<f64, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<f64>>, f64> for Dirichlet<D>
impl<D> Continuous<&Matrix<f64, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<f64>>, f64> for Dirichlet<D>
Source§fn pdf(&self, x: &OVector<f64, D>) -> f64
fn pdf(&self, x: &OVector<f64, D>) -> 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
Source§fn ln_pdf(&self, x: &OVector<f64, D>) -> f64
fn ln_pdf(&self, x: &OVector<f64, D>) -> 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
Source§impl<D> Distribution<Matrix<f64, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<f64>>> for Dirichlet<D>
Available on crate feature rand
only.
impl<D> Distribution<Matrix<f64, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<f64>>> for Dirichlet<D>
rand
only.Source§fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> OVector<f64, D>
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> OVector<f64, D>
T
, using rng
as the source of randomness.Source§fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
T
, using rng
as
the source of randomness. Read moreSource§impl<D> MeanN<Matrix<f64, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<f64>>> for Dirichlet<D>
impl<D> MeanN<Matrix<f64, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<f64>>> for Dirichlet<D>
Source§impl<D> VarianceN<Matrix<f64, D, D, <DefaultAllocator as Allocator<D, D>>::Buffer<f64>>> for Dirichlet<D>
impl<D> VarianceN<Matrix<f64, D, D, <DefaultAllocator as Allocator<D, D>>::Buffer<f64>>> for Dirichlet<D>
impl<D> StructuralPartialEq for Dirichlet<D>
Auto Trait Implementations§
impl<D> !Freeze for Dirichlet<D>
impl<D> !RefUnwindSafe for Dirichlet<D>
impl<D> !Send for Dirichlet<D>
impl<D> !Sync for Dirichlet<D>
impl<D> !Unpin for Dirichlet<D>
impl<D> !UnwindSafe for Dirichlet<D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.