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's a general-purpose statistical tool — natural language processing is one consumer among many (information retrieval, ecology, genomics, categorical analytics, ML features).
import freqprob
counts = {"the": 100, "cat": 50, "dog": 30, "bird": 10}
laplace = freqprob.Laplace(counts, bins=10_000, logprob=False)
laplace("cat") # 0.0050 — an observed element
laplace("elephant") # 0.0001 — an unseen element still gets non-zero mass
That last line is the whole point: a raw relative-frequency estimate would
assign probability 0 to "elephant" and break any model that multiplies or
takes logs of probabilities. Smoothing reserves a bit of mass for what you
haven't seen yet — and FreqProb gives you a dozen well-tested ways to do it
behind one consistent interface.
Install
pip install freqprob
The interface
Every estimator follows the same contract: construct it with a frequency distribution, then call it to score an element.
import freqprob
bigram_counts = {("the", "cat"): 5, ("the", "dog"): 3, ("a", "cat"): 2}
scorer = freqprob.KneserNey(bigram_counts, discount=0.75)
scorer(("the", "cat")) # score one element
scorer.predict([("the", "cat"), ("a", "dog")]) # score many (scikit-learn-style)
freqprob.perplexity(scorer, [("the", "cat"), ("a", "cat")]) # evaluate a model
fit/predict/score aliases are available for scikit-learn familiarity, and
any fitted estimator can be saved with .save(path) and reloaded with
.load(path) without re-fitting.
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, streaming (incremental) estimators, and memory-efficient
compressed/sparse representations. Sample-based estimators — sample_coverage
and the bias-corrected entropy estimators chao_shen_entropy and nsb_entropy
— work directly on a frequency distribution.
Why FreqProb
- One consistent API across a dozen smoothing methods — swap estimators without rewriting your code.
- Mathematically validated against reference implementations (NLTK, SciPy) and checked with property-based tests.
- Typed and production-ready — full type hints (
py.typed), strict linting and type-checking, and a test suite run across Python 3.10–3.12 on Linux, macOS, and Windows.
Documentation
- Documentation site — user guide and full API reference.
- User Guide — concepts, choosing a method, and worked examples across text, ecology, genomics, and categorical data.
- API Reference — every public class and function, generated from the source with a runnable example.
Citation
If you use FreqProb in academic research, please cite:
@software{tresoldi_freqprob_2026,
author = {Tresoldi, Tiago},
title = {FreqProb: A Python library for probability smoothing and frequency-based estimation},
url = {https://github.com/tresoldi/freqprob},
version = {0.7.0},
year = {2026}
}
License
MIT — see LICENSE.
Metadata
Release files for freqprob 0.7.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 | |
|---|---|---|---|
| freqprob-0.7.0.tar.gz | 117.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| freqprob-0.7.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 192.0 kB
Release files / freqprob-0.7.0.tar.gz
| Download URL | freqprob-0.7.0.tar.gz |
|---|---|
| Size | 117.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9df7eacd30ffd6266a9bc4b7cd2461daf6abcad12a87acb3a0998982aef7e0a4
|
|
BLAKE2b-256 checksum How to use checksums |
fc66ec61e0d4af3ed35293d2b06ee02a240d026b371f1256e50ed6ce2c2fea0e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 4, 2026.
Transparency logRelease files / freqprob-0.7.0-py3-none-any.whl
| Download URL | freqprob-0.7.0-py3-none-any.whl |
|---|---|
| Size | 74.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
acc1a6df45e0281b56fe55b4c0eaab6b42d5bbea3f4d9865feddf646edd0da5c
|
|
BLAKE2b-256 checksum How to use checksums |
e9670efaea1a77eee203a49d7f4e8e49a97e7d453c6154cd294a774f0b92f661
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 4, 2026.
Transparency log