# Calculate stationary distribution of markov chain in python

When working with Markov chains, it is often necessary to calculate the stationary distribution. The stationary distribution represents the long-term probabilities of being in each state of the Markov chain. In this article, we will explore three different ways to calculate the stationary distribution of a Markov chain in Python.

## Option 1: Using NumPy

One way to calculate the stationary distribution is by using the NumPy library. NumPy provides various mathematical functions and tools that are useful for working with arrays and matrices. Here is a sample code that demonstrates how to calculate the stationary distribution using NumPy:

``````
import numpy as np

# Define the transition matrix
transition_matrix = np.array([[0.2, 0.8],
[0.6, 0.4]])

# Calculate the eigenvalues and eigenvectors of the transpose of the transition matrix
eigenvalues, eigenvectors = np.linalg.eig(transition_matrix.T)

# Find the eigenvector corresponding to the eigenvalue of 1
stationary_distribution = np.real(eigenvectors[:, np.isclose(eigenvalues, 1)])

# Normalize the stationary distribution
stationary_distribution /= np.sum(stationary_distribution)

print(stationary_distribution)
``````

In this code, we first define the transition matrix of the Markov chain. Then, we use the NumPy function np.linalg.eig to calculate the eigenvalues and eigenvectors of the transpose of the transition matrix. We find the eigenvector corresponding to the eigenvalue of 1, which represents the stationary distribution. Finally, we normalize the stationary distribution by dividing it by the sum of its elements.

## Option 2: Using SciPy

Another way to calculate the stationary distribution is by using the SciPy library. SciPy is a powerful library for scientific computing in Python, and it provides various functions for linear algebra, optimization, and more. Here is a sample code that demonstrates how to calculate the stationary distribution using SciPy:

``````
import numpy as np
from scipy.linalg import eig

# Define the transition matrix
transition_matrix = np.array([[0.2, 0.8],
[0.6, 0.4]])

# Calculate the eigenvalues and eigenvectors of the transpose of the transition matrix
eigenvalues, eigenvectors = eig(transition_matrix.T)

# Find the eigenvector corresponding to the eigenvalue of 1
stationary_distribution = np.real(eigenvectors[:, np.isclose(eigenvalues, 1)])

# Normalize the stationary distribution
stationary_distribution /= np.sum(stationary_distribution)

print(stationary_distribution)
``````

In this code, we import the eig function from the scipy.linalg module to calculate the eigenvalues and eigenvectors of the transpose of the transition matrix. The rest of the code is similar to Option 1.

## Option 3: Using SymPy

A third way to calculate the stationary distribution is by using the SymPy library. SymPy is a Python library for symbolic mathematics, and it provides various functions for algebraic manipulations, solving equations, and more. Here is a sample code that demonstrates how to calculate the stationary distribution using SymPy:

``````
import sympy as sp

# Define the transition matrix
transition_matrix = sp.Matrix([[0.2, 0.8],
[0.6, 0.4]])

# Calculate the eigenvalues and eigenvectors of the transpose of the transition matrix
eigenvalues, eigenvectors = transition_matrix.T.eigenvects()

# Find the eigenvector corresponding to the eigenvalue of 1
stationary_distribution = np.real(eigenvectors[0][2][0])

# Normalize the stationary distribution
stationary_distribution /= sp.Sum(stationary_distribution).doit()

print(stationary_distribution)
``````

In this code, we first import the sympy module as sp. We define the transition matrix as a SymPy Matrix object. Then, we use the eigenvects method to calculate the eigenvalues and eigenvectors of the transpose of the transition matrix. The rest of the code is similar to Option 1 and 2.

After exploring these three options, it is clear that Option 1, which uses NumPy, is the most straightforward and efficient way to calculate the stationary distribution of a Markov chain in Python. NumPy provides a wide range of mathematical functions and tools that are specifically designed for numerical computations, making it the ideal choice for this task.

Rate this post

### 11 Responses

1. Beckham says:

Option 1 using NumPy seems more straightforward, but Option 3 using SymPy offers more flexibility. What do you guys think?

1. says:

I personally prefer Option 1 with NumPy. It gets the job done efficiently without the need for extra complexity. Flexibility is good, but simplicity is key in my book. Different strokes for different folks, I guess!

2. Elaina Walls says:

Option 3 with SymPy seems pretty cool, but would it be more efficient than the others? 🤔

3. Zyon says:

Option 2: Using SciPy seems more reliable and efficient than the others. What do you guys think? 🤔💭

4. Ellie says:

Option 2: Using SciPy seems more practical, but Option 1: Using NumPy is worth exploring too!

1. Makayla says:

I completely disagree. Option 1: Using NumPy is the way to go. It offers a solid foundation and is widely used in the scientific community. SciPy might have its perks, but NumPy is definitely worth exploring and mastering.

5. Mabel OConnor says:

Option 3: Using SymPy seems like a game-changer! Cant wait to try it out.

6. Yara Blevins says:

Option 2 is the bomb! SciPy all the way, baby! 💥 So much power and flexibility, Im loving it! 😍🔥

7. Sienna Alexander says:

Option 1 with NumPy seems cool, but Im more curious about Option 3 with SymPy.

1. Thatcher says:

I totally get your curiosity about Option 3 with SymPy. Its always good to explore different tools and see what works best for you. Dont hesitate to give it a try and let us know your thoughts on it. Happy coding!

8. Hamza Rich says:

Option 3 using SymPy seems interesting, but can it handle large-scale Markov chains efficiently? 🤔