Additive smoothing: Laplace, Lidstone, ELE, Bayesian¶
Lidstone, Laplace, ELE, Bayesian · additive smoothing
Additive smoothing adds a pseudo-count to every possible element before normalizing, so that no element has probability zero. It is the oldest and simplest form of smoothing (Laplace's rule of succession), and a robust default when the number of possible elements is known.
Definition¶
With pseudo-count \(\gamma\) and \(B\) possible elements (bins),
| Estimator | \(\gamma\) |
|---|---|
Lidstone(counts, gamma, bins) |
any \(\gamma \geq 0\) |
Laplace(counts, bins) |
1 ("add one") |
ELE(counts, bins) |
0.5 (the expected likelihood estimate; Jeffreys' prior) |
This is the posterior mean of a multinomial under a symmetric Dirichlet prior
with concentration \(\gamma\). bins defaults to the observed vocabulary \(V\);
see the properties below.
Bayesian smoothing¶
Bayesian(counts, alpha) computes the same estimate with \(\gamma = \alpha\) and
\(B = V\), framing \(\alpha\) as the strength of a Dirichlet prior. It has no
bins argument.
Example¶
With the example counts and a support of 100 possible words:
>>> import freqprob
>>> counts = dict(the=10, cat=5, sat=4, on=3, mat=2, a=2, dog=1, ran=1, far=1, away=1)
>>> for name, model in [
... ("Laplace", freqprob.Laplace(counts, bins=100, logprob=False)),
... ("ELE", freqprob.ELE(counts, bins=100, logprob=False)),
... ("Lidstone 0.1", freqprob.Lidstone(counts, gamma=0.1, bins=100, logprob=False)),
... ]:
... print(name, [round(model(w), 4) for w in ("the", "dog", "zebra")])
Laplace [0.0846, 0.0154, 0.0077]
ELE [0.1313, 0.0187, 0.0063]
Lidstone 0.1 [0.2525, 0.0275, 0.0025]
With 30 observations and 100 possible words, add-one smoothing gives the 90 unseen words \(90/130 \approx 69\%\) of the mass, and the most frequent word falls from \(1/3\) to \(0.085\). A smaller \(\gamma\) stays closer to the counts.
Properties¶
- Simple and always positive. Every element gets at least \(\gamma/(N + B\gamma)\).
- Over-smooths large supports. The reserved mass is \((B - V)\gamma / (N + B\gamma)\), which approaches 1 when \(B\) is much larger than \(N\). Add-one smoothing is known to perform poorly for language data for this reason; prefer a small \(\gamma\) or Simple Good–Turing.
binsmust be the support size. With the default \(B = V\), the observed elements already sum to one, and each unseen element is scored on top; see Pitfalls and Choosebins.- Treats all unseen elements alike, and all observed elements with the same count alike.
API¶
freqprob.Lidstone ¶
Lidstone(freqdist: FrequencyDistribution, gamma: float, bins: int | None = None, logprob: bool = True)
Bases: ScoringMethod
Lidstone additive smoothing probability distribution.
Additive smoothing tackles the zero-probability problem by adding a virtual
count gamma to every possible element before normalizing. For counts
c_i, total N, and B bins, each observed element gets
(c_i + gamma) / (N + B * gamma) and any unobserved element gets
gamma / (N + B * gamma). This is the posterior mean under a symmetric
Dirichlet prior with concentration gamma. Use it when you need a simple,
principled way to keep unseen elements from scoring zero, and tune gamma
to trade off between staying close to the data (small gamma) and a more
uniform distribution (large gamma).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqdist
|
FrequencyDistribution
|
Mapping of elements to observed counts. |
required |
gamma
|
float
|
Additive smoothing parameter ( |
required |
bins
|
int | None
|
Total number of possible elements. If |
None
|
logprob
|
bool
|
Return log-probabilities if |
True
|
Examples:
Add-gamma smoothing gives unseen elements non-zero probability:
>>> from freqprob import Lidstone
>>> freqdist = {"apple": 3, "banana": 1}
>>> lidstone = Lidstone(freqdist, gamma=1.0, logprob=False)
>>> round(lidstone("apple"), 3) # (3+1)/(4+2*1) = 4/6
0.667
>>> round(lidstone("banana"), 3) # (1+1)/(4+2*1) = 2/6
0.333
>>> round(lidstone("cherry"), 3) # 1/(4+2*1) = 1/6
0.167
A larger gamma pulls probabilities toward uniform:
>>> smooth = Lidstone(freqdist, gamma=2.0, logprob=False)
>>> smooth("apple") # (3+2)/(4+2*2) = 5/8
0.625
>>> smooth("cherry") # 2/(4+2*2) = 2/8
0.25
A larger bins reserves more mass for potential unseen elements:
>>> lidstone_big = Lidstone(freqdist, gamma=1.0, bins=1000, logprob=False)
>>> round(lidstone_big("apple"), 4) # (3+1)/(4+1000) = 4/1004
0.004
>>> round(lidstone_big("unseen"), 4) # 1/(4+1000) = 1/1004
0.001
Note
The choice of gamma is a bias-variance tradeoff: small values stay
close to MLE (low bias, high variance) while large values approach a
uniform distribution (higher bias, lower variance).
Initialize Lidstone smoothing.
freqprob.Laplace ¶
Bases: Lidstone
Laplace (add-one) smoothing probability distribution.
The special case of Lidstone smoothing with gamma = 1.0, also called
"add-one smoothing." Each observed element gets (c_i + 1) / (N + B) and
any unobserved element gets 1 / (N + B) — i.e. one virtual observation
added to every possible element, corresponding to a uniform Dirichlet prior.
It is the most common additive smoother: reach for it when you want a simple,
reasonable default that never assigns zero probability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqdist
|
FrequencyDistribution
|
Mapping of elements to observed counts. |
required |
bins
|
int | None
|
Total number of possible elements. If |
None
|
logprob
|
bool
|
Return log-probabilities if |
True
|
Examples:
Add one to every count, then normalize:
>>> from freqprob import Laplace
>>> freqdist = {"red": 3, "blue": 2, "green": 1}
>>> laplace = Laplace(freqdist, logprob=False)
>>> round(laplace("red"), 3) # (3+1)/(6+3) = 4/9
0.444
>>> round(laplace("blue"), 3) # (2+1)/(6+3) = 3/9
0.333
>>> round(laplace("yellow"), 3) # 1/(6+3) = 1/9
0.111
Initialize Laplace smoothing.
freqprob.ELE ¶
Bases: Lidstone
Expected Likelihood Estimation probability distribution.
The special case of Lidstone smoothing with gamma = 0.5, corresponding
to the Jeffreys prior for multinomial distributions. Each observed element
gets (c_i + 0.5) / (N + 0.5 * B) and any unobserved element gets
0.5 / (N + 0.5 * B) — half a virtual observation per element. This is a
middle ground between MLE and Laplace: use it when add-one smoothing feels
too aggressive but you still want unseen elements handled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqdist
|
FrequencyDistribution
|
Mapping of elements to observed counts. |
required |
bins
|
int | None
|
Total number of possible elements. If |
None
|
logprob
|
bool
|
Return log-probabilities if |
True
|
Examples:
Add half a count to each element, then normalize:
>>> from freqprob import ELE
>>> freqdist = {"cat": 4, "dog": 2}
>>> ele = ELE(freqdist, logprob=False)
>>> round(ele("cat"), 3) # (4+0.5)/(6+0.5*2) = 4.5/7
0.643
>>> round(ele("dog"), 3) # (2+0.5)/(6+0.5*2) = 2.5/7
0.357
>>> round(ele("bird"), 3) # 0.5/(6+0.5*2) = 0.5/7
0.071
Note
ELE applies less smoothing than Laplace (gamma = 1), so it stays
closer to the observed frequencies while still avoiding zero
probabilities.
Initialize Expected Likelihood Estimation.
freqprob.Bayesian ¶
Bases: ScoringMethod
Bayesian smoothing with a Dirichlet prior.
Estimates probabilities as the posterior mean under a symmetric Dirichlet
prior, adding alpha pseudocounts to each possible outcome. Each observed
element gets (c_i + alpha) / (N + V * alpha) and any unobserved element
gets alpha / (N + V * alpha), where N is the total count and V
the support size. This is numerically identical to Lidstone smoothing with
gamma = alpha, but framed so that alpha reads as prior belief: use it
when you want a theoretically principled smoother whose strength you can tune
from near-MLE (small alpha) to strongly uniform (large alpha).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqdist
|
FrequencyDistribution
|
Mapping of elements to observed counts. |
required |
alpha
|
float
|
Dirichlet concentration parameter ( |
1.0
|
logprob
|
bool
|
Return log-probabilities if |
True
|
Examples:
Bayesian smoothing with a uniform prior (alpha = 1):
>>> from freqprob import Bayesian
>>> freqdist = {"apple": 8, "banana": 4, "cherry": 1}
>>> bayes = Bayesian(freqdist, alpha=1.0, logprob=False)
>>> bayes("apple") # (8+1)/(13+3*1) = 9/16
0.5625
>>> bayes("banana") # (4+1)/(13+3*1) = 5/16
0.3125
>>> bayes("unseen") # 1/(13+3*1) = 1/16
0.0625
A larger alpha smooths more strongly toward uniform:
>>> bayes_smooth = Bayesian(freqdist, alpha=2.0, logprob=False)
>>> round(bayes_smooth("apple"), 3) # (8+2)/(13+3*2) = 10/19
0.526
>>> round(bayes_smooth("unseen"), 3) # 2/(13+3*2) = 2/19
0.105
A small alpha stays closer to the observed frequencies:
>>> bayes_minimal = Bayesian(freqdist, alpha=0.1, logprob=False)
>>> round(bayes_minimal("apple"), 3) # (8+0.1)/(13+3*0.1) = 8.1/13.3
0.609
Note
A positive alpha is required; passing alpha <= 0 raises
ValueError. Equivalent to Lidstone with gamma = alpha.
Initialize Bayesian smoothing.
References¶
- Laplace (1814); Lidstone (1920); Manning & Schütze (1999, §6.2); MacKay & Peto (1995) for the Dirichlet view. See References.