Permutation test: Difference between revisions
m
→{{header|Wren}}: Minor tidy
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
m (→{{header|Wren}}: Minor tidy) |
||
(11 intermediate revisions by 10 users not shown) | |||
Line 9:
was measured and reported in this table.
:::::::{| style="text-align: left; width:
|+ Table of experimental results
|- style="background-color: rgb(255, 204, 255);"
Line 44:
* Report the percentage of alternative groupings for which the difference in means is less or equal to the actual experimentally observed difference in means, and the percentage for which it is greater.
* Note that they should sum to 100%.
Extremely dissimilar values are evidence of an effect not entirely due
Line 51 ⟶ 52:
that's easier than loading them at run time. Test your solution on the
data given above.
<br><br>
=={{header|11l}}==
{{trans|Kotlin}}
<syntaxhighlight lang="11l">V data = [85, 88, 75, 66, 25, 29, 83, 39, 97,
68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
F pick(at, remain, accu, treat)
I remain == 0
R I accu > treat {1} E 0
R pick(at - 1, remain - 1, accu + :data[at - 1], treat) +
(I at > remain {pick(at - 1, remain, accu, treat)} E 0)
V treat = 0
V total = 1.0
L(i) 0..8
treat += data[i]
L(i) (19..11).step(-1)
total *= i
L(i) (9..1).step(-1)
total /= i
V gt = pick(19, 9, 0, treat)
V le = Int(total - gt)
print(‘<= : #.6% #.’.format(100 * le / total, le))
print(‘ > : #.6% #.’.format(100 * gt / total, gt))</syntaxhighlight>
{{out}}
<pre>
<= : 87.197168% 80551
> : 12.802832% 11827
</pre>
=={{header|Ada}}==
<
procedure Permutation_Test is
Line 115 ⟶ 150:
New_Line;
end;
end Permutation_Test;</
This solution uses an auxiliary package Iterate_Subsets. Here is the Spec:
<
Subset_Size, More_Elements: Positive;
package Iterate_Subsets is
Line 134 ⟶ 169:
end Iterate_Subsets;
</syntaxhighlight>
And here is the implementation:
<
function First return Subset is
Line 171 ⟶ 206:
end Last;
end Iterate_Subsets;</
{{out}}
Line 179 ⟶ 214:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PERMUTATION_TEST.AWK
# converted from C
Line 205 ⟶ 240:
return pick(at-1,remain-1,accu+data[at-1],treat) + ( (at > remain) ? pick(at-1,remain,accu,treat) : 0 )
}
</syntaxhighlight>
{{out}}
<pre>
Line 214 ⟶ 249:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
nplacebo% = 10
DIM results%(ntreated% + nplacebo% - 1)
Line 251 ⟶ 286:
N% = (N% + (N% >>> 4)) AND &0F0F0F0F
N% += N% >>> 8 : N% += N% >>> 16
= N% AND &7F</
{{out}}
<pre>
Line 259 ⟶ 294:
=={{header|C}}==
<
int data[] = { 85, 88, 75, 66, 25, 29, 83, 39, 97,
Line 287 ⟶ 322:
100 * le / total, le, 100 * gt / total, gt);
return 0;
}</
Output:<syntaxhighlight lang="text"><= : 87.197168% 80551
> : 12.802832% 11827</
=={{header|C sharp|C#}}==
{{trans|Java}}
<
using System.Collections.Generic;
Line 329 ⟶ 364:
}
}
}</
{{out}}
<pre><= 87.1971681569205% 80551
Line 336 ⟶ 371:
=={{header|C++}}==
This is a translaion of C
<
#include<vector>
#include<numeric>
Line 374 ⟶ 409:
std::cout << "<= : " << 100.0 * lesser / total << "% " << lesser << std::endl
<< " > : " << 100.0 * greater / total << "% " << greater << std::endl;
}</
Output:<syntaxhighlight lang="text"><= : 87.197168% 80551
> : 12.802832% 11827</
=={{header|Common Lisp}}==
<
(let ((more 0) (leq 0)
(all-data (append s1 s2))
Line 402 ⟶ 437:
(format t "<=: ~a ~6f%~% >: ~a ~6f%~%"
x (* 100e0 (/ x s))
y (* 100e0 (/ y s))))</
>: 11827 12.803%</
=={{header|D}}==
<
auto permutationTest(T)(in T[] a, in T[] b) pure nothrow @safe {
Line 420 ⟶ 455:
immutable under = permutationTest(treatmentGroup, controlGroup);
writefln("Under =%6.2f%%\nOver =%6.2f%%", under, 100.0 - under);
}</
{{out}}
<pre>Under = 87.20%
Line 427 ⟶ 462:
Alternative version:
{{trans|C}}
<
import std.stdio, std.algorithm, std.range;
Line 450 ⟶ 485:
writefln(" > : %2.2f%% %d", 100.0 * gt / t, gt);
writefln("<= : %2.2f%% %d", 100.0 * le / t, le);
}</
{{out}}
<pre> > : 12.80% 11827
<= : 87.20% 80551</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Permutation_test;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
procedure Comb(n, m: Integer; emit: TProc<TArray<Integer>>);
var
s: TArray<Integer>;
last: Integer;
procedure rc(i, next: Integer);
begin
for var j := next to n - 1 do
begin
s[i] := j;
if i = last then
emit(s)
else
rc(i + 1, j + 1);
end;
end;
begin
SetLength(s, m);
last := m - 1;
rc(0, 0);
end;
begin
var tr: TArray<Integer> := [85, 88, 75, 66, 25, 29, 83, 39, 97];
var ct: TArray<Integer> := [68, 41, 10, 49, 16, 65, 32, 92, 28, 98];
// collect all results in a single list
var all: TArray<Integer> := concat(tr, ct);
// compute sum of all data, useful as intermediate result
var sumAll := 0;
for var r in all do
inc(sumAll, r);
// closure for computing scaled difference.
// compute results scaled by len(tr)*len(ct).
// this allows all math to be done in integers.
var sd :=
function(trc: TArray<Integer>): Integer
begin
var sumTr := 0;
for var x in trc do
inc(sumTr, all[x]);
result := sumTr * length(ct) - (sumAll - sumTr) * length(tr);
end;
// compute observed difference, as an intermediate result
var a: TArray<Integer>;
SetLength(a, length(tr));
for var i := 0 to High(a) do
a[i] := i;
var sdObs := sd(a);
// iterate over all combinations. for each, compute (scaled)
// difference and tally whether leq or gt observed difference.
var nLe, nGt: Integer;
comb(length(all), length(tr),
procedure(c: TArray<Integer>)
begin
if sd(c) > sdObs then
inc(nGt)
else
inc(nle);
end);
// print results as percentage
var pc := 100 / (nLe + nGt);
writeln(format('differences <= observed: %f%%', [nle * pc]));
writeln(format('differences > observed: %f%%', [ngt * pc]));
{$IFNDEF UNIX} readln; {$ENDIF}
end.</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Ruby}}
<
def statistic(ab, a) do
sumab = Enum.sum(ab)
Line 484 ⟶ 605:
controlGroup = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
under = Permutation.test(treatmentGroup, controlGroup)
:io.fwrite "under = ~.2f%, over = ~.2f%~n", [under, 100-under]</
{{out}}
Line 492 ⟶ 613:
=={{header|GAP}}==
<
b := [68, 41, 10, 49, 16, 65, 32, 92, 28, 98];
Line 542 ⟶ 663:
# in order, % less or greater than original diff
PermTest(a, b);
[ "87.197", "12.802" ]</
=={{header|FreeBASIC}}==
{{trans|Phix}}
<syntaxhighlight lang="freebasic">
Dim Shared datos(18) As Integer => {85, 88, 75, 66, 25, 29, 83, 39, 97,_
68, 41, 10, 49, 16, 65, 32, 92, 28, 98}
Function pick(at As Integer, remain As Integer, accu As Integer, treat As Integer) As Integer
If remain = 0 Then
If accu > treat Then Return 1 Else Return 0
End If
Dim a As Integer
If at > remain Then a = pick(at-1, remain, accu, treat) Else a = 0
Return pick(at-1, remain-1, accu+datos(at), treat) + a
End Function
Dim As Integer treat = 0, le, gt, total = 1, i
For i = 1 To 9
treat += datos(i)
Next i
For i = 19 To 11 Step -1
total *= i
Next i
For i = 9 To 1 Step -1
total /= i
Next i
gt = pick(19, 9, 0, treat)
le = total - gt
Print Using "<= : ##.######% #####"; 100*le/total; le
Print Using " > : ##.######% #####"; 100*gt/total; gt
Sleep
</syntaxhighlight>
{{out}}
<pre>
<= : 92.076035% 85058
> : 7.923965% 7320
</pre>
=={{header|Go}}==
A version doing all math in integers until computing final percentages.
<
import "fmt"
Line 617 ⟶ 781:
}
rc(0, 0)
}</
{{out}}
<pre>
Line 625 ⟶ 789:
=={{header|Haskell}}==
<
where f = scanl (*) 1 [1..]
Line 645 ⟶ 809:
[68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
in do putStr "> : "; print r
putStr "<=: "; print $ 100 - r</
{{out}}
<pre>
Line 653 ⟶ 817:
Somewhat faster, this goes from top down:
<
where f = scanl (*) 1 [1..]
Line 671 ⟶ 835:
(a, b) = perms [85, 88, 75, 66, 25, 29, 83, 39, 97]
[68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
lt = (fromIntegral a) / (fromIntegral b) * 100</
In cases where the sample data are a large number of relatively small positive integers, counting number of partial sums is a lot faster:
<
f a n = merge a (map (addNum n) $ filter (\(l,_,_) -> l < len) a)
addNum n (a,s,c)
Line 699 ⟶ 863:
-- how many combinations are less than current sum
main = print$ permtest [85, 88, 75, 66, 25, 29, 83, 39, 97]
[68, 41, 10, 49, 16, 65, 32, 92, 28, 98]</
=={{header|J}}==
<
trmt=: 0.85 0.88 0.75 0.66 0.25 0.29 0.83 0.39 0.97
ctrl=: 0.68 0.41 0.1 0.49 0.16 0.65 0.32 0.92 0.28 0.98
Line 709 ⟶ 873:
all=: trmt(#@[ ({. difm }.) |:@([ (comb ~.@,"1 i.@])&# ,) { ,) ctrl
smoutput 'under: ','%',~":100*mean all <: result
smoutput 'over: ','%',~":100*mean all > result</
Result:
<syntaxhighlight lang="text">under: 87.1972%
over: 12.8028%</
=={{header|Java}}==
{{trans|Kotlin}}
<
private static final int[] data = new int[]{
85, 88, 75, 66, 25, 29, 83, 39, 97,
Line 746 ⟶ 910:
System.out.printf(" > : %f%% %d\n", 100.0 * gt / total, gt);
}
}</
{{out}}
<pre><= : 87.197168% 80551
Line 754 ⟶ 918:
{{works with|jq|1.4}}
'''Part 1: Combinations'''
<
def combination(r):
if r > length or r < 0 then empty
Line 761 ⟶ 925:
( .[1:]|combination(r))
end;
</syntaxhighlight>
'''Part 2: Permutation Test'''
<
def permutationTest(a; b):
Line 788 ⟶ 952:
| .[1] += 1 )
| .[0] * 100.0 / .[1] # under/count
;</
'''Example:'''
<
def controlGroup: [68, 41, 10, 49, 16, 65, 32, 92, 28, 98];
permutationTest(treatmentGroup; controlGroup) as $under
| "% under=\($under)", "% over=\(100 - $under)"</
{{out}}
$ jq -n -r -f permutation_test.jq
Line 805 ⟶ 969:
'''Functions'''
<
meandiff(a::Vector{T}, b::Vector{T}) where T <: Real = mean(a) - mean(b)
Line 832 ⟶ 996:
end
return better, worse
end</
'''Main'''
<
const control = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
Line 847 ⟶ 1,011:
println("\nThere are $tot different permuted groups of these data.")
@printf("%8d, %5.2f%% showed better than actual results.\n", better, 100 * better / tot)
print(@sprintf("%8d, %5.2f%% showed equalivalent or worse results.", worse, 100 * worse / tot))</
{{out}}
Line 860 ⟶ 1,024:
=={{header|Kotlin}}==
{{trans|C}}
<
val data = intArrayOf(
Line 883 ⟶ 1,047:
System.out.printf("<= : %f%% %d\n", 100.0 * le / total, le)
System.out.printf(" > : %f%% %d\n", 100.0 * gt / total, gt)
}</
{{out}}
Line 893 ⟶ 1,057:
=={{header|M2000 Interpreter}}==
{{trans|C}}
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
Global data(), treat=0
Line 911 ⟶ 1,075:
}
Checkit
</syntaxhighlight>
{{out}}
<pre><= : 87.2% 80551
Line 919 ⟶ 1,083:
Slower version, using a lambda function with a series of inner lambda functions to return each combination at a time.
<syntaxhighlight lang="m2000 interpreter">
Module CheckThis {
Function CombinationsStep (a, nn) {
Line 964 ⟶ 1,128:
}
CheckThis
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
ToString[#2 - #1] <> " " <> ToString[100. (1 - #1/#2)] <> "%" &[
Count[Total /@ Subsets[Join[#1, #2], {Length@#1}],
n_ /; n <= Total@#1],
Binomial[Length@#1 + Length@#2, Length@#1]] &[{85, 88, 75, 66, 25,
29, 83, 39, 97}, {68, 41, 10, 49, 16, 65, 32, 92, 28, 98}]</
{{out}}
<pre><=: 80551 87.1972%
Line 979 ⟶ 1,143:
=={{header|Nim}}==
{{trans|C}}
<
const data = [85, 88, 75, 66, 25,
Line 1,006 ⟶ 1,170:
le = int(total - float(gt))
echo fmt"<= : {100.0 * float(le) / total:.6f}% {le}"
echo fmt" > : {100.0 * float(gt) / total:.6f}% {gt}"</
{{out}}
<pre>
Line 1,014 ⟶ 1,178:
=={{header|Perl}}==
<
use warnings;
use strict;
Line 1,097 ⟶ 1,261:
92
28
98</
{{out}}
equal 313 0.339%
Line 1,105 ⟶ 1,269:
=={{header|Phix}}==
{{Trans|C}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">85</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">88</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">75</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">66</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">29</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">83</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">39</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">97</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">68</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">49</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">16</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">65</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">92</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">28</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">98</span> <span style="color: #0000FF;">}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pick</span><span style="color: #0000FF;">(</span><span style="color: #004080;">int</span> <span style="color: #000000;">at</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">int</span> <span style="color: #000000;">remain</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">int</span> <span style="color: #000000;">accu</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">int</span> <span style="color: #000000;">treat</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">remain</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accu</span><span style="color: #0000FF;">></span><span style="color: #000000;">treat</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">pick</span><span style="color: #0000FF;">(</span><span style="color: #000000;">at</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">remain</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">accu</span><span style="color: #0000FF;">+</span><span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">at</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">treat</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span>
<span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">at</span><span style="color: #0000FF;">></span><span style="color: #000000;">remain</span><span style="color: #0000FF;">?</span><span style="color: #000000;">pick</span><span style="color: #0000FF;">(</span><span style="color: #000000;">at</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">remain</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">accu</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">treat</span><span style="color: #0000FF;">):</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">int</span> <span style="color: #000000;">treat</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">le</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">gt</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">total</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">;</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span> <span style="color: #000000;">treat</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">19</span> <span style="color: #008080;">to</span> <span style="color: #000000;">11</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> <span style="color: #000000;">total</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">i</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">9</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> <span style="color: #000000;">total</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">i</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">gt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pick</span><span style="color: #0000FF;">(</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">treat</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">le</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">total</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">gt</span><span style="color: #0000FF;">;</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"<= : %f%% %d\n > : %f%% %d\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">100</span><span style="color: #0000FF;">*</span><span style="color: #000000;">le</span><span style="color: #0000FF;">/</span><span style="color: #000000;">total</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">le</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">100</span><span style="color: #0000FF;">*</span><span style="color: #000000;">gt</span><span style="color: #0000FF;">/</span><span style="color: #000000;">total</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">gt</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,132 ⟶ 1,298:
=={{header|PicoLisp}}==
<
(scl 2)
Line 1,163 ⟶ 1,329:
(let N (permutationTest *TreatmentGroup *ControlGroup)
(prinl "under = " (round N) "%, over = " (round (- 100.0 N)) "%") )</
{{out}}
<pre>under = 87.85%, over = 12.15%</pre>
Line 1,178 ⟶ 1,344:
the treatment group or the control group
<syntaxhighlight lang="purebasic">
Define.f meanTreated,meanControl,diffInMeans
Line 1,270 ⟶ 1,436:
Debug StrF(100*diffLessOrEqual/TotalComBinations,2)+" "+Str(diffLessOrEqual)
Debug StrF(100*diffGreater /TotalComBinations,2)+" "+Str(diffGreater)
</syntaxhighlight>
{{out}}
Line 1,280 ⟶ 1,446:
=={{header|Python}}==
{{trans|Tcl}}
<
def statistic(ab, a):
Line 1,299 ⟶ 1,465:
controlGroup = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
under = permutationTest(treatmentGroup, controlGroup)
print("under=%.2f%%, over=%.2f%%" % (under, 100. - under))</
{{out}}
<pre>under=89.11%, over=10.89%</pre>
The above solution does a different thing than the other solutions. I'm not really sure why. If you want to do the same thing as the other solutions, then this is the solution:
<
def permutationTest(a, b):
Line 1,318 ⟶ 1,484:
controlGroup = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
under = permutationTest(treatmentGroup, controlGroup)
print("under=%.2f%%, over=%.2f%%" % (under, 100. - under))</
{{out}}
<pre>under=87.20%, over=12.80%</pre>
Line 1,324 ⟶ 1,490:
=={{header|R}}==
<
perms <- combinations(length(treatment)+length(control),
length(treatment),
Line 1,331 ⟶ 1,497:
p <- mean(rowMeans(perms) <= mean(treatment))
c(under=p, over=(1-p))
}</
<
+ c(68, 41, 10, 49, 16, 65, 32, 92, 28, 98))
under over
0.8719717 0.1280283 </
=={{header|Racket}}==
{{trans|Common Lisp}}
<
(define-syntax-rule (inc! x)
Line 1,366 ⟶ 1,532:
more (real->decimal-string (* 100. (/ more sum)) 2)
leq (real->decimal-string (* 100. (/ leq sum)) 2))))
</syntaxhighlight>
{{out}}
Line 1,379 ⟶ 1,545:
{{works with|Rakudo|2018.09}}
<syntaxhighlight lang="raku"
([+] @test / +@test) - ([+] flat @all, (@test X* -1)) / @all - @test
}
Line 1,397 ⟶ 1,563:
say 'Equal to : %', 100 * @trials[1] / [+] @trials;
say 'Greater than : %', 100 * @trials[2] / [+] @trials;
say 'Less or Equal: %', 100 * ( [+] @trials[0,1] ) / [+] @trials;</
{{out}}
Line 1,410 ⟶ 1,576:
=={{header|REXX}}==
This REXX program is modeled after the '''C''' version, with some generalizations and optimization added.
<
/* ↑ ↑ */
/* │ │ */
Line 1,418 ⟶ 1,584:
data= 85 88 75 66 25 29 83 39 97 68 41 10 49 16 65 32 92 28 98
w= words(data); m= w - n /*w: volunteers + control population*/
L= length(w) /*L: used to align some output numbers*/
say '# of volunteers & control population: ' w
say 'volunteer population given treatment: ' right(n, L)
Line 1,442 ⟶ 1,608:
else q= 0
itP= it - 1 /*set temporary var.*/
return picker(itP, rest - 1, eff + #.itP) + q /*recurse. */</
{{out|output|text= when using the default input:}}
<pre>
Line 1,458 ⟶ 1,624:
=={{header|Ruby}}==
{{trans|Python}}
<
sumab, suma = ab.inject(:+).to_f, a.inject(:+).to_f
suma / a.size - (sumab - suma) / (ab.size - a.size)
Line 1,477 ⟶ 1,643:
controlGroup = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
under = permutationTest(treatmentGroup, controlGroup)
puts "under=%.2f%%, over=%.2f%%" % [under, 100 - under]</
{{out}}
<pre>
under=87.20%, over=12.80%
</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">
fn main() {
let treatment = vec![85, 88, 75, 66, 25, 29, 83, 39, 97];
let control = vec![68, 41, 10, 49, 16, 65, 32, 92, 28, 98];
let mut data_set = control.clone();
data_set.extend_from_slice(&treatment);
let greater = combinations(treatment.iter().sum(), treatment.len() as i64, &data_set) as f64;
let lesser = combinations(control.iter().sum(), control.len() as i64, &data_set) as f64;
let total = binomial(data_set.len() as i64, treatment.len() as i64) as f64;
println!("<= : {}%", (lesser / total * 100.0));
println!(" > : {}%", (greater / total * 100.0));
}
fn factorial(x: i64) -> i64 {
let mut product = 1;
for a in 1..(x + 1) {
product *= a;
}
product
}
fn binomial(n: i64, k: i64) -> i64 {
let numerator = factorial(n);
let denominator = factorial(k) * factorial(n - k);
numerator / denominator
}
fn combinations(total: i64, number: i64, data: &[i64]) -> i64 {
if total < 0 {
return binomial(data.len() as i64, number);
}
if number == 0 {
return 0;
}
if number > data.len() as i64 {
return 0;
}
if number == data.len() as i64 {
if total < data.iter().sum() {
return 1;
} else {
return 0;
}
}
let tail = &data[1..];
combinations(total - data[0], number - 1, &tail) + combinations(total, number, &tail)
}
</syntaxhighlight>
{{out}}
<pre>
<= : 86.8583428954946%
> : 12.80283184307952%
</pre>
Line 1,487 ⟶ 1,715:
===Imperative version (Ugly, side effects)===
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/69o8tJX/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/jpUyNPetRXabZkEKc68FAA Scastie (remote JVM)].
<
private val data =
Array(85, 88, 75, 66, 25, 29, 83, 39, 97, 68, 41, 10, 49, 16, 65, 32, 92, 28, 98)
Line 1,509 ⟶ 1,737:
println(f" > : ${100.0 * gt / total}%f%% ${gt}%d")
}</
=={{header|Seed7}}==
<
include "float.s7i";
Line 1,550 ⟶ 1,778:
writeln("<= : " <& 100.0 * flt(le) / flt(total) digits 6 <& "% " <& le);
writeln(" > : " <& 100.0 * flt(gt) / flt(total) digits 6 <& "% " <& gt);
end func;</
{{out}}
<= : 87.197168% 80551
Line 1,557 ⟶ 1,785:
=={{header|Sidef}}==
{{trans|Ruby}}
<
var(sumab, suma) = (ab.sum, a.sum)
suma/a.size - ((sumab-suma) / (ab.size-a.size))
Line 1,576 ⟶ 1,804:
var controlGroup = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
var under = permutationTest(treatmentGroup, controlGroup)
say ("under=%.2f%%, over=%.2f%%" % (under, 100 - under))</
{{out}}
<pre>
Line 1,583 ⟶ 1,811:
=={{header|Tcl}}==
<
# Difference of means; note that the first list must be the concatenation of
Line 1,629 ⟶ 1,857:
set count [tcl::mathfunc::double $count]
list [expr {$overcount / $count}] [expr {$undercount / $count}]
}</
Demonstration code:
<
set controlGroup {0.68 0.41 0.10 0.49 0.16 0.65 0.32 0.92 0.28 0.98}
lassign [permutationTest $treatmentGroup $controlGroup] over under
puts [format "under=%.2f%%, over=%.2f%%" [expr {$under*100}] [expr {$over*100}]]</
{{out}}
<pre>under=86.90%, over=13.10%</pre>
=={{header|Ursala}}==
<
#import nat
#import flo
Line 1,654 ⟶ 1,882:
#show+
t = --* *-'%'@lrNCC printf/$'%0.2f' times/$100. f(treatment_group,control_group)</
{{out}}
<pre>
12.80%
87.20%</pre>
=={{header|Wren}}==
{{trans|C}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
var data = [85, 88, 75, 66, 25, 29, 83, 39, 97, 68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
var pick // recursive
pick = Fn.new { |at, remain, accu, treat|
if (remain == 0) return (accu > treat) ? 1 : 0
return pick.call(at-1, remain-1, accu + data[at-1], treat) +
((at > remain) ? pick.call(at-1, remain, accu, treat) : 0)
}
var treat = 0
var total = 1
for (i in 0..8) treat = treat + data[i]
for (i in 19..11) total = total * i
for (i in 9..1) total = total / i
var gt = pick.call(19, 9, 0, treat)
var le = (total - gt).truncate
Fmt.print("<= : $f\% $d", 100 * le / total, le)
Fmt.print(" > : $f\% $d", 100 * gt / total, gt)</syntaxhighlight>
{{out}}
<pre>
<= : 87.197168% 80551
> : 12.802832% 11827
</pre>
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \For Print
int Data;
func Pick(At, Remain, Accu, Treat);
int At, Remain, Accu, Treat;
[if Remain = 0 then return if Accu > Treat then 1 else 0;
return Pick(At-1, Remain-1, Accu + Data(At-1), Treat) +
(if At > Remain then Pick(At-1, Remain, Accu, Treat) else 0);
];
int Treat, GT, LE, I;
real Total;
[Treat:= 0; Total:= 1.;
Data:= [85, 88, 75, 66, 25, 29, 83, 39, 97,
68, 41, 10, 49, 16, 65, 32, 92, 28, 98];
for I:= 0 to 8 do Treat:= Treat + Data(I);
for I:= 19 downto 11 do Total:= Total * float(I);
for I:= 9 downto 1 do Total:= Total / float(I);
GT:= Pick(19, 9, 0, Treat);
LE:= (fix(Total) - GT);
Print("<= : %f% %d\n", 100. * float(LE) / Total, LE);
Print(" > : %f% %d\n", 100. * float(GT) / Total, GT);
]</syntaxhighlight>
{{out}}
<pre>
<= : 87.19717% 80551
> : 12.80283% 11827
</pre>
=={{header|zkl}}==
A solution that is not going to scale gracefully at all.
{{trans|D}}
<
ab := a.extend(b);
tObs := a.sum(0);
Line 1,674 ⟶ 1,964:
controlGroup := T(68, 41, 10, 49, 16, 65, 32, 92, 28, 98);
under := permutationTest(treatmentGroup, controlGroup);
println("Under =%6.2f%%\nOver =%6.2f%%".fmt(under, 100.0 - under));</
{{out}}
<pre>
|