From efa1a09810c198692fb0c53608ceb15481f28568 Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Sat, 13 Jul 2019 21:01:30 +0100 Subject: [PATCH] updated markov to final version for jack --- markov/markov.py | 84 +++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/markov/markov.py b/markov/markov.py index cb74e11..3d9ac39 100644 --- a/markov/markov.py +++ b/markov/markov.py @@ -27,14 +27,18 @@ transitionMatrix = np.array([[0.6, 0.1, 0.3], [0.1, 0.7, 0.2], [0.2, 0.2, 0.6]]) -# Starting state -# startingState = 'L' +# Starting state - Given as a list of probabilities of starting +# in each state. To always start at the same state set one of them +# to 1 and the rest to 0. These must add to 1! initial_dist = np.array([1, 0, 0]) -# initial_dist = None -# Steps to run -stepTime = 5 -# End state you want to find probabilites of +# Steps to run - How many steps or jumps we want the markov chain to make +stepTime = 1 +# End state you want to find probabilites of - we can estimate the stationary +# distribution by running this many times for many steps. +# E.g to find the stat distribution for L run the simulation 10,000 times +# for 1000 jumps. This should be very close to the actual value which +# we can find below. endState = 'L' """ Find the transition matrix for the given number of steps @@ -82,9 +86,12 @@ class markov(object): @staticmethod def transition_matrix_step(transitionMatrix, steps): - for _ in itertools.repeat(None, steps): - step_mat = np.matmul(transitionMatrix, transitionMatrix) - return step_mat + try: + for _ in itertools.repeat(None, steps - 1): + step_mat = np.matmul(transitionMatrix, transitionMatrix) + return step_mat + except UnboundLocalError: + return transitionMatrix @staticmethod def stationary_dist(transitionMatrix, initial_dist, steps): @@ -116,46 +123,46 @@ class markov(object): self.currentState = self.states[1] self.stateList.append(self.states[1]) else: - self.prob == self.transitionMatrix[0][2] + self.prob = self.transitionMatrix[0][2] self.currentState = self.states[2] self.stateList.append(self.states[2]) - elif self.currentState == "w": + elif self.currentState == self.states[1]: self.change = np.random.choice(self.transitionName[1], replace=True, p=transitionMatrix[1]) - if self.change == "ww": - self.prob = self.prob * 0.15 - self.stateList.append("w") + if self.change == self.transitionName[1][0]: + self.prob = self.prob * self.transitionMatrix[1][0] + self.currentState = self.states[0] + self.stateList.append(self.states[0]) + elif self.change == self.transitionName[1][1]: + self.prob = self.prob * self.transitionMatrix[1][1] + self.stateList.append(self.states[1]) pass - elif self.change == "wL": - self.prob = self.prob * 0.8 - self.currentState = "L" - self.stateList.append("L") else: - self.prob = self.prob * 0.05 - self.currentState = "W" - self.stateList.append("W") - elif self.currentState == "W": + self.prob = self.prob * self.transitionMatrix[1][2] + self.currentState = self.states[2] + self.stateList.append(self.states[2]) + elif self.currentState == self.states[2]: self.change = np.random.choice(self.transitionName[2], replace=True, p=transitionMatrix[2]) - if self.change == "WW": - self.prob = self.prob * 0.05 - self.stateList.append("W") - pass - elif self.change == "WL": - self.prob = self.prob * 0.8 - self.currentState = "L" - self.stateList.append("L") + if self.change == self.transitionName[2][0]: + self.prob = self.prob * self.transitionMatrix[2][0] + self.currentState = self.states[0] + self.stateList.append(self.states[0]) + elif self.change == self.transitionName[2][1]: + self.prob = self.prob * self.transitionMatrix[2][1] + self.currentState = self.states[1] + self.stateList.append(self.states[1]) else: - self.prob = self.prob * 0.15 - self.currentState = "w" - self.stateList.append("w") + self.prob = self.prob * self.transitionMatrix[2][2] + self.stateList.append(self.states[2]) + pass i += 1 print(f'Path Markov Chain took in this iteration: {self.stateList}') print(f'End state after {self.steps} steps: {self.currentState}') - print(f'Probability of this specific path:' - f' {self.prob:.4f} or {self.prob:.2%}\n') + print(f'Probability of taking these exact steps in this order is:' + f' {self.prob:.2f} or {self.prob:.2%}\n') return self.stateList @@ -215,8 +222,11 @@ def main(*args, **kwargs): f' {endState!s}:', list) if setSim is False: simNum = 1 - print(f'\nThe probability of starting in {startingState} and finishing' - f' in {endState} after {stepTime} steps is {(count / simNum):.2%}\n') + print(f'\nTherefore the estimated probability of starting in' + f' {startingState} and finishing in {endState} after ' + f'{stepTime} steps is ' + f'{(count / simNum):.2%}.\n' + f'This is calculated by number of success/total steps\n') if transition_matrix_step: print(f'P_{stepTime} is: \n' f'{markov.transition_matrix_step(transitionMatrix, stepTime)}\n') -- 2.49.1