ChainoPy: A Python Library for Discrete Time Markov Chain based stochastic analysis
Last updated Apr 9, 2026
23
Stars
1
Forks
7
Issues
0
Stars/day
Attention Score
6
Topics
Language breakdown
Jupyter Notebook 49.4%
Python 43.5%
Cython 4.3%
TeX 2.8%
▸ Files
click to expand
README
ChainoPy 1.0
A Python 🐍 Package for Markov Chains, Markov Chain Neural Networks and Markov Switching Models.Why ChainoPy?
- Covers most of the fundamental agorithms for Markov Chain Analysis
- Memory efficient Model saving
- Faster than other libraries (eg: 5x Faster than PyDTMC)
- First Package to contain functions to build equivalent Markov Chain Neural Networks from Markov Chains.
- Contains Markov Switching Models for Univariate Time Series Analysis
- Supports Numpy 2.0.0
How to Install ChainoPy?
Using pip
pip install chainopy
Build from Source
Before you begin, ensure you have the following installed on your system:
- Python (> 3.9 )
1. Clone the Repository
Fork and Clone the Chainopy repository to your local machine using Git:git clone https://github.com/aadya940/chainopy.git
Navigate to the directory which contains the pyproject.toml file.
2. Install the package
python -m build
How to run ChainoPy Tests?
1. Clone the project locally 2. Install packages mentioned inrequirements.txt and requirements_test.txt
3. Navigate to the directory containing tests folder
4. Run the following command:
python -m pytest tests/
You're all Set! 😃 👍
The Basics
Create Markov Chains and Markov Chain Neural Networks as follows:>>> import chainopy
>>> mc = chainopy.MarkovChain([[0, 1], [1, 0]], states = ["Rain", "No-Rain"]) # Creates a two-states Markov Chain stored in mc.
>>> neuralnetwork = chainopy.MarkovChainNeuralNetwork(mc, numlayers = 5) # Creates a 5-layered Neural Network that simulates mc.
Create a Markov Switching Model as follows:
>>> import numpy as np
>>> import random
>>> from chainopy import MarkovSwitchingModel
>>> X = np.random.normal(0, 1, 1000) + np.random.logistic(5, 10, 1000) # Generate Random Training Data
>>> regimecol = [random.choice(["High", "Low", "Stagnant"]) for in range(1000)] # Generate Regimes for Training Data
>>> mod = MarkovSwitchingModel()
>>> mod.fit(X, regime_col)
>>> y, regime_y = mod.predict("High", steps=20)
Generates Data as follows:
X: We generate 1000 data points by combining a normal distribution (mean = 0, standard deviation = 1) with a logistic
regime_col: We assign one of three possible regimes ("High", "Low", "Stagnant") to each data point. This is done by randomly
Later, Creates a Markov Switching Model using chainopy.MarkovSwitchingModel with 3 regimes (High, Low and Stagnant) and predicts the next twenty steps if the start states is "High".
Example - Apple Weekly High Stock data prediction using chainopy.MarkovSwitchingModel
How to Contribute?
- Fork the Project.
- Clone the Project locally.
- Create a New Branch to Contribute.
- run
pip install -r requirements.txtandpip install -r requirements_test.txtto download dependencies. - Do the changes of interest (Make sure to write docstrings).
- Write Unit Tests and test your implementation.
- Format the code using the Black Formatter.
- Push the changes and submit a Pull Request.
Cite ChinoPy
@article{Chinubhai2024ChainoPy,
author = {Aadya A. Chinubhai},
title = {ChainoPy: A Python Library for Discrete Time Markov Chain Based Stochastic Analysis},
journal = {Journal of Open Source Software},
volume = {9},
number = {100},
pages = {6880},
year = {2024},
doi = {10.21105/joss.06880},
url = {https://joss.theoj.org/papers/10.21105/joss.06880}
}🔗 More in this category