Statistics/Chi-squared distribution: Difference between revisions
Statistics/Chi-squared distribution (view source)
Revision as of 20:52, 20 December 2022
, 1 year ago→{{header|jq}}: use recursion formula for gamma
(→{{header|jq}}: simplify) |
(→{{header|jq}}: use recursion formula for gamma) |
||
Line 91:
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
The implementation of Chi_cdf here uses the recusive relation for the
gamma function for efficiency and to simplify the avoidance of
numerical overflow issues.
'''Formatting'''
Line 120 ⟶ 124:
# $k is the degrees of freedom
# Use
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 / ((($
| until (.term|length < $tol;
| .m += 1
.term = (((.m * (($x/2)|log)) - (($k/2 + .m + 1)|lgamma)) | exp)▼
| .
▲ | .m += 1)
end ;
</syntaxhighlight>
'''The Tasks'''
<syntaxhighlight lang=jq>
def tables:
def r: round(
" Values of the χ2 probability distribution function",
" x/k 1 2 3 4 5",
Line 168 ⟶ 169:
tables,
airport(airport; expected)
</syntaxhighlight>
{{output}}
<pre>
Values of the χ2 probability distribution function
x/k 1 2 3 4 5
0 0 0 0 0 0
1
2
3
4
5
6
7
8
9
10
Values for χ2 with 3 degrees of freedom
χ2 cum pdf p-value
1
2
4
8
16
32 0.999999
For airport data table:
diff sum : 4.512820512820513
d.o.f. : 3
χ2 value : 0.
p-value : 0.7889
</pre>
|