Statistics/Chi-squared distribution: Difference between revisions

m
correct formula
No edit summary
m (correct formula)
Line 31:
The lower incomplete gamma function can be calculated as
 
<math display="block"> x^s \, \Gamma(s) \, e^{-x} \sum_{m=0}^\infty\frac{x^m}{\Gamma(s+m+1)} </math>
 
and so, for the chi-squared cumulative probability distribution withand substituting chi-squaredsquare k, weinto have,s substitutingas k/2 and chi-squaresquared kx into sx as kx / 2,
 
: <math display="block"> F(x;\,k) = \frac{1}{\Gamma(kx/2)} x^{(k/2)} \, \Gamma(k/2) \, e^{-x/2} \sum_{m=0}^\infty\frac{(x/2)^m}{\Gamma(\frac{k}{2}+m+1)}. </math>
 
In practice, thisBecause series formulaformulas ismay oftenbe subject to accumulated errors from rounding in the frequently used region where x and k are under 10 and near one another., Youyou may therefore instead use a mathematics function library, if available for your programming task, to calculate gamma and incomplete gamma.
<br />
<br />
Line 96:
end
 
""" gamma CDF from lower incomplete gamma function,by usingseries MPFRformula library to get upper incompletewith gamma """
function gamma_cdf(ak, x)
return x^k * exp(-x) * sum(x^m / gamma(k + m + 1) for m in 0:100)
z, biga, bigx = BigFloat(), BigFloat(a), BigFloat(x)
end
ccall((:mpfr_gamma_inc, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Ref{BigFloat}, Int32), z, biga, bigx, 0)
 
return Float64(1 - z / gamma(a))
""" Cumulative probability function (cdf) for chi-squared """
function cdf_χ2(x, k)
return x <= 0 || k <= 0 ? 0.0 : gamma_cdf(k / 2, x / 2)
end
 
4,107

edits