Introduction

The density function of the Normal (Gaussian) distribution:

$$ f(x; \mu, \sigma) = \frac{1}{\sqrt{2 \pi} \sigma} \exp \left( - \frac{(x-\mu)^2}{2 \sigma^2} \right) $$

Parameters:

  • mu_par is $\mu$
  • sigma_par is $\sigma$

Density


Definition:

 
template<typename Ta, typename Tb>
statslib_constexpr
return_t<Ta> dnorm(const Ta x, const Tb mu_par, const Tb sigma_par, const bool log_form = false);

Computes the density function.


Examples:

// parameters
double mu = 0.0;
double sigma = 1.0;

// standard input
double dens_val = stats::dnorm(0.5,mu,sigma);
double log_dens_val = stats::dnorm(0.5,mu,sigma,true);

// Armadillo input
arma::mat X(10,1);
X.fill(0.5);

arma::mat dens_vals_mat = stats::dnorm(X,mu,sigma);
arma::mat log_dens_vals_mat = stats::dnorm(X,mu,sigma,true);

Probability


Definition:

 
template<typename Ta, typename Tb>
statslib_constexpr
return_t<Ta> pnorm(const Ta x, const Tb mu_par, const Tb sigma_par, const bool log_form = false);

Computes the cumulative distribution function (CDF).


Examples:

// parameters
double mu = 0.0;
double sigma = 1.0;

// standard input
double prob_val = stats::pnorm(0.5,mu,sigma);
double log_prob_val = stats::pnorm(0.5,mu,sigma,true);

// Armadillo input
arma::mat X(10,1);
X.fill(0.5);

arma::mat prob_vals_mat = stats::pnorm(X,mu,sigma);
arma::mat log_prob_vals_mat = stats::pnorm(X,mu,sigma,true);

Quantile


Definition:

 
template<typename Ta, typename Tb>
statslib_constexpr
Ta qnorm(const Ta p, const Tb mu_par, const Tb sigma_par);

Computes the quantile function.


Examples:

// parameters
double mu = 0.0;
double sigma = 1.0;

// standard input
double quant_val = stats::qnorm(0.7,mu,sigma);

// Armadillo input
arma::mat X(10,1);
X.fill(0.7);

arma::mat quant_vals_mat = stats::qnorm(X,mu,sigma);

Random Sampling


Definition:

 
// random engine seeding
template<typename T>
statslib_inline
return_t<T> rnorm(const T mu_par, const T sigma_par, rand_engine_t& engine);

// seeding values
template<typename T>
statslib_inline
return_t<T> rnorm(const T mu_par, const T sigma_par, uint_t seed_val = std::random_device{}());

// generate N(0,1) draws
template<typename T = double>
statslib_inline
T rnorm();

// matrix output
template<typename mT, typename eT = double>
statslib_inline
mT rnorm(const uint_t n, const uint_t k, const eT mu_par = eT(0), const eT sigma_par = eT(1));

Generates pseudo-random draws.


Examples:

// parameters
double mu = 0.0;
double sigma = 1.0;

// standard input
double rand_val = stats::rnorm(mu,sigma);

// Armadillo output: 10 x 1 matrix
arma::mat rand_mat = stats::rnorm<arma::mat>(10,1,mu,sigma);