Skip to content

Commit

Permalink
Add a method for Jaccard similarity between BitArrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
kernelmethod committed Feb 17, 2020
1 parent 4b05ba3 commit 517e8a2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/similarities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,37 @@ function jaccard(A::Set, B::Set) :: Float64
end
end

@doc raw"""
function jaccard(x::BitArray{1}, y::BitArray{1})
Computes the Jaccard similarity between a pair of binary vectors. Here, Jaccard similarity is defined as
``J(x, y) = \\frac{\\sum_{i} \\min{(x_i,y_i)}}{\\sum_{i} \\max{(x_i,y_i)}}``
# Arguments
- `x::BitArray{1}`, `y::BitArray{1}`: two binary vectors, in the form of `BitArray`s.
# Examples
```jldoctest; setup = :(using LSHFunctions)
julia> x = BitArray([true, false, true, true, false]);
julia> y = BitArray([false, false, true, true, true]);
julia> jaccard(x,y)
0.5
```
"""
function jaccard(x::BitArray{1}, y::BitArray{1}) :: Float64
union = sum(x .| y)
if union == 0
# To avoid corner cases where x and y are both full of zeros
Float64(0)
else
intersection = sum(x .& y)
intersection / union
end
end

#====================
Inner product and norms
====================#
Expand Down
13 changes: 13 additions & 0 deletions test/test_similarities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ end
# Convention used in this module
@test jaccard(Set(), Set()) == 0
end

@testset "Compute Jaccard similarity between binary vectors" begin
x = BitArray([true, false, true, true, false])
y = BitArray([false, false, true, true, true])

@test jaccard(x, y) == jaccard(y, x) == 2 / 4

# When x and y are both full of false values, we define the
# Jaccard similarity between them to be zero.
x = falses(5)
y = falses(5)
@test jaccard(x, y) == 0
end
end

@testset "Inner product similarity tests" begin
Expand Down

0 comments on commit 517e8a2

Please sign in to comment.