Verify distribution uniformity/Chi-squared test: Difference between revisions

Content added Content deleted
m (spacing)
(→‎{{header|jq}}: use recursion formula for gamma)
Line 742: Line 742:
enough for the χ2 distribution to be used.
enough for the χ2 distribution to be used.


The implementation of `Chi2_cdf` below is intended to be fairly
The implementation of `Chi2_cdf` here uses the recursion relation for
simple and robust rather than highly accurate. For an
the gamma function and should be both fast, accurate and quite robust.
industrial-strength algorithm, see
For an industrial-strength algorithm, see
e.g. https://people.sc.fsu.edu/~jburkardt/c_src/asa239/asa239.c
e.g. https://people.sc.fsu.edu/~jburkardt/c_src/asa239/asa239.c


Line 760: Line 760:
# Cumulative density function of the chi-squared distribution with $k
# Cumulative density function of the chi-squared distribution with $k
# degrees of freedom
# degrees of freedom
# The recursion formula for gamma is used for efficiency and robustness.
# Use lgamma to avoid $x^m (for large $x and large m) and
# to avoid calling gamma for large $k
def Chi2_cdf($x; $k):
def Chi2_cdf($x; $k):
if $x == 0 then 0
if $x == 0 then 0
elif $x > (1e3 * $k) then 1
elif $x > (1e3 * $k) then 1
else 1e-15 as $tol # for example
else 1e-15 as $tol # for example
| { s: 0, m: 0, term: $tol}
| { s: 0, m: 0, term: (1 / ((($k/2)+1)|gamma)) }
| until (.term|length < $tol; # length here is abs
| until (.term|length < $tol; # length here is abs
.s += .term
# .term = (pow($x/2; .m) / (($k/2 + .m + 1)|gamma))
| .m += 1
.term = (((.m * (($x/2)|log)) - (($k/2 + .m + 1)|lgamma)) | exp)
| .s += .term
| .term *= (($x/2) / (($k/2) + .m )) )
| .s * ( ((-$x/2) + ($k/2)*(($x/2)|log)) | exp)
| .m += 1)
| .s * ( (-$x/2) + ($k/2)*(($x/2)|log)|exp)
end ;
end ;
</syntaxhighlight>
</syntaxhighlight>
Line 824: Line 822:
task
task
</syntaxhighlight>
</syntaxhighlight>
{{output}}
<pre>
<pre>
Dataset: [199809,200665,199607,200270,199649]
Dataset: [199809,200665,199607,200270,199649]