Statistics/Chi-squared distribution: Difference between revisions
Statistics/Chi-squared distribution (view source)
Revision as of 21:52, 19 December 2022
, 1 year ago→{{header|jq}}: simplify
(→{{header|jq}}: simplify) |
|||
Line 91:
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
<syntaxhighlight lang=sh>▼
| . + ((( ($x|f) + 4 * (($x + ($h/2))|f) + (($x + $h)|f)) / 6)) )▼
| . * $h;▼
end ;▼
if $a == 1 then 1 - ((-$x)|exp)▼
elif $x > $a * 1e4 then 1▼
.n *= 2▼
end ;▼
def Chi2_cdf($x; $k):▼
if $x == 0 then 0▼
def Chi2_pdf($x; $k):▼
else ((-$x/2)|exp) * pow($x; $k/2 -1) / (pow(2;$k/2) * (($k/2)|gamma))▼
</syntaxhighlight>▼
'''Formatting'''
<syntaxhighlight lang=
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
Line 158 ⟶ 110:
// .
end;
▲</syntaxhighlight>
'''Chi-squared pdf and cdf'''
▲def Chi2_pdf($x; $k):
▲ else # ((-$x/2)|exp) * pow($x; $k/2 -1) / (pow(2;$k/2) * (($k/2)|gamma))
▲
# $k is the degrees of freedom
# 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):
else 1e-15 as $tol # for example
| { s: 0, m: 0, term: $tol}
| until (.term|length < $tol;
# .term = (pow($x/2; .m) / (($k/2 + .m + 1)|gamma))
.term = (((.m * (($x/2)|log)) - (($k/2 + .m + 1)|lgamma)) | exp)
| .s * ( (-$x/2) + ($k/2)*(($x/2)|log)|exp)
</syntaxhighlight>
'''The Tasks'''
<syntaxhighlight lang=
def tables:
def r: round(4) | lpad(10);
Line 174 ⟶ 151:
| "\($x|lpad(2)) \($cdf|r)\(1-$cdf|r)"
);
def airport: [[77, 23], [88, 12], [79, 21], [81, 19]];
def expected: [81.25, 18.75];
Line 195 ⟶ 172:
{{output}}
<pre>
Values of the χ2 probability distribution function
x/k 1 2 3 4 5
0 0 0 0 0 0
Line 211 ⟶ 188:
Values for χ2 with 3 degrees of freedom
χ2 cum pdf p-value
1 0.
2 0.4276 0.5724
4 0.
8 0.9540 0.0460
16 0.9989 0.0011
32 1.0000
For airport data table:
diff sum : 4.512820512820513
d.o.f. : 3
χ2 value : 0.
p-value : 0.7889
</pre>
=={{header|Julia}}==
|