[][src]Struct rv::dist::MvGaussian

pub struct MvGaussian { /* fields omitted */ }

Multivariate Gaussian/Normal Distribution, 𝒩(μ, Σ).

Example

Generate a Wishart random 3x3 matrix Σ ~ Wν(S)

use nalgebra::{DMatrix, DVector};
use rv::prelude::*;

let k = 3;   // number of dimensions
let df = 6;  // The degrees of freedom is an unsigned int > k

// The scale matrix of the wishar distribution
let scale_mat: DMatrix<f64> = DMatrix::identity(k, k);

// The draw procedure outlined in the appendices of "Bayesian Data
// Analysis" by Andrew Gelman and colleagues.
let mut rng = rand::thread_rng();

// 1. Create a multivariate normal with zero mean and covariance matrix S
let mvg = MvGaussian::new(DVector::zeros(k), scale_mat).unwrap();

// 2. Draw ν (df) vectors {x_1, ..., x_ν}
let xs = mvg.sample(df, &mut rng);

// 3. Compute the sum Σ xx'
let mat = xs.iter().fold(DMatrix::<f64>::zeros(k, k), |acc, x| {
    acc +x*x.transpose()
});

// Check that the matrix is square and has the right size
assert_eq!(mat.nrows(), k);
assert_eq!(mat.ncols(), k);

// check that the matrix is positive definite by attempting a Cholesky
// decomposition.
assert!(mat.cholesky().is_some());

Methods

impl MvGaussian[src]

pub fn new(mu: DVector<f64>, cov: DMatrix<f64>) -> Result<Self, MvGaussianError>[src]

Create a new multivariate Gaussian distribution

Arguments

  • mu: k-length mean vector
  • cov: k-by-k positive-definite covariance matrix

pub fn new_cholesky(
    mu: DVector<f64>,
    cov_chol: Cholesky<f64, Dynamic>
) -> Result<Self, MvGaussianError>
[src]

Create a new multivariate Gaussian distribution from Cholesky factorized Dov

Arguments

  • mu: k-length mean vector
  • cov_chol: Choleksy decomposition of k-by-k positive-definite covariance matrix
use nalgebra::{DMatrix, DVector};
use rv::prelude::*;

let mu = DVector::zeros(3);
let cov = DMatrix::from_row_slice(3, 3, &[
    2.0, 1.0, 0.0,
    1.0, 2.0, 0.0,
    0.0, 0.0, 1.0
]);

let chol = cov.clone().cholesky().unwrap();
let mvg_r = MvGaussian::new_cholesky(mu, chol);

assert!(mvg_r.is_ok());
let mvg = mvg_r.unwrap();
assert!(cov.relative_eq(mvg.cov(), 1E-8, 1E-8));

pub fn new_unchecked(mu: DVector<f64>, cov: DMatrix<f64>) -> Self[src]

Creates a new MvGaussian from mean and covariance without checking whether the parameters are valid.

pub fn new_cholesky_unchecked(
    mu: DVector<f64>,
    cov_chol: Cholesky<f64, Dynamic>
) -> Self
[src]

Creates a new MvGaussian from mean and covariance's Cholesky factorization without checking whether the parameters are valid.

pub fn standard(dims: usize) -> Result<Self, MvGaussianError>[src]

Create a standard Gaussian distribution with zero mean and identiry covariance matrix.

pub fn dims(&self) -> usize[src]

Get the number of dimensions

Example

let mvg = MvGaussian::standard(4).unwrap();
assert_eq!(mvg.dims(), 4);

pub fn mu(&self) -> &DVector<f64>[src]

Get a reference to the mean

pub fn cov(&self) -> &DMatrix<f64>[src]

Get a reference to the covariance

pub fn set_mu(&mut self, mu: DVector<f64>) -> Result<(), MvGaussianError>[src]

Set the mean

Example

let mut mvg = MvGaussian::standard(3).unwrap();
let x = DVector::<f64>::zeros(3);

assert::close(mvg.ln_f(&x), -2.756815599614018, 1E-8);

let cov_vals = vec![
    1.01742788,
    0.36586652,
    -0.65620486,
    0.36586652,
    1.00564553,
    -0.42597261,
    -0.65620486,
    -0.42597261,
    1.27247972,
];
let cov: DMatrix<f64> = DMatrix::from_row_slice(3, 3, &cov_vals);
let mu = DVector::<f64>::from_column_slice(&vec![0.5, 3.1, -6.2]);

mvg.set_mu(mu).unwrap();
mvg.set_cov(cov).unwrap();

assert::close(mvg.ln_f(&x), -24.602370253215661, 1E-8);

pub fn set_mu_unchecked(&mut self, mu: DVector<f64>)[src]

pub fn set_cov(&mut self, cov: DMatrix<f64>) -> Result<(), MvGaussianError>[src]

Set the covariance matrix

Example

let mut mvg = MvGaussian::standard(3).unwrap();
let x = DVector::<f64>::zeros(3);

assert::close(mvg.ln_f(&x), -2.756815599614018, 1E-8);

let cov_vals = vec![
    1.01742788,
    0.36586652,
    -0.65620486,
    0.36586652,
    1.00564553,
    -0.42597261,
    -0.65620486,
    -0.42597261,
    1.27247972,
];
let cov: DMatrix<f64> = DMatrix::from_row_slice(3, 3, &cov_vals);
let mu = DVector::<f64>::from_column_slice(&vec![0.5, 3.1, -6.2]);

mvg.set_mu(mu).unwrap();
mvg.set_cov(cov).unwrap();

assert::close(mvg.ln_f(&x), -24.602370253215661, 1E-8);

pub fn set_cov_unchecked(&mut self, cov: DMatrix<f64>)[src]

Trait Implementations

impl Clone for MvGaussian[src]

impl ConjugatePrior<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>, MvGaussian> for NormalInvWishart[src]

type Posterior = NormalInvWishart

impl ContinuousDistr<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian[src]

impl ContinuousDistr<MvGaussian> for NormalInvWishart[src]

impl Debug for MvGaussian[src]

impl Display for MvGaussian[src]

impl Entropy for MvGaussian[src]

impl<'_> From<&'_ MvGaussian> for String[src]

impl HasSuffStat<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian[src]

impl Mean<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian[src]

impl Mode<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian[src]

impl PartialEq<MvGaussian> for MvGaussian[src]

impl Rv<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian[src]

impl Rv<MvGaussian> for NormalInvWishart[src]

impl Support<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian[src]

impl Support<MvGaussian> for NormalInvWishart[src]

impl Variance<Matrix<f64, Dynamic, Dynamic, <DefaultAllocator as Allocator<f64, Dynamic, Dynamic>>::Buffer>> for MvGaussian[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<Fx, X> ContinuousDistr<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: ContinuousDistr<X>, 
[src]

impl<Fx> Entropy for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Entropy
[src]

impl<T> From<T> for T[src]

impl<Fx, X> HasSuffStat<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: HasSuffStat<X>, 
[src]

type Stat = <<Fx as Deref>::Target as HasSuffStat<X>>::Stat

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<Fx, X> Mean<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Mean<X>, 
[src]

impl<Fx, X> Mode<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Mode<X>, 
[src]

impl<Fx, X> Rv<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Rv<X>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

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

impl<Fx, X> Support<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Support<X>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

impl<Fx, X> Variance<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Variance<X>, 
[src]