A framework for discrete-time Markov chains analysis.
Project description
PyDTMC is a full-featured, lightweight library for discrete-time Markov chains analysis. It provides classes and functions for creating, manipulating and simulating markovian stochastic processes.
Requirements
PyDTMC supports only Python 3 and the minimum version required is 3.6. In addition, the environment must include the following libraries:
In order to use the plot_graph function, Graphviz and PyDot must be installed too.
Installation & Upgrade
Via PyPI:
$ pip install PyDTMC
$ pip install --upgrade PyDTMC
Via GitHub:
$ pip install git+https://github.com/TommasoBelluzzo/PyDTMC.git@master#egg=PyDTMC
$ pip install --upgrade git+https://github.com/TommasoBelluzzo/PyDTMC.git@master#egg=PyDTMC
Usage
The core element of the library is the MarkovChain class, which can be instantiated as follows:
>>> import numpy as np
>>> p = np.array([[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'])
>>> print(mc)
DISCRETE-TIME MARKOV CHAIN
- TRANSITION MATRIX:
A B C D
------- ------- ------- -------
A | 0.20000 0.70000 0.00000 0.10000
B | 0.00000 0.60000 0.30000 0.10000
C | 0.00000 0.00000 1.00000 0.00000
D | 0.50000 0.00000 0.50000 0.00000
- PROPERTIES:
ABSORBING: YES
APERIODIC: YES
IRREDUCIBLE: NO
ERGODIC: NO
- COMMUNICATING CLASSES:
[A,B,D] | [C]
TYPE: T | R
PERIOD: 1 | 1
Static values of a MarkovChain instance can be retrieved through its properties:
>>> print(mc.size)
4
>>> print(mc.recurrent_states)
['C']
>>> print(mc.transient_states)
['A', 'B', 'D']
>>> print(mc.steady_states)
[array([0., 0., 1., 0.])]
>>>print(mc.fundamental_matrix)
[[1.50943396 2.64150943 0.41509434]
[0.18867925 2.83018868 0.30188679]
[0.75471698 1.32075472 1.20754717]]
>>> print(mc.absorption_times)
[4.56603774 3.32075472 3.28301887]
>>> print(mc.topological_entropy)
0.6931471805599457
Dynamic computations on a MarkovChain instance can be performed through its methods:
>>> print(mc.expected_rewards(10, [2, -3, 8, -7]))
[-2.76071635 -12.01665113 23.23460025 -8.45723276]
>>> print(mc.expected_transitions(2))
[[0.085 0.2975 0. 0.0425]
[0. 0.345 0.1725 0.0575]
[0. 0. 0.7 0. ]
[0.15 0. 0.15 0. ]]
>>> print(mc.walk(10))
['D', 'A', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C']
Plotting functions can provide a visual representation of a MarkovChain instance and its attributes. In order to immediately display function outputs, the interactive mode of Matplotlib must be turned on.
>>> plot_eigenvalues(mc)
>>> plot_graph(mc)
>>> plot_walk(mc, 10, 'sequence')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file PyDTMC-1.2.0.tar.gz.
File metadata
- Download URL: PyDTMC-1.2.0.tar.gz
- Upload date:
- Size: 28.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f28a2d15e7b1d42d2dfa5e4dd4389f2679c99acf08bd171689e6894a7cc1430e
|
|
| MD5 |
cd9f9ee9725416012852a3bee123c597
|
|
| BLAKE2b-256 |
05a4fc96505d1022f89be6f3a73f7f6a93d33e19ae06b60a02a3f33d4f0b95f1
|