markov chain in python code example
Example: markov chain python code
import numpy as np class MarkovChain(object): def __init__(self, transition_prob): """ Initialize the MarkovChain instance. Parameters ---------- transition_prob: dict A dict object representing the transition probabilities in Markov Chain. Should be of the form: {'state1': {'state1': 0.1, 'state2': 0.4}, 'state2': {...}} """ self.transition_prob = transition_prob self.states = list(transition_prob.keys()) 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_prob[current_state][next_state] for next_state in self.states] ) 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