Skip to main content

Markov Entropy — Finite Markov Categories in Python

markov-entropy

Finite Markov categories and experimental general-space information calculations in Python.

CI Python 3.11+ MIT License Version 0.3.0

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

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.

Download files

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

Source Distribution

markov_entropy-0.3.0.tar.gz (630.0 kB view details)

Uploaded Source

Built Distribution

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

markov_entropy-0.3.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file markov_entropy-0.3.0.tar.gz.

File metadata

  • Download URL: markov_entropy-0.3.0.tar.gz
  • Upload date:
  • Size: 630.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for markov_entropy-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e84d80b83836ddc3b7cfe8946751c30d18bc087af73db0363af7fda83e459866
MD5 0b32c40b57cd9b8f95a3656953a67931
BLAKE2b-256 432073d1b9410049aef6b995f606755511ee2d82517c803725c6f5caa1662022

See more details on using hashes here.

Provenance

The following attestation bundles were made for markov_entropy-0.3.0.tar.gz:

Publisher: publish.yml on jiangnan030-del/markov-entropy

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

File details

Details for the file markov_entropy-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: markov_entropy-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for markov_entropy-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6fa6ec84acdfd7a24c178564872adcd0b67cffc4fa61e4104eda62c6a4a45ef5
MD5 ec6d7c7d0516d739687c5dc88b8409ff
BLAKE2b-256 16137be8680d6d7ccb956ab2031037fcb20e228f46ec22c5c1e2802ba372dcb0

See more details on using hashes here.

Provenance

The following attestation bundles were made for markov_entropy-0.3.0-py3-none-any.whl:

Publisher: publish.yml on jiangnan030-del/markov-entropy

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

Release history Release notifications | RSS feed

This release

0.3.0 This release

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