Normal Distribution

Table of contents


Density Function

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)\]

Methods for scalar input, as well as for vector/matrix input, are listed below.

Scalar Input

template<typename T1, typename T2, typename T3>
constexpr common_return_t<T1, T2, T3> dnorm(const T1 x, const T2 mu_par, const T3 sigma_par, const bool log_form = false) noexcept

Density function of the Normal distribution.

Example:

stats::dnorm(0.5,1.0,2.0,false); 

Parameters
  • x – a real-valued input.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-density or the true form.

Returns

the density function evaluated at x.

template<typename T>
constexpr return_t<T> dnorm(const T x, const bool log_form = false) noexcept

Density function of the standard Normal distribution.

Example:

stats::dnorm(0.5,false); 

Parameters
  • x – a real-valued input.

  • log_form – return the log-density or the true form.

Returns

the density function evaluated at x.

Vector/Matrix Input

STL Containers

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>>
inline std::vector<rT> dnorm(const std::vector<eT> &x, const T1 mu_par, const T2 sigma_par, const bool log_form = false)

Density function of the Normal distribution.

Example:

std::vector<double> x = {0.0, 1.0, 2.0};
stats::dnorm(x,1.0,2.0,false);

Parameters
  • x – a standard vector.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-density or the true form.

Returns

a vector of density function values corresponding to the elements of x.

Armadillo

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>>
inline ArmaMat<rT> dnorm(const ArmaMat<eT> &X, const T1 mu_par, const T2 sigma_par, const bool log_form = false)

Density function of the Normal distribution.

Example:

arma::mat X = { {0.2, -1.7,  0.1},
                {0.9,  4.0, -0.3} };
stats::dnorm(X,1.0,1.0,false);

Parameters
  • X – a matrix of input values.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-density or the true form.

Returns

a matrix of density function values corresponding to the elements of X.

Blaze

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>, bool To = blaze::columnMajor>
inline BlazeMat<rT, To> dnorm(const BlazeMat<eT, To> &X, const T1 mu_par, const T2 sigma_par, const bool log_form = false)

Density function of the Normal distribution.

Example:

stats::dnorm(X,1.0,1.0,false);

Parameters
  • X – a matrix of input values.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-density or the true form.

Returns

a matrix of density function values corresponding to the elements of X.

Eigen

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>, int iTr = Eigen::Dynamic, int iTc = Eigen::Dynamic>
inline EigenMat<rT, iTr, iTc> dnorm(const EigenMat<eT, iTr, iTc> &X, const T1 mu_par, const T2 sigma_par, const bool log_form = false)

Density function of the Normal distribution.

Example:

stats::dnorm(X,1.0,1.0,false);

Parameters
  • X – a matrix of input values.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-density or the true form.

Returns

a matrix of density function values corresponding to the elements of X.


Cumulative Distribution Function

The cumulative distribution function of the Normal (Gaussian) distribution:

\[F(x; \mu, \sigma) = \int_{-\infty}^x f(z; \mu, \sigma) dz = \frac{1}{2} \times \left( 1 + \text{erf} \left( \frac{x - \mu}{\sqrt{2} \sigma} \right) \right)\]

where \(\text{erf}(\cdot)\) denotes the Gaussian error function.

Methods for scalar input, as well as for vector/matrix input, are listed below.

Scalar Input

template<typename T1, typename T2, typename T3>
constexpr common_return_t<T1, T2, T3> pnorm(const T1 x, const T2 mu_par, const T3 sigma_par, const bool log_form = false) noexcept

Distribution function of the Normal distribution.

Example:

stats::pnorm(2.0,1.0,2.0,false); 

Parameters
  • x – a real-valued input.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-probability or the true form.

Returns

the cumulative distribution function evaluated at x.

template<typename T>
constexpr return_t<T> pnorm(const T x, const bool log_form = false) noexcept

Distribution function of the standard Normal distribution.

Example:

stats::pnorm(1.0,false); 

Parameters
  • x – a real-valued input.

  • log_form – return the log-probability or the true form.

Returns

the cumulative distribution function evaluated at x.

Vector/Matrix Input

STL Containers

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>>
inline std::vector<rT> pnorm(const std::vector<eT> &x, const T1 mu_par, const T2 sigma_par, const bool log_form = false)

Distribution function of the Normal distribution.

Example:

std::vector<double> x = {0.0, 1.0, 2.0};
stats::pnorm(x,1.0,2.0,false);

Parameters
  • x – a standard vector.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-probability or the true form.

Returns

a vector of CDF values corresponding to the elements of x.

Armadillo

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>>
inline ArmaMat<rT> pnorm(const ArmaMat<eT> &X, const T1 mu_par, const T2 sigma_par, const bool log_form = false)

Distribution function of the Normal distribution.

Example:

arma::mat X = { {0.2, -1.7,  0.1},
                {0.9,  4.0, -0.3} };
stats::pnorm(X,1.0,1.0,false);

Parameters
  • X – a matrix of input values.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-probability or the true form.

Returns

a matrix of CDF values corresponding to the elements of X.

Blaze

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>, bool To = blaze::columnMajor>
inline BlazeMat<rT, To> pnorm(const BlazeMat<eT, To> &X, const T1 mu_par, const T2 sigma_par, const bool log_form = false)

Distribution function of the Normal distribution.

Example:

stats::pnorm(X,1.0,1.0,false);

Parameters
  • X – a matrix of input values.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-probability or the true form.

Returns

a matrix of CDF values corresponding to the elements of X.

Eigen

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>, int iTr = Eigen::Dynamic, int iTc = Eigen::Dynamic>
inline EigenMat<rT, iTr, iTc> pnorm(const EigenMat<eT, iTr, iTc> &X, const T1 mu_par, const T2 sigma_par, const bool log_form = false)

Distribution function of the Normal distribution.

Example:

stats::pnorm(X,1.0,1.0,false);

Parameters
  • X – a matrix of input values.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • log_form – return the log-probability or the true form.

Returns

a matrix of CDF values corresponding to the elements of X.


Quantile Function

The quantile function of the log-Normal distribution:

\[q(p; \mu, \sigma) = \mu + \sqrt{2} \sigma \times \text{erf}^{-1} \left( 2 p - 1 \right)\]

where \(\text{erf}^{-1}(\cdot)\) denotes the inverse Gaussian error function.

Methods for scalar input, as well as for vector/matrix input, are listed below.

Scalar Input

template<typename T1, typename T2, typename T3>
constexpr common_return_t<T1, T2, T3> qnorm(const T1 p, const T2 mu_par, const T3 sigma_par) noexcept

Quantile function of the Normal distribution.

Example:

stats::qnorm(0.5,1.0,2.0); 

Parameters
  • p – a real-valued input.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

Returns

the quantile function evaluated at p.

template<typename T>
constexpr return_t<T> qnorm(const T p) noexcept

Quantile function of the standard Normal distribution.

Example:

stats::qnorm(0.5); 

Parameters

p – a real-valued input.

Returns

the quantile function evaluated at p.

Vector/Matrix Input

STL Containers

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>>
inline std::vector<rT> qnorm(const std::vector<eT> &x, const T1 mu_par, const T2 sigma_par)

Quantile function of the Normal distribution.

Example:

std::vector<double> x = {0.1, 0.3, 0.7};
stats::qnorm(x,1.0,2.0);

Parameters
  • x – a standard vector.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

Returns

a vector of quantile values corresponding to the elements of x.

Armadillo

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>>
inline ArmaMat<rT> qnorm(const ArmaMat<eT> &X, const T1 mu_par, const T2 sigma_par)

Quantile function of the Normal distribution.

Example:

arma::mat X = { {0.2, 0.7, 0.9},
                {0.1, 0.8, 0.3} };
stats::qnorm(X,1.0,1.0);

Parameters
  • X – a matrix of input values.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

Returns

a matrix of quantile values corresponding to the elements of X.

Blaze

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>, bool To = blaze::columnMajor>
inline BlazeMat<rT, To> qnorm(const BlazeMat<eT, To> &X, const T1 mu_par, const T2 sigma_par)

Quantile function of the Normal distribution.

Example:

stats::qnorm(X,1.0,1.0);

Parameters
  • X – a matrix of input values.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

Returns

a matrix of quantile values corresponding to the elements of X.

Eigen

template<typename eT, typename T1, typename T2, typename rT = common_return_t<eT, T1, T2>, int iTr = Eigen::Dynamic, int iTc = Eigen::Dynamic>
inline EigenMat<rT, iTr, iTc> qnorm(const EigenMat<eT, iTr, iTc> &X, const T1 mu_par, const T2 sigma_par)

Quantile function of the Normal distribution.

Example:

stats::qnorm(X,1.0,1.0);

Parameters
  • X – a matrix of input values.

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

Returns

a matrix of quantile values corresponding to the elements of X.


Random Sampling

Random sampling for the Normal distribution is achieved via the normal_distribution class from the C++ standard library, imported from <random>.

Scalar Output

  1. Random number engines

template<typename T1, typename T2>
inline common_return_t<T1, T2> rnorm(const T1 mu_par, const T2 sigma_par, rand_engine_t &engine)

Random sampling function for the Normal distribution.

Example:

stats::rand_engine_t engine(1776);
stats::rnorm(1.0,2.0,engine);

Parameters
  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • engine – a random engine, passed by reference.

Returns

a pseudo-random draw from the Normal distribution.

  1. Seed values

template<typename T1, typename T2>
inline common_return_t<T1, T2> rnorm(const T1 mu_par, const T2 sigma_par, const ullint_t seed_val = std::random_device{}())

Random sampling function for the Normal distribution.

Example:

stats::rnorm(1.0,2.0,1776);

Parameters
  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • seed_val – initialize the random engine with a non-negative integral-valued seed.

Returns

a pseudo-random draw from the Normal distribution.

  1. Convenience

template<typename T = double>
inline T rnorm()

Random sampling function for the standard Normal distribution.

Example:

stats::rnorm();

Returns

a pseudo-random draw from the standard Normal distribution.

Vector/Matrix Output

  1. Random number engines

template<typename mT, typename T1 = double, typename T2 = double>
inline mT rnorm(const ullint_t n, const ullint_t k, const T1 mu_par, const T2 sigma_par, rand_engine_t &engine)

Random matrix sampling function for the Normal distribution.

Example:

stats::rand_engine_t engine(1776);
// std::vector
stats::rnorm<std::vector<double>>(5,4,1.0,2.0,engine);
// Armadillo matrix
stats::rnorm<arma::mat>(5,4,1.0,2.0,engine);
// Blaze dynamic matrix
stats::rnorm<blaze::DynamicMatrix<double,blaze::columnMajor>>(5,4,1.0,2.0,engine);
// Eigen dynamic matrix
stats::rnorm<Eigen::MatrixXd>(5,4,1.0,2.0,engine);

Note

This function requires template instantiation; acceptable output types include: std::vector, with element type float, double, etc., as well as Armadillo, Blaze, and Eigen dense matrices.

Parameters
  • n – the number of output rows

  • k – the number of output columns

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • engine – a random engine, passed by reference.

Returns

a matrix of pseudo-random draws from the Normal distribution.

  1. Seed values

template<typename mT, typename T1, typename T2>
inline mT rnorm(const ullint_t n, const ullint_t k, const T1 mu_par, const T2 sigma_par, const ullint_t seed_val = std::random_device{}())

Random matrix sampling function for the Normal distribution.

Example:

// std::vector
stats::rnorm<std::vector<double>>(5,4,1.0,2.0);
// Armadillo matrix
stats::rnorm<arma::mat>(5,4,1.0,2.0);
// Blaze dynamic matrix
stats::rnorm<blaze::DynamicMatrix<double,blaze::columnMajor>>(5,4,1.0,2.0);
// Eigen dynamic matrix
stats::rnorm<Eigen::MatrixXd>(5,4,1.0,2.0);

Note

This function requires template instantiation; acceptable output types include: std::vector, with element type float, double, etc., as well as Armadillo, Blaze, and Eigen dense matrices.

Parameters
  • n – the number of output rows

  • k – the number of output columns

  • mu_par – the mean parameter, a real-valued input.

  • sigma_par – the standard deviation parameter, a real-valued input.

  • seed_val – initialize the random engine with a non-negative integral-valued seed.

Returns

a matrix of pseudo-random draws from the Normal distribution.