June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
30
Task/Permutation-test/Java/permutation-test.java
Normal file
30
Task/Permutation-test/Java/permutation-test.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
public class PermutationTest {
|
||||
private static final int[] data = new int[]{
|
||||
85, 88, 75, 66, 25, 29, 83, 39, 97,
|
||||
68, 41, 10, 49, 16, 65, 32, 92, 28, 98
|
||||
};
|
||||
|
||||
private static int pick(int at, int remain, int accu, int treat) {
|
||||
if (remain == 0) return (accu > treat) ? 1 : 0;
|
||||
return pick(at - 1, remain - 1, accu + data[at - 1], treat)
|
||||
+ ((at > remain) ? pick(at - 1, remain, accu, treat) : 0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int treat = 0;
|
||||
double total = 1.0;
|
||||
for (int i = 0; i <= 8; ++i) {
|
||||
treat += data[i];
|
||||
}
|
||||
for (int i = 19; i >= 11; --i) {
|
||||
total *= i;
|
||||
}
|
||||
for (int i = 9; i >= 1; --i) {
|
||||
total /= i;
|
||||
}
|
||||
int gt = pick(19, 9, 0, treat);
|
||||
int le = (int) (total - gt);
|
||||
System.out.printf("<= : %f%% %d\n", 100.0 * le / total, le);
|
||||
System.out.printf(" > : %f%% %d\n", 100.0 * gt / total, gt);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,27 +1,28 @@
|
|||
delta_mean{T<:Real}(a::Array{T,1}, b::Array{T,1}) = mean(a) - mean(b)
|
||||
using Combinatorics
|
||||
|
||||
function bifurcate{T<:Integer}(a::AbstractVector, sel::Array{T,1})
|
||||
x = a[sel]
|
||||
asel = trues(length(a))
|
||||
meandiff(a::Vector{T}, b::Vector{T}) where T <: Real = mean(a) - mean(b)
|
||||
|
||||
function bifurcate(a::AbstractVector, sel::Vector{T}) where T <: Integer
|
||||
x = a[sel]
|
||||
asel = trues(length(a))
|
||||
asel[sel] = false
|
||||
y = a[asel]
|
||||
return (x, y)
|
||||
y = a[asel]
|
||||
return x, y
|
||||
end
|
||||
|
||||
function perm_sig_test{T<:Real}(treat::Array{T,1}, control::Array{T,1})
|
||||
base_effect = delta_mean(treat, control)
|
||||
pool = [treat, control]
|
||||
tlen = length(treat)
|
||||
plen = length(pool)
|
||||
better = 0
|
||||
worse = 0
|
||||
for s in combinations(1:plen, tlen)
|
||||
(t, c) = bifurcate(pool, s)
|
||||
if base_effect < delta_mean(t, c)
|
||||
function permutation_test(treated::Vector{T}, control::Vector{T}) where T <: Real
|
||||
effect0 = meandiff(treated, control)
|
||||
pool = vcat(treated, control)
|
||||
tlen = length(treated)
|
||||
plen = length(pool)
|
||||
better = worse = 0
|
||||
for subset in combinations(1:plen, tlen)
|
||||
t, c = bifurcate(pool, subset)
|
||||
if effect0 < meandiff(t, c)
|
||||
better += 1
|
||||
else
|
||||
worse += 1
|
||||
end
|
||||
end
|
||||
return (better, worse)
|
||||
return better, worse
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,17 +1,13 @@
|
|||
treat = [85, 88, 75, 66, 25, 29, 83, 39, 97]
|
||||
control = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
|
||||
const treated = [85, 88, 75, 66, 25, 29, 83, 39, 97]
|
||||
const control = [68, 41, 10, 49, 16, 65, 32, 92, 28, 98]
|
||||
|
||||
(better, worse) = perm_sig_test(treat, control)
|
||||
(better, worse) = permutation_test(treated, control)
|
||||
|
||||
tot = better + worse
|
||||
|
||||
println("Permutation test using the following data:")
|
||||
println("Treated: ", treat)
|
||||
println("Treated: ", treated)
|
||||
println("Control: ", control)
|
||||
|
||||
println()
|
||||
println("There are ", tot, " different permuted groups of these data.")
|
||||
print(@sprintf("%8d, %5.2f%% ", better, 100better/tot))
|
||||
println("showed better than actual results.")
|
||||
print(@sprintf("%8d, %5.2f%% ", worse, 100worse/tot))
|
||||
println("showed equalivalent or worse results.")
|
||||
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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue