Skip to content

Interpolation: Interpolated and Jelinek–Mercer

Interpolated, JelinekMercer · linear interpolation

Interpolation mixes a specific model with a general one: a trigram model with a bigram model, say, or a small in-domain sample with a large background corpus. The specific model is sharper where it has evidence; the general model covers what it has not seen. Jelinek and Mercer (1980) fit the mixing weight on held-out data.

Definition

With \(P_{\text{high}}\) and \(P_{\text{low}}\) the relative frequencies of the two distributions and \(\lambda\) = lambda_weight,

\[ P(w) = \lambda\,P_{\text{high}}(w) + (1 - \lambda)\,P_{\text{low}}(\operatorname{ctx}(w)). \]

When the high-order keys are longer tuples than the low-order ones, the low-order key is the suffix of the high-order n-gram: bigram \((w_2, w_3)\) for trigram \((w_1, w_2, w_3)\). When both have the same type of key, keys are matched directly. JelinekMercer estimates \(\lambda\) by expectation–maximization on a held-out distribution (held_out_dist), maximizing its likelihood; without held-out data it uses lambda_weight.

Example

>>> import freqprob
>>> trigrams = {("the", "big", "cat"): 3, ("a", "big", "dog"): 2}
>>> bigrams = {("big", "cat"): 5, ("big", "dog"): 3, ("small", "cat"): 2}
>>> interp = freqprob.Interpolated(trigrams, bigrams, lambda_weight=0.7, logprob=False)
>>> round(interp(("the", "big", "cat")), 3)  # 0.7 * 3/5 + 0.3 * 5/10
0.57
>>> round(interp(("one", "big", "cat")), 3)  # unseen trigram: 0.3 * 5/10
0.15
>>> held_out = {("the", "big", "cat"): 8, ("a", "big", "dog"): 6}
>>> jm = freqprob.JelinekMercer(trigrams, bigrams, held_out_dist=held_out, logprob=False)
>>> jm.estimated_lambda > 0.5
True

The held-out data repeats the trigrams the high-order model already knows, so EM moves the weight towards it.

Properties

  • Keeps specificity and coverage: unseen high-order n-grams still get the low-order mass.
  • The weight matters. Tune it on held-out data, which JelinekMercer does for you; the held-out data must be disjoint from the training data.
  • One global weight. The original scheme uses weights per context bucket; JelinekMercer fits a single \(\lambda\).
  • Relative frequencies on both levels. To smooth each level as well, combine a smoothed estimator's scores yourself.

API

freqprob.Interpolated

Interpolated(high_order_dist: FrequencyDistribution, low_order_dist: FrequencyDistribution, lambda_weight: float = 0.7, logprob: bool = True)

Bases: ScoringMethod

Linear interpolation smoothing between two frequency distributions.

Blends a higher-order distribution with a lower-order one using a weighted average, lambda_weight * P_high + (1 - lambda_weight) * P_low. This keeps the specificity of the high-order model while borrowing robustness from the smoother low-order model. Two modes are detected automatically from the keys:

  • N-gram interpolation, when the high-order keys are longer tuples than the low-order keys. The lower-order context is taken as the suffix of each high-order n-gram (e.g. bigram (w2, w3) from trigram (w1, w2, w3)).
  • Same-type interpolation, when both distributions share the same key type (strings or equal-length tuples). Keys are matched directly.

Parameters:

Name Type Description Default
high_order_dist FrequencyDistribution

Higher-order frequency distribution mapping elements to counts (e.g. trigrams). For n-gram mode its tuple keys must be at least as long as those of low_order_dist.

required
low_order_dist FrequencyDistribution

Lower-order frequency distribution (e.g. bigrams).

required
lambda_weight float

Interpolation weight 0 <= lambda_weight <= 1 for the high-order model. Higher values favor specificity, lower values favor smoothing. Defaults to 0.7.

0.7
logprob bool

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

True

Examples:

N-gram interpolation (trigrams backed off to bigrams). The score of ('the', 'big', 'cat') is 0.7 * (3/5) + 0.3 * (5/10), and an unseen trigram backs off to 0.3 * (5/10):

>>> trigrams = {("the", "big", "cat"): 3, ("a", "big", "dog"): 2}
>>> bigrams = {("big", "cat"): 5, ("big", "dog"): 3, ("small", "cat"): 2}
>>> interp = Interpolated(trigrams, bigrams, lambda_weight=0.7, logprob=False)
>>> round(interp(("the", "big", "cat")), 3)
0.57
>>> round(interp(("unseen", "big", "cat")), 3)
0.15

Same-type interpolation between two string distributions. The score of "word1" is 0.6 * (10/15) + 0.4 * (3/10):

>>> model1 = {"word1": 10, "word2": 5}
>>> model2 = {"word1": 3, "word3": 7}
>>> interp = Interpolated(model1, model2, lambda_weight=0.6, logprob=False)
>>> round(interp("word1"), 3)
0.52
>>> round(interp("word2"), 3)
0.2
>>> round(interp("word3"), 3)
0.28
Note

The mode is inferred from the key types, so mixed tuple lengths within a single distribution raise an error. Unseen high-order n-grams back off to the low-order model, and all probabilities are floored at 1e-10 for numerical stability. The weight lambda_weight is best tuned on held-out data.

Initialize Interpolated smoothing.

freqprob.JelinekMercer

JelinekMercer(high_order_dist: FrequencyDistribution, low_order_dist: FrequencyDistribution, held_out_dist: FrequencyDistribution | None = None, lambda_weight: float = 0.5, em_iterations: int = 10, logprob: bool = True)

Bases: Interpolated

Jelinek-Mercer smoothing with an EM-estimated interpolation weight.

Jelinek-Mercer smoothing linearly interpolates a higher-order model with a lower-order one, exactly as Interpolated does, but chooses the interpolation weight lambda by deleted interpolation: given a held-out distribution, the weight is fitted with the Expectation-Maximization algorithm to maximize the likelihood of that held-out data. Without held-out data it behaves as fixed-weight interpolation using lambda_weight.

Parameters:

Name Type Description Default
high_order_dist FrequencyDistribution

Higher-order frequency distribution (e.g. trigrams).

required
low_order_dist FrequencyDistribution

Lower-order frequency distribution (e.g. bigrams).

required
held_out_dist FrequencyDistribution | None

Optional held-out frequency distribution used to fit lambda by EM. If None (the default), the supplied lambda_weight is used unchanged.

None
lambda_weight float

Interpolation weight 0 <= lambda_weight <= 1 for the high-order model. Used directly when there is no held-out data, and as the EM starting point otherwise. Defaults to 0.5.

0.5
em_iterations int

Number of EM iterations when fitting lambda from held-out data (a positive integer). Defaults to 10.

10
logprob bool

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

True

Attributes:

Name Type Description
estimated_lambda

The interpolation weight actually used — either the EM-fitted value or lambda_weight when no held-out data was given.

Examples:

With no held-out data the estimated weight is exactly lambda_weight, and scoring matches fixed-weight interpolation:

>>> trigrams = {("the", "big", "cat"): 3, ("a", "big", "dog"): 2}
>>> bigrams = {("big", "cat"): 5, ("big", "dog"): 3}
>>> jm = JelinekMercer(trigrams, bigrams, lambda_weight=0.6, logprob=False)
>>> round(jm.estimated_lambda, 2)
0.6
>>> round(jm(("the", "big", "cat")), 3)   # 0.6 * (3/5) + 0.4 * (5/8)
0.61

Held-out data that the high-order model predicts well pulls the weight up:

>>> held_out = {("the", "big", "cat"): 8, ("a", "big", "dog"): 6}
>>> jm = JelinekMercer(trigrams, bigrams, held_out_dist=held_out, logprob=False)
>>> jm.estimated_lambda > 0.5
True
Note

EM fits a single global lambda (not the per-context bucketed weights of the original Jelinek-Mercer scheme). The held-out data should be disjoint from the training data for the estimate to be meaningful.

Initialize Jelinek-Mercer smoothing, fitting lambda if held-out data is given.

References