In Our Last Chapter, We have discussed getting the Transition Matrix/ Initial Probability Matrix using python. Now, We shall dive into more detail into Markov Chain and its relation with matrix theory more theoretically.
Let’s do a random walk using out NSEPython
Library and use the index_history()
function. But instead of getting 100 days, Let’s get ∞ days –
symbol = "NIFTY 50"
days = 9204
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["state"]=df["CLOSE"].astype(float).pct_change()
df['state']=df['state'].apply(lambda x: 'Upside' if (x > 0.001) else ('Downside' if (x<=0.001) else 'Consolidation'))
print(df.tail())
#
Index Name INDEX_NAME HistoricalDate OPEN HIGH LOW CLOSE state
6267 Nifty 50 Nifty 50 26 Apr 1996 1133.17 1133.17 1106.29 1123.63 Upside
6268 Nifty 50 Nifty 50 25 Apr 1996 1157.94 1160.16 1110.61 1120.82 Downside
6269 Nifty 50 Nifty 50 24 Apr 1996 1136.97 1145.11 1126.77 1145.11 Upside
6270 Nifty 50 Nifty 50 23 Apr 1996 1090.04 1100.51 1090.04 1095.82 Downside
6271 Nifty 50 Nifty 50 22 Apr 1996 1136.28 1136.28 1102.83 1106.93 Upside
As We theoretically can not have ∞ days of data, We took the best that we could. The Nifty 50 index was launched on 22 April 1996
. So there are roughly 9204
days from the start date to the end date (4th July
as We’re writing), but not including the end date.
Now, As We are supposedly “walking” randomly from one day to another, We need to consider our 9204 days of walking as ∞ steps.
So, After ∞ steps, Let’s analyse the probabilities of the states.
df['state'].value_counts(normalize=True)
Downside 0.574298
Upside 0.425542
Consolidation 0.000159
Name: state, dtype: float64
So, After ∞ steps, The Probability of having a “Downside” state day is 57.4%.
This probability distribution has a special name – “The Stationary Distribution” or “The Equilibrium State”. It simply means that, This probability distribution does not change with time with respect to this Markov Chain. So, We found our Stationary State but this method does not sound very efficient right?
This Markov Chain is an example model with random values as a probability which can be seen in the weighted arrows!
Random Walking in this Markov Chain is comparatively similar. Here is an amazing tutorial here with full code for a similar Markov chain.
Here are the results of iterating this assumptive Markov Chain with 1,00,000 random walks –
Downside 0.35191
Upside 0.21245
Consolidation 0.43564
Name: state, dtype: float64
So, We managed to find a Stationary State. But this method is not efficient as We do not know if there is another Stationary State. Well, A better way to approach this problem is Linear Algebra.