Uniform Distribution

Table of contents


Density Function

The density function of the Uniform distribution:

\[f(x; a, b) = \frac{1}{b-a} \times \mathbf{1}[ a \leq x \leq b]\]

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> dunif(const T1 x, const T2 a_par, const T3 b_par, const bool log_form = false) noexcept

Density function of the Uniform distribution.

Example:

stats::dunif(0.5,-1.0,2.0,false); 

Parameters
  • x – a real-valued input.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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 T2, typename rT = common_return_t<eT, T1, T2>>
inline std::vector<rT> dunif(const std::vector<eT> &x, const T1 a_par, const T2 b_par, const bool log_form = false)

Density function of the Uniform distribution.

Example:

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

Parameters
  • x – a standard vector.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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> dunif(const ArmaMat<eT> &X, const T1 a_par, const T2 b_par, const bool log_form = false)

Density function of the Uniform distribution.

Example:

arma::mat X = { {0.2,  0.7,  0.1},
                {0.9, -0.3,  1.3} };
stats::dunif(X,-1.0,3.0,false);

Parameters
  • X – a matrix of input values.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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> dunif(const BlazeMat<eT, To> &X, const T1 a_par, const T2 b_par, const bool log_form = false)

Density function of the Uniform distribution.

Example:

stats::dunif(X,-1.0,3.0,false);

Parameters
  • X – a matrix of input values.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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> dunif(const EigenMat<eT, iTr, iTc> &X, const T1 a_par, const T2 b_par, const bool log_form = false)

Density function of the Uniform distribution.

Example:

stats::dunif(X,-1.0,3.0,false);

Parameters
  • X – a matrix of input values.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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 Uniform distribution:

\[F(x; a, b) = \int_{a}^x f(z; a, b) dz = \frac{x - a}{b-a} \times \mathbf{1}[ a \leq x \leq b] + \times \mathbf{1}[x > b]\]

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> punif(const T1 x, const T2 a_par, const T3 b_par, const bool log_form = false) noexcept

Distribution function of the Uniform distribution.

Example:

stats::punif(0.5,-1.0,2.0,false); 

Parameters
  • x – a real-valued input.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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 T2, typename rT = common_return_t<eT, T1, T2>>
inline std::vector<rT> punif(const std::vector<eT> &x, const T1 a_par, const T2 b_par, const bool log_form = false)

Distribution function of the Uniform distribution.

Example:

std::vector<double> x = {0.3, 0.5, 0.9};
stats::punif(x,3.0,2.0,false);

Parameters
  • x – a standard vector.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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> punif(const ArmaMat<eT> &X, const T1 a_par, const T2 b_par, const bool log_form = false)

Distribution function of the Uniform distribution.

Example:

arma::mat X = { {0.2,  0.7,  0.1},
                {0.9, -0.3,  1.3} };
stats::punif(X,3.0,2.0,false);

Parameters
  • X – a matrix of input values.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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> punif(const BlazeMat<eT, To> &X, const T1 a_par, const T2 b_par, const bool log_form = false)

Distribution function of the Uniform distribution.

Example:

stats::punif(X,3.0,2.0,false);

Parameters
  • X – a matrix of input values.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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> punif(const EigenMat<eT, iTr, iTc> &X, const T1 a_par, const T2 b_par, const bool log_form = false)

Distribution function of the Uniform distribution.

Example:

stats::punif(X,3.0,2.0,false);

Parameters
  • X – a matrix of input values.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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 Uniform distribution:

\[q(p; a, b) = a + p(b-a)\]

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> qunif(const T1 p, const T2 a_par, const T3 b_par) noexcept

Quantile function of the Uniform distribution.

Example:

stats::qunif(0.5,-1.0,2.0); 

Parameters
  • p – a real-valued input.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound parameter, 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> qunif(const std::vector<eT> &x, const T1 a_par, const T2 b_par)

Quantile function of the Uniform distribution.

Example:

std::vector<double> x = {0.3, 0.5, 0.9};
stats::qunif(x,3.0,2.0);

Parameters
  • x – a standard vector.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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> qunif(const ArmaMat<eT> &X, const T1 a_par, const T2 b_par)

Quantile function of the Uniform distribution.

Example:

arma::mat X = { {0.2,  0.7,  0.1},
                {0.9,  0.3,  0.87} };
stats::qunif(X,3.0,2.0);

Parameters
  • X – a matrix of input values.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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> qunif(const BlazeMat<eT, To> &X, const T1 a_par, const T2 b_par)

Quantile function of the Uniform distribution.

Example:

stats::qunif(X,3.0,2.0);

Parameters
  • X – a matrix of input values.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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> qunif(const EigenMat<eT, iTr, iTc> &X, const T1 a_par, const T2 b_par)

Quantile function of the Uniform distribution.

Example:

stats::qunif(X,3.0,2.0);

Parameters
  • X – a matrix of input values.

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound parameter, a real-valued input.

Returns

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


Random Sampling

Random sampling for the Uniform distribution is achieved via the uniform_real_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> runif(const T1 a_par, const T2 b_par, rand_engine_t &engine)

Random sampling function for the Uniform distribution.

Example:

stats::rand_engine_t engine(1776);
stats::runif(3.0,2.0,engine);

Parameters
  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound parameter, a real-valued input.

  • engine – a random engine, passed by reference.

Returns

a pseudo-random draw from the Uniform distribution.

  1. Seed values

template<typename T1, typename T2>
inline common_return_t<T1, T2> runif(const T1 a_par, const T2 b_par, const ullint_t seed_val = std::random_device{}())

Random sampling function for the Uniform distribution.

Example:

stats::runif(3.0,2.0,1776);

Parameters
  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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 Uniform distribution.

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

Random sampling function for the Uniform distribution on the unit interval.

Example:

stats::runif();

Returns

a pseudo-random draw from the Uniform distribution.

Vector/Matrix Output

  1. Random number engines

template<typename mT, typename T1, typename T2>
inline mT runif(const ullint_t n, const ullint_t k, const T1 a_par, const T2 b_par, rand_engine_t &engine)

Random matrix sampling function for the Uniform distribution.

Example:

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

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound parameter, a real-valued input.

  • engine – a random engine, passed by reference.

Returns

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

  1. Seed values

template<typename mT, typename T1, typename T2>
inline mT runif(const ullint_t n, const ullint_t k, const T1 a_par, const T2 b_par, const ullint_t seed_val = std::random_device{}())

Random matrix sampling function for the Uniform distribution.

Example:

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

  • a_par – the lower bound parameter, a real-valued input.

  • b_par – the upper bound 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 Uniform distribution.