Skip to content

Commit daeac2c

Browse files
authored
Merge pull request #15 from s-ccs/feature-unpairedt
feature unpairedt
2 parents 835eef4 + 96391d4 commit daeac2c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/utils.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,24 @@ function sign_permute!(rng, x::AbstractArray)
7373
return x
7474
end
7575

76-
function batch_unpaired_ttest_unequal_var(x::AbstractArray, group)
76+
"""
77+
studentt_unpaired(x::AbstractArray, group)
78+
79+
Implements a unpaired two groups t-test with unequal variances. 10x as fast as HypothesisTests because we don't allocate that much
80+
Use like this:
81+
82+
```julia
83+
studentt_unpaired([1,2,1,1,4,5,4],[false,false,false,false,true,true,true])
84+
```
85+
86+
To use with ClusterDepth, you have to "bake-in" the group membership by defining your own method:
87+
```julia
88+
grp = ["bla","bla","bla","bla","blub","blub","blub"] .== "blub"
89+
my_statfun = x->studentt_unpaired(x,grp)
90+
```
91+
92+
"""
93+
function studentt_unpaired(x::AbstractArray, group)
7794
x_reshaped = reshape(x, :, size(x, ndims(x)))
7895
x₁ = x_reshaped[:, group]
7996
x₂ = x_reshaped[:, .!group]

test/utils.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ end
2424
@testset "studentt" begin
2525
x = randn(StableRNG(1), 10000, 50)
2626
t = ClusterDepth.studentt(x)
27+
28+
t_true = [HypothesisTests.OneSampleTTest(r).t for r in eachrow(x)]
29+
@test all(t .≈ t_true)
2730
@test length(t) == 10000
2831
@test maximum(abs.(t)) < 10 # we'd need to be super lucky ;)
2932
@test mean(abs.(t) .> 2) < 0.06
@@ -40,6 +43,7 @@ end
4043
t = rand(10000)
4144
ClusterDepth.studentt!(t, x)
4245
@test t ClusterDepth.studentt(x)
46+
4347
@test length(t) == 10000
4448
@test maximum(abs.(t)) < 10 # we'd need to be super lucky ;)
4549
@test mean(abs.(t) .> 2) < 0.06
@@ -54,3 +58,27 @@ end
5458
data = randn(StableRNG(1), 3, 4, 5)
5559
@test size(ClusterDepth.studentt(data)) == (3, 4)
5660
end
61+
62+
63+
@testset begin
64+
"ttest unpaired"
65+
_x = randn(StableRNG(1), 10000, 50, 2)
66+
x1 = _x[:, :, 1]
67+
x2 = _x[:, :, 2]
68+
x = hcat(x1, x2)
69+
group = repeat([false, true], inner=size(x1, 2))
70+
t = ClusterDepth.studentt_unpaired(x, group .== 1)
71+
72+
t_true = [HypothesisTests.UnequalVarianceTTest(r[group], r[.!group]).t for r in eachrow(x)]
73+
@test all(t .≈ t_true)
74+
@test length(t) == 10000
75+
76+
77+
_x = randn(StableRNG(1), 10000, 50, 30, 2)
78+
x1 = _x[:, :, :, 1]
79+
x2 = _x[:, :, :, 2]
80+
x = cat(x1, x2, dims=3)
81+
group = repeat([false, true], inner=size(x1, 3))
82+
t = ClusterDepth.studentt_unpaired(x, group .== 1)
83+
@test size(t) == (1000, 50)
84+
end

0 commit comments

Comments
 (0)