Introduction

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 > 0] $$

Parameters:

  • dof_par is $k$. Note: this need not be an integer.

Density


Definition:

 
template<typename Ta, typename Tb>
statslib_constexpr
return_t<Ta> dchisq(const Ta x, const Tb dof_par, const bool log_form = false);

Computes the density function.


Examples:

// parameters
double dof = 14.0;

// standard input
double dens_val = stats::dchisq(0.5,dof);
double log_dens_val = stats::dchisq(0.5,dof,true);

// Armadillo input
arma::mat X(10,1);
X.fill(0.5);

arma::mat dens_vals_mat = stats::dchisq(X,dof);
arma::mat log_dens_vals_mat = stats::dchisq(X,dof,true);

Probability


Definition:

 
template<typename Ta, typename Tb>
statslib_constexpr
return_t<Ta> pchisq(const Ta x, const Tb dof_par, const bool log_form = false);

Computes the cumulative distribution function (CDF).


Examples:

// parameters
double dof = 14.0;

// standard input
double prob_val = stats::pchisq(0.5,dof);
double log_prob_val = stats::pchisq(0.5,dof,true);

// Armadillo input
arma::mat X(10,1);
X.fill(0.5);

arma::mat prob_vals_mat = stats::pchisq(X,dof);
arma::mat log_prob_vals_mat = stats::pchisq(X,dof,true);

Quantile


Definition:

 
template<typename Ta, typename Tb>
statslib_constexpr
Ta qchisq(const Ta p, const Tb dof_par);

Computes the quantile function.


Examples:

// parameters
double dof = 14.0;

// standard input
double quant_val = stats::qchisq(0.7,dof);

// Armadillo input
arma::mat X(10,1);
X.fill(0.7);

arma::mat quant_vals_mat = stats::qchisq(X,dof);

Random Sampling


Definition:

 
// random engine seeding
template<typename T>
statslib_inline
return_t<T> rchisq(const T dof_par, rand_engine_t& engine);

// seeding values
template<typename T>
statslib_inline
return_t<T> rchisq(const T dof_par, uint_t seed_val = std::random_device{}());

// matrix output
template<typename mT, typename eT>
statslib_inline
mT rchisq(const uint_t n, const uint_t k, const eT dof_par);

Generates pseudo-random draws.


Examples:

// parameters
double dof = 14.0;

// standard input
double rand_val = stats::rchisq(dof);

// Armadillo output: 10 x 1 matrix
arma::mat rand_mat = stats::rchisq<arma::mat>(10,1,dof);