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.

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

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

freqprob-0.6.0.tar.gz (107.7 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.0-py3-none-any.whl (73.5 kB view details)

Uploaded Python 3

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

Hashes for freqprob-0.6.0.tar.gz
Algorithm Hash digest
SHA256 ab66abc4eecfe2c917a9135ed6031152c40e8fd3fdc7befe09ee0b5314bf90a7
MD5 2ac721ad5185422cc9b3d7b47bb3bdfc
BLAKE2b-256 07a050646fcb9e8118f9b8d43b4ce48ac2d145a951555a7dc716dd02a546e139

See more details on using hashes here.

Provenance

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

Hashes for freqprob-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 26d592d30a3d31c59be5e1d00b9a77f1f51b22f7e25b50277a6efa153d463856
MD5 b310189c35509e8f60dd1f705db1806b
BLAKE2b-256 f4236f171e8282f5ae4baef8504502dede43836c43fcd6175d57ce0fa513063e

See more details on using hashes here.

Provenance

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

0.7.0

2 files

0.6.2

2 files

0.6.1

2 files

This release

0.6.0 This release

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