This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -0,0 +1,18 @@
import std.stdio, std.algorithm, std.array, combinations3;
alias sum = reduce!q{a + b};
auto permutationTest(T)(in T[] a, in T[] b) /*pure nothrow*/ {
immutable tObs = a.sum;
//auto combs = combinations!false(a ~ b, a.length); // Not a Range.
const combs = combinations(a ~ b, a.length).array; // Slow.
immutable under = combs.count!(perm => perm.sum <= tObs);
return under * 100.0 / combs.length;
}
void main() {
immutable treatmentGroup = [85, 88, 75, 66, 25, 29, 83, 39, 97];
immutable controlGroup = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98];
immutable under = permutationTest(treatmentGroup, controlGroup);
writefln("Under =%6.2f%%\nOver =%6.2f%%", under, 100.0 - under);
}

View file

@ -0,0 +1,24 @@
import std.stdio, std.algorithm, std.range;
void main() {
immutable treatment = [85, 88, 75, 66, 25, 29, 83, 39, 97];
immutable control = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98];
immutable both = treatment ~ control;
immutable sTreat = treatment.reduce!q{a + b};
T pick(T)(in size_t at, in size_t remain, in T accu) pure nothrow {
if (remain == 0)
return accu > sTreat;
return pick(at - 1, remain - 1, accu + both[at - 1]) +
(at > remain ? pick(at - 1, remain, accu) : 0);
}
alias mul = reduce!q{a * b};
immutable t = mul(1.0, iota(both.length, treatment.length + 1, -1))
.reduce!q{a / b}(iota(treatment.length, 0, -1));
immutable gt = pick(both.length, treatment.length, 0);
immutable le = cast(int)(t - gt);
writefln(" > : %2.2f%% %d", 100.0 * gt / t, gt);
writefln("<= : %2.2f%% %d", 100.0 * le / t, le);
}

View file

@ -0,0 +1,31 @@
proto combine (Int, @) {*}
multi combine (0, @) { [] }
multi combine ($, []) { () }
multi combine ($n, [$head, *@tail]) {
gather {
take [$head, @$_] for combine($n-1, @tail);
take [ @$_ ] for combine($n , @tail);
}
}
sub stats ( @test, @all ) {
(([+] @test) / +@test ) - ([+] @all, (@test X* -1)) / (@all - @test)
}
my @treated = <85 88 75 66 25 29 83 39 97>;
my @control = <68 41 10 49 16 65 32 92 28 98>;
my @all = @treated, @control;
my $base = stats( @treated, @all );
my @trials = 0, 0, 0;
map { @trials[ 1 + ( stats( $_, @all ) <=> $base ) ]++ }, combine( +@treated, @all );
say 'Counts: <, =, > ', @trials;
say 'Less than : %', 100 * @trials[0] / [+] @trials;
say 'Equal to : %', 100 * @trials[1] / [+] @trials;
say 'Greater than : %', 100 * @trials[2] / [+] @trials;
say 'Less or Equal: %', 100 * ( [+] @trials[0,1] ) / [+] @trials;