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);
}