MarkovM - Python library for Markov Models.
Install from Python Package Index:
pip install markovm
Install from Source Code:
pip install .
You can use markovm.create_markov_model
to create a Markov model. Please provide all valid states and an n-by-n matrix describing the probabilities of transitions, where n is the number of states. If two states i
and j
do not have a connection in between, set matrix[i][j]
to 0
. For example,
>>> import markovm
>>> import numpy
>>> m = markovm.create_markov_model(
... states=("A", "B", "C"),
... transitions=numpy.array([
... [0.0, 1.0, 0.0], # A must goto B
... [0.2, 0.0, 0.8], # B can goto A (20%) or C (80%)
... [0.0, 0.5, 0.5], # C can goto B or stay
... ]),
... )
You can use markovm.random_walk
to randomly walk through a Markov model. By default, it will start with the first state. If you want it to start with another state, please provide the index of the expected starting state to index
in the function call. You can also set a seed to the function call, and it uses None
by default. For example,
>>> import itertools
>>> for state in itertools.islice(
... markovm.random_walk(m, seed=0), 5
... ):
... print(state)
...
A
B
C
B
A