pub struct MultivariateNormal { /* private fields */ }
Expand description

Implements the Multivariate Normal distribution using the “nalgebra” crate for matrix operations

Examples

use statrs::distribution::{MultivariateNormal, Continuous};
use nalgebra::{DVector, DMatrix};
use statrs::statistics::{MeanN, VarianceN};

let mvn = MultivariateNormal::new(vec![0., 0.], vec![1., 0., 0., 1.]).unwrap();
assert_eq!(mvn.mean().unwrap(), DVector::from_vec(vec![0., 0.]));
assert_eq!(mvn.variance().unwrap(), DMatrix::from_vec(2, 2, vec![1., 0., 0., 1.]));
assert_eq!(mvn.pdf(&DVector::from_vec(vec![1.,  1.])), 0.05854983152431917);

Implementations§

source§

impl MultivariateNormal

source

pub fn new(mean: Vec<f64>, cov: Vec<f64>) -> Result<Self>

Constructs a new multivariate normal distribution with a mean of mean and covariance matrix cov

Errors

Returns an error if the given covariance matrix is not symmetric or positive-definite

source

pub fn entropy(&self) -> Option<f64>

Returns the entropy of the multivariate normal distribution

Formula
(1 / 2) * ln(det(2 * π * e * Σ))

where Σ is the covariance matrix and det is the determinant

Trait Implementations§

source§

impl Clone for MultivariateNormal

source§

fn clone(&self) -> MultivariateNormal

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Continuous<&'a Matrix<f64, Dynamic, Const<1>, VecStorage<f64, Dynamic, Const<1>>>, f64> for MultivariateNormal

source§

fn pdf(&self, x: &'a DVector<f64>) -> f64

Calculates the probability density function for the multivariate normal distribution at x

Formula
(2 * π) ^ (-k / 2) * det(Σ) ^ (1 / 2) * e ^ ( -(1 / 2) * transpose(x - μ) * inv(Σ) * (x - μ))

where μ is the mean, inv(Σ) is the precision matrix, det(Σ) is the determinant of the covariance matrix, and k is the dimension of the distribution

source§

fn ln_pdf(&self, x: &'a DVector<f64>) -> f64

Calculates the log probability density function for the multivariate normal distribution at x. Equivalent to pdf(x).ln().

source§

impl Debug for MultivariateNormal

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Distribution<Matrix<f64, Dynamic, Const<1>, VecStorage<f64, Dynamic, Const<1>>>> for MultivariateNormal

source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> DVector<f64>

Samples from the multivariate normal distribution

Formula

L * Z + μ

where L is the Cholesky decomposition of the covariance matrix, Z is a vector of normally distributed random variables, and μ is the mean vector

source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
source§

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more
source§

impl Max<Matrix<f64, Dynamic, Const<1>, VecStorage<f64, Dynamic, Const<1>>>> for MultivariateNormal

source§

fn max(&self) -> DVector<f64>

Returns the maximum value in the domain of the multivariate normal distribution represented by a real vector

source§

impl MeanN<Matrix<f64, Dynamic, Const<1>, VecStorage<f64, Dynamic, Const<1>>>> for MultivariateNormal

source§

fn mean(&self) -> Option<DVector<f64>>

Returns the mean of the normal distribution

Remarks

This is the same mean used to construct the distribution

source§

impl Min<Matrix<f64, Dynamic, Const<1>, VecStorage<f64, Dynamic, Const<1>>>> for MultivariateNormal

source§

fn min(&self) -> DVector<f64>

Returns the minimum value in the domain of the multivariate normal distribution represented by a real vector

source§

impl Mode<Matrix<f64, Dynamic, Const<1>, VecStorage<f64, Dynamic, Const<1>>>> for MultivariateNormal

source§

fn mode(&self) -> DVector<f64>

Returns the mode of the multivariate normal distribution

Formula
μ

where μ is the mean

source§

impl PartialEq<MultivariateNormal> for MultivariateNormal

source§

fn eq(&self, other: &MultivariateNormal) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl VarianceN<Matrix<f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>> for MultivariateNormal

source§

fn variance(&self) -> Option<DMatrix<f64>>

Returns the covariance matrix of the multivariate normal distribution

source§

impl StructuralPartialEq for MultivariateNormal

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

source§

fn vzip(self) -> V