Skip to main content

PyDTMC is a lightweight Python library focused on discrete-time Markov chains and hidden Markov models. It provides a comprehensive set of classes and functions for creating, manipulating, analyzing, simulating, and visualizing Markovian stochastic processes.

Status: Build Docs Coverage
Info: License DOI Size
PyPI: Version Python Wheel Downloads
Conda: Version Python Platforms Downloads

Installation

PyDTMC requires Python 3.12 or greater and depends on NumPy, SciPy, NetworkX, and Matplotlib.

For the PyPI installation:

pip install pydtmc

For the Conda installation:

conda install -c conda-forge pydtmc

For the latest development version:

pip install git+https://github.com/TommasoBelluzzo/PyDTMC.git

Note: Graphviz and pydot are recommended for graph visualization using the plot_graph function.

Resources

For additional functionality, mathematical definitions, and usage examples, refer to the documentation.

If you use PyDTMC in your work, please cite the software using its Zenodo DOI.
Citation metadata is also available in the CITATION.cff file.

Quick Start

Import the classes from PyDTMC and create instances of a discrete-time Markov chain and a hidden Markov model:

from pydtmc import MarkovChain, HiddenMarkovModel

# Markov Chain

p = [
    [0.2, 0.7, 0.0, 0.1],
    [0.0, 0.6, 0.3, 0.1],
    [0.0, 0.0, 1.0, 0.0],
    [0.5, 0.0, 0.5, 0.0]
]

mc = MarkovChain(p, ['A', 'B', 'C', 'D'])

# Hidden Markov Model

p = [
    [0.4, 0.6],
    [0.8, 0.2]
]

e = [
    [0.5, 0.0, 0.0, 0.5],
    [0.2, 0.2, 0.2, 0.4]
]

hmm = HiddenMarkovModel(p, e, ['A', 'B'], ['H1', 'H2', 'H3', 'H4'])

Usage: MarkovChain Class

Using the mc instance created in the Quick Start section, the following examples showcase some of the class properties, methods, and visualization functions:

Properties

print(mc.is_absorbing)
print(mc.is_ergodic)
print(mc.is_irreducible)
print(mc.recurrent_states)
print(mc.transient_states)
print(mc.communicating_classes)
print(mc.period)
print(mc.pi)
print(mc.fundamental_matrix)
print(mc.entropy_rate)

Methods

print(mc.absorption_probabilities())
print(mc.expected_rewards(10, [2.0, -3.0, 8.0, -7.0]))
print(mc.expected_transitions(2))
print(mc.expected_visits())
print(mc.first_passage_probabilities(5, 3))
print(mc.hitting_probabilities([0, 1]))
print(mc.simulate(10, seed=32))

Visualization

from pydtmc import plot_graph, plot_redistributions, plot_sequence, plot_spectrum

plot_spectrum(mc, dpi=300)
plot_graph(mc, dpi=300)
plot_sequence(mc, 10, plot_type='histogram', dpi=300)
plot_sequence(mc, 10, plot_type='heatmap', dpi=300)
plot_sequence(mc, 10, plot_type='matrix', dpi=300)
plot_redistributions(mc, 10, plot_type='heatmap', dpi=300)
plot_redistributions(mc, 10, plot_type='projection', dpi=300)

MarkovChain Plots

Usage: HiddenMarkovModel Class

Using the hmm instance created in the Quick Start section, the following examples showcase some of the class properties, methods, and visualization functions:

Properties

print(hmm.is_ergodic)
print(hmm.is_regular)
print(hmm.pi)

Methods

print(hmm.emission_probability('H1', 'A'))
print(hmm.transition_probability('A', 'B'))
print(hmm.decode(['H1', 'H2', 'H3']))
print(hmm.predict('vit', ['H1', 'H2', 'H3']))
print(hmm.simulate(12, seed=1488))

Visualization

from pydtmc import plot_graph, plot_sequence, plot_trellis

plot_graph(hmm, dpi=300)
plot_sequence(hmm, 10, plot_type='histogram', dpi=300)
plot_sequence(hmm, 10, plot_type='heatmap', dpi=300)
plot_sequence(hmm, 10, plot_type='matrix', dpi=300)
plot_trellis(hmm, 10, dpi=300)

HiddenMarkovModel Plots

Metadata

Release files for PyDTMC 11.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyDTMC 11.0.0
File Size Uploaded Provenance Actions
pydtmc-11.0.0.tar.gz 293.6 kB Without provenance Details for pydtmc-11.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for PyDTMC 11.0.0
File Interpreter ABI Platform Actions
pydtmc-11.0.0-py3-none-any.whl Python 3 none any Details for pydtmc-11.0.0-py3-none-any.whl

Total release size: 433.6 kB

Release files / pydtmc-11.0.0.tar.gz

Download URL pydtmc-11.0.0.tar.gz
Size 293.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2600b964ab82b3f7f21ed54f56efeb8fac9313c1d660f2d22af5cac2f0a06d21
BLAKE2b-256 checksum
How to use checksums
f466b35d539f02a5f5af0bd45def9333c8e03af016f2850ea77d04eba2bf99f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.18

Release files / pydtmc-11.0.0-py3-none-any.whl

Download URL pydtmc-11.0.0-py3-none-any.whl
Size 140.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7583dab4a091645c0470d19022b2dad6771c25de463302c1274763d29f87dae
BLAKE2b-256 checksum
How to use checksums
c372a2d9e92346c181a9b9276d1970aca43c347a12d4e10b32841a757c58d850
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.18

Security for PyDTMC 11.0.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Release history Release notifications | RSS feed

This release

11.0.0 This release

2 release files No provenance

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