Simple Good–Turing¶
SimpleGoodTuring · discounting · estimates the unseen mass from the
singletons
Good–Turing estimation (Good, 1953, after Turing) uses the frequencies of frequencies to decide how much probability belongs to elements not seen. Its central insight is that the mass of everything unseen is estimated by the share of observations that are singletons. Gale and Sampson's (1995) "simple" version smooths the frequencies of frequencies with a log-linear fit, which makes it practical for real data.
Definition¶
Let \(N_r\) be the number of elements seen exactly \(r\) times. The total mass reserved for unseen elements is
and an element seen \(r\) times is given the adjusted count
where \(S\) is the log-linear smoothing of \(N_r\). For small \(r\), the empirical
Turing estimate (\(S(N_r) = N_r\)) is used for as long as it differs
significantly from the smoothed one, at the level p_value. The adjusted
counts are renormalized so that the observed elements share \(1 - p_0\).
An unseen element is given \(p_0 / (B - V)\). The support size \(B\) = bins
defaults to \(V + N_1\): as many unseen elements as singletons.
total_unseen_mass reports \(p_0\).
Example¶
With the example counts, four of the 30 observations are singletons:
>>> import freqprob
>>> counts = dict(the=10, cat=5, sat=4, on=3, mat=2, a=2, dog=1, ran=1, far=1, away=1)
>>> sgt = freqprob.SimpleGoodTuring(counts, logprob=False)
>>> round(sgt.total_unseen_mass, 4) # N1 / N = 4 / 30
0.1333
>>> [round(sgt(w), 4) for w in ("the", "dog", "zebra")]
[0.3066, 0.0247, 0.0333]
The four default unseen elements share \(0.133\), so each gets \(0.033\). Each singleton gives up a quarter of its relative frequency (\(0.033 \to 0.025\)), while the most frequent word gives up less (\(0.333 \to 0.307\)).
Properties¶
- Principled unseen mass. \(N_1/N\) is a direct estimate of the probability that the next observation is new, which is what most applications need.
- Discounts rare elements most. High counts are reliable and barely adjusted.
- Needs a spread of low counts. The log-linear fit needs several distinct
values of \(r\), and the method assumes singletons are present. By default it
proceeds when these assumptions fail;
allow_fail=Falseraises aRuntimeWarninginstead. With no singletons, \(p_0\) isdefault_p0, or \(1/(N + 1)\) if that is not given. binsonly shares the unseen mass out. It does not change \(p_0\); pass the true support size when it is known.- The same estimate of the unseen mass underlies
sample_coverage.
API¶
freqprob.SimpleGoodTuring ¶
SimpleGoodTuring(freqdist: FrequencyDistribution, p_value: float = 0.05, default_p0: float | None = None, bins: int | None = None, logprob: bool = True, allow_fail: bool = True)
Bases: ScoringMethod
Simple Good-Turing smoothing.
Reestimates counts using the frequency of frequencies (how many elements were seen once, twice, and so on), fitting a log-linear model to smooth the tail so that mass can be reserved for unseen elements. This is the practical Gale-Sampson formulation of the Good-Turing method originally developed by Turing and Good, and is a strong general-purpose smoother when the data show a spread of low-frequency counts (many singletons, fewer doubletons, etc.).
This implementation follows those by "maxbane" (2011, https://github.com/maxbane/simplegoodturing/blob/master/sgt.py), Geoffrey Sampson's original C code (1995-2008, https://www.grsampson.net/Resources.html), and the NLTK project (Loper, Bird et al., 2001-2018). Because of minor differences intended to guarantee non-zero probabilities under expected underflow, the reliance on scipy, and the handling of probabilities that are not computable when SGT assumptions are not met, most results will not exactly match the Gale-Sampson "gold standard", though the differences are never expected to be significant.
Note
Since v0.4.0, unseen queries return the per-word probability
p0 / (bins - V) rather than the total mass p0, making the method
compatible with perplexity calculations and consistent with the other
smoothers. Use the total_unseen_mass property to recover p0
(the total mass for all unseen words combined).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqdist
|
FrequencyDistribution
|
Mapping of elements to observed counts. |
required |
p_value
|
float
|
The p-value for the confidence interval of the empirical Turing
estimate, guiding the choice between the Turing estimate |
0.05
|
default_p0
|
float | None
|
Optional probability for unobserved samples ( |
None
|
bins
|
int | None
|
Total support size for computing per-word unseen probabilities. If
|
None
|
logprob
|
bool
|
Return log-probabilities if |
True
|
allow_fail
|
bool
|
If |
True
|
Examples:
SGT needs a spread of low-frequency counts, so use a Zipfian-ish distribution with several singletons. Observed elements are discounted and unseen elements receive a share of the reserved mass:
>>> from freqprob import SimpleGoodTuring
>>> freqdist = {"a": 10, "b": 5, "c": 4, "d": 3, "e": 2,
... "f": 2, "g": 1, "h": 1, "i": 1, "j": 1}
>>> sgt = SimpleGoodTuring(freqdist, logprob=False)
>>> round(float(sgt("a")), 3)
0.307
>>> round(float(sgt("g")), 3) # a singleton
0.025
>>> round(sgt("unknown"), 3) # per-word unseen probability
0.033
>>> round(sgt.total_unseen_mass, 3) # total mass for all unseen words
0.133
Initialize Simple Good-Turing smoothing.
total_unseen_mass
property
¶
Total probability mass reserved for ALL unseen words (p0).
This is the value that Good-Turing actually estimates. The per-word
unseen probability returned by __call__() is this value divided by
the estimated number of unseen types (bins - V_observed).
Returns:
| Type | Description |
|---|---|
float
|
Total probability mass for all unseen words combined. |
Examples:
>>> from freqprob import SimpleGoodTuring
>>> freqdist = {"a": 10, "b": 5, "c": 1, "d": 1, "e": 1}
>>> sgt = SimpleGoodTuring(freqdist, logprob=False)
>>> round(sgt.total_unseen_mass, 3) # total mass for all unseen words
0.167
>>> round(sgt("unknown"), 3) # per-word probability for one unseen word
0.056
References¶
- Good (1953); Gale & Sampson (1995). See References.