/
rankcorr.jl
324 lines (289 loc) · 9.22 KB
/
rankcorr.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Rank-based correlations
#
# - Spearman's correlation
# - Kendall's correlation
#
#######################################
#
# Spearman correlation
#
#######################################
"""
corspearman(x, y=x)
Compute Spearman's rank correlation coefficient. If `x` and `y` are vectors, the
output is a float, otherwise it's a matrix corresponding to the pairwise correlations
of the columns of `x` and `y`.
"""
function corspearman(x::AbstractVector{<:Real}, y::AbstractVector{<:Real})
n = length(x)
n == length(y) || throw(DimensionMismatch("vectors must have same length"))
(any(isnan, x) || any(isnan, y)) && return NaN
return cor(tiedrank(x), tiedrank(y))
end
function corspearman(X::AbstractMatrix{<:Real}, y::AbstractVector{<:Real})
size(X, 1) == length(y) ||
throw(DimensionMismatch("X and y have inconsistent dimensions"))
n = size(X, 2)
C = Matrix{Float64}(I, n, 1)
any(isnan, y) && return fill!(C, NaN)
yrank = tiedrank(y)
for j = 1:n
Xj = view(X, :, j)
if any(isnan, Xj)
C[j,1] = NaN
else
Xjrank = tiedrank(Xj)
C[j,1] = cor(Xjrank, yrank)
end
end
return C
end
function corspearman(x::AbstractVector{<:Real}, Y::AbstractMatrix{<:Real})
size(Y, 1) == length(x) ||
throw(DimensionMismatch("x and Y have inconsistent dimensions"))
n = size(Y, 2)
C = Matrix{Float64}(I, 1, n)
any(isnan, x) && return fill!(C, NaN)
xrank = tiedrank(x)
for j = 1:n
Yj = view(Y, :, j)
if any(isnan, Yj)
C[1,j] = NaN
else
Yjrank = tiedrank(Yj)
C[1,j] = cor(xrank, Yjrank)
end
end
return C
end
function corspearman(X::AbstractMatrix{<:Real})
n = size(X, 2)
C = Matrix{Float64}(I, n, n)
anynan = Vector{Bool}(undef, n)
for j = 1:n
Xj = view(X, :, j)
anynan[j] = any(isnan, Xj)
if anynan[j]
C[:,j] .= NaN
C[j,:] .= NaN
C[j,j] = 1
continue
end
Xjrank = tiedrank(Xj)
for i = 1:(j-1)
Xi = view(X, :, i)
if anynan[i]
C[i,j] = C[j,i] = NaN
else
Xirank = tiedrank(Xi)
C[i,j] = C[j,i] = cor(Xjrank, Xirank)
end
end
end
return C
end
function corspearman(X::AbstractMatrix{<:Real}, Y::AbstractMatrix{<:Real})
size(X, 1) == size(Y, 1) ||
throw(ArgumentError("number of rows in each array must match"))
nr = size(X, 2)
nc = size(Y, 2)
C = Matrix{Float64}(undef, nr, nc)
for j = 1:nr
Xj = view(X, :, j)
if any(isnan, Xj)
C[j,:] .= NaN
continue
end
Xjrank = tiedrank(Xj)
for i = 1:nc
Yi = view(Y, :, i)
if any(isnan, Yi)
C[j,i] = NaN
else
Yirank = tiedrank(Yi)
C[j,i] = cor(Xjrank, Yirank)
end
end
end
return C
end
#######################################
#
# Kendall correlation
#
#######################################
# Knight, William R. “A Computer Method for Calculating Kendall's Tau with Ungrouped Data.”
# Journal of the American Statistical Association, vol. 61, no. 314, 1966, pp. 436–439.
# JSTOR, www.jstor.org/stable/2282833.
function corkendall!(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}, permx::AbstractArray{<:Integer}=sortperm(x))
if any(isnan, x) || any(isnan, y) return NaN end
n = length(x)
if n != length(y) error("Vectors must have same length") end
# Initial sorting
permute!(x, permx)
permute!(y, permx)
# Use widen to avoid overflows on both 32bit and 64bit
npairs = div(widen(n) * (n - 1), 2)
ntiesx = ndoubleties = nswaps = widen(0)
k = 0
@inbounds for i = 2:n
if x[i - 1] == x[i]
k += 1
elseif k > 0
# Sort the corresponding chunk of y, so the rows of hcat(x,y) are
# sorted first on x, then (where x values are tied) on y. Hence
# double ties can be counted by calling countties.
sort!(view(y, (i - k - 1):(i - 1)))
ntiesx += div(widen(k) * (k + 1), 2) # Must use wide integers here
ndoubleties += countties(y, i - k - 1, i - 1)
k = 0
end
end
if k > 0
sort!(view(y, (n - k):n))
ntiesx += div(widen(k) * (k + 1), 2)
ndoubleties += countties(y, n - k, n)
end
nswaps = merge_sort!(y, 1, n)
ntiesy = countties(y, 1, n)
# Calls to float below prevent possible overflow errors when
# length(x) exceeds 77_936 (32 bit) or 5_107_605_667 (64 bit)
(npairs + ndoubleties - ntiesx - ntiesy - 2 * nswaps) /
sqrt(float(npairs - ntiesx) * float(npairs - ntiesy))
end
"""
corkendall(x, y=x)
Compute Kendall's rank correlation coefficient, τ. `x` and `y` must both be either
matrices or vectors.
"""
corkendall(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) = corkendall!(copy(x), copy(y))
function corkendall(X::AbstractMatrix{<:Real}, y::AbstractVector{<:Real})
permy = sortperm(y)
return([corkendall!(copy(y), X[:,i], permy) for i in 1:size(X, 2)])
end
function corkendall(x::AbstractVector{<:Real}, Y::AbstractMatrix{<:Real})
n = size(Y, 2)
permx = sortperm(x)
return(reshape([corkendall!(copy(x), Y[:,i], permx) for i in 1:n], 1, n))
end
function corkendall(X::AbstractMatrix{<:Real})
n = size(X, 2)
C = Matrix{Float64}(I, n, n)
for j = 2:n
permx = sortperm(X[:,j])
for i = 1:j - 1
C[j,i] = corkendall!(X[:,j], X[:,i], permx)
C[i,j] = C[j,i]
end
end
return C
end
function corkendall(X::AbstractMatrix{<:Real}, Y::AbstractMatrix{<:Real})
nr = size(X, 2)
nc = size(Y, 2)
C = Matrix{Float64}(undef, nr, nc)
for j = 1:nr
permx = sortperm(X[:,j])
for i = 1:nc
C[j,i] = corkendall!(X[:,j], Y[:,i], permx)
end
end
return C
end
# Auxiliary functions for Kendall's rank correlation
"""
countties(x::AbstractVector{<:Real}, lo::Integer, hi::Integer)
Return the number of ties within `x[lo:hi]`. Assumes `x` is sorted.
"""
function countties(x::AbstractVector, lo::Integer, hi::Integer)
# Use of widen below prevents possible overflow errors when
# length(x) exceeds 2^16 (32 bit) or 2^32 (64 bit)
thistiecount = result = widen(0)
checkbounds(x, lo:hi)
@inbounds for i = (lo + 1):hi
if x[i] == x[i - 1]
thistiecount += 1
elseif thistiecount > 0
result += div(thistiecount * (thistiecount + 1), 2)
thistiecount = widen(0)
end
end
if thistiecount > 0
result += div(thistiecount * (thistiecount + 1), 2)
end
result
end
# Tests appear to show that a value of 64 is optimal,
# but note that the equivalent constant in base/sort.jl is 20.
const SMALL_THRESHOLD = 64
# merge_sort! copied from Julia Base
# (commit 28330a2fef4d9d149ba0fd3ffa06347b50067647, dated 20 Sep 2020)
"""
merge_sort!(v::AbstractVector, lo::Integer, hi::Integer, t::AbstractVector=similar(v, 0))
Mutates `v` by sorting elements `x[lo:hi]` using the merge sort algorithm.
This method is a copy-paste-edit of sort! in base/sort.jl, amended to return the bubblesort distance.
"""
function merge_sort!(v::AbstractVector, lo::Integer, hi::Integer, t::AbstractVector=similar(v, 0))
# Use of widen below prevents possible overflow errors when
# length(v) exceeds 2^16 (32 bit) or 2^32 (64 bit)
nswaps = widen(0)
@inbounds if lo < hi
hi - lo <= SMALL_THRESHOLD && return insertion_sort!(v, lo, hi)
m = midpoint(lo, hi)
(length(t) < m - lo + 1) && resize!(t, m - lo + 1)
nswaps = merge_sort!(v, lo, m, t)
nswaps += merge_sort!(v, m + 1, hi, t)
i, j = 1, lo
while j <= m
t[i] = v[j]
i += 1
j += 1
end
i, k = 1, lo
while k < j <= hi
if v[j] < t[i]
v[k] = v[j]
j += 1
nswaps += m - lo + 1 - (i - 1)
else
v[k] = t[i]
i += 1
end
k += 1
end
while k < j
v[k] = t[i]
k += 1
i += 1
end
end
return nswaps
end
# insertion_sort! and midpoint copied from Julia Base
# (commit 28330a2fef4d9d149ba0fd3ffa06347b50067647, dated 20 Sep 2020)
midpoint(lo::T, hi::T) where T <: Integer = lo + ((hi - lo) >>> 0x01)
midpoint(lo::Integer, hi::Integer) = midpoint(promote(lo, hi)...)
"""
insertion_sort!(v::AbstractVector, lo::Integer, hi::Integer)
Mutates `v` by sorting elements `x[lo:hi]` using the insertion sort algorithm.
This method is a copy-paste-edit of sort! in base/sort.jl, amended to return the bubblesort distance.
"""
function insertion_sort!(v::AbstractVector, lo::Integer, hi::Integer)
if lo == hi return widen(0) end
nswaps = widen(0)
@inbounds for i = lo + 1:hi
j = i
x = v[i]
while j > lo
if x < v[j - 1]
nswaps += 1
v[j] = v[j - 1]
j -= 1
continue
end
break
end
v[j] = x
end
return nswaps
end