Skip to main content

A Python Library for Markov Chain based Stochastic Analysis!

Project description

logo1

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

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 in requirements.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`.
>>> neural_network = chainopy.MarkovChainNeuralNetwork(mc, num_layers = 5)    # Creates a 5-layered Neural Network that simulates `mc`. 

image

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
>>> regime_col = [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 distribution (mean = 5, scale = 10). This creates a complex dataset with variations.
  • regime_col: We assign one of three possible regimes ("High", "Low", "Stagnant") to each data point. This is done by randomly selecting one of these regimes for each of the 1000 data points.

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

image

How to Contribute?

  1. Fork the Project.
  2. Clone the Project locally.
  3. Create a New Branch to Contribute.
  4. run pip install -r requirements.txt and pip install -r requirements_test.txt to download dependencies.
  5. Do the changes of interest (Make sure to write docstrings).
  6. Write Unit Tests and test your implementation.
  7. Format the code using the Black Formatter.
  8. Push the changes and submit a Pull Request.

Note: If your implementation is Cython, justify its usage in your PR to make the code more maintainable.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

chainopy-1.0.2-cp312-cp312-win_amd64.whl (257.7 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

chainopy-1.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

chainopy-1.0.2-cp312-cp312-macosx_11_0_arm64.whl (256.3 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

chainopy-1.0.2-cp312-cp312-macosx_10_9_x86_64.whl (268.9 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

chainopy-1.0.2-cp311-cp311-win_amd64.whl (255.5 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

chainopy-1.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

chainopy-1.0.2-cp311-cp311-macosx_11_0_arm64.whl (255.8 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

chainopy-1.0.2-cp311-cp311-macosx_10_9_x86_64.whl (267.1 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

chainopy-1.0.2-cp310-cp310-win_amd64.whl (255.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

chainopy-1.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

chainopy-1.0.2-cp310-cp310-macosx_11_0_arm64.whl (256.3 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

chainopy-1.0.2-cp310-cp310-macosx_10_9_x86_64.whl (267.5 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page