Skip to content

Coverage and entropy of a sample

sample_coverage, chao_shen_entropy, nsb_entropy · work on counts directly

These functions estimate properties of the population a sample came from, directly from the frequency distribution, without fitting a model: how much of the population's probability the sample has already covered, and the entropy of the population, corrected for the elements the sample missed. They are widely used in ecology (species diversity) and in the information-theoretic measures of ASymCat.

Definition

Sample coverage is the Good–Turing estimate of the observed probability mass, the complement of the unseen mass that Simple Good–Turing reserves:

\[ C = 1 - \frac{N_1}{N}. \]

The plug-in entropy \(\hat H = -\sum_w \hat p_w \ln \hat p_w\), with \(\hat p_w = c(w)/N\), underestimates the population entropy when the sample misses elements. The Chao–Shen estimator (Chao & Shen, 2003) scales the relative frequencies by the coverage and corrects each term for the chance that its element was observed at all:

\[ \hat H_{\text{CS}} = -\sum_w \frac{C\hat p_w \ln (C\hat p_w)}{1 - (1 - C\hat p_w)^N}. \]

When every observation is a singleton, \(N_1\) is taken as \(N - 1\), so that \(C\) stays positive. The NSB estimator (Nemenman, Shafee & Bialek, 2002) is a Bayesian estimate that averages the posterior-mean entropy of a Dirichlet model over its concentration, with a prior chosen to be nearly uniform in entropy. bins sets the number of possible elements, reserving entropy for elements that could occur but were not observed.

Example

With the example counts:

>>> import math
>>> import freqprob
>>> counts = dict(the=10, cat=5, sat=4, on=3, mat=2, a=2, dog=1, ran=1, far=1, away=1)
>>> round(freqprob.sample_coverage(counts), 4)  # 1 - 4/30
0.8667
>>> n = sum(counts.values())
>>> round(-sum(c / n * math.log(c / n) for c in counts.values()), 3)  # plug-in
1.978
>>> round(freqprob.chao_shen_entropy(counts), 3)
2.22
>>> round(freqprob.nsb_entropy(counts), 3), round(freqprob.nsb_entropy(counts, bins=100), 3)
(2.089, 2.299)

Both corrected estimates exceed the plug-in entropy: with four singletons in 30 observations, the sample has likely missed some words. Allowing for 100 possible words raises the NSB estimate further.

Properties

  • Coverage is a quick diagnostic. Close to 1, the sample has seen almost everything that matters, and smoothing has little to do; far from 1, the choice of smoothing method matters.
  • Chao–Shen needs no parameters and is a good default for undersampled data.
  • NSB uses the support size when it is known, and is well suited to samples smaller than the number of possible elements. It is slower than Chao–Shen.

API

freqprob.sample_coverage

sample_coverage(freqdist: FrequencyDistribution) -> float

Estimate the proportion of total probability mass that was observed.

Uses the Good-Turing estimator of the missing mass: the share of probability belonging to unseen types is approximately N1 / N, where N1 is the number of types seen exactly once (singletons) and N the total number of observations. Coverage is the complement, 1 - N1 / N.

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

Mapping of elements to observed counts.

required

Returns:

Type Description
float

The estimated observed mass, in [0, 1]. Returns 0.0 for an empty

float

distribution.

Examples:

>>> from freqprob import sample_coverage
>>> sample_coverage({"a": 10, "b": 5, "c": 3})  # no singletons
1.0
>>> round(sample_coverage({"a": 8, "b": 1, "c": 1}), 3)  # two singletons of 10
0.8

freqprob.chao_shen_entropy

chao_shen_entropy(freqdist: FrequencyDistribution) -> float

Estimate Shannon entropy with the Chao-Shen bias correction.

The naive plug-in entropy underestimates the true entropy when the sample misses probability mass. The Chao-Shen estimator combines Good-Turing coverage adjustment with a Horvitz-Thompson correction for unseen types, which reduces this bias:

H = - sum_i (C p_i) log(C p_i) / (1 - (1 - C p_i) ** N)

where p_i is the relative frequency of type i, C the sample coverage (see sample_coverage), and N the total count.

When every observation is a singleton the Good-Turing coverage is zero and the estimate would collapse to 0. Following Chao and Shen (2003), and reference implementations such as R's entropy::entropy.ChaoShen, the singleton count is then taken as N - 1, which keeps the coverage at 1 / N.

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

Mapping of elements to observed counts.

required

Returns:

Type Description
float

The estimated entropy in nats (natural log). Returns 0.0 for an

float

empty distribution.

Examples:

>>> from freqprob import chao_shen_entropy
>>> import math
>>> # Approaches the true entropy of a uniform distribution when well sampled.
>>> round(chao_shen_entropy({"a": 1000, "b": 1000, "c": 1000}), 3)
1.099
>>> round(math.log(3), 3)
1.099

freqprob.nsb_entropy

nsb_entropy(freqdist: FrequencyDistribution, bins: int | None = None) -> float

Estimate Shannon entropy with the Nemenman-Shafee-Bialek estimator.

The NSB estimator is a Bayesian entropy estimator that averages the posterior mean entropy of a symmetric Dirichlet model over its concentration parameter, using a prior chosen so the a priori entropy is (nearly) uniform. It is well suited to the undersampled regime, where the number of possible types is comparable to or larger than the number of observations.

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

Mapping of elements to observed counts.

required
bins int | None

The size of the assumed alphabet (number of possible types), reserving entropy for types that could occur but were not seen. If None (the default), the number of observed types is used, so no unseen types are assumed. Must be at least the number of observed types.

None

Returns:

Type Description
float

The estimated entropy in nats (natural log), in [0, log(bins)].

float

Returns 0.0 for an empty distribution or a single possible type.

Raises:

Type Description
ValueError

If bins is smaller than the number of observed types.

Examples:

>>> from freqprob import nsb_entropy
>>> import math
>>> # Well-sampled uniform distribution approaches log(K).
>>> round(nsb_entropy({"a": 500, "b": 500, "c": 500, "d": 500}), 2)
1.39
>>> round(math.log(4), 2)
1.39

References

  • Good (1953); Chao & Shen (2003); Nemenman, Shafee & Bialek (2002); Wolpert & Wolf (1995). See References.