Skip to content

Instantly share code, notes, and snippets.

@arjunprakash027
Created October 20, 2023 06:37
Show Gist options
  • Save arjunprakash027/444ac7c088c47f3ed5ee25c14d924bbf to your computer and use it in GitHub Desktop.
Save arjunprakash027/444ac7c088c47f3ed5ee25c14d924bbf to your computer and use it in GitHub Desktop.
A small program to demonstrate markov chains in python.
#this program is to create a markov chain in python
import numpy as np
class MarkovChain:
def __init__(self, transition_matrix,states):
self.transition_matrix = np.array(transition_matrix)
self.states = states
def next_state(self,current_state:str) -> str:
current_state_matrix = np.array([0]*len(self.states))
current_state_matrix[self.states.index(current_state)] = 1
next_state = current_state_matrix.dot(self.transition_matrix)
random_state_probablity = np.random.choice(len(self.states),p=next_state)
return self.states[random_state_probablity]
def nStates(self,starting_state:str,nstates:int):
current_state = np.array([0]*len(self.states))
current_state[self.states.index(starting_state)] = 1
n_states = []
state_transition_record = []
for i in range(nstates):
state_transition_record.append(current_state)
next_state = current_state.dot(self.transition_matrix)
random_state_probablity = np.random.choice(len(self.states),p=next_state)
n_states.append(self.states[random_state_probablity])
current_state = next_state
return n_states,state_transition_record
if __name__ == "__main__":
mc = MarkovChain([[0.7,0.3],[0.2,0.8]],["sunny","rainy"])
print(mc.next_state("rainy"))
print(mc.nStates("rainy",10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment