develop #2
@@ -27,14 +27,18 @@ transitionMatrix = np.array([[0.6, 0.1, 0.3],
|
|||||||
[0.1, 0.7, 0.2],
|
[0.1, 0.7, 0.2],
|
||||||
[0.2, 0.2, 0.6]])
|
[0.2, 0.2, 0.6]])
|
||||||
|
|
||||||
# Starting state
|
# Starting state - Given as a list of probabilities of starting
|
||||||
# startingState = 'L'
|
# 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 = np.array([1, 0, 0])
|
||||||
# initial_dist = None
|
|
||||||
|
|
||||||
# Steps to run
|
# Steps to run - How many steps or jumps we want the markov chain to make
|
||||||
stepTime = 5
|
stepTime = 1
|
||||||
# End state you want to find probabilites of
|
# 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'
|
endState = 'L'
|
||||||
|
|
||||||
""" Find the transition matrix for the given number of steps
|
""" Find the transition matrix for the given number of steps
|
||||||
@@ -82,9 +86,12 @@ class markov(object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def transition_matrix_step(transitionMatrix, steps):
|
def transition_matrix_step(transitionMatrix, steps):
|
||||||
for _ in itertools.repeat(None, steps):
|
try:
|
||||||
step_mat = np.matmul(transitionMatrix, transitionMatrix)
|
for _ in itertools.repeat(None, steps - 1):
|
||||||
return step_mat
|
step_mat = np.matmul(transitionMatrix, transitionMatrix)
|
||||||
|
return step_mat
|
||||||
|
except UnboundLocalError:
|
||||||
|
return transitionMatrix
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def stationary_dist(transitionMatrix, initial_dist, steps):
|
def stationary_dist(transitionMatrix, initial_dist, steps):
|
||||||
@@ -116,46 +123,46 @@ class markov(object):
|
|||||||
self.currentState = self.states[1]
|
self.currentState = self.states[1]
|
||||||
self.stateList.append(self.states[1])
|
self.stateList.append(self.states[1])
|
||||||
else:
|
else:
|
||||||
self.prob == self.transitionMatrix[0][2]
|
self.prob = self.transitionMatrix[0][2]
|
||||||
self.currentState = self.states[2]
|
self.currentState = self.states[2]
|
||||||
self.stateList.append(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],
|
self.change = np.random.choice(self.transitionName[1],
|
||||||
replace=True,
|
replace=True,
|
||||||
p=transitionMatrix[1])
|
p=transitionMatrix[1])
|
||||||
if self.change == "ww":
|
if self.change == self.transitionName[1][0]:
|
||||||
self.prob = self.prob * 0.15
|
self.prob = self.prob * self.transitionMatrix[1][0]
|
||||||
self.stateList.append("w")
|
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
|
pass
|
||||||
elif self.change == "wL":
|
|
||||||
self.prob = self.prob * 0.8
|
|
||||||
self.currentState = "L"
|
|
||||||
self.stateList.append("L")
|
|
||||||
else:
|
else:
|
||||||
self.prob = self.prob * 0.05
|
self.prob = self.prob * self.transitionMatrix[1][2]
|
||||||
self.currentState = "W"
|
self.currentState = self.states[2]
|
||||||
self.stateList.append("W")
|
self.stateList.append(self.states[2])
|
||||||
elif self.currentState == "W":
|
elif self.currentState == self.states[2]:
|
||||||
self.change = np.random.choice(self.transitionName[2],
|
self.change = np.random.choice(self.transitionName[2],
|
||||||
replace=True,
|
replace=True,
|
||||||
p=transitionMatrix[2])
|
p=transitionMatrix[2])
|
||||||
if self.change == "WW":
|
if self.change == self.transitionName[2][0]:
|
||||||
self.prob = self.prob * 0.05
|
self.prob = self.prob * self.transitionMatrix[2][0]
|
||||||
self.stateList.append("W")
|
self.currentState = self.states[0]
|
||||||
pass
|
self.stateList.append(self.states[0])
|
||||||
elif self.change == "WL":
|
elif self.change == self.transitionName[2][1]:
|
||||||
self.prob = self.prob * 0.8
|
self.prob = self.prob * self.transitionMatrix[2][1]
|
||||||
self.currentState = "L"
|
self.currentState = self.states[1]
|
||||||
self.stateList.append("L")
|
self.stateList.append(self.states[1])
|
||||||
else:
|
else:
|
||||||
self.prob = self.prob * 0.15
|
self.prob = self.prob * self.transitionMatrix[2][2]
|
||||||
self.currentState = "w"
|
self.stateList.append(self.states[2])
|
||||||
self.stateList.append("w")
|
pass
|
||||||
i += 1
|
i += 1
|
||||||
print(f'Path Markov Chain took in this iteration: {self.stateList}')
|
print(f'Path Markov Chain took in this iteration: {self.stateList}')
|
||||||
print(f'End state after {self.steps} steps: {self.currentState}')
|
print(f'End state after {self.steps} steps: {self.currentState}')
|
||||||
print(f'Probability of this specific path:'
|
print(f'Probability of taking these exact steps in this order is:'
|
||||||
f' {self.prob:.4f} or {self.prob:.2%}\n')
|
f' {self.prob:.2f} or {self.prob:.2%}\n')
|
||||||
return self.stateList
|
return self.stateList
|
||||||
|
|
||||||
|
|
||||||
@@ -215,8 +222,11 @@ def main(*args, **kwargs):
|
|||||||
f' {endState!s}:', list)
|
f' {endState!s}:', list)
|
||||||
if setSim is False:
|
if setSim is False:
|
||||||
simNum = 1
|
simNum = 1
|
||||||
print(f'\nThe probability of starting in {startingState} and finishing'
|
print(f'\nTherefore the estimated probability of starting in'
|
||||||
f' in {endState} after {stepTime} steps is {(count / simNum):.2%}\n')
|
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:
|
if transition_matrix_step:
|
||||||
print(f'P_{stepTime} is: \n'
|
print(f'P_{stepTime} is: \n'
|
||||||
f'{markov.transition_matrix_step(transitionMatrix, stepTime)}\n')
|
f'{markov.transition_matrix_step(transitionMatrix, stepTime)}\n')
|
||||||
|
|||||||
Reference in New Issue
Block a user