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.
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, test_bigrams) # evaluate a model
scorer.save("model.pkl") # persist a fitted model...
scorer = freqprob.KneserNey.load("model.pkl") # ...and load it back
fit/predict/score aliases are available for scikit-learn familiarity, and
any fitted estimator can be saved and reloaded 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 |
Bayesian |
Dirichlet-prior smoothing | alpha |
Interpolated |
combining models of different orders | lambda_weight |
WittenBell, CertaintyDegree, Uniform, Random |
baselines & specialized cases | — |
For large or streaming data, FreqProb also provides vectorized batch scoring, lazy evaluation, streaming (incremental) estimators, and memory-efficient compressed/sparse representations.
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 and worked examples.
- Tutorials (executable, Nhandu format): basics, advanced methods, efficiency & memory, applications.
Citation
If you use FreqProb in academic research, please cite:
@software{tresoldi_freqprob_2025,
author = {Tresoldi, Tiago},
title = {FreqProb: A Python library for probability smoothing and frequency-based estimation},
url = {https://github.com/tresoldi/freqprob},
version = {0.6.0},
publisher = {Department of Linguistics and Philology, Uppsala University},
address = {Uppsala},
year = {2025}
}
License
MIT — see LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file freqprob-0.6.0.tar.gz.
File metadata
- Download URL: freqprob-0.6.0.tar.gz
- Upload date:
- Size: 107.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab66abc4eecfe2c917a9135ed6031152c40e8fd3fdc7befe09ee0b5314bf90a7
|
|
| MD5 |
2ac721ad5185422cc9b3d7b47bb3bdfc
|
|
| BLAKE2b-256 |
07a050646fcb9e8118f9b8d43b4ce48ac2d145a951555a7dc716dd02a546e139
|
Provenance
The following attestation bundles were made for freqprob-0.6.0.tar.gz:
Publisher:
release.yml on tresoldi/freqprob
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
freqprob-0.6.0.tar.gz -
Subject digest:
ab66abc4eecfe2c917a9135ed6031152c40e8fd3fdc7befe09ee0b5314bf90a7 - Sigstore transparency entry: 2243135560
- Sigstore integration time:
-
Permalink:
tresoldi/freqprob@d3b6833b1eac675ec7eeb8af4c55fc778da5b795 -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/tresoldi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d3b6833b1eac675ec7eeb8af4c55fc778da5b795 -
Trigger Event:
push
-
Statement type:
File details
Details for the file freqprob-0.6.0-py3-none-any.whl.
File metadata
- Download URL: freqprob-0.6.0-py3-none-any.whl
- Upload date:
- Size: 73.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26d592d30a3d31c59be5e1d00b9a77f1f51b22f7e25b50277a6efa153d463856
|
|
| MD5 |
b310189c35509e8f60dd1f705db1806b
|
|
| BLAKE2b-256 |
f4236f171e8282f5ae4baef8504502dede43836c43fcd6175d57ce0fa513063e
|
Provenance
The following attestation bundles were made for freqprob-0.6.0-py3-none-any.whl:
Publisher:
release.yml on tresoldi/freqprob
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
freqprob-0.6.0-py3-none-any.whl -
Subject digest:
26d592d30a3d31c59be5e1d00b9a77f1f51b22f7e25b50277a6efa153d463856 - Sigstore transparency entry: 2243136026
- Sigstore integration time:
-
Permalink:
tresoldi/freqprob@d3b6833b1eac675ec7eeb8af4c55fc778da5b795 -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/tresoldi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d3b6833b1eac675ec7eeb8af4c55fc778da5b795 -
Trigger Event:
push
-
Statement type: