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.1.tar.gz (114.2 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.1-py3-none-any.whl (73.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: freqprob-0.6.1.tar.gz
  • Upload date:
  • Size: 114.2 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.1.tar.gz
Algorithm Hash digest
SHA256 bd9ef7863bb295f5cf7c4043f6c5f9ca118c1633f1ffd0051367ecb76d3c967c
MD5 1007c8ada4a238988d41cca99c5057c6
BLAKE2b-256 7d74698c223c5a222b95f876b4003b75111edeb4611557236aeaacc7708efc2e

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: freqprob-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 73.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4aa6ac2a67b046cc785f6d82f14d4d3bbaf04d70e6e94c5560991599d724672e
MD5 bf7d8c7b9e5766493aa70671f0913cd6
BLAKE2b-256 a0713444e9f2bc6c8a00365125f642e6c0122363c3cdb539b9013ef996b53456

See more details on using hashes here.

Provenance

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

This release

0.6.1 This release

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