freqprob

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

v0.9.0·DOI 10.5281/zenodo.23917393·MIT·Python 3.10–3.12·GitHub·PyPI

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

Quick start

>>> import freqprob
>>> counts = {"the": 100, "cat": 50, "dog": 30, "bird": 10}
>>> freqprob.MLE(counts, logprob=False)("elephant")
0.0
>>> laplace = freqprob.Laplace(counts, bins=10_000,
...                            logprob=False)
>>> round(laplace("cat"), 4)  # observed
0.005
>>> round(laplace("elephant"), 4)  # unseen
0.0001

A raw relative-frequency estimate gives the unseen "elephant" probability 0, which breaks any model that multiplies probabilities or takes their logarithm. Smoothing reserves some mass for what has not been seen yet; every estimator is built from counts and called the same way, so swapping methods does not change the call site.

The estimate

relative frequency (MLE) Simple Good–Turing estimate mass reserved for the unseen
Figure 1. Computed with FreqProb on the 143 words of The Zen of Python (import this): 85 distinct words, 60 of them seen only once. Bars show the relative frequency of the eleven words seen at least three times; the line shows their Simple Good–Turing estimates. Because so many words are singletons, the estimator infers that many more remain unseen and moves 42% of the probability mass to them (strip below), taking it mostly from the rarer observed words.

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
AbsoluteDiscounting, PitmanYorbigram discounting with back-off to a unigram modeldiscount, strength
KatzBackoff, StupidBackoffbigram back-off, normalised (Katz) or fast and unnormalised (Stupid)k, alpha
Bayesiana Dirichlet prior is assumedalpha
Interpolated, JelinekMercera specific and a general model are combined, with a fixed or EM-fitted weightlambda_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

Related projects

  • nltk.probability. several of the same estimators (Laplace, Lidstone, ELE, Simple Good–Turing, Kneser–Ney, Witten–Bell) within a broader NLP toolkit; FreqProb is standalone, works on any frequency counts, and is tested against NLTK.
  • ASymCat. uses FreqProb for smoothing and bias-corrected entropy in its directional measures of association between categorical variables.

Citation

Please cite the version you used. See How to cite for APA and classic BibTeX forms, and References for the papers behind each method.

@software{tresoldi_freqprob,
  author  = {Tresoldi, Tiago},
  title   = {{FreqProb}: A {Python} library for probability smoothing
             and frequency-based estimation},
  year    = {2026},
  version = {0.9.0},
  doi     = {10.5281/zenodo.23917393},
  url     = {https://freqprob.tresoldi.org/}
}