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
WittenBell parameter-free discounting by distinct-type count bins
Bayesian Dirichlet-prior smoothing alpha
Interpolated combining models of different orders 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.

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

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

freqprob-0.6.2.tar.gz (105.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

freqprob-0.6.2-py3-none-any.whl (62.9 kB view details)

Uploaded Python 3

File details

Details for the file freqprob-0.6.2.tar.gz.

File metadata

  • Download URL: freqprob-0.6.2.tar.gz
  • Upload date:
  • Size: 105.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for freqprob-0.6.2.tar.gz
Algorithm Hash digest
SHA256 8874eed5eddbbadb42c6f5ca88a9f06e1aeb7219fead2e06a8680465ec471fde
MD5 26f2c7806000c225d9c163be047d2cb7
BLAKE2b-256 94e1644a6b458022224a2ed3b14ee3a344b01844d8a4665fb970a553c58c3a9d

See more details on using hashes here.

Provenance

The following attestation bundles were made for freqprob-0.6.2.tar.gz:

Publisher: release.yml on tresoldi/freqprob

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file freqprob-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: freqprob-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 62.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for freqprob-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 061a0fe9ea3c346a33ff31a0d96de688d6541c7420d2c2b39c05580f50f12936
MD5 4ac669b89f20dc342aa7a90833f67bcb
BLAKE2b-256 9f8961f4b1535a1bba28fa6e79b8c0618da388f71656cc7ec7cc569bb404a6d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for freqprob-0.6.2-py3-none-any.whl:

Publisher: release.yml on tresoldi/freqprob

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.7.0

2 files

This release

0.6.2 This release

2 files

0.6.1

2 files

0.6.0

2 files

0.4.0

2 files

0.3.1

2 files

0.2.0

2 files

0.1.0

2 files

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