FreqProb
Turn frequency counts into probability estimates.
FreqProb converts a mapping of elements to observed counts into smoothed probabilities that handle unseen elements sensibly. It is a general-purpose statistical tool: natural language processing is one use among many, alongside information retrieval, ecology, genomics and categorical data.
If you use FreqProb in research, please cite it: doi:10.5281/zenodo.23917393 (version 0.9.0; see Citation).
Install
pip install freqprob
FreqProb needs only NumPy and SciPy.
Quick start
>>> import freqprob
>>> counts = {"the": 100, "cat": 50, "dog": 30, "bird": 10}
>>> freqprob.MLE(counts, logprob=False)("elephant") # raw relative frequency
0.0
>>> laplace = freqprob.Laplace(counts, bins=10_000, logprob=False)
>>> round(laplace("cat"), 4) # an observed element
0.005
>>> round(laplace("elephant"), 4) # an unseen element still gets probability mass
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, and FreqProb offers
a dozen well-tested ways to do it behind one interface.
Scoring and evaluating
Every estimator is built from a frequency distribution and then called to score
an element. predict scores many at once, metrics such as perplexity
evaluate a model, and sample-based estimators work directly on the counts:
>>> import freqprob
>>> bigrams = {("the", "cat"): 5, ("the", "dog"): 3, ("a", "cat"): 2}
>>> kneser_ney = freqprob.KneserNey(bigrams, discount=0.75, logprob=False)
>>> [round(p, 3) for p in kneser_ney.predict([("the", "cat"), ("a", "dog")])]
[0.656, 0.125]
>>> round(
... freqprob.perplexity(freqprob.KneserNey(bigrams, discount=0.75), [("the", "cat"), ("a", "cat")]),
... 3,
... )
1.32
>>> species = {"oak": 12, "pine": 7, "birch": 3, "elm": 1, "ash": 1, "yew": 1}
>>> round(freqprob.sample_coverage(species), 3) # share of the population already observed
0.88
>>> round(freqprob.chao_shen_entropy(species), 3) # entropy, corrected for unseen species
1.56
Estimators also offer fit/predict/score aliases for scikit-learn
familiarity, and can be saved with .save(path) and reloaded with
.load(path).
Choosing a method
| Method | Use it for | Key parameter |
|---|---|---|
MLE |
raw relative frequencies (no smoothing) | — |
Laplace / Lidstone / ELE |
simple, robust additive smoothing | bins, gamma |
SimpleGoodTuring |
heavy-tailed count data (many rare items) | p_value |
KneserNey / ModifiedKneserNey |
n-gram language models | discount |
AbsoluteDiscounting / PitmanYor |
bigram discounting with unigram back-off | discount, strength |
KatzBackoff / StupidBackoff |
bigram back-off models | k, alpha |
WittenBell |
parameter-free discounting by distinct-type count | bins |
Bayesian |
Dirichlet-prior smoothing | alpha |
Interpolated / JelinekMercer |
combining models of different orders (fixed or EM-fit weight) | lambda_weight |
CertaintyDegree |
reserving mass by how fully the support is observed (experimental) | bins |
Uniform / Random |
non-informative baselines | — |
For large or streaming data, FreqProb also provides vectorized batch scoring, lazy evaluation, incremental (streaming) estimators and memory-efficient representations. Choosing a method helps pick one, and each family has a page of its own with its definition and a worked example.
Highlights
- One interface for every method: swap estimators without rewriting your code.
- Handles the unseen: every smoothing method reserves probability mass for elements absent from the data.
- Beyond smoothing: sample coverage, bias-corrected entropy (Chao–Shen, NSB), perplexity, cross-entropy and KL divergence.
- Validated: estimators are checked against NLTK's implementations and with property-based tests.
- Typed and tested: full type hints (
py.typed), with tests on Python 3.10–3.12 across Linux, macOS and Windows.
Documentation
- Tutorial: from raw text to smoothed probabilities, held-out evaluation and n-grams.
- Concepts: how estimators work, notation, choosing a method and common pitfalls.
- Methods: one page per method family, with its definition, a worked example, its properties and references.
- Evaluation: perplexity and cross-entropy, sample coverage and bias-corrected entropy.
- How-to guides: building counts,
choosing
bins, log space, scaling, persistence and worked examples by domain. - API Reference: every public class and function, generated from the source with a runnable example.
- References: the published source of each method, with a BibTeX file.
- Changelog and Migration guide.
Related projects
- NLTK's
nltk.probabilitymodule includes several of the same estimators (Laplace, Lidstone, ELE, Simple Good–Turing, Kneser–Ney, Witten–Bell) as part of a broader NLP toolkit. FreqProb is a standalone library for estimation from any frequency counts, and its estimators are tested against NLTK's. - ASymCat uses FreqProb for smoothing and bias-corrected entropy in its measures of directional association between categorical variables.
Citation
If you use FreqProb in published work, please cite the version you used:
@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/}
}
The DOI 10.5281/zenodo.23917393 always resolves to the latest
version, and the Zenodo record lists the DOI of each version. See
How to cite for APA and classic BibTeX
forms, and References for the papers
behind each method. Citation metadata is also in
CITATION.cff.
License
MIT; see LICENSE.
Metadata
Release files for freqprob 0.9.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| freqprob-0.9.0.tar.gz | 135.9 kB | With provenance | Details for freqprob-0.9.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| freqprob-0.9.0-py3-none-any.whl | Python 3 | none | any | Details for freqprob-0.9.0-py3-none-any.whl |
Total release size: 212.5 kB
Release files / freqprob-0.9.0.tar.gz
| Download URL | freqprob-0.9.0.tar.gz |
|---|---|
| Size | 135.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9906d3d654b6a2efb51046a1ebc416fe7d8f150ed97bcc7cc9a1aa666719d1a7
|
|
BLAKE2b-256 checksum How to use checksums |
cc15162b16d8234ad152789309942a63ba1fe0bcfb633ab692b67a1315d62ba9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.
Transparency logRelease files / freqprob-0.9.0-py3-none-any.whl
| Download URL | freqprob-0.9.0-py3-none-any.whl |
|---|---|
| Size | 76.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a04f50ce4361bfe6986b8606706a65c7dda2ac0db56ad874f7baac5e497519dc
|
|
BLAKE2b-256 checksum How to use checksums |
86075f8a64ccef12d496567ffd6964e1c9b9d0d2ac7979739eb91604e89b5fda
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.
Transparency logSecurity for freqprob 0.9.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for freqprob-0.9.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.