-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from bjarthur/permutationtest
added permutation test
- Loading branch information
Showing
5 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export ExactPermutationTest, ApproximatePermutationTest | ||
|
||
function ptstats(x,y,f) | ||
xy = vcat(x,y) | ||
rx = 1:length(x) | ||
ry = (length(xy)-length(y)+1):length(xy) | ||
(xy, rx, ry) | ||
end | ||
|
||
struct PermutationTest{T<:Real} <: HypothesisTest | ||
observation::T | ||
samples::Vector{T} | ||
end | ||
|
||
""" | ||
ExactPermutationTest(x::Vector, y::Vector, f::Function) | ||
Perform a permutation test (a.k.a. randomization test) of the null hypothesis | ||
that `f(x)` is equal to `f(y)`. All possible permutations are sampled. | ||
""" | ||
function ExactPermutationTest(x::AbstractVector{R}, y::AbstractVector{S}, | ||
f::Function) where {R<:Real,S<:Real} | ||
xy, rx, ry = ptstats(x,y,f) | ||
P = permutations(xy) | ||
samples = [f(view(p,rx)) - f(view(p,ry)) for p in P] | ||
PermutationTest(f(x) - f(y), samples) | ||
end | ||
|
||
""" | ||
ApproximatePermutationTest(x::Vector, y::Vector, f::Function, n::Int) | ||
Perform a permutation test (a.k.a. randomization test) of the null hypothesis | ||
that `f(x)` is equal to `f(y)`. `n` of the `factorial(length(x)+length(y))` | ||
permutations are sampled at random. | ||
""" | ||
function ApproximatePermutationTest(x::AbstractVector{R}, y::AbstractVector{S}, | ||
f::Function, n::Int) where {R<:Real,S<:Real} | ||
xy, rx, ry = ptstats(x,y,f) | ||
samples = [(shuffle!(xy); f(view(xy,rx)) - f(view(xy,ry))) for i = 1:n] | ||
PermutationTest(f(x) - f(y), samples) | ||
end | ||
|
||
function pvalue(apt::PermutationTest; tail=:both) | ||
if tail == :both | ||
count = sum(abs(apt.observation) >= abs(x) for x in apt.samples) | ||
elseif tail == :left | ||
count = sum(apt.observation >= x for x in apt.samples) | ||
elseif tail == :right | ||
count = sum(apt.observation <= x for x in apt.samples) | ||
end | ||
return count / length(apt.samples) | ||
end | ||
|
||
testname(::PermutationTest) = "Permutation Test" | ||
|
||
function show_params(io::IO, apt::PermutationTest, ident) | ||
println(io, ident, "observation: ", apt.observation) | ||
println(io, ident, "samples: ", apt.samples) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using HypothesisTests, Base.Test | ||
|
||
@test isapprox(pvalue(ExactPermutationTest([1,2,3],[4,5,6],mean), tail=:both), 1.0) | ||
@test isapprox(pvalue(ExactPermutationTest([4,5,6],[1,2,3],mean), tail=:both), 1.0) | ||
@test isapprox(pvalue(ExactPermutationTest([1,2,3],[4,5,6],mean), tail=:left), 0.05) | ||
@test isapprox(pvalue(ExactPermutationTest([4,5,6],[1,2,3],mean), tail=:right), 0.05) | ||
|
||
srand(12345) | ||
@test isapprox(pvalue(ApproximatePermutationTest([1,2,3],[4,5,6],mean,100), tail=:both), 1.0) | ||
@test isapprox(pvalue(ApproximatePermutationTest([4,5,6],[1,2,3],mean,100), tail=:both), 1.0) | ||
@test isapprox(pvalue(ApproximatePermutationTest([1,2,3],[4,5,6],mean,100), tail=:left), 0.08) | ||
@test isapprox(pvalue(ApproximatePermutationTest([4,5,6],[1,2,3],mean,100), tail=:right), 0.07) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters