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
Donation: PayPal

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.

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.first_passage_probabilities(5, 3))
print(mc.hitting_probabilities([0, 1]))
print(mc.mean_number_visits())
print(mc.simulate(10, seed=32))

Visualization

from pydtmc import plot_eigenvalues, plot_graph, plot_redistributions, plot_sequence, plot_trellis

plot_eigenvalues(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('viterbi', ['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

References

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.

Metadata

Release files for PyDTMC 10.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 10.0.0
File Size Uploaded
pydtmc-10.0.0.tar.gz 173.1 kB Details

Built distribution (wheel)

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

Total release size: 269.8 kB

Release files / pydtmc-10.0.0.tar.gz

Download URL pydtmc-10.0.0.tar.gz
Size 173.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7ab673d6e82f59b17d4588e2683ea672fb2a3f603eb798d3f8b05ecb681f80bb
BLAKE2b-256 checksum
How to use checksums
bd531108a85e58d7b07b95f129166137e181b8dbe296fe1f03aa5d52add7a671
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

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

Download URL pydtmc-10.0.0-py3-none-any.whl
Size 96.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e7799a45d924a38945c80e9f7193a9bf234d60960d93089a0d700a018e1ff99
BLAKE2b-256 checksum
How to use checksums
d106d1fffe72d9c340abb6fd1ddaf7346a11735a59f4235ebf6358fa2b7ba22f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release history Release notifications | RSS feed

This release

10.0.0 This release

2 release files

9.0.0

2 release files

8.7.0

2 release files

8.0.0

2 release files

7.0.0

2 release files

6.11.0

2 release files

6.10.0

2 release files

6.9.0

2 release files

6.8.0

2 release files

6.6.0

2 release files

6.5.0

2 release files

6.4.0

2 release files

6.3.0

2 release files

6.2.0

2 release files

6.1.0

2 release files

6.0.0

2 release files

5.9.0

2 release files

5.8.0

2 release files

5.7.0

2 release files

5.6.0

2 release files

5.5.0

2 release files

5.4.0

2 release files

5.3.0

1 release file

5.2.0

1 release file

5.1.0

1 release file

5.0.0

1 release file

4.9.0

1 release file

4.8.0

1 release file

4.7.0

1 release file

4.6.0

1 release file

4.5.0

1 release file

4.4.0

1 release file

4.3.0

1 release file

4.2.0

1 release file

4.1.0

1 release file

4.0.0

1 release file

3.9.0

1 release file

3.8.0

1 release file

3.7.0

1 release file

3.6.0

1 release file

3.5.0

1 release file

3.4.0

1 release file

3.3.0

1 release file

3.2.0

1 release file

3.1.0

1 release file

3.0.0

1 release file

2.9.0

1 release file

2.8.0

1 release file

2.7.0

1 release file

2.6.0

1 release file

2.5.0

1 release file

2.4.0

1 release file

2.3.0

1 release file

2.2.0

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.9.0

1 release file

1.8.0

1 release file

1.7.0

1 release file

1.6.0

1 release file

1.5.0

1 release file

1.4.0

1 release file

1.3.0

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.1.2

1 release file

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