Skip to content

Katz and Stupid back-off

KatzBackoff, StupidBackoff · n-gram back-off · bigram input

Back-off models use the bigram estimate when the bigram has been seen, and fall back ("back off") to the unigram model when it has not. Katz (1987) discounts the observed counts with Good–Turing and normalizes the back-off, so the result is a probability distribution. Stupid Back-off (Brants et al., 2007) skips the discounting and normalization, which makes it very cheap at web scale, but its scores are not probabilities.

Definition

For a bigram \((v, w)\) with count \(c = c(v, w)\) and context total \(c(v)\), Katz back-off gives

\[ P(w \mid v) = \begin{cases} d_c\,\dfrac{c}{c(v)} & \text{if } c > 0,\\[1ex] \alpha(v)\,P_{\text{uni}}(w) & \text{otherwise,} \end{cases} \]

where \(d_c\) is the Good–Turing discount ratio for counts \(1 \leq c \leq k\) (\(d_c = 1\) above \(k\)) and \(\alpha(v)\) makes each context's distribution sum to one. Stupid Back-off scores

\[ S(w \mid v) = \begin{cases} c / c(v) & \text{if } c > 0,\\ \alpha\,P_{\text{uni}}(w) & \text{otherwise,} \end{cases} \]

with a fixed \(\alpha\) = alpha, 0.4 by default.

Example

Katz back-off needs a spread of low counts to estimate its discounts:

>>> import freqprob
>>> bigrams = {
...     ("the", "cat"): 4,
...     ("the", "dog"): 3,
...     ("a", "cat"): 2,
...     ("a", "dog"): 2,
...     ("my", "cat"): 1,
...     ("my", "dog"): 1,
...     ("my", "bird"): 1,
...     ("his", "fish"): 1,
...     ("her", "fish"): 1,
...     ("its", "bird"): 1,
... }
>>> katz = freqprob.KatzBackoff(bigrams, logprob=False)
>>> [round(katz(b), 3) for b in [("the", "cat"), ("my", "cat"), ("my", "fish")]]
[0.571, 0.222, 0.333]
>>> sb = freqprob.StupidBackoff(bigrams, logprob=False)
>>> [round(sb(b), 3) for b in [("the", "cat"), ("my", "cat"), ("my", "fish")]]
[0.571, 0.333, 0.047]

Under Katz back-off, the singletons after "my" are discounted from \(1/3\) to \(0.222\), and the freed mass goes to unseen words such as "fish". Stupid Back-off leaves the observed scores alone and scores "fish" after "my" as \(0.4 \times P_{\text{uni}}(\text{fish}) = 0.4 \times 2/17\).

Properties

  • Katz is normalized; Stupid Back-off is not, so its scores are for ranking and not for perplexity (see Pitfalls).
  • Katz needs varied low counts. With too few distinct counts, some discounts default to 1, leaving little mass for unseen bigrams.
  • Bigrams only. Keys must be (context, word) pairs.

API

freqprob.KatzBackoff

KatzBackoff(freqdist: FrequencyDistribution, k: int = 5, logprob: bool = True)

Bases: BigramBackoff

Katz back-off bigram smoothing.

Katz's method discounts observed bigram counts with Good-Turing and redistributes the freed mass to a lower-order unigram model through normalized back-off weights. Following Katz (1987), only counts up to a threshold k are discounted (counts above k are assumed reliable). For a bigram (context, word) with count c and context total C,

P(word | context) = d_c * c / C if c > 0,

P(word | context) = alpha(context) * P_uni(word) otherwise,

where d_c is the Good-Turing discount ratio for count c and alpha(context) is chosen so each context's distribution sums to one. It is one of the classic n-gram smoothing methods and pairs naturally with freqprob.SimpleGoodTuring, which uses the same count-of-counts machinery.

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

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

required
k int

Good-Turing discounting is applied to counts 1 <= c <= k; larger counts are left undiscounted. Defaults to 5.

5
logprob bool

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

True

Examples:

Reliable (high) counts are barely discounted, rare counts are discounted more, and unseen bigrams back off to the unigram model:

>>> bigram_counts = {
...     ("the", "cat"): 4, ("the", "dog"): 3,
...     ("a", "cat"): 2, ("a", "dog"): 2,
...     ("my", "cat"): 1, ("my", "dog"): 1, ("my", "bird"): 1,
...     ("his", "fish"): 1, ("her", "fish"): 1, ("its", "bird"): 1,
... }
>>> katz = KatzBackoff(bigram_counts, logprob=False)
>>> round(katz(("the", "cat")), 3)    # reliable count, undiscounted
0.571
>>> round(katz(("my", "cat")), 3)     # singleton, Good-Turing discounted
0.222
>>> round(katz(("my", "fish")), 3)    # unseen in context, backs off
0.333
Note

This implementation assumes bigram input. Good-Turing discounting needs a spread of low counts (singletons, doubletons, ...) to estimate discounts; with too few distinct counts some discounts default to 1 (no discounting), leaving little or no mass for unseen bigrams.

Initialize Katz back-off smoothing.

freqprob.StupidBackoff

StupidBackoff(freqdist: FrequencyDistribution, alpha: float = 0.4, logprob: bool = True)

Bases: BigramBackoff

Stupid Back-off bigram scoring.

A deliberately simple, unnormalized scheme introduced by Brants et al. (2007) for web-scale language modeling. An observed bigram scores its relative frequency; an unseen bigram scores a fixed weight alpha times the lower-order unigram score:

S(word | context) = c(context, word) / c(context) if observed,

S(word | context) = alpha * P_uni(word) otherwise.

Because it skips the normalization Katz back-off performs, the scores do not form a probability distribution (they do not sum to one), but they are very cheap to compute and work well for ranking when data is plentiful.

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

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

required
alpha float

Back-off weight applied when falling back to the unigram model. Defaults to 0.4 (the value recommended by Brants et al.).

0.4
logprob bool

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

True

Examples:

Observed bigrams score their relative frequency; unseen bigrams are penalized by alpha and scored from the unigram model:

>>> bigram_counts = {
...     ("the", "cat"): 5, ("the", "dog"): 3, ("a", "cat"): 2,
...     ("a", "dog"): 1, ("big", "cat"): 1, ("small", "dog"): 1,
... }
>>> sb = StupidBackoff(bigram_counts, logprob=False)
>>> round(sb(("the", "cat")), 3)      # 5 / 8
0.625
>>> round(sb(("a", "dog")), 3)        # 1 / 3
0.333
>>> round(sb(("big", "dog")), 3)      # 0.4 * P_uni("dog")
0.154
Note

Stupid Back-off returns scores, not normalized probabilities, so metrics that assume a valid distribution (such as perplexity) are not meaningful here. Use it for ranking and comparison, where only the relative ordering of scores matters.

Initialize Stupid Back-off scoring.

References

  • Katz (1987); Brants et al. (2007). See References.