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
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types