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

→‎{{header|jq}}: use recursion formula for gamma
m (spacing)
(→‎{{header|jq}}: use recursion formula for gamma)
Line 742:
enough for the χ2 distribution to be used.
 
The implementation of `Chi2_cdf` belowhere isuses intendedthe torecursion berelation fairlyfor
simplethe gamma function and robustshould ratherbe thanboth highlyfast, accurate. and Forquite anrobust.
For an industrial-strength algorithm, see
e.g. https://people.sc.fsu.edu/~jburkardt/c_src/asa239/asa239.c
 
Line 760:
# Cumulative density function of the chi-squared distribution with $k
# 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):
if $x == 0 then 0
elif $x > (1e3 * $k) then 1
else 1e-15 as $tol # for example
| { s: 0, m: 0, term: (1 / ((($tolk/2)+1)|gamma)) }
| until (.term|length < $tol; # length here is abs
| .ms += 1).term
# .term = (pow($x/2; .m) / (($k/2 + .m + 1)|gamma))
| .m += 1
.term = (((.m * (($x/2)|log)) - (($k/2 + .m + 1)|lgamma)) | exp)
| .sterm +*= (($x/2) / (($k/2) + .termm )) )
| .terms =* (((.m * ((-$x/2)|log)) -+ (($k/2 + .m + 1)*(($x/2)|lgammalog)) | exp)
| .m += 1)
| .s * ( (-$x/2) + ($k/2)*(($x/2)|log)|exp)
end ;
</syntaxhighlight>
Line 824 ⟶ 822:
task
</syntaxhighlight>
{{output}}
<pre>
Dataset: [199809,200665,199607,200270,199649]
2,442

edits