Markov Chains - Markov Models - Finding Equilibrium Matrx with Python

from nsepython import *

symbol = "NIFTY 50"
days = 100
end_date = datetime.datetime.now().strftime("%d-%b-%Y")
end_date = str(end_date)

start_date = (datetime.datetime.now()- datetime.timedelta(days=days)).strftime("%d-%b-%Y")
start_date = str(start_date)

df=index_history("NIFTY 50",start_date,end_date)
df["daily_change"]=df["CLOSE"].astype(float).pct_change()

df = df.iloc[1: , :]

df['state']=df['daily_change']

df['state']=df['state'].apply(lambda x: 'Up' if (x > 0.001) else ('Down' if (x<=0.001) else 'Flat'))

df['priorstate']=df['state'].shift(1)

states = df [['priorstate','state']].dropna()
states_matrix = states.groupby(['priorstate','state']).size().unstack()

transition_matrix= states_matrix.apply(lambda x: x/float(x.sum()),axis=1)

t_0 = transition_matrix.copy()

import numpy as np

i = 1
a = t_0.copy()
b = t_0.dot(t_0)

while (not(a.equals(b))):    
    a = b.copy()
    b = b.dot(t_0)
    i = i+1

print("Equilibrium Matrix is at: "+str(i-1))

As discussed in Session. I will drop the article shortly.