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.7.0},
  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.7.0.tar.gz (117.4 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.7.0-py3-none-any.whl (74.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for freqprob-0.7.0.tar.gz
Algorithm Hash digest
SHA256 9df7eacd30ffd6266a9bc4b7cd2461daf6abcad12a87acb3a0998982aef7e0a4
MD5 82d8b40e9b53fcf50e209c000a892e7b
BLAKE2b-256 fc66ec61e0d4af3ed35293d2b06ee02a240d026b371f1256e50ed6ce2c2fea0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for freqprob-0.7.0.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.7.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for freqprob-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 acc1a6df45e0281b56fe55b4c0eaab6b42d5bbea3f4d9865feddf646edd0da5c
MD5 d71348a4e67de59c6d046eda2858097e
BLAKE2b-256 e9670efaea1a77eee203a49d7f4e8e49a97e7d453c6154cd294a774f0b92f661

See more details on using hashes here.

Provenance

The following attestation bundles were made for freqprob-0.7.0-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

This release

0.7.0 This release

2 files

0.6.2

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