markov-entropy
Finite Markov categories and experimental general-space information calculations in Python.
An executable implementation of finite-state constructions from Paolo Perrone's Markov Categories and Entropy.
Overview
markov-entropy models FinStoch with finite spaces and column-stochastic matrices. Version 0.2 added an optional DisCoPy layer for symbolic Markov string diagrams. Version 0.3 adds explicitly experimental density, finite-partition, and sampling calculations while keeping the stable finite API unchanged.
Scope. Finite state spaces are the stable core. Experimental calculations live under
markov_entropy.stoch; they do not constitute a full implementation of general measurable spaces, arbitrary kernels, Radon–Nikodym derivatives, exact partition suprema, or differential entropy.
Highlights
- Immutable finite spaces, probability distributions, and stochastic channels.
- Identity, composition, tensor product, copy, discard, joints, and marginals.
- KL, Rényi, and total-variation divergences.
- Mutual information, conditional mutual information, entropy, and conditional entropy.
- Optional DisCoPy boxes and string diagrams for categorical constructions.
- Experimental density divergences using a caller-supplied common-base-measure integrator.
- Finite-partition lower-bound sequences with monotonicity diagnostics.
- Monte Carlo expectation and KL estimates with standard errors and confidence intervals.
- Reproducible SVG gallery, six notebooks, theorem tests, and Python 3.11–3.13 CI.
Installation
pip install markov-entropy
Install string-diagram support:
pip install "markov-entropy[discopy]"
For development:
git clone https://github.com/jiangnan030-del/markov-entropy.git
cd markov-entropy
uv sync --all-extras
uv run pytest
Quick start
from markov_entropy import Distribution, FiniteSpace
from markov_entropy.divergences import KL
from markov_entropy.information import entropy
X = FiniteSpace(["0", "1"])
p = Distribution(X, [0.25, 0.75])
assert entropy(p, KL()) > 0
String diagrams
from markov_entropy import Channel, FiniteSpace
from markov_entropy.adapters import DiscopyAdapter
X = FiniteSpace([0, 1])
Y = FiniteSpace(["a", "b"])
f = Channel(X, Y, [[0.8, 0.1], [0.2, 0.9]])
adapter = DiscopyAdapter({X: "X", Y: "Y"})
diagram = adapter.channel_box(f, "f")
Render the complete SVG gallery:
uv run python examples/render_discopy_gallery.py
Experimental Stoch calculations
import math
from markov_entropy.stoch import DensityDistribution, density_kl
p = DensityDistribution(lambda _: 0.0, "uniform")
q = DensityDistribution(lambda x: math.log(x + 0.5), "tilted")
# The caller explicitly supplies the domain and quadrature rule.
value = density_kl(p, q, integrator)
The experimental namespace also provides finite-partition lower-bound sequences and Monte Carlo estimates with reported uncertainty. See the experimental Stoch guide and notebook.
Documentation
- API reference
- Numerical conventions
- Formula-to-code map
- DisCoPy adapter
- Diagram gallery
- Experimental Stoch backends
- Changelog
Validation
uv run ruff check .
uv run mypy src/markov_entropy
uv run pytest
uv run pytest --nbval examples/ -p no:cacheprovider --override-ini="addopts="
uv run python examples/render_discopy_gallery.py --output build/diagrams
uv build
Roadmap
- FinStoch numerical core
- Divergence-induced information and entropy
- Paper-theorem and property-based validation
- Optional DisCoPy adapter
- Reproducible SVG diagram gallery and visual notebook
- Initial experimental density, partition, and sampling interfaces
- Domain-specific integrators, essential suprema, dependent-sample errors, and measurable kernels
Citation
If this software supports your research, cite the repository using CITATION.cff and cite Paolo Perrone's original paper.
License
Released under the MIT License.
Metadata
Release files for markov-entropy 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| markov_entropy-0.3.0.tar.gz | 630.0 kB | With provenance | Details for markov_entropy-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| markov_entropy-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for markov_entropy-0.3.0-py3-none-any.whl |
Total release size: 650.6 kB
Release files / markov_entropy-0.3.0.tar.gz
| Download URL | markov_entropy-0.3.0.tar.gz |
|---|---|
| Size | 630.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e84d80b83836ddc3b7cfe8946751c30d18bc087af73db0363af7fda83e459866
|
|
BLAKE2b-256 checksum How to use checksums |
432073d1b9410049aef6b995f606755511ee2d82517c803725c6f5caa1662022
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 26, 2026.
Transparency logRelease files / markov_entropy-0.3.0-py3-none-any.whl
| Download URL | markov_entropy-0.3.0-py3-none-any.whl |
|---|---|
| Size | 20.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6fa6ec84acdfd7a24c178564872adcd0b67cffc4fa61e4104eda62c6a4a45ef5
|
|
BLAKE2b-256 checksum How to use checksums |
16137be8680d6d7ccb956ab2031037fcb20e228f46ec22c5c1e2802ba372dcb0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 26, 2026.
Transparency logSecurity for markov-entropy 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for markov_entropy-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 26, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.