Skip to content

Absolute discounting and Pitman–Yor

AbsoluteDiscounting, PitmanYor · n-gram smoothing · bigram input

Absolute discounting (Ney, Essen & Kneser, 1994) subtracts a fixed discount from every observed bigram count and gives the freed mass to a unigram model. It is Kneser–Ney without the continuation counts. The Pitman–Yor estimator adds a concentration parameter, following the Pitman–Yor process (Pitman & Yor, 1997) that underlies hierarchical Bayesian language models (Teh, 2006).

Definition

For a bigram \((v, w)\) with count \(c(v, w)\) and context total \(c(v)\), absolute discounting with discount \(d\) gives

\[ P(w \mid v) = \frac{\max\big(c(v, w) - d,\ 0\big)}{c(v)} + \frac{d\,N_{1+}(v)}{c(v)}\,P_{\text{uni}}(w), \]

where \(P_{\text{uni}}\) is the relative frequency of \(w\) as the second element of a bigram. With concentration \(\theta\) = strength and \(t = N_{1+}(v)\), Pitman–Yor gives

\[ P(w \mid v) = \frac{\max\big(c(v, w) - d,\ 0\big) + (\theta + d\,t)\,P_{\text{uni}}(w)}{\theta + c(v)}, \]

which is absolute discounting when \(\theta = 0\). Unseen contexts back off to \(P_{\text{uni}}(w)\).

Example

With the example bigrams:

>>> import freqprob
>>> bigrams = {
...     ("the", "cat"): 5,
...     ("the", "dog"): 3,
...     ("a", "cat"): 2,
...     ("a", "dog"): 1,
...     ("big", "cat"): 1,
...     ("small", "dog"): 1,
... }
>>> queries = [("the", "cat"), ("big", "dog"), ("new", "cat")]
>>> ad = freqprob.AbsoluteDiscounting(bigrams, discount=0.75, logprob=False)
>>> [round(ad(b), 3) for b in queries]
[0.647, 0.288, 0.615]
>>> py = freqprob.PitmanYor(bigrams, discount=0.75, strength=1.0, logprob=False)
>>> [round(py(b), 3) for b in queries]
[0.643, 0.337, 0.615]

"cat" is the more frequent word (\(8/13\)), so unlike under Kneser–Ney it keeps that weight in a new context. The concentration parameter moves more mass to the unigram model: "dog" after "big" rises from 0.288 to 0.337.

Properties

  • Normalized over the words of each context.
  • Uses raw unigram frequencies for the back-off, so a frequent word keeps its weight even if it occurs in few contexts; Kneser–Ney corrects this.
  • strength controls the mass given to the unigram model independently of the discount; it must exceed \(-d\).
  • Bigrams only, as for Kneser–Ney.

API

freqprob.AbsoluteDiscounting

AbsoluteDiscounting(freqdist: FrequencyDistribution, discount: float = 0.75, logprob: bool = True)

Bases: BigramBackoff

Absolute-discounting bigram smoothing.

Subtracts a fixed discount d from every observed bigram count and redistributes the freed mass to a lower-order unigram model. For a bigram (context, word) with count c and context total C,

P(word | context) = max(c - d, 0) / C + (d * N1+(context) / C) * P_uni(word)

where N1+(context) is the number of distinct words seen after the context and P_uni is the unigram maximum-likelihood distribution. This is the classic Ney-Essen-Kneser discounting scheme and the foundation Kneser-Ney builds on — the difference is that Kneser-Ney replaces P_uni with a continuation distribution. Reach for absolute discounting when you want simple, well-understood discounting without Kneser-Ney's continuation weighting.

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

Frequency distribution mapping (context, word) bigrams to counts. Non-bigram keys are ignored.

required
discount float

Absolute discounting parameter 0 <= d < 1. Common values are 0.5-0.8. Defaults to 0.75.

0.75
logprob bool

Return log-probabilities if True (the default), otherwise plain probabilities.

True

Examples:

Observed bigrams are discounted and interpolated with the unigram model, while unseen bigrams back off to it:

>>> bigram_counts = {
...     ("the", "cat"): 5, ("the", "dog"): 3, ("a", "cat"): 2,
...     ("a", "dog"): 1, ("big", "cat"): 1, ("small", "dog"): 1,
... }
>>> ad = AbsoluteDiscounting(bigram_counts, discount=0.75, logprob=False)
>>> round(ad(("the", "cat")), 3)
0.647
>>> round(ad(("a", "cat")), 3)
0.724
>>> round(ad(("big", "dog")), 3)      # unseen word in a known context
0.288
>>> round(ad(("new", "cat")), 3)      # unseen context backs off to unigram
0.615
Note

This implementation assumes bigram input. Unlike Kneser-Ney it uses raw unigram frequencies for the lower-order model, so a frequent word keeps its weight even if it appears in few distinct contexts.

Initialize absolute-discounting smoothing.

freqprob.PitmanYor

PitmanYor(freqdist: FrequencyDistribution, discount: float = 0.75, strength: float = 1.0, logprob: bool = True)

Bases: BigramBackoff

Pitman-Yor process bigram smoothing.

A two-parameter generalization of absolute discounting based on the Pitman-Yor process. With discount d and concentration (strength) theta, an observed bigram (context, word) with count c, context total C, and t distinct words seen after the context scores

P(word | context) = (c - d + (theta + d * t) * P_uni(word)) / (theta + C)

and a word unseen in the context gets (theta + d * t) / (theta + C) * P_uni(word). The concentration theta controls how much mass is reserved for the base distribution independently of the discount: theta = 0 recovers absolute discounting, while larger theta pulls estimates toward the unigram model. Pitman-Yor priors match the power-law behavior of natural language and are the basis of the hierarchical Pitman-Yor language model, which subsumes Kneser-Ney as a special case.

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

Frequency distribution mapping (context, word) bigrams to counts. Non-bigram keys are ignored.

required
discount float

Discount parameter 0 <= d < 1. Defaults to 0.75.

0.75
strength float

Concentration parameter theta > -discount. Defaults to 1.0. 0 recovers absolute discounting.

1.0
logprob bool

Return log-probabilities if True (the default), otherwise plain probabilities.

True

Examples:

Larger counts still dominate, but the concentration parameter reserves extra mass for the unigram base distribution:

>>> bigram_counts = {
...     ("the", "cat"): 5, ("the", "dog"): 3, ("a", "cat"): 2,
...     ("a", "dog"): 1, ("big", "cat"): 1, ("small", "dog"): 1,
... }
>>> py = PitmanYor(bigram_counts, discount=0.75, strength=1.0, logprob=False)
>>> round(py(("the", "cat")), 3)
0.643
>>> round(py(("a", "cat")), 3)
0.697
>>> round(py(("big", "dog")), 3)      # unseen word in a known context
0.337
>>> round(py(("new", "cat")), 3)      # unseen context backs off to unigram
0.615
Note

This implementation is a single-level (bigram) Pitman-Yor model using the unigram maximum-likelihood distribution as its base measure and the number of observed types as the table count. It is a practical approximation of the full hierarchical Pitman-Yor process.

Initialize Pitman-Yor smoothing.

References

  • Ney, Essen & Kneser (1994); Pitman & Yor (1997); Teh (2006). See References.