updating latest
This commit is contained in:
118
markov/markov.py
118
markov/markov.py
@@ -18,14 +18,14 @@ simNum = 15000
|
||||
states = np.array(['L', 'w', 'W'])
|
||||
|
||||
# Possible sequences of events
|
||||
transitionName = np.array([['LL', 'Lw', 'LW'],
|
||||
['wL', 'ww', 'wW'],
|
||||
['WL', 'Ww', 'WW']])
|
||||
transitionName = np.array(
|
||||
[['LL', 'Lw', 'LW'], ['wL', 'ww', 'wW'], ['WL', 'Ww', 'WW']]
|
||||
)
|
||||
|
||||
# Probabilities Matrix (transition matrix)
|
||||
transitionMatrix = np.array([[0.6, 0.1, 0.3],
|
||||
[0.1, 0.7, 0.2],
|
||||
[0.2, 0.2, 0.6]])
|
||||
transitionMatrix = np.array(
|
||||
[[0.6, 0.1, 0.3], [0.1, 0.7, 0.2], [0.2, 0.2, 0.6]]
|
||||
)
|
||||
|
||||
# Starting state - Given as a list of probabilities of starting
|
||||
# in each state. To always start at the same state set one of them
|
||||
@@ -69,9 +69,14 @@ class markov(object):
|
||||
currentState: a string indicating the starting state
|
||||
steps: an integer determining how many steps (or times) to simulate"""
|
||||
|
||||
def __init__(self, states: np.array, transitionName: np.array,
|
||||
transitionMatrix: np.array, currentState: str,
|
||||
steps: int):
|
||||
def __init__(
|
||||
self,
|
||||
states: np.array,
|
||||
transitionName: np.array,
|
||||
transitionMatrix: np.array,
|
||||
currentState: str,
|
||||
steps: int,
|
||||
):
|
||||
super(markov, self).__init__()
|
||||
self.states = states
|
||||
self.list = list
|
||||
@@ -110,10 +115,11 @@ class markov(object):
|
||||
# To calculate the probability of the stateList
|
||||
self.prob = 1
|
||||
while i != self.steps:
|
||||
import pdb; pdb.set_trace() # breakpoint 24e62119 //
|
||||
if self.currentState == self.states[0]:
|
||||
self.change = np.random.choice(self.transitionName[0],
|
||||
replace=True,
|
||||
p=transitionMatrix[0])
|
||||
self.change = np.random.choice(
|
||||
self.transitionName[0], replace=True, p=transitionMatrix[0]
|
||||
)
|
||||
if self.change == self.transitionName[0][0]:
|
||||
self.prob = self.prob * self.transitionMatrix[0][0]
|
||||
self.stateList.append(self.states[0])
|
||||
@@ -127,9 +133,9 @@ class markov(object):
|
||||
self.currentState = self.states[2]
|
||||
self.stateList.append(self.states[2])
|
||||
elif self.currentState == self.states[1]:
|
||||
self.change = np.random.choice(self.transitionName[1],
|
||||
replace=True,
|
||||
p=transitionMatrix[1])
|
||||
self.change = np.random.choice(
|
||||
self.transitionName[1], replace=True, p=transitionMatrix[1]
|
||||
)
|
||||
if self.change == self.transitionName[1][0]:
|
||||
self.prob = self.prob * self.transitionMatrix[1][0]
|
||||
self.currentState = self.states[0]
|
||||
@@ -143,9 +149,9 @@ class markov(object):
|
||||
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])
|
||||
self.change = np.random.choice(
|
||||
self.transitionName[2], replace=True, p=transitionMatrix[2]
|
||||
)
|
||||
if self.change == self.transitionName[2][0]:
|
||||
self.prob = self.prob * self.transitionMatrix[2][0]
|
||||
self.currentState = self.states[0]
|
||||
@@ -161,8 +167,10 @@ class markov(object):
|
||||
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 taking these exact steps in this order is:'
|
||||
f' {self.prob:.2f} 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
|
||||
|
||||
|
||||
@@ -196,43 +204,69 @@ def main(*args, **kwargs):
|
||||
if initial_dist is not None:
|
||||
startingState = np.random.choice(states, p=initial_dist)
|
||||
for _ in itertools.repeat(None, simNum):
|
||||
markovChain = markov(states, transitionName,
|
||||
transitionMatrix, startingState,
|
||||
stepTime)
|
||||
markovChain = markov(
|
||||
states,
|
||||
transitionName,
|
||||
transitionMatrix,
|
||||
startingState,
|
||||
stepTime,
|
||||
)
|
||||
list_state.append(markovChain.forecast())
|
||||
startingState = np.random.choice(states, p=initial_dist)
|
||||
else:
|
||||
for _ in itertools.repeat(None, simNum):
|
||||
markovChain = markov(states, transitionName,
|
||||
transitionMatrix, startingState,
|
||||
stepTime)
|
||||
markovChain = markov(
|
||||
states,
|
||||
transitionName,
|
||||
transitionMatrix,
|
||||
startingState,
|
||||
stepTime,
|
||||
)
|
||||
list_state.append(markovChain.forecast())
|
||||
else:
|
||||
for _ in range(1, 2):
|
||||
list_state.append(markov(states, transitionName,
|
||||
transitionMatrix, startingState,
|
||||
stepTime).forecast())
|
||||
list_state.append(
|
||||
markov(
|
||||
states,
|
||||
transitionName,
|
||||
transitionMatrix,
|
||||
startingState,
|
||||
stepTime,
|
||||
).forecast()
|
||||
)
|
||||
for list in list_state:
|
||||
if(list[-1] == f'{endState!s}'):
|
||||
print(f'SUCCESS - path ended in the requested state {endState!s}'
|
||||
f':', list)
|
||||
if list[-1] == f'{endState!s}':
|
||||
print(
|
||||
f'SUCCESS - path ended in the requested state {endState!s}'
|
||||
f':',
|
||||
list,
|
||||
)
|
||||
count += 1
|
||||
else:
|
||||
print(f'FAILURE - path did not end in the requested state'
|
||||
f' {endState!s}:', list)
|
||||
print(
|
||||
f'FAILURE - path did not end in the requested state'
|
||||
f' {endState!s}:',
|
||||
list,
|
||||
)
|
||||
if setSim is False:
|
||||
simNum = 1
|
||||
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')
|
||||
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')
|
||||
print(
|
||||
f'P_{stepTime} is: \n'
|
||||
f'{markov.transition_matrix_step(transitionMatrix, stepTime)}\n'
|
||||
)
|
||||
if stat_dist:
|
||||
checker(initial_dist, 'initial distribution')
|
||||
print(f'Stat dist is {markov.stationary_dist(transitionMatrix,initial_dist, stepTime)}')
|
||||
print(
|
||||
f'Stat dist is {markov.stationary_dist(transitionMatrix,initial_dist, stepTime)}'
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
1
markov/requirements.txt
Normal file
1
markov/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
numpy==1.17.4
|
||||
Reference in New Issue
Block a user