Information Theoretic Measures of Entropy and Divergence
Project description
Divergence
The Dissolution of Uncertainty — One Bit at a Time
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
.venvenvironment. Runningjupyter labfrom a system Python or different environment will fail to find thedivergencepackage.
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
- Shannon, C. E. (1948). "A Mathematical Theory of Communication." Bell System Technical Journal, 27(3), 379-423.
- Kullback, S. & Leibler, R. A. (1951). "On Information and Sufficiency." Annals of Mathematical Statistics, 22(1), 79-86.
- Cover, T. M. & Thomas, J. A. (2006). Elements of Information Theory, 2nd edition. Wiley.
- Entropy (information theory) — Wikipedia
- Kullback-Leibler divergence — Wikipedia
- Jensen-Shannon divergence — Wikipedia
- Mutual information — Wikipedia
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file divergence-1.3.0.tar.gz.
File metadata
- Download URL: divergence-1.3.0.tar.gz
- Upload date:
- Size: 1.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adbc38d1ac30204fb1c927ad4164eaab78069321c55844bdeb51c68146eff6ca
|
|
| MD5 |
c23871c413f13d467b61160668c839dd
|
|
| BLAKE2b-256 |
62061d862309cb7019e543a77ce62ed8dac31aac0b3af4fe97f65a9c2ded2a7c
|
Provenance
The following attestation bundles were made for divergence-1.3.0.tar.gz:
Publisher:
release.yml on michaelnowotny/divergence
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
divergence-1.3.0.tar.gz -
Subject digest:
adbc38d1ac30204fb1c927ad4164eaab78069321c55844bdeb51c68146eff6ca - Sigstore transparency entry: 1115877321
- Sigstore integration time:
-
Permalink:
michaelnowotny/divergence@4a093d05a9ee354ba1669eb7e7e8dd83fd2a6cb8 -
Branch / Tag:
refs/tags/1.3.0 - Owner: https://github.com/michaelnowotny
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4a093d05a9ee354ba1669eb7e7e8dd83fd2a6cb8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file divergence-1.3.0-py3-none-any.whl.
File metadata
- Download URL: divergence-1.3.0-py3-none-any.whl
- Upload date:
- Size: 50.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c98a06d750ebda0ff100833bfd7881be63eabb512593947baae1b3efc1af90e2
|
|
| MD5 |
77ef180c19954825eeee895648c863dc
|
|
| BLAKE2b-256 |
11f8242d65272726ab4d91ab2fa80820ddcb782cc614dffda9451da8aac52ce3
|
Provenance
The following attestation bundles were made for divergence-1.3.0-py3-none-any.whl:
Publisher:
release.yml on michaelnowotny/divergence
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
divergence-1.3.0-py3-none-any.whl -
Subject digest:
c98a06d750ebda0ff100833bfd7881be63eabb512593947baae1b3efc1af90e2 - Sigstore transparency entry: 1115877372
- Sigstore integration time:
-
Permalink:
michaelnowotny/divergence@4a093d05a9ee354ba1669eb7e7e8dd83fd2a6cb8 -
Branch / Tag:
refs/tags/1.3.0 - Owner: https://github.com/michaelnowotny
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4a093d05a9ee354ba1669eb7e7e8dd83fd2a6cb8 -
Trigger Event:
push
-
Statement type: