[−][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>
[src]
Create a new multivariate Gaussian distribution
Arguments
- mu: k-length mean vector
- cov: k-by-k positive-definite covariance matrix
pub fn new_unchecked(mu: DVector<f64>, cov: DMatrix<f64>) -> Self
[src]
Creates a new MvGaussian without checking whether the parameters are valid.
pub fn standard(dims: usize) -> Result<Self>
[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<()>
[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<()>
[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 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 ContinuousDistr<Matrix<f64, Dynamic, U1, VecStorage<f64, Dynamic, U1>>> for MvGaussian
[src]
impl ContinuousDistr<MvGaussian> for NormalInvWishart
[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 Variance<Matrix<f64, Dynamic, Dynamic, <DefaultAllocator as Allocator<f64, Dynamic, Dynamic>>::Buffer>> for MvGaussian
[src]
impl Entropy for MvGaussian
[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 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<'_> From<&'_ MvGaussian> for String
[src]
fn from(mvg: &MvGaussian) -> String
[src]
impl Clone for MvGaussian
[src]
fn clone(&self) -> MvGaussian
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl PartialEq<MvGaussian> for MvGaussian
[src]
fn eq(&self, other: &MvGaussian) -> bool
[src]
fn ne(&self, other: &MvGaussian) -> bool
[src]
impl PartialOrd<MvGaussian> for MvGaussian
[src]
fn partial_cmp(&self, other: &MvGaussian) -> Option<Ordering>
[src]
fn lt(&self, other: &MvGaussian) -> bool
[src]
fn le(&self, other: &MvGaussian) -> bool
[src]
fn gt(&self, other: &MvGaussian) -> bool
[src]
fn ge(&self, other: &MvGaussian) -> bool
[src]
impl Display for MvGaussian
[src]
impl Debug for MvGaussian
[src]
Auto Trait Implementations
impl Send for MvGaussian
impl Sync for MvGaussian
impl Unpin for MvGaussian
impl UnwindSafe for MvGaussian
impl RefUnwindSafe for MvGaussian
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
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<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<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
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<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,