Skip to content

msg087/markov-chain

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Markov Chain Transition Diagrams in Python

Simple Markov Chain visualization module in Python. Only requires matplotlib and numpy to work.

Description

Thanks to updates made by @yagoduppel, the code can now support more than four states. Examples below. @SHaf373 has also added a gui-version for two-state Markov Chains.

Getting Started

Dependencies

  • matplotlib
  • numpy
  • Tkinter standard GUI library for Python

Installation

Copy the files src/node.py and src/markovchain.py in your script directory. Then

from markovchain import MarkovChain

2 X 2 Matrix

Script-based

P = np.array([[0.8, 0.2], [0.1, 0.9]]) # Transition matrix
mc = MarkovChain(P, ['1', '2'])
mc.draw("../img/markov-chain-two-states.png")

Two-state Markov-Chain

Dynamic Random Entery by the user

For GUI-based version use src/2X2.py.

3-state Markov chain demo

P = np.array([
    [0.8, 0.1, 0.1],
    [0.1, 0.7, 0.2],
    [0.1, 0.7, 0.2],
])
mc = MarkovChain(P, ['A', 'B', 'C'])
mc.draw("../img/markov-chain-three-states.png")

Three-state Markov-Chain

4-state Markov chain demo

P = np.array([
    [0.8, 0.1, 0.1, 0.0], 
    [0.1, 0.7, 0.0, 0.2],
    [0.1, 0.0, 0.7, 0.2],
    [0.1, 0.0, 0.7, 0.2]
])
mc = MarkovChain(P, ['1', '2', '3', '4'])
mc.draw("../img/markov-chain-four-states.png")

Four-state Markov-Chain

5-state Markov chain demo

P = np.array([
    [0.8, 0.1, 0.1, 0.0, 0.0], 
    [0.1, 0.6, 0.0, 0.2, 0.1],
    [0.1, 0.0, 0.7, 0.2, 0.0],
    [0.1, 0.0, 0.4, 0.2, 0.3],
    [0.6, 0.1, 0.1, 0.0, 0.2], 
])
mc = MarkovChain(P, ['1', '2', '3', '4', '5'])
mc.draw("../img/markov-chain-five-states.png")

Five-state Markov-Chain

Author

Credit: Naysan Saran Modification: [M Shaf Khattak].(https://github.com/SHaf373)

Related links in my blog:

License

This project is licensed under the GPL V3 licence.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 90.5%
  • Python 9.5%