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.
  • References — the published source of each method, with a BibTeX file.

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.8.1},
  url     = {https://freqprob.tresoldi.org/}
}

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

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.1
File Size Uploaded Provenance Actions
freqprob-0.8.1.tar.gz 132.7 kB With provenance Details for freqprob-0.8.1.tar.gz

Built distribution (wheel)

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

Total release size: 208.4 kB

Release files / freqprob-0.8.1.tar.gz

Download URL freqprob-0.8.1.tar.gz
Size 132.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a19a07ccf8a44a3a6c9eeb918606a5d3c333bae5501bca0462c5882e1c503fc8
BLAKE2b-256 checksum
How to use checksums
7dfb8c41f411ff77c54b74a34d598813364426d9d38ea0d3b1f8d3c277d8aff3
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.1-py3-none-any.whl

Download URL freqprob-0.8.1-py3-none-any.whl
Size 75.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
155a8b347d36ae77538bc5bdb63f4fc2a9be269e994eeac531c21900054c2e2a
BLAKE2b-256 checksum
How to use checksums
77ab3b22205b6eb16dd1b10f6366ab7ca63ec2965ea4d10f05fd6e2f7e020d8e
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.1

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