Verify distribution uniformity/Chi-squared test: Difference between revisions
Verify distribution uniformity/Chi-squared test (view source)
Revision as of 22:26, 19 December 2022
, 1 year ago→{{header|jq}}: simplify
(→{{header|jq}}: simplify) |
|||
Line 741:
enough for the χ2 distribution to be used.
The implementation of `
simple and robust rather than
industrial-strength algorithm, see
e.g. https://people.sc.fsu.edu/~jburkardt/c_src/asa239/asa239.c
'''Generic Functions'''
<syntaxhighlight lang=jq>
Line 756 ⟶ 755:
# sum of squares
def ss(s): reduce s as $x (0; . + ($x * $x));
def integrate($a; $b; $n; f):▼
| . + ((( ($x|f) + 4 * (($x + ($h/2))|f) + (($x + $h)|f)) / 6)) )▼
| . * $h;▼
(($a-$b)|length) < 1e-10; # length here is abs▼
def f0: (if . == 0 then 1 else pow(.; $a-1) end) * ((- .) |exp) ;▼
end ;▼
if $a == 1 then 1 - ((-$x)|exp)▼
elif $x > $a * 1e4 then 1▼
.n *= 2▼
# Cumulative density function of the chi-squared distribution with $k
# degrees of freedom
# Use lgamma to avoid $x^m (for large $x and large m) and
def cdf($x; $k):▼
# to avoid calling gamma for large $k
else 1e-15 as $tol # for example
| { s: 0, m: 0, term: $tol}
# .term = (pow($x/2; .m) / (($k/2 + .m + 1)|gamma))
| .s * ( (-$x/2) + ($k/2)*(($x/2)|log)|exp)
</syntaxhighlight>▼
<syntaxhighlight lang=jq>▼
# Input: array of frequencies
def chi2UniformDistance:
Line 807 ⟶ 785:
# of freedom
def chi2Probability($dof):
(1 -
| if . < 1e-10 then "< 1e-10"
else .
end;
▲</syntaxhighlight>
▲'''The Tasks
▲<syntaxhighlight lang=jq>
# Input: array of frequencies
# Output: result of a two-tailed test based on the chi-squared statistic
Line 820 ⟶ 796:
(length - 1) as $dof
| chi2UniformDistance
|
| if $cdf
then ($significance/2) as $s
Line 827 ⟶ 803:
end;
def
[199809, 200665, 199607, 200270, 199649],
[522573, 244456, 139979, 71531, 21461],
Line 835 ⟶ 811:
];
def
| "Dataset: \(.)",
( chi2UniformDistance as $dist
Line 842 ⟶ 818:
| "DOF: \($dof) D (Distance): \($dist)",
" Estimated probability of observing a value >= D: \($dist|chi2Probability($dof)|round(2))",
" Uniform? \( (select(chiIsUniform(0.05)) | "Yes") // "No" )\n" ) ;
def task0($a):
task
</syntaxhighlight>
<pre>
Dataset: [199809,200665,199607,200270,199649]
Line 861 ⟶ 839:
Dataset: [19,14,6,18,7,5,1]
DOF: 6 D (Distance): 29.2
Estimated probability of observing a value >= D:
Uniform? No
Line 874 ⟶ 852:
Uniform? No
</pre>
=={{header|Julia}}==
|