Chi-squared Distribution

Table of contents


Density Function

The density function of the Chi-squared distribution:

\[f(x; k) = \dfrac{x^{k/2 - 1} \exp(-x/2)}{ 2^{k/2} \Gamma(k/2)} \times \mathbf{1}[ x \geq 0]\]

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> dchisq(const T1 x, const T2 dof_par, const bool log_form = false) noexcept

Density function of the Chi-squared distribution.

Example:

stats::dchisq(4,5,false); 

Parameters
  • x – a real-valued input.

  • dof_par – the degrees of freedom 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> dchisq(const std::vector<eT> &x, const T1 dof_par, const bool log_form = false)

Density function of the Chi-squared distribution.

Example:

std::vector<double> x = {1.8, 0.7, 4.2};
stats::dchisq(x,4,false);

Parameters
  • x – a standard vector.

  • dof_par – the degrees of freedom 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> dchisq(const ArmaMat<eT> &X, const T1 dof_par, const bool log_form = false)

Density function of the Chi-squared distribution.

Example:

arma::mat X = { {1.8, 0.7, 4.2},
                {0.3, 5.3, 3.7} };
stats::dchisq(X,4,false);

Parameters
  • X – a matrix of input values.

  • dof_par – the degrees of freedom 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> dchisq(const BlazeMat<eT, To> &X, const T1 dof_par, const bool log_form = false)

Density function of the Chi-squared distribution.

Example:

stats::dchisq(X,4,false);

Parameters
  • X – a matrix of input values.

  • dof_par – the degrees of freedom 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> dchisq(const EigenMat<eT, iTr, iTc> &X, const T1 dof_par, const bool log_form = false)

Density function of the Chi-squared distribution.

Example:

stats::dchisq(X,4,false);

Parameters
  • X – a matrix of input values.

  • dof_par – the degrees of freedom 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 Chi-squared distribution:

\[F(x; k) = \int_0^x f(z; k) dz = \frac{\gamma(k/2,x/2)}{\Gamma (k/2)}\]

where \(\Gamma(\cdot)\) denotes the gamma function and \(\gamma(\cdot, \cdot)\) denotes the incomplete gamma function.

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> pchisq(const T1 x, const T2 dof_par, const bool log_form = false) noexcept

Distribution function of the Chi-squared distribution.

Example:

stats::pchisq(4,5,false); 

Parameters
  • x – a real-valued input.

  • dof_par – the degrees of freedom 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> pchisq(const std::vector<eT> &x, const T1 dof_par, const bool log_form = false)

Distribution function of the Chi-squared distribution.

Example:

std::vector<double> x = {1.8, 0.7, 4.2};
stats::pchisq(x,4,false);

Parameters
  • x – a standard vector.

  • dof_par – the degrees of freedom 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> pchisq(const ArmaMat<eT> &X, const T1 dof_par, const bool log_form = false)

Distribution function of the Chi-squared distribution.

Example:

arma::mat X = { {1.8, 0.7, 4.2},
                {0.3, 5.3, 3.7} };
stats::pchisq(X,4,false);

Parameters
  • X – a matrix of input values.

  • dof_par – the degrees of freedom 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> pchisq(const BlazeMat<eT, To> &X, const T1 dof_par, const bool log_form = false)

Distribution function of the Chi-squared distribution.

Example:

stats::pchisq(X,4,false);

Parameters
  • X – a matrix of input values.

  • dof_par – the degrees of freedom 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> pchisq(const EigenMat<eT, iTr, iTc> &X, const T1 dof_par, const bool log_form = false)

Distribution function of the Chi-squared distribution.

Example:

stats::pchisq(X,4,false);

Parameters
  • X – a matrix of input values.

  • dof_par – the degrees of freedom 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 Chi-squared distribution:

\[q(p; k) = \inf \left\{ x : p \leq \gamma(k/2,x/2) / \Gamma (k/2) \right\}\]

where \(\Gamma(\cdot)\) denotes the gamma function and \(\gamma(\cdot, \cdot)\) denotes the incomplete gamma function.

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> qchisq(const T1 p, const T2 dof_par) noexcept

Quantile function of the Chi-squared distribution.

Example:

stats::qchisq(0.5,5); 

Parameters
  • p – a real-valued input.

  • dof_par – the degrees of freedom parameter, a real-valued input.

Returns

the quantile 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> qchisq(const std::vector<eT> &x, const T1 dof_par)

Quantile function of the Chi-squared distribution.

Example:

std::vector<double> x = {0.3, 0.5, 0.8};
stats::qchisq(x,4);

Parameters
  • x – a standard vector.

  • dof_par – the degrees of freedom 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> qchisq(const ArmaMat<eT> &X, const T1 dof_par)

Quantile function of the Chi-squared distribution.

Example:

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

Parameters
  • X – a matrix of input values.

  • dof_par – the degrees of freedom 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> qchisq(const BlazeMat<eT, To> &X, const T1 dof_par)

Quantile function of the Chi-squared distribution.

Example:

stats::qchisq(X,4);

Parameters
  • X – a matrix of input values.

  • dof_par – the degrees of freedom 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> qchisq(const EigenMat<eT, iTr, iTc> &X, const T1 dof_par)

Quantile function of the Chi-squared distribution.

Example:

stats::qchisq(X,4);

Parameters
  • X – a matrix of input values.

  • dof_par – the degrees of freedom parameter, a real-valued input.

Returns

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


Random Sampling

Scalar Output

  1. Random number engines

template<typename T>
inline return_t<T> rchisq(const T dof_par, rand_engine_t &engine)

Random sampling function for the Chi-squared distribution.

Example:

stats::rand_engine_t engine(1776);
stats::rchisq(4,engine);

Parameters
  • dof_par – the degrees of freedom parameter, a real-valued input.

  • engine – a random engine, passed by reference.

Returns

a pseudo-random draw from the Chi-squared distribution.

  1. Seed values

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

Random sampling function for the Chi-squared distribution.

Example:

stats::rchisq(4,1776);

Parameters
  • dof_par – the degrees of freedom 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 Chi-squared distribution.

Vector/Matrix Output

  1. Random number engines

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

Random matrix sampling function for the Chi-squared distribution.

Example:

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

  • dof_par – the degrees of freedom parameter, a real-valued input.

  • engine – a random engine, passed by reference.

Returns

a matrix of pseudo-random draws from the Chi-squared distribution.

  1. Seed values

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

Random matrix sampling function for the Chi-squared distribution.

Example:

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

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

  • dof_par – the degrees of freedom 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 Chi-squared distribution.