Skip to content

Commit

Permalink
Jaccard similarity here
Browse files Browse the repository at this point in the history
  • Loading branch information
richardreeve committed Nov 10, 2023
1 parent 0522fc8 commit 3801b38
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- v0.5.8
- Add in Pielou diversity measures
- Add docs for PRs
- Fix Jaccard to be differentiation measure
- Generalise Jaccard with similarity
- v0.5.7
- Update compat and some doc fixes
- v0.5.6
Expand Down
7 changes: 4 additions & 3 deletions src/Ecology.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ end
generalisedjaccard(proportions::AbstractArray, qs, sim::AbstractTypes)
generalisedjaccard(meta::AbstractAssemblage, qs)
Calculates a generalisation of the Jaccard index of two columns
Calculates a generalisation of the Jaccard similarity of two columns
representing the counts of two subcommunities. This evaluates to raw
alpha / gamma - 1 for a series of orders, repesented as a vector of qs
(or a single number). It also includes an optional similarity matrix
Expand Down Expand Up @@ -244,7 +244,7 @@ generalisedjaccard(proportions::AbstractMatrix, sim::AbstractTypes) =
function generalisedjaccard(meta::AbstractAssemblage)
countsubcommunities(meta) == 2 ||
error("Can only calculate Jaccard index for 2 subcommunities")
num = sum(abs.(diff(getordinariness!(meta), dims = 2)))
num = sum(minimum(getordinariness!(meta), dims = 2))
denom = sum(maximum(getordinariness!(meta), dims = 2))
jac = metadiv(Gamma(meta), 0)
jac[!,:diversity] .= num / denom
Expand All @@ -257,12 +257,13 @@ end
jaccard(proportions::AbstractMatrix)
jaccard(asm::AbstractAssemblage)
Calculates Jaccard index (Jaccard similarity coefficient) of two
Calculates Jaccard similarity coefficient of two
columns representing independent subcommunity counts
#### Arguments:
- `proportions`: population proportions
- `asm`: assemblage / metacommunity
#### Returns:
Expand Down
8 changes: 4 additions & 4 deletions test/run_rcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ if !skipR
jag = gower(pops, countzeros = false).diversity[1]
jag1 = gower([x > 0 ? 1 : 0 for x in pops], countzeros = false).diversity[1]

@test jj rj
@test jj1 rj1
@test jj 1.0 - rj
@test jj1 1.0 - rj1
@test jg rg
@test jg1 rg1
@test jag rag
Expand Down Expand Up @@ -123,8 +123,8 @@ if !skipR
jag = gower(pops, countzeros = false).diversity[1]
jag1 = gower([x > 0 ? 1 : 0 for x in pops], countzeros = false).diversity[1]

@test jj rj
@test jj1 rj1
@test jj 1.0 - rj
@test jj1 1.0 - rj1
@test jg rg
@test jg1 rg1
@test jag rag
Expand Down
10 changes: 5 additions & 5 deletions test/test_Ecology.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ Z1 = ones(typeof(weights[1]), (length(weights), length(weights)));
@test_throws ErrorException simpson(Metacommunity(communities,
GeneralTypes(rand(numspecies,numspecies))))

@test jaccard([1 0 0; 0 1 1]'/3)[!,:diversity][1] 1.0
@test jaccard([1 0 0; 0 1 1]'/3)[!,:diversity][1] + 1.0 1.0
@test jaccard(Metacommunity([1 0 1; 0 1 1]'/4))[!,:diversity] ==
jaccard([1 0 1; 0 1 1]'/4)[!,:diversity]
@test_throws ErrorException jaccard(Metacommunity([1 0 1; 0 1 1]'/4,
GeneralTypes(rand(3, 3))))
@test jaccard([1 0 1; 0 1 1]'/4)[!,:diversity][1] 2.0 / 3.0
@test jaccard([1 0 1; 0 1 1]'/4)[!,:diversity][1] 1.0 / 3.0
@test_throws ErrorException jaccard([1 1 0; 0 1 1; 1 1 1]/7)
@test_throws ErrorException jaccard(Metacommunity([1 1 0; 0 1 1; 1 1 1]/7))

Expand Down Expand Up @@ -82,9 +82,9 @@ end
@test_throws ErrorException generalisedsimpson(individualDiversity,
communities, Z1)

@test generalisedjaccard([1 0 1; 0 1 1]'/4, UniqueTypes(3))[!,:diversity][1] 2.0/3.0
@test generalisedjaccard([1 0 1; 0 1 1]'/4, ones(Float64, 3, 3))[!,:diversity][1] 0.0
@test generalisedjaccard([1 1 1; 1 1 1]'/6, Matrix(1.0I, 3, 3))[!,:diversity][1] 0.0
@test generalisedjaccard([1 0 1; 0 1 1]'/4, UniqueTypes(3))[!,:diversity][1] 1.0/3.0
@test generalisedjaccard([1 0 1; 0 1 1]'/4, ones(Float64, 3, 3))[!,:diversity][1] 1.0
@test generalisedjaccard([1 1 1; 1 1 1]'/6, Matrix(1.0I, 3, 3))[!,:diversity][1] 1.0

@test all(Diversity.Ecology.generalisedpielou(subcommunityDiversity, [1/6 2/6; 1/6 2/6]).diversity .≈ 1.0)

Expand Down

0 comments on commit 3801b38

Please sign in to comment.