Introduction

The density function of the Multivariate Normal distribution:

$$ f(\mathbf{x}; \boldsymbol{\mu}, \boldsymbol{\Sigma}) = \dfrac{1}{\sqrt{(2\pi)^k |\boldsymbol{\Sigma}|}} \exp \left( - \frac{1}{2} (\mathbf{x} - \boldsymbol{\mu})^\top \boldsymbol{\Sigma}^{-1} (\mathbf{x} - \boldsymbol{\mu}) \right) $$

where $k$ is the dimension of the real-valued vector $\mathbf{x}$.

Parameters:

  • mu_par is $\boldsymbol{\mu}$
  • sigma_par is $\boldsymbol{\Sigma}$

Density


Definition:

template<typename mT, typename eT = double>
statslib_inline
eT dmvnorm(const mT& X, const mT& mu_par, const mT& Sigma_par, bool log_form = false);

Computes the density function.


Examples:

// parameters
arma::mat mu = arma::zeros(5,1);
arma::mat Sigma = arma::eye(5,5);

// Armadillo input
arma::mat X(5,1);
X.fill(1.5);

arma::mat dens_vals_mat = stats::dmvnorm(X,mu,Sigma);
arma::mat log_dens_vals_mat = stats::dmvnorm(X,mu,Sigma,true);

Random Sampling


Definition:

 
// vector draw
template<typename T>
statslib_inline
T rmvnorm(const T& mu_par, const T& Sigma_par, const bool pre_chol = false);

// n samples, outputs n x k matrix
template<typename T>
statslib_inline
T rmvnorm(const uint_t n, const T& mu_par, const T& Sigma_par, const bool pre_chol = false);

Generates pseudo-random draws.


Examples:

// parameters
arma::mat mu = arma::zeros(5,1);
arma::mat Sigma = arma::eye(5,5);

// Armadillo output
arma::mat rand_mat = stats::rmvnorm<arma::mat>(mu,Sigma);