Skip to content

Commit

Permalink
corrected accidental changes to test/rankcorr.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
PGS62 committed Jan 31, 2024
1 parent 1371c47 commit e8a61fd
Showing 1 changed file with 10 additions and 111 deletions.
121 changes: 10 additions & 111 deletions test/rankcorr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,117 +187,16 @@ include("compare_implementations.jl")
missing missing
5 6]

c11 = f(x1, x1)
c12 = f(x1, x2)
c22 = f(x2, x2)

# AbstractMatrix{<:Real}, AbstractMatrix{<:Real}
@test f(x, x) [c11 c12; c12 c22]
# AbstractMatrix{<:Real}
@test f(x) [c11 c12; c12 c22]

@test c11 == 1.0
@test c22 == 1.0
@test c12 == 3 / sqrt(20)
# Finished testing for overflow, so redefine n for speedier tests
n = 100

@test f(repeat(x, n), repeat(x, n)) [c11 c12; c12 c22]
@test f(repeat(x, n)) [c11 c12; c12 c22]

# All eight three-element permutations
z = [1 1 1
1 1 2
1 2 2
1 2 2
1 2 1
2 1 2
1 1 2
2 2 2]

@test f(z) == [1 0 1/3; 0 1 0; 1/3 0 1]
@test f(z, z) == [1 0 1/3; 0 1 0; 1/3 0 1]
@test f(z[:, 1], z) == [1 0 1 / 3]
@test f(z, z[:, 1]) == [1; 0; 1 / 3]

z = float(z)
@test f(z) == [1 0 1/3; 0 1 0; 1/3 0 1]
@test f(z, z) == [1 0 1/3; 0 1 0; 1/3 0 1]
@test f(z[:, 1], z) == [1 0 1 / 3]
@test f(z, z[:, 1]) == [1; 0; 1 / 3]

w = repeat(z, n)
@test f(w) == [1 0 1/3; 0 1 0; 1/3 0 1]
@test f(w, w) == [1 0 1/3; 0 1 0; 1/3 0 1]
@test f(w[:, 1], w) == [1 0 1 / 3]
@test f(w, w[:, 1]) == [1; 0; 1 / 3]

KendallTau.midpoint(1, 10) == 5
KendallTau.midpoint(1, widen(10)) == 5

# NaN handling

Xnan = copy(x)
Xnan[1, 1] = NaN
Ynan = copy(Y)
Ynan[2, 1] = NaN

@test isnan(f([1.0, NaN, 2.0], [2.0, 1.0, 3.4]))
@test all(isnan, f([1.0, NaN], [1 2; 3 4]))
@test all(isnan, f([1 2; 3 4], [1.0, NaN]))
@test isequal(f([1 NaN; NaN 4]), [1 NaN; NaN 1])
@test all(isnan, f([1 NaN; NaN 4], [1 NaN; NaN 4]))
@test all(isnan, f([1 NaN; NaN 4], [NaN 1; NaN 4]))

@test isequal(f(Xnan, Ynan),
[f(Xnan[:, i], Ynan[:, j]) for i in axes(Xnan, 2), j in axes(Ynan, 2)])
@test isequal(f(Xnan),
[i == j ? 1.0 : f(Xnan[:, i], Xnan[:, j])
for i in axes(Xnan, 2), j in axes(Xnan, 2)])
for k in 1:2
@test isequal(f(Xnan[:, k], Ynan),
[f(Xnan[:, k], Ynan[:, j]) for i in 1:1, j in axes(Ynan, 2)])
@test isequal(f(Xnan, Ynan[:, k]),
[f(Xnan[:, i], Ynan[:, k]) for i in axes(Xnan, 2)])
end

# Wrong dimensions
@test_throws DimensionMismatch f([1], [1, 2])
@test_throws DimensionMismatch f([1], [1 2; 3 4])
@test_throws DimensionMismatch f([1 2; 3 4], [1])
@test_throws DimensionMismatch f([1 2; 3 4; 5 6], [1 2; 3 4])

# x has sufficient columns to ensure that variable use_atomic evaluates to false
n_reps = Threads.nthreads()
@test f(repeat(hcat(a, b), outer=[1, n_reps])) == repeat(f(hcat(a, b)), outer=[n_reps, n_reps])

#= Test functions against corkendall_naive, a "reference implementation" that has the
advantage of simplicity.
=#
if f !== corkendall_naive
@test compare_implementations(f, corkendall_naive, abstol=0.0, maxcols=10, maxrows=10, numtests=200, fns_handle_missings=true) == true
@test compare_implementations(f, corkendall_naive, abstol=0.0, maxcols=10, maxrows=100, numtests=200, fns_handle_missings=true) == true
@test compare_implementations(f, corkendall_naive, abstol=1e14, maxcols=2, maxrows=20000, numtests=5, fns_handle_missings=true) == true
end
end

#Auxiliary functions for corkendall
x = [1, 2, 3, missing, 4]
y = [missing, 1, 2, 3, 4]
u = [missing, missing, 1, 2]
v = [3, 4, missing, missing]

mx = [1 2
missing 3
4 missing
missing missing
5 6]
@test KendallTau.handle_pairwise!(x, y, similar(x), similar(y)) == ([2, 3, 4], [1, 2, 4])
@test KendallTau.handle_pairwise!(float.(x), y, similar(float.(x)), similar(y)) == ([2.0, 3.0, 4.0], [1, 2, 4])
@test KendallTau.handle_pairwise!(x, float.(y), similar(x), similar(float.(y))) == ([2, 3, 4], [1.0, 2.0, 4.0])
@test KendallTau.handle_pairwise!(u, v, similar(u), similar(v)) == (Int64[], Int64[])
@test KendallTau.handle_listwise!(mx, mx) == ([1 2; 5 6], [1 2; 5 6])

@test KendallTau.handlepairwise!(x, y, similar(x), similar(y)) == ([2, 3, 4], [1, 2, 4])
@test KendallTau.handlepairwise!(float.(x), y, similar(float.(x)), similar(y)) == ([2.0, 3.0, 4.0], [1, 2, 4])
@test KendallTau.handlepairwise!(x, float.(y), similar(x), similar(float.(y))) == ([2, 3, 4], [1.0, 2.0, 4.0])
@test KendallTau.handlepairwise!(u, v, similar(u), similar(v)) == (Int64[], Int64[])
@test KendallTau.handlelistwise(mx, mx) == ([1 2; 5 6], [1 2; 5 6])
smallx = randn(MersenneTwister(123), 1000, 3)
indicators = rand(MersenneTwister(456), 1000, 3) .< 0.05
smallx = ifelse.(indicators, missing, smallx)
@test corkendall_naive(smallx, skipmissing=:pairwise) == KendallTau.corkendall(smallx, skipmissing=:pairwise)


end #testset
end

0 comments on commit e8a61fd

Please sign in to comment.