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: |
|
| Info: |
|
| PyPI: |
|
| Conda: |
|
| Donation: |
|
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)
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)
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)
| File | Size | Uploaded | |
|---|---|---|---|
| pydtmc-10.0.0.tar.gz | 173.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|