freqprob

Frequency-based probability estimation with smoothing — a small, well-tested Python library.

MIT·Python 3.10–3.12·GitHub·PyPI·Documentation

The central difficulty in working with frequency counts is the unobserved: how much probability to assign to events that never appeared in the data. FreqProb converts counts into smoothed probability estimates that address this directly — reserving a principled share of mass for what was not seen — and offers a range of classical smoothing methods, from Laplace and Lidstone to Simple Good-Turing and Kneser-Ney, behind a single, consistent interface.

Installation

pip install freqprob

Example

import freqprob

counts = {"the": 100, "cat": 50, "dog": 30, "bird": 10}
model = freqprob.Laplace(counts, bins=10_000, logprob=False)

model("cat")       # 0.0050   observed element
model("elephant")  # 0.0001   unobserved, yet non-zero

A maximum-likelihood estimate would assign probability zero to "elephant", which is undefined under a logarithm and collapses any product of probabilities. Smoothing reserves a small amount of probability mass for the unobserved; the estimator is selected by construction and the call site does not change.

The estimate

observed counts smoothed probability reserved for unobserved
Figure 1. Observed counts (bars) are normalised into a smoothed probability distribution (curve). A share of the total mass is held back for elements that were never observed, shown at right.

Methods

EstimatorAppropriate whenParameter
MLErelative frequencies are wanted, without smoothing
Laplace, Lidstone, ELEsimple additive smoothing sufficesbins, gamma
SimpleGoodTuringthe distribution is heavy-tailed with many rare typesp_value
WittenBella parameter-free discount tied to the number of distinct types is wantedbins
KneserNey, ModifiedKneserNeyestimating an n-gram language modeldiscount
Bayesiana Dirichlet prior is assumedalpha
Interpolateda specific and a general model are combinedlambda_weight
CertaintyDegreemass is reserved by how fully the support has been observed (experimental)bins
Uniform, Randoma non-informative baseline is needed

Beyond text

The problem — estimating probabilities from counts, including for the unobserved — recurs across fields. FreqProb is domain-neutral:

  • Text. n-gram and word-frequency models; term weighting.
  • Ecology. species-abundance data and the classical unseen-species problem.
  • Genomics. k-mer frequencies over the space of possible subsequences.
  • Categorical data. smoothed category priors for features and event counts.

Documentation

Citation

@software{tresoldi_freqprob_2026,
  author  = {Tresoldi, Tiago},
  title   = {FreqProb: A Python library for probability smoothing
             and frequency-based estimation},
  year    = {2026},
  version = {0.6.2},
  url     = {https://github.com/tresoldi/freqprob}
}