Skip to content

Commit aca38e7

Browse files
author
Guy
committed
use newer pattern
1 parent 1aa1265 commit aca38e7

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ Each distance corresponds to a distance type. The type name and the correspondin
132132
| Chebyshev | chebyshev(x, y) | max(abs(x - y)) |
133133
| Minkowski | minkowski(x, y, p) | sum(abs(x - y).^p) ^ (1/p) |
134134
| Hamming | hamming(x, y) | sum(x .!= y) |
135+
| Rogers-Tanimoto | rogerstanimoto(x, y)| (2(ntf + nft)) / (2(ntf + nft) + ntt + nff) |
136+
| Jaccard | jaccard(x, y) | 1 - (sum(min(xi, yi)) / sum(max(xi, yi))) |
135137
| CosineDist | cosine_dist(x, y) | 1 - dot(x, y) / (norm(x) * norm(y)) |
136138
| CorrDist | corr_dist(x, y) | cosine_dist(x - mean(x), y - mean(y)) |
137139
| ChiSqDist | chisq_dist(x, y) | sum((x - y).^2 / (x + y)) |

src/metrics.jl

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type JSDivergence <: SemiMetric end
2828

2929
type SpanNormDist <: SemiMetric end
3030

31-
typealias UnionMetrics @compat(Union{Euclidean, SqEuclidean, Chebyshev, Cityblock, Minkowski, Hamming, CosineDist, CorrDist, ChiSqDist, KLDivergence, JSDivergence, SpanNormDist})
31+
typealias UnionMetrics @compat(Union{Euclidean, SqEuclidean, Chebyshev, Cityblock, Minkowski, Hamming, Jaccard, RogersTanimoto, CosineDist, CorrDist, ChiSqDist, KLDivergence, JSDivergence, SpanNormDist})
3232

3333
###########################################################
3434
#
@@ -157,7 +157,7 @@ js_divergence(a::AbstractArray, b::AbstractArray) = evaluate(JSDivergence(), a,
157157

158158
# SpanNormDist
159159
function eval_start(::SpanNormDist, a::AbstractArray, b::AbstractArray)
160-
a[1] - b[1], a[1]- b[1]
160+
a[1] - b[1], a[1] - b[1]
161161
end
162162
@compat @inline eval_op(::SpanNormDist, ai, bi) = ai - bi
163163
@compat @inline function eval_reduce(::SpanNormDist, s1, s2)
@@ -248,46 +248,44 @@ end
248248

249249
# Jaccard
250250

251-
function evaluate(dist::Jaccard, a::AbstractVector, b::AbstractVector)
252-
n = get_common_len(a, b)::Int
253-
numerator = 0
254-
denominator = 0
255-
for i = 1:n
256-
@inbounds ai = a[i]
257-
@inbounds bi = b[i]
258-
if ai > bi
259-
numerator += bi
260-
denominator += ai
261-
else
262-
numerator += ai
263-
denominator += bi
264-
end
265-
end
266-
1 - (numerator / denominator)
251+
@compat @inline eval_start(::Jaccard, a::AbstractArray, b::AbstractArray) = 0, 0
252+
@compat @inline function eval_op(::Jaccard, s1, s2)
253+
denominator = max(s1, s2)
254+
numerator = min(s1, s2)
255+
numerator, denominator
267256
end
268-
269-
jaccard(a::AbstractVector, b::AbstractVector) = evaluate(Jaccard(), a, b)
257+
@compat @inline function eval_reduce(::Jaccard, s1, s2)
258+
a = s1[1] + s2[1]
259+
b = s1[2] + s2[2]
260+
a, b
261+
end
262+
@compat @inline eval_end(::Jaccard, a) = 1 - (a[1]/a[2])
263+
jaccard(a::AbstractArray, b::AbstractArray) = evaluate(Jaccard(), a, b)
270264

271265
# Tanimoto
272266

273-
function evaluate{T <: Bool}(dist::RogersTanimoto, a::AbstractVector, b::AbstractVector)
274-
n = get_common_len(a, b)::Int
275-
tt = 0
276-
tf = 0
277-
ft = 0
278-
ff = 0
279-
for i = 1:n
280-
@inbounds tt += (a[i] && b[i])
281-
@inbounds tf += (a[i] && !b[i])
282-
@inbounds ft += (!a[i] && b[i])
283-
@inbounds ff += (!a[i] && !b[i])
284-
end
285-
numerator = 2(tf + ft)
286-
denominator = ntt + nff + 2(ntf + nft)
267+
@compat @inline eval_start(::RogersTanimoto, a::AbstractArray, b::AbstractArray) = 0, 0, 0, 0
268+
@compat @inline function eval_op(::RogersTanimoto, s1, s2)
269+
tt = s1 && s2
270+
tf = s1 && !s2
271+
ft = !s1 && s2
272+
ff = !s1 && !s2
273+
tt, tf, ft, ff
274+
end
275+
@compat @inline function eval_reduce(::RogersTanimoto, s1, s2)
276+
a = s1[1] + s2[1]
277+
b = s1[2] + s2[2]
278+
c = s1[3] + s2[3]
279+
d = s1[4] + s1[4]
280+
a, b, c, d
281+
end
282+
@compat @inline function eval_end(::RogersTanimoto, a)
283+
numerator = 2(a[2] + a[3])
284+
denominator = a[1] + a[4] + 2(a[2] + a[3])
287285
numerator / denominator
288286
end
287+
rogerstanimoto{T <: Bool}(a::AbstractArray{T}, b::AbstractArray{T}) = evaluate(RogersTanimoto(), a, b)
289288

290-
rogerstanimoto(a::AbstractVector, b::AbstractVector) = evaluate(RogersTanimoto(), a, b)
291289

292290
function pairwise!(r::AbstractMatrix, dist::CosineDist, a::AbstractMatrix, b::AbstractMatrix)
293291
m::Int, na::Int, nb::Int = get_pairwise_dims(r, a, b)

test/test_dists.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ b = 2
3232
@test hamming(a, a) == 0
3333
@test hamming(a, b) == 1
3434

35+
bt = [true, false, true]
36+
bf = [false, true, true]
37+
@test rogerstanimoto(bt, bt) == 0
38+
@test rogerstanimoto(bt, bf) == 4./5
3539

3640

3741
p = rand(12)
@@ -63,10 +67,6 @@ for (x, y) in (([4., 5., 6., 7.], [3., 9., 8., 1.]),
6367
@test_throws DimensionMismatch cosine_dist(1.:2, 1.:3)
6468
@test_approx_eq_eps cosine_dist(x, y) (1.0 - 112. / sqrt(19530.)) 1.0e-12
6569

66-
bt = [true, false, true]
67-
bf = [false, true, true]
68-
@test rogerstanimoto(bt, bt) == 0
69-
@test rogerstanimoto(bt, bf) == 4./5
7070

7171
@test_approx_eq_eps corr_dist(x, x) 0. 1.0e-12
7272
@test_approx_eq corr_dist(x, y) cosine_dist(x .- mean(x), vec(y) .- mean(y))

0 commit comments

Comments
 (0)