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:
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:
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 ¶
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 |
float
|
distribution. |
Examples:
freqprob.chao_shen_entropy ¶
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 |
float
|
empty distribution. |
Examples:
freqprob.nsb_entropy ¶
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
|
Returns:
| Type | Description |
|---|---|
float
|
The estimated entropy in nats (natural log), in |
float
|
Returns |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
References¶
- Good (1953); Chao & Shen (2003); Nemenman, Shafee & Bialek (2002); Wolpert & Wolf (1995). See References.