Binomial Distribution

Table of contents


Density Function

The density function of the Binomial distribution:

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

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

Scalar Input

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

Density function of the Binomial distribution.

Example:

stats::dbinom(2,4,0.4,false); 

Parameters
  • x – a real-valued input.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> dbinom(const std::vector<eT> &x, const llint_t n_trials_par, const T1 prob_par, const bool log_form = false)

Density function of the Binomial distribution.

Example:

std::vector<int> x = {2, 3, 4};
stats::dbinom(x,5,0.5,false);

Parameters
  • x – a standard vector.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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 eT, typename T1, typename rT = common_return_t<eT, T1>>
inline ArmaMat<rT> dbinom(const ArmaMat<eT> &X, const llint_t n_trials_par, const T1 prob_par, const bool log_form = false)

Density function of the Binomial distribution.

Example:

stats::dbinom(X,5,0.5,false);

Parameters
  • X – a matrix of input values.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> dbinom(const BlazeMat<eT, To> &X, const llint_t n_trials_par, const T1 prob_par, const bool log_form = false)

Density function of the Binomial distribution.

Example:

stats::dbinom(X,5,0.5,false);

Parameters
  • X – a matrix of input values.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> dbinom(const EigenMat<eT, iTr, iTc> &X, const llint_t n_trials_par, const T1 prob_par, const bool log_form = false)

Density function of the Binomial distribution.

Example:

stats::dbinom(X,5,0.5,false);

Parameters
  • X – a matrix of input values.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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 Binomial distribution:

\[F(x; n, p) = \sum_{z \leq x} f(z; n, p)\]

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

Scalar Input

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

Distribution function of the Binomial distribution.

Example:

stats::pbinom(2,4,0.4,false); 

Parameters
  • x – a real-valued input.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> pbinom(const std::vector<eT> &x, const llint_t n_trials_par, const T1 prob_par, const bool log_form = false)

Distribution function of the Binomial distribution.

Example:

std::vector<int> x = {2, 3, 4};
stats::pbinom(x,5,0.5,false);

Parameters
  • x – a standard vector.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> pbinom(const ArmaMat<eT> &X, const llint_t n_trials_par, const T1 prob_par, const bool log_form = false)

Distribution function of the Binomial distribution.

Example:

stats::pbinom(X,5,0.5,false);

Parameters
  • X – a matrix of input values.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> pbinom(const BlazeMat<eT, To> &X, const llint_t n_trials_par, const T1 prob_par, const bool log_form = false)

Distribution function of the Binomial distribution.

Example:

stats::pbinom(X,5,0.5,false);

Parameters
  • X – a matrix of input values.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> pbinom(const EigenMat<eT, iTr, iTc> &X, const llint_t n_trials_par, const T1 prob_par, const bool log_form = false)

Distribution function of the Binomial distribution.

Example:

stats::pbinom(X,5,0.5,false);

Parameters
  • X – a matrix of input values.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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 Binomial distribution:

\[q(r; n, p) = \inf \left\{ x : r \leq F(x; n, p) \right\}\]

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> qbinom(const T1 p, const llint_t n_trials_par, const T2 prob_par) noexcept

Quantile function of the Binomial distribution.

Example:

stats::qbinom(0.4,4,0.4); 

Parameters
  • p – a real-valued input.

  • n_trials_par – the number of trials, a non-negative integral-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> qbinom(const std::vector<eT> &x, const llint_t n_trials_par, const T1 prob_par)

Quantile function of the Binomial distribution.

Example:

std::vector<int> x = {2, 3, 4};
stats::qbinom(x,5,0.5);

Parameters
  • x – a standard vector.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> qbinom(const ArmaMat<eT> &X, const llint_t n_trials_par, const T1 prob_par)

Quantile function of the Binomial distribution.

Example:

stats::qbinom(X,5,0.5);

Parameters
  • X – a matrix of input values.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> qbinom(const BlazeMat<eT, To> &X, const llint_t n_trials_par, const T1 prob_par)

Quantile function of the Binomial distribution.

Example:

stats::qbinom(X,5,0.5);

Parameters
  • X – a matrix of input values.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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> qbinom(const EigenMat<eT, iTr, iTc> &X, const llint_t n_trials_par, const T1 prob_par)

Quantile function of the Binomial distribution.

Example:

stats::qbinom(X,5,0.5);

Parameters
  • X – a matrix of input values.

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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 Binomial distribution is achieved by summing the results of simulating n Bernoulli-distributed random variables.

Scalar Output

  1. Random number engines

template<typename T>
inline return_t<T> rbinom(const llint_t n_trials_par, const T prob_par, rand_engine_t &engine)

Random sampling function for the Binomial distribution.

Example:

stats::rand_engine_t engine(1776);
stats::rbinom(4,0.4,engine);

Parameters
  • n_trials_par – the number of trials, a non-negative integral-valued input.

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

  • engine – a random engine, passed by reference.

Returns

a pseudo-random draw from the Beta distribution.

  1. Seed values

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

Random sampling function for the Binomial distribution.

Example:

stats::rbinom(4,0.4,1776);

Parameters
  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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 Beta distribution.

Vector/Matrix Output

  1. Random number engines

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

Random matrix sampling function for the Binomial distribution.

Example:

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

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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 Binomial distribution.

  1. Seed values

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

Random matrix sampling function for the Binomial distribution.

Example:

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

  • n_trials_par – the number of trials, a non-negative integral-valued input.

  • 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 Binomial distribution.