Permutation test: Difference between revisions

m (→‎{{header|REXX}}: added more whitespace, changed some comments.)
(→‎{{header|jq}}: normalize)
Line 571:
def permutationTest(a; b):
 
def normalize(a;b): # mainly to avoid having to compute $sumab
def statistic(ab; a): # avg(a) - avg(b) (assuming ab==a+b)
(ab a| add) as $sumabsa
| (a b| add) as $sumasb
| (($sumasa /+ $sb)/((a|length) + (b|length))) -as $avg
| [(a | map(($sumab .- $sumaavg)), /(b ((ab|length) map(.- (a|length)$avg))];
 
def statistic(ab; a): # avg(a) - avg(b) (assuming ab==a+b and avg(ab) is 0)
def statistic(ab; a):
(a | add) as $suma
# (ab|add) should be 0, by normalization
| ($suma / (a|length)) +
($suma / ((ab|length) - (a|length)));
normalize(a;b)
(a + b) as $ab # pooled observations
| statistic($ab; a + b) as $t_observedab # observed difference in means # pooled observations
| .[0] as $a | .[1] as $b
| reduce ($ab|combination(a|length)) as $perm # for each combination...
| statistic([0,0]$ab; $a) as $t_observed # observed difference in # state: [under,count]means
| reduce ($ab|combination($a|length)) as $perm # for each combination...
(a[0,0]; + b) as $ab # pooledstate: observations[under,count]
if statistic($ab; $perm) <= $t_observed then .[0] += 1 else . end
| .[1] += 1 )
Line 590 ⟶ 599:
 
permutationTest(treatmentGroup; controlGroup) as $under
| "% under=\($under)", "% over=\(100 - $under)"</lang>
{{out}}
$ jq -n -r -f permutation_test.jq
% under=87.1971681569204814304271579813
% over=12.802831843079517856957284201869
 
=={{header|Mathematica}}==
2,479

edits