Skip to content

Baselines: MLE, Uniform, Random

MLE, Uniform, Random · no smoothing · reference points

These three estimators are the reference points against which smoothing is judged. MLE uses the counts as they are, Uniform ignores them, and Random replaces them with random ones.

Definition

The maximum-likelihood estimate is the relative frequency:

\[ P_{\text{MLE}}(w) = (1 - p_0)\,\frac{c(w)}{N}, \qquad P_{\text{MLE}}(\text{unseen}) = p_0, \]

with \(p_0\) = unobs_prob, 0 by default. Uniform gives every observed element \((1 - p_0)/V\), whatever its count. Random draws a random count between the smallest and largest observed counts for each element, and normalizes those like MLE; seed makes it reproducible.

Example

With the example counts:

>>> import freqprob
>>> counts = dict(the=10, cat=5, sat=4, on=3, mat=2, a=2, dog=1, ran=1, far=1, away=1)
>>> mle = freqprob.MLE(counts, logprob=False)
>>> round(mle("the"), 4), round(mle("dog"), 4), mle("zebra")
(0.3333, 0.0333, 0.0)
>>> freqprob.Uniform(counts, logprob=False)("the")
0.1
>>> random = freqprob.Random(counts, seed=0, logprob=False)
>>> round(sum(random(w) for w in counts), 6)
1.0

Reserving mass with unobs_prob rescales the observed elements:

>>> mle = freqprob.MLE(counts, unobs_prob=0.1, logprob=False)
>>> round(mle("the"), 4), mle("zebra")
(0.3, 0.1)

Properties

  • MLE is unbiased for the observed elements and gives nothing to the rest, which makes it unsuitable whenever unseen elements can occur.
  • unobs_prob is a per-element score: every unseen element receives \(p_0\) (see Pitfalls).
  • Uniform and Random are controls. A smoothing method that does no better than them on held-out data is not using the counts well.

API

freqprob.MLE

MLE(freqdist: FrequencyDistribution, unobs_prob: Probability = 0.0, logprob: bool = True)

Bases: ScoringMethod

Maximum Likelihood Estimation.

Estimates each element's probability as its relative frequency in the observed data — the most intuitive baseline. For counts c_i with total N, each observed element gets (1 - p0) * c_i / N and any unobserved element gets the reserved mass p0 (0.0 by default, i.e. unseen elements score zero).

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

Mapping of elements to observed counts.

required
unobs_prob Probability

Reserved probability mass p0 for unobserved elements (0.0 <= p0 <= 1.0). Defaults to 0.0.

0.0
logprob bool

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

True

Examples:

Probabilities are relative frequencies; unseen elements score zero:

>>> mle = MLE({"apple": 6, "banana": 3, "cherry": 1}, logprob=False)
>>> mle("apple")
0.6
>>> mle("banana")
0.3
>>> mle("unknown")
0.0

Reserving mass for unseen elements rescales the observed ones:

>>> smoothed = MLE({"apple": 6, "banana": 3, "cherry": 1},
...                unobs_prob=0.1, logprob=False)
>>> round(smoothed("apple"), 3)
0.54
>>> smoothed("unknown")
0.1
Note

MLE underlies most smoothing methods but assigns zero probability to unseen events, which is problematic for sparse data. Prefer Laplace, Lidstone, or a more advanced method when unseen elements are likely.

Initialize MLE distribution.

freqprob.Uniform

Uniform(freqdist: FrequencyDistribution, unobs_prob: Probability = 0.0, logprob: bool = True)

Bases: ScoringMethod

Uniform probability distribution.

The simplest baseline: assign equal probability to every observed element, ignoring the counts entirely. Useful as a non-informative prior or as a reference point when comparing smoothing methods.

For a vocabulary of size V with reserved mass p0, each observed element gets (1 - p0) / V and any unobserved element gets p0.

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

Mapping of elements to observed counts. The counts are ignored; only the number of distinct elements matters.

required
unobs_prob Probability

Reserved probability mass p0 for unobserved elements (0.0 <= p0 <= 1.0). Defaults to 0.0.

0.0
logprob bool

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

True

Examples:

Counts are ignored, so both observed elements score the same:

>>> uniform = Uniform({"apple": 10, "banana": 1}, unobs_prob=0.1, logprob=False)
>>> uniform("apple")
0.45
>>> uniform("banana")
0.45
>>> uniform("cherry")  # unobserved
0.1
Note

Because it discards frequency information, Uniform is meant as a baseline. Use MLE, Lidstone, or Laplace when counts should matter.

Initialize Uniform distribution.

freqprob.Random

Random(freqdist: FrequencyDistribution, unobs_prob: Probability = 0.0, logprob: bool = True, seed: int | None = None)

Bases: ScoringMethod

Random probability distribution.

Assigns pseudo-random probabilities, useful for testing and as a randomized baseline. Each element is given a random count drawn from the observed count range, and those counts are then normalized like MLE. A fixed seed makes the result reproducible.

Parameters:

Name Type Description Default
freqdist FrequencyDistribution

Mapping of elements to observed counts. Only the min/max counts are used, to bound the random counts.

required
unobs_prob Probability

Reserved probability mass for unobserved elements. Defaults to 0.0.

0.0
logprob bool

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

True
seed int | None

Seed for the random number generator, for reproducible results. Defaults to None.

None

Examples:

The probabilities are random but form a valid distribution, and a fixed seed is reproducible:

>>> freqdist = {"apple": 5, "banana": 2, "cherry": 8}
>>> a = Random(freqdist, seed=123, logprob=False)
>>> b = Random(freqdist, seed=123, logprob=False)
>>> a("apple") == b("apple")
True
>>> round(sum(a(e) for e in freqdist), 6)
1.0
Note

Primarily for testing, debugging, and providing a randomized baseline for comparison. It is not a smoothing method in any meaningful sense.

Initialize Random distribution.

References

Standard baselines; see References.