Skip to main content

FreqProb

CI codecov Docs PyPI version Python versions License: MIT Code style: ruff

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.8.0},
  year = {2026}
}

License

MIT — see LICENSE.

Metadata

Release files for freqprob 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for freqprob 0.8.0
File Size Uploaded Provenance Actions
freqprob-0.8.0.tar.gz 132.5 kB With provenance Details for freqprob-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for freqprob 0.8.0
File Interpreter ABI Platform Actions
freqprob-0.8.0-py3-none-any.whl Python 3 none any Details for freqprob-0.8.0-py3-none-any.whl

Total release size: 208.1 kB

Release files / freqprob-0.8.0.tar.gz

Download URL freqprob-0.8.0.tar.gz
Size 132.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8635d36713c7ce29e44634169d614dee849c51f380ab94b633a458ab30c2edad
BLAKE2b-256 checksum
How to use checksums
0859b79ac5a7b9829d86798b753d0e2c970581be80b7ec15f0caf9a26999aee9
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 3, 2026.

Transparency log

Release files / freqprob-0.8.0-py3-none-any.whl

Download URL freqprob-0.8.0-py3-none-any.whl
Size 75.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03474cda805f975d094ef980fa524eb9a366ad885166a0875262b06999bdae2d
BLAKE2b-256 checksum
How to use checksums
e4f3f646e01d9b397cd2eafd0215e74b9d4732a71ad127152225835df36681a5
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 3, 2026.

Transparency log

Security for freqprob 0.8.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.

Select from 2 files

Attestation for freqprob-0.8.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 3, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page