Bernoulli Distribution

Table of contents


Density Function

The density function of the Bernoulli distribution:

\[f(x; p) = p^x (1-p)^{1-x} \times \mathbf{1}[x \in \{0,1\}]\]

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

Scalar Input

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

Density function of the Bernoulli distribution.

Example:

stats::dbern(1,0.6,false); 

Parameters
  • x – an integral-valued input, equal to 0 or 1.

  • prob_par – the probability parameter, 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 rT = common_return_t<eT, T1>>
inline std::vector<rT> dbern(const std::vector<eT> &x, const T1 prob_par, const bool log_form = false)

Density function of the Bernoulli distribution.

Example:

std::vector<int> x = {0, 1, 0};
stats::dbern(x,0.5,false);

Parameters
  • x – a standard vector.

  • prob_par – the probability 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 mT, typename tT, typename T1>
inline mT dbern(const ArmaGen<mT, tT> &X, const T1 prob_par, const bool log_form = false)

Density function of the Bernoulli distribution.

Example:

arma::mat X = { {1, 0, 1},
                {0, 1, 0} };
stats::dbern(X,0.5,false);

Parameters
  • X – a matrix of input values.

  • prob_par – the probability 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 rT = common_return_t<eT, T1>, bool To = blaze::columnMajor>
inline BlazeMat<rT, To> dbern(const BlazeMat<eT, To> &X, const T1 prob_par, const bool log_form = false)

Density function of the Bernoulli distribution.

Example:

stats::dbern(X,0.5,false);

Parameters
  • X – a matrix of input values.

  • prob_par – the probability 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 rT = common_return_t<eT, T1>, int iTr = Eigen::Dynamic, int iTc = Eigen::Dynamic>
inline EigenMat<rT, iTr, iTc> dbern(const EigenMat<eT, iTr, iTc> &X, const T1 prob_par, const bool log_form = false)

Density function of the Bernoulli distribution.

Example:

stats::dbern(X,0.5,false);

Parameters
  • X – a matrix of input values.

  • prob_par – the probability 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 Bernoulli distribution:

\[F(x; p) = \sum_{z \leq x} f(z; p) = \begin{cases} 0 & \text{ if } x < 0 \\ 1-p & \text{ if } 0 \leq x < 1 \\ 1 & \text{ if } x \geq 1 \end{cases}\]

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

Scalar Input

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

Distribution function of the Bernoulli distribution.

Example:

stats::pbern(1,0.6,false); 

Parameters
  • x – a value equal to 0 or 1.

  • prob_par – the probability parameter, 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 rT = common_return_t<eT, T1>>
inline std::vector<rT> pbern(const std::vector<eT> &x, const T1 prob_par, const bool log_form = false)

Density function of the Bernoulli distribution.

Example:

std::vector<int> x = {0, 1, 0};
stats::pbern(x,0.5,false);

Parameters
  • x – a standard vector.

  • prob_par – the probability 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 rT = common_return_t<eT, T1>>
inline ArmaMat<rT> pbern(const ArmaMat<eT> &X, const T1 prob_par, const bool log_form = false)

Density function of the Bernoulli distribution.

Example:

arma::mat X = { {1, 0, 1},
                {0, 1, 0} };
stats::pbern(X,0.5,false);

Parameters
  • X – a matrix of input values.

  • prob_par – the probability 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 rT = common_return_t<eT, T1>, bool To = blaze::columnMajor>
inline BlazeMat<rT, To> pbern(const BlazeMat<eT, To> &X, const T1 prob_par, const bool log_form = false)

Density function of the Bernoulli distribution.

Example:

stats::pbern(X,0.5,false);

Parameters
  • X – a matrix of input values.

  • prob_par – the probability 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 rT = common_return_t<eT, T1>, int iTr = Eigen::Dynamic, int iTc = Eigen::Dynamic>
inline EigenMat<rT, iTr, iTc> pbern(const EigenMat<eT, iTr, iTc> &X, const T1 prob_par, const bool log_form = false)

Density function of the Bernoulli distribution.

Example:

stats::pbern(X,0.5,false);

Parameters
  • X – a matrix of input values.

  • prob_par – the probability 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 Bernoulli distribution:

\[q(r; p) = \begin{cases} 0 & \text{ if } r \leq 1 - p \\ 1 & \text{ else } \end{cases}\]

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

Scalar Input

template<typename T1, typename T2>
constexpr common_return_t<T1, T2> qbern(const T1 p, const T2 prob_par) noexcept

Quantile function of the Bernoulli distribution.

Example:

stats::qbern(0.5,0.4); 

Parameters
  • p – a real-valued input.

  • prob_par – the probability parameter, a real-valued input.

Returns

the quantile function evaluated at p.

Vector/Matrix Input

STL Containers

template<typename eT, typename T1, typename rT = common_return_t<eT, T1>>
inline std::vector<rT> qbern(const std::vector<eT> &x, const T1 prob_par)

Quantile function of the Bernoulli distribution.

Example:

std::vector<int> x = {0.4, 0.5, 0.9};
stats::qbern(x,0.5);

Parameters
  • x – a standard vector.

  • prob_par – the probability parameter, a real-valued input.

Returns

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

Armadillo

template<typename eT, typename T1, typename rT = common_return_t<eT, T1>>
inline ArmaMat<rT> qbern(const ArmaMat<eT> &X, const T1 prob_par)

Quantile function of the Bernoulli distribution.

Example:

arma::mat X = { {0.4, 0.5, 0.9},
                {0.3, 0.6, 0.7} };
stats::qbern(X,0.5);

Parameters
  • X – a matrix of input values.

  • prob_par – the probability parameter, a real-valued input.

Returns

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

Blaze

template<typename eT, typename T1, typename rT = common_return_t<eT, T1>, bool To = blaze::columnMajor>
inline BlazeMat<rT, To> qbern(const BlazeMat<eT, To> &X, const T1 prob_par)

Quantile function of the Bernoulli distribution.

Example:

stats::qbern(X,0.5);

Parameters
  • X – a matrix of input values.

  • prob_par – the probability parameter, a real-valued input.

Returns

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

Eigen

template<typename eT, typename T1, typename rT = common_return_t<eT, T1>, int iTr = Eigen::Dynamic, int iTc = Eigen::Dynamic>
inline EigenMat<rT, iTr, iTc> qbern(const EigenMat<eT, iTr, iTc> &X, const T1 prob_par)

Quantile function of the Bernoulli distribution.

Example:

stats::qbern(X,0.5);

Parameters
  • X – a matrix of input values.

  • prob_par – the probability parameter, a real-valued input.

Returns

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


Random Sampling

Random sampling for the Bernoulli distribution is achieved via the inverse probability integral transform.

Scalar Output

  1. Random number engines

template<typename T>
inline return_t<T> rbern(const T prob_par, rand_engine_t &engine)

Random sampling function for the Bernoulli distribution.

Example:

stats::rand_engine_t engine(1776);
stats::rbern(0.7,engine);

Parameters
  • prob_par – the probability parameter, a real-valued input.

  • engine – a random engine, passed by reference.

Returns

a pseudo-random draw from the Bernoulli distribution.

  1. Seed values

template<typename T>
inline return_t<T> rbern(const T prob_par, const ullint_t seed_val = std::random_device{}())

Random sampling function for the Bernoulli distribution.

Example:

stats::rbern(0.7,1776);

Parameters
  • prob_par – the probability 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 Bernoulli distribution.

Vector/Matrix Output

  1. Random number engines

template<typename mT, typename T1>
inline mT rbern(const ullint_t n, const ullint_t k, const T1 prob_par, rand_engine_t &engine)

Random matrix sampling function for the Bernoulli distribution.

Example:

stats::rand_engine_t engine(1776);
// std::vector
stats::rbern<std::vector<double>>(5,4,0.7,engine);
// Armadillo matrix
stats::rbern<arma::mat>(5,4,0.7,engine);
// Blaze dynamic matrix
stats::rbern<blaze::DynamicMatrix<double,blaze::columnMajor>>(5,4,0.7,engine);
// Eigen dynamic matrix
stats::rbern<Eigen::MatrixXd>(5,4,0.7,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

  • prob_par – the probability parameter, a real-valued input.

  • engine – a random engine, passed by reference.

Returns

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

  1. Seed values

template<typename mT, typename T1>
inline mT rbern(const ullint_t n, const ullint_t k, const T1 prob_par, const ullint_t seed_val = std::random_device{}())

Random matrix sampling function for the Bernoulli distribution.

Example:

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

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

  • prob_par – the probability 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 Bernoulli distribution.