Definitionintro
Simpson diversity index
Also called Simpson's index.
The probability that two individuals drawn at random with replacement from a community belong to the same species, or one minus it.
The Simpson index of a community is , the probability that two individuals drawn at random with replacement belong to the same species. It is a measure of concentration: it is 1 when one species makes up the whole community and small when many species share it evenly. Three forms are in use, and papers often call each of them "Simpson's index": the concentration itself, the Gini-Simpson index (the probability that the two individuals differ), and the inverse Simpson index (the effective number of species of order 2). From a sample, Simpson's form without replacement, , is an unbiased estimate of . Always say which form you report.
Definition
Let a community have species with relative abundances and . The Simpson concentration is
If two individuals are drawn independently, each of species with probability (that is, with replacement), then is the probability that they are of the same species. The Gini-Simpson index is , and the inverse Simpson index is .
For a sample of individuals with of species , the plug-in value is , and the form without replacement is
the probability that two different individuals drawn from the sample are of the same species.
Properties that follow from the definition (theorems):
- for species with . if and only if all , and if and only if . So .
- Unbiasedness. Under multinomial sampling of individuals, is binomial with . By linearity of expectation, . The plug-in value has , so it is too high.
- is not an unbiased estimate of , because the reciprocal is not linear.
- Order 2 of Hill's family. is the Hill number for . It counts common species more than the Shannon index does, which is the order 1 member . So .
Examples and non-examples
- The abundances give and , so the Gini-Simpson index is and the inverse Simpson index is . This is less than for the same community.
- As a sample of , the form without replacement is , so and .
- The counts have the same proportions and the same , but . The two forms get closer as grows.
- Four species with equal abundance give and . The counts give and : one species dominates.
- Non-example: a sample of singletons. Five individuals of five species give , and has no value. The sample shows only that no two individuals share a species.
- Non-example: the index of a large and a small sample. The plug-in of a small sample is too high by , so a small sample looks less diverse. Use to compare samples of different sizes.
- Non-example: "higher is more diverse". For a higher value means less diversity, and for and it means more. A value without its form is ambiguous.
The pseudocode below computes all forms from a list of counts.
SIMPSON(counts)- 1
n ← the sum of the counts - 2
λ ← the sum of (c / n)^2 over the counts c - 3
λ_u ← the sum of c (c - 1) over the counts, divided by n (n - 1), if n ≥ 2 - 4
return (λ, 1 - λ, 1 / λ, λ_u, 1 - λ_u)
Related definitions
- Gini impurity of a node of a decision tree is the Gini-Simpson index of its class proportions.
- The Shannon diversity index is the order 1 member of Hill's family, and it gives rare species more weight. Shannon entropy and the Rényi entropy of order 2, , are the information measures behind the two indices.
- Hurlbert's probability of an interspecific encounter, , is also the expected number of species in a random subsample of 2 individuals, minus 1. So it is the first nontrivial point of the rarefaction curve: for the sample , .
- The Chao1 estimator estimates the number of species, the diversity of order 0, which depends most on the rare species.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/simpson-diversity-index/. The authors are listed in prov/2B5Z97.jsonl.
"""The forms of the Simpson index of a list of integer species counts.
Input: counts n_i >= 0 with a positive sum n. Output, as exact fractions:
the concentration lambda = sum (n_i / n)^2, the Gini-Simpson index
1 - lambda, the inverse Simpson index 1 / lambda, and the form without
replacement lambda_u = sum n_i (n_i - 1) / (n (n - 1)) with 1 - lambda_u
(both None when n < 2). Cost: Theta(k) arithmetic operations for k counts.
"""
from __future__ import annotations
from dataclasses import dataclass
from fractions import Fraction
from typing import Optional, Sequence
@dataclass(frozen=True)
class Simpson:
concentration: Fraction
gini_simpson: Fraction
inverse: Fraction
unbiased: Optional[Fraction]
gini_simpson_unbiased: Optional[Fraction]
def simpson(counts: Sequence[int]) -> Simpson:
"""Compute the Simpson forms from the counts."""
if any(c < 0 for c in counts):
raise ValueError("counts must be >= 0")
n = sum(counts) # @line L1
if n <= 0:
raise ValueError("counts must have a positive sum")
lam = sum((Fraction(c, n) ** 2 for c in counts), Fraction(0)) # @line L2
if n >= 2:
lam_u = Fraction(sum(c * (c - 1) for c in counts), n * (n - 1)) # @line L3
gs_u = 1 - lam_u
else:
lam_u = None
gs_u = None
return Simpson(lam, 1 - lam, 1 / lam, lam_u, gs_u) # @line L4
test_simpson_diversity_index.py 78 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/simpson-diversity-index/. The authors are listed in prov/2B5Z97.jsonl.
"""Tests of the Simpson forms against the page values, a count of pairs of
individuals, and the exact mean of both estimates by enumeration."""
import itertools
import random
from fractions import Fraction
import pytest
from simpson_diversity_index import simpson
def pair_oracle(counts):
"""Count ordered pairs of individuals of the same species directly."""
labels = [i for i, c in enumerate(counts) for _ in range(c)]
n = len(labels)
same_with = sum(1 for a in labels for b in labels if a == b)
same_without = sum(1 for x in range(n) for y in range(n) if x != y and labels[x] == labels[y])
return Fraction(same_with, n * n), (Fraction(same_without, n * (n - 1)) if n > 1 else None)
def test_page_values():
r = simpson([5, 3, 2])
assert r.concentration == Fraction(38, 100)
assert r.gini_simpson == Fraction(62, 100)
assert abs(float(r.inverse) - 2.631579) < 1e-6
assert r.unbiased == Fraction(28, 90)
assert abs(float(r.gini_simpson_unbiased) - 0.688889) < 1e-6
assert abs(float(1 / r.unbiased) - 3.214286) < 1e-6
assert abs(float(simpson([10, 6, 4]).unbiased) - 0.347368) < 1e-6
assert simpson([25] * 4).inverse == 4
assert abs(float(simpson([97, 1, 1, 1]).inverse) - 1.062473) < 1e-6
def test_edge_cases():
one = simpson([1])
assert one.concentration == 1 and one.unbiased is None
assert simpson([1] * 5).unbiased == 0
assert simpson([0, 6]).concentration == 1
with pytest.raises(ValueError):
simpson([0])
with pytest.raises(ValueError):
simpson([2, -1])
def test_random_against_pairs():
rng = random.Random(41)
for _ in range(500):
counts = [rng.randint(0, 6) for _ in range(rng.randint(1, 6))]
if sum(counts) == 0:
counts[0] = 1
r = simpson(counts)
lam, lam_u = pair_oracle(counts)
assert r.concentration == lam and r.unbiased == lam_u
s = sum(1 for c in counts if c > 0)
assert Fraction(1, s) <= r.concentration <= 1
def test_exact_means_by_enumeration():
rng = random.Random(42)
for _ in range(20):
w = [rng.randint(1, 5) for _ in range(rng.randint(2, 3))]
p = [Fraction(x, sum(w)) for x in w]
n = rng.randint(2, 6)
lam = sum(q * q for q in p)
mean_u = Fraction(0)
mean_plug = Fraction(0)
for seq in itertools.product(range(len(p)), repeat=n):
prob = Fraction(1)
for i in seq:
prob *= p[i]
r = simpson([seq.count(i) for i in range(len(p))])
mean_u += prob * r.unbiased
mean_plug += prob * r.concentration
assert mean_u == lam
assert mean_plug == lam + (1 - lam) / n
References
- E. H. Simpson, Measurement of diversity, Nature 163(4148), 1949, page 688 (the concentration as the probability that two individuals belong to the same group, and its estimate from a sample)
- Stuart H. Hurlbert, The nonconcept of species diversity, a critique and alternative parameters, Ecology 52(4), 1971, pages 577 to 586 (the probability of an interspecific encounter, and the expected number of species in a subsample)
- M. O. Hill, Diversity and evenness, a unifying notation and its consequences, Ecology 54(2), 1973, pages 427 to 432 (the inverse of the Simpson concentration as the diversity number of order 2)
- Anne E. Magurran, Measuring Biological Diversity, Blackwell, 2004, the chapter on diversity indices (Simpson's index, its forms and its sampling properties)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 2B5Z97 Edit this page History Report an error Markdown