[Stable]

Chi-square distributions show up often in frequentist settings as the sampling distribution of test statistics, especially in maximum likelihood estimation settings.

dist_chisq(df, ncp = 0)

Arguments

df

Degrees of freedom (non-centrality parameter). Can be any positive real number.

ncp

Non-centrality parameter. Can be any non-negative real number. Defaults to 0 (central chi-squared distribution).

Details

We recommend reading this documentation on pkgdown which renders math nicely. https://pkg.mitchelloharawild.com/distributional/reference/dist_chisq.html

In the following, let \(X\) be a \(\chi^2\) random variable with df = \(k\) and ncp = \(\lambda\).

Support: \(R^+\), the set of positive real numbers

Mean: \(k + \lambda\)

Variance: \(2(k + 2\lambda)\)

Probability density function (p.d.f):

For the central chi-squared distribution (\(\lambda = 0\)):

$$ f(x) = \frac{1}{2^{k/2} \Gamma(k/2)} x^{k/2 - 1} e^{-x/2} $$

For the non-central chi-squared distribution (\(\lambda > 0\)):

$$ f(x) = \frac{1}{2} e^{-(x+\lambda)/2} \left(\frac{x}{\lambda}\right)^{k/4-1/2} I_{k/2-1}\left(\sqrt{\lambda x}\right) $$

where \(I_\nu(z)\) is the modified Bessel function of the first kind.

Cumulative distribution function (c.d.f):

For the central chi-squared distribution (\(\lambda = 0\)):

$$ F(x) = \frac{\gamma(k/2, x/2)}{\Gamma(k/2)} = P(k/2, x/2) $$

where \(\gamma(s, x)\) is the lower incomplete gamma function and \(P(s, x)\) is the regularized gamma function.

For the non-central chi-squared distribution (\(\lambda > 0\)):

$$ F(x) = \sum_{j=0}^{\infty} \frac{e^{-\lambda/2} (\lambda/2)^j}{j!} P(k/2 + j, x/2) $$

This is approximated numerically.

Moment generating function (m.g.f):

For the central chi-squared distribution (\(\lambda = 0\)):

$$ E(e^{tX}) = (1 - 2t)^{-k/2}, \quad t < 1/2 $$

For the non-central chi-squared distribution (\(\lambda > 0\)):

$$ E(e^{tX}) = \frac{e^{\lambda t / (1 - 2t)}}{(1 - 2t)^{k/2}}, \quad t < 1/2 $$

Skewness:

$$ \gamma_1 = \frac{2^{3/2}(k + 3\lambda)}{(k + 2\lambda)^{3/2}} $$

For the central case (\(\lambda = 0\)), this simplifies to \(\sqrt{8/k}\).

Excess Kurtosis:

$$ \gamma_2 = \frac{12(k + 4\lambda)}{(k + 2\lambda)^2} $$

For the central case (\(\lambda = 0\)), this simplifies to \(12/k\).

See also

Examples

dist <- dist_chisq(df = c(1,2,3,4,6,9))

dist
#> <distribution[6]>
#> [1] ᵪ²(1) ᵪ²(2) ᵪ²(3) ᵪ²(4) ᵪ²(6) ᵪ²(9)
mean(dist)
#> [1] 1 2 3 4 6 9
variance(dist)
#> [1]  2  4  6  8 12 18
skewness(dist)
#> [1] 2.828427 2.000000 1.632993 1.414214 1.154701 0.942809
kurtosis(dist)
#> [1] 12.000000  6.000000  4.000000  3.000000  2.000000  1.333333

generate(dist, 10)
#> [[1]]
#>  [1] 2.74111573 1.49296878 0.31240827 3.16334044 0.62391198 0.43243997
#>  [7] 0.77595564 0.05184544 0.19436561 0.04336552
#> 
#> [[2]]
#>  [1] 2.42126767 0.01032491 1.00804265 0.29338495 1.67375460 2.23805805
#>  [7] 1.08482574 3.90738319 0.08643692 1.33958793
#> 
#> [[3]]
#>  [1] 0.9182044 3.6686716 0.1109399 1.0783473 3.7730830 9.9781000 2.5513203
#>  [8] 5.1439292 1.4973387 0.6841993
#> 
#> [[4]]
#>  [1] 12.0680911  4.5565400  0.8961778  1.3136361  2.6503396  1.7013860
#>  [7]  0.1334359  0.5162994  1.6414160 12.5224584
#> 
#> [[5]]
#>  [1]  4.974866  4.634239  3.931018 12.700909 14.156703  7.496162  7.719546
#>  [8]  4.072847  4.662391  6.940263
#> 
#> [[6]]
#>  [1]  9.341929  9.533922 13.157604  4.677217 12.698442  9.926195  1.312321
#>  [8]  7.137607  2.606337 15.704691
#> 

density(dist, 2)
#> [1] 0.10377687 0.18393972 0.20755375 0.18393972 0.09196986 0.01581362
density(dist, 2, log = TRUE)
#> [1] -2.265512 -1.693147 -1.572365 -1.693147 -2.386294 -4.146884

cdf(dist, 4)
#> [1] 0.95449974 0.86466472 0.73853587 0.59399415 0.32332358 0.08858747

quantile(dist, 0.7)
#> [1]  1.074194  2.407946  3.664871  4.878433  7.231135 10.656372