Skip to content

Instantly share code, notes, and snippets.

@cindyangelira
Created January 23, 2023 14:37
Show Gist options
  • Save cindyangelira/fb6f95a6172cba9b99e83380365ed9ea to your computer and use it in GitHub Desktop.
Save cindyangelira/fb6f95a6172cba9b99e83380365ed9ea to your computer and use it in GitHub Desktop.
Markov Chain Prediction
class MarkovChainPred(object):
def __init__(self, transition_matrix, states):
"""
Initialize the MarkovChain instance.
Parameters
----------
transition_matrix: 2-D array
A 2-D array representing the probabilities of change of
state in the Markov Chain.
states: 1-D array
An array representing the states of the Markov Chain. It
needs to be in the same order as transition_matrix.
"""
self.transition_matrix = np.atleast_2d(transition_matrix)
self.states = states
self.index_dict = {self.states[index]: index for index in
range(len(self.states))}
self.state_dict = {index: self.states[index] for index in
range(len(self.states))}
def next_state(self, current_state):
"""
Returns the state of the random variable at the next time
instance.
Parameters
----------
current_state: str
The current state of the system.
"""
return np.random.choice(
self.states,
p=self.transition_matrix[self.index_dict[current_state], :])
def generate_states(self, current_state, no=10):
"""
Generates the next states of the system.
Parameters
----------
current_state: str
The state of the current random variable.
no: int
The number of future states to generate.
"""
future_states = []
for i in range(no):
next_state = self.next_state(current_state)
future_states.append(next_state)
current_state = next_state
return future_states
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment