[−][src]Struct rv::dist::MvGaussian
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]
mu: DVector<f64>,
cov_chol: Cholesky<f64, Dynamic>
) -> Result<Self, MvGaussianError>
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]
mu: DVector<f64>,
cov_chol: Cholesky<f64, Dynamic>
) -> Self
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]
fn clone(&self) -> MvGaussian
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl ConjugatePrior<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>, MvGaussian> for NormalInvWishart
[src]
type Posterior = NormalInvWishart
fn posterior(
&self,
x: &DataOrSuffStat<'a, DVector<f64>, MvGaussian>
) -> NormalInvWishart
[src]
&self,
x: &DataOrSuffStat<'a, DVector<f64>, MvGaussian>
) -> NormalInvWishart
fn ln_m(&self, x: &DataOrSuffStat<'a, DVector<f64>, MvGaussian>) -> f64
[src]
fn ln_pp(
&self,
y: &DVector<f64>,
x: &DataOrSuffStat<'a, DVector<f64>, MvGaussian>
) -> f64
[src]
&self,
y: &DVector<f64>,
x: &DataOrSuffStat<'a, DVector<f64>, MvGaussian>
) -> f64
fn m(&self, x: &DataOrSuffStat<X, Fx>) -> f64
[src]
fn pp(&self, y: &X, x: &DataOrSuffStat<X, Fx>) -> f64
[src]
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]
fn from(mvg: &MvGaussian) -> String
[src]
impl HasSuffStat<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian
[src]
type Stat = MvGaussianSuffStat
fn empty_suffstat(&self) -> Self::Stat
[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]
fn eq(&self, other: &MvGaussian) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl Rv<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian
[src]
fn ln_f(&self, x: &DVector<f64>) -> f64
[src]
fn draw<R: Rng>(&self, rng: &mut R) -> DVector<f64>
[src]
fn f(&self, x: &X) -> f64
[src]
fn sample<R: Rng>(&self, n: usize, rng: &mut R) -> Vec<X>
[src]
impl Rv<MvGaussian> for NormalInvWishart
[src]
fn ln_f(&self, x: &MvGaussian) -> f64
[src]
fn draw<R: Rng>(&self, rng: &mut R) -> MvGaussian
[src]
fn f(&self, x: &X) -> f64
[src]
fn sample<R: Rng>(&self, n: usize, rng: &mut R) -> Vec<X>
[src]
impl Support<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian
[src]
impl Support<MvGaussian> for NormalInvWishart
[src]
fn supports(&self, x: &MvGaussian) -> bool
[src]
impl Variance<Matrix<f64, Dynamic, Dynamic, <DefaultAllocator as Allocator<f64, Dynamic, Dynamic>>::Buffer>> for MvGaussian
[src]
Auto Trait Implementations
impl RefUnwindSafe for MvGaussian
impl Send for MvGaussian
impl Sync for MvGaussian
impl Unpin for MvGaussian
impl UnwindSafe for MvGaussian
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<Fx, X> ContinuousDistr<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: ContinuousDistr<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: ContinuousDistr<X>,
impl<Fx> Entropy for Fx where
Fx: Deref,
<Fx as Deref>::Target: Entropy,
[src]
Fx: Deref,
<Fx as Deref>::Target: Entropy,
impl<T> From<T> for T
[src]
impl<Fx, X> HasSuffStat<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: HasSuffStat<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: HasSuffStat<X>,
type Stat = <<Fx as Deref>::Target as HasSuffStat<X>>::Stat
fn empty_suffstat(&Self) -> <Fx as HasSuffStat<X>>::Stat
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<Fx, X> Mean<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Mean<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Mean<X>,
impl<Fx, X> Mode<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Mode<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Mode<X>,
impl<Fx, X> Rv<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Rv<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Rv<X>,
fn ln_f(&Self, &X) -> f64
[src]
fn f(&Self, &X) -> f64
[src]
fn draw<R>(&Self, &mut R) -> X where
R: Rng,
[src]
R: Rng,
fn sample<R>(&Self, usize, &mut R) -> Vec<X> where
R: Rng,
[src]
R: Rng,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn is_in_subset(&self) -> bool
unsafe fn to_subset_unchecked(&self) -> SS
fn from_subset(element: &SS) -> SP
impl<Fx, X> Support<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Support<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Support<X>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,
fn vzip(self) -> V
impl<Fx, X> Variance<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Variance<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Variance<X>,