I am trying to learn how to write good julia code. I would like to encode the following statistics.
(note 1 {A} = 1 if A is true, 0 if A is false)

Where

and

function cohens_kappa(x::Vector{Int}, k::Int)
support = unique(x)
m = length(support)
n = length(x)
y = BitArray(n, m)
for j in eachindex(support)
y[:,j] = (X .== support[j])
end
num = 0.0
den = 0.0
for j in eachindex(support)
pjjk = sum(y[(1 + k):n, j] & y[1:(n - k), j]) / (n - k)
pj = sum(y[:, j]) / n
num += pjjk - pj ^ 2
den += (1 / m) - pj ^ 2
end
return (num / den)
end
Is this the most efficient way to encode this?
EDIT: Thanks for all the guys suggestions. Can you explain why your code is more efficient? I would like to know how to keep writing good code in the future.
testing against @ user3580870 two examples we have
@time [cohens_kappa(X, k) for k in 1:15]
0.000507 seconds (1.58 k allocations: 269.016 KB)
@time [cohens_kappa2(X, k) for k in 1:15]
0.000336 seconds (166 allocations: 12.375 KB)
@time [cohens_kappa3(X, k) for k in 1:15]
0.000734 seconds (303 allocations: 84.109 KB)
It seems your second sentence is not so fast, but it makes fewer distributions than my original version, so it can be faster for very large vectors.
source
share