\(|S|\) is the number of elements in a set \(S\). The index is between 0 and 1. It is undefined when both sets are empty, because the union is then empty too.
An element outside both sets is in neither \(A \cap B\) nor \(A \cup B\), so the size of the universe never enters the index. Twelve letters or twelve thousand give the same \(J\) for the same two sets.
\(1 - J(A,B)\) is the Jaccard distance. It is a true distance: it is 0 only for identical sets, it is symmetric, and it satisfies the triangle inequality.
length(intersect(A, B)) / length(union(A, B)) computes \(J\) for two vectors of labels. For two 0/1 vectors over the same elements, dist(rbind(a, b), method = "binary") gives the Jaccard distance. Part 1 of The Script computes both.
The index is due to Levandowsky and Winter (1971). Memberships must be 0 or more, and GJ is undefined only when both profiles are 0 on every axis.
Write a crisp set as \(a_i = 1\) when element \(i\) is in \(A\) and \(a_i = 0\) otherwise. Then \(\min(a_i, b_i) = 1\) exactly when \(i\) is in \(A \cap B\), and \(\max(a_i, b_i) = 1\) exactly when \(i\) is in \(A \cup B\), so the two sums are \(|A \cap B|\) and \(|A \cup B|\).
Raising \(a_i\) where \(a_i \ge b_i\) raises only the bottom sum, so GJ falls (or stays 0 when the profiles share nothing). Raising \(a_i\) where \(a_i < b_i\) raises the top sum until \(a_i\) reaches \(b_i\), so GJ rises.
Both sums add each axis on its own, so relabeling or reordering the axes does not change GJ.
sum(pmin(a, b)) / sum(pmax(a, b)) computes GJ in base R. With the proxy package, 1 - proxy::dist(rbind(a, b), method = "fJaccard") gives the same number. Part 2 of The Script computes both.
Neighboring spokes are \(2\pi/n\) apart. The triangle between spokes \(i\) and \(i+1\), with sides \(r_i\) and \(r_{i+1}\), has area \(\tfrac12 r_i r_{i+1} \sin(2\pi/n)\). Adding the \(n\) triangles gives
Rotating the chart gives the same polygons, so fix axis 1 at the top; the other \(n-1\) axes can then come in \((n-1)!\) orders. Reading the chart counterclockwise also gives the same polygons, which halves the count to \((n-1)!/2\): 1, 3, 12, 60, 360, and 2,520 orders for 3 to 8 axes.
The gold polygon joins the smaller radius on each spoke with a straight line. Where the blue and ember outlines cross between two spokes, the region inside both outlines reaches past that line, so it is a little larger than the gold polygon; for the same reason the region inside either outline is a little smaller than the dashed polygon. IoU divides the gold polygon's area by the dashed polygon's.
A product of two radii shrinks small values more than a sum does, so IoU is usually below GJ for the same profiles. The pattern has exceptions: when the values both profiles have are on neighboring spokes and the values only one profile has are next to small radii, IoU can come out slightly above GJ.
If no two neighboring spokes both have a positive maximum, the union polygon has zero area and IoU is 0/0. Orders where the union has zero area have no IoU and are left out of the plot of every order.
Define area <- function(r) 0.5 * sin(2 * pi / length(r)) * sum(r * c(r[-1], r[1])); then area(pmin(a, b)) / area(pmax(a, b)) is IoU. Part 3 of The Script computes IoU under every order.
IoU's top target under every axis order
The indices are compared by the target each one scores highest, so two indices agree when their top targets match, whatever their scores.
\(U\) is the set of axes where the profile reaches the threshold \(t\), and \(T\) is the set where the target does. The crisp score is \(|U \cap T| / |U \cup T|\). It is undefined (n/a in the table) when neither profile reaches \(t\) on any axis.
The verdict lists every target that reaches the top score.
The bar reorders the axes of the profile and of every target the same way, computes IoU under each of the \((n-1)!/2\) orders, and counts how often each target scores highest. The share is the fraction of orders with the same top target as the current order.
Target set \(d\) (1 to 20) for \(k\) axes comes from set.seed(100 * k + d + 3), with each target drawn as round(runif(k, 0.05, 1), 2). With \(m\) candidate targets, the tab uses the first \(m\) targets of the set. The default, five axes and set 1, is seed 504.
Part 4 of The Script draws the default targets, scores them with all three indices, and counts IoU's top target across the 12 orders.
| Call | What it returns |
|---|---|
length(intersect(A, B)) / length(union(A, B)) | The Jaccard index of two vectors of labels. |
dist(rbind(a, b), method = "binary") | The Jaccard distance, 1 − J, for two 0/1 vectors (base R). |
proxy::simil(rbind(a, b), method = "Jaccard") | The Jaccard index for two logical vectors. |
sum(pmin(a, b)) / sum(pmax(a, b)) | The generalized Jaccard index of two membership vectors. |
proxy::dist(rbind(a, b), method = "fJaccard") | 1 − GJ, so 1 - proxy::dist() is GJ. |
area(r), defined in the script | The area of a radar polygon with radius r[i] on spoke i. |
orders(n), defined in the script | Every distinct order of n axes around the chart, (n − 1)!/2 of them. |