Skip to main content

Information Theoretic Measures of Entropy and Divergence

Project description

Divergence

Divergence

The Dissolution of Uncertainty — One Bit at a Time

Tests PyPI Python License: MIT


Divergence is a Python package for computing information-theoretic measures of entropy and divergence from probability distributions and samples.

In 1948, Claude Shannon published "A Mathematical Theory of Communication", laying the foundation for information theory and giving us a rigorous way to quantify uncertainty. Divergence puts Shannon's toolkit — and the extensions built upon it over the following decades — into your hands.

What You Can Compute

Measure Discrete Continuous What it tells you
Entropy yes yes How much uncertainty a distribution carries
Cross Entropy yes yes The cost of encoding one distribution using another's code
KL Divergence yes yes How much information is lost when approximating one distribution with another
Jensen-Shannon Divergence yes yes A symmetric, bounded measure of distributional difference
Mutual Information yes yes How much knowing one variable tells you about another
Joint Entropy yes yes The total uncertainty in a pair of variables
Conditional Entropy yes yes The remaining uncertainty in one variable after observing another

All measures support configurable logarithm bases: base=np.e (nats, default), base=2 (bits), base=10 (hartleys).

In a Bayesian context, relative entropy can be used as a measure of the information gained by moving from a prior distribution q to a posterior distribution p.

Installation

pip install divergence

Quick Start

import numpy as np
from divergence import entropy_from_samples, relative_entropy_from_samples

# How much uncertainty does this distribution carry?
sample = np.random.normal(0, 1, size=10000)
h = entropy_from_samples(sample, discrete=False)

# How different are these two distributions?
p = np.random.normal(0, 1, size=10000)
q = np.random.normal(0.5, 1.2, size=10000)
kl = relative_entropy_from_samples(p, q, discrete=False)

Explore the Notebook

The interactive Divergence notebook walks through every measure with explanations, historical context, and worked examples — a self-contained introduction to information theory through code.

Development Setup

Requires uv (a fast Python package manager):

git clone https://github.com/michaelnowotny/divergence.git
cd divergence

# Create an isolated virtual environment and install everything
uv venv .venv --python 3.12
source .venv/bin/activate          # Windows: .venv\Scripts\activate
uv pip install -e ".[dev]"

Run the Notebook

./scripts/lab

This script ensures Jupyter Lab runs inside the project's virtual environment with all dependencies correctly installed — no version conflicts, no wrong Python.

Tip: Always launch Jupyter from within the activated .venv environment. Running jupyter lab from a system Python or different environment will fail to find the divergence package.

Run Tests and Linting

pytest                              # All 122 tests (~18s)
pytest tests/test_discrete.py       # Discrete tests only (fast, ~2s)

ruff check src/ tests/              # Lint
ruff format src/ tests/             # Format

References

  1. Shannon, C. E. (1948). "A Mathematical Theory of Communication." Bell System Technical Journal, 27(3), 379-423.
  2. Kullback, S. & Leibler, R. A. (1951). "On Information and Sufficiency." Annals of Mathematical Statistics, 22(1), 79-86.
  3. Cover, T. M. & Thomas, J. A. (2006). Elements of Information Theory, 2nd edition. Wiley.
  4. Entropy (information theory) — Wikipedia
  5. Kullback-Leibler divergence — Wikipedia
  6. Jensen-Shannon divergence — Wikipedia
  7. Mutual information — Wikipedia

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

divergence-1.2.0.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

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

divergence-1.2.0-py3-none-any.whl (42.0 kB view details)

Uploaded Python 3

File details

Details for the file divergence-1.2.0.tar.gz.

File metadata

  • Download URL: divergence-1.2.0.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for divergence-1.2.0.tar.gz
Algorithm Hash digest
SHA256 e063a39be8054d5cd605c82641b4849ee5970fe880b60c1a8ac0d59130631c8b
MD5 8895ef32d3b083c872af6b385a1b103c
BLAKE2b-256 643948bda1c87b90ca13a14729f996ecc7df9ee123ad9d460f626fbb44bdd4ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for divergence-1.2.0.tar.gz:

Publisher: release.yml on michaelnowotny/divergence

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

File details

Details for the file divergence-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: divergence-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 42.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for divergence-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ffdbe4c5a3a178644faad4d49bbd0a781258f9c12c3c79b0b41d82a998aa3f99
MD5 710eb57742fa64a0034da3437a7c01f3
BLAKE2b-256 3b1edf0c58c41272bfd6597646a09490ad8da88700d2393ec68cdc68ead64337

See more details on using hashes here.

Provenance

The following attestation bundles were made for divergence-1.2.0-py3-none-any.whl:

Publisher: release.yml on michaelnowotny/divergence

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

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page