updated markov for general case (incomplete)
This commit is contained in:
125
markov/markov.py
125
markov/markov.py
@@ -11,7 +11,7 @@ seedNum = 27
|
||||
""" Simulation parameters """
|
||||
# Should we simulate more than once?
|
||||
setSim = True
|
||||
simNum = 1
|
||||
simNum = 5000
|
||||
|
||||
""" Define our data """
|
||||
# The Statespace
|
||||
@@ -23,23 +23,32 @@ transitionName = np.array([['LL', 'Lw', 'LW'],
|
||||
['WL', 'Ww', 'WW']])
|
||||
|
||||
# Probabilities Matrix (transition matrix)
|
||||
# Fill in
|
||||
transitionMatrix = None
|
||||
transitionMatrix = np.array([[0.6, 0.1, 0.3],
|
||||
[0.1, 0.7, 0.2],
|
||||
[0.2, 0.2, 0.6]])
|
||||
|
||||
# Starting state
|
||||
startingState = 'w'
|
||||
initial_dist = None
|
||||
# startingState = 'L'
|
||||
initial_dist = np.array([1, 0, 0])
|
||||
# initial_dist = None
|
||||
|
||||
# Steps to run
|
||||
stepTime = 2
|
||||
stepTime = 5
|
||||
# End state you want to find probabilites of
|
||||
endState = 'W'
|
||||
endState = 'L'
|
||||
|
||||
# Get P_steps
|
||||
p_steps = False
|
||||
""" Find the transition matrix for the given number of steps
|
||||
This finds the probabilities of going from step i to step j
|
||||
in N number of steps, where N is your step size
|
||||
E.g 10 steps would give P_10 which is prob of going from step i
|
||||
to step j in exactly 10 steps """
|
||||
transition_matrix_step = True
|
||||
|
||||
# Get Stationary Dist
|
||||
stat_dist = False
|
||||
""" Get Stationary distribution of the Markov Chain
|
||||
This tells you what % of time you spend in each state if you
|
||||
simulated the Markov Chain for a high number of steps
|
||||
THIS NEEDS THE INTIIAL DISTRIBUTION SETTING """
|
||||
stat_dist = True
|
||||
|
||||
|
||||
# A class that implements the Markov chain to forecast the state/mood:
|
||||
@@ -72,56 +81,91 @@ class markov(object):
|
||||
return np.random.seed(num)
|
||||
|
||||
@staticmethod
|
||||
def p_steps(transitionMatrix, initial_dist, steps):
|
||||
def transition_matrix_step(transitionMatrix, steps):
|
||||
for _ in itertools.repeat(None, steps):
|
||||
initial_dist = transitionMatrix.T.dot(initial_dist)
|
||||
return initial_dist
|
||||
step_mat = np.matmul(transitionMatrix, transitionMatrix)
|
||||
return step_mat
|
||||
|
||||
@staticmethod
|
||||
def stationary_dist(transitionMatrix, initial_dist, steps):
|
||||
for _ in itertools.repeat(None, steps):
|
||||
initial_dist = transitionMatrix.T.dot(initial_dist)
|
||||
return initial_dist
|
||||
w, v = np.linalg.eig(transitionMatrix.T)
|
||||
j_stationary = np.argmin(abs(w - 1.0))
|
||||
p_stationary = v[:, j_stationary].real
|
||||
p_stationary /= p_stationary.sum()
|
||||
return p_stationary
|
||||
|
||||
@functools.lru_cache(maxsize=128)
|
||||
def forecast(self):
|
||||
print(f'Start state: {self.currentState}')
|
||||
print(f'Start state: {self.currentState}\n')
|
||||
# Shall store the sequence of states taken
|
||||
self.stateList = [self.currentState]
|
||||
i = 0
|
||||
# To calculate the probability of the stateList
|
||||
self.prob = 1
|
||||
while i != self.steps:
|
||||
if self.currentState == 'L':
|
||||
if self.currentState == self.states[0]:
|
||||
self.change = np.random.choice(self.transitionName[0],
|
||||
replace=True,
|
||||
p=transitionMatrix[0])
|
||||
if self.change == 'LL':
|
||||
self.prob = self.prob * 0.8
|
||||
self.stateList.append('L')
|
||||
if self.change == self.transitionName[0][0]:
|
||||
self.prob = self.prob * self.transitionMatrix[0][0]
|
||||
self.stateList.append(self.states[0])
|
||||
pass
|
||||
elif self.change == 'Lw':
|
||||
elif self.change == self.transitionName[0][1]:
|
||||
self.prob = self.prob * self.transitionMatrix[0][1]
|
||||
self.currentState = self.states[1]
|
||||
self.stateList.append(self.states[1])
|
||||
else:
|
||||
self.prob == self.transitionMatrix[0][2]
|
||||
self.currentState = self.states[2]
|
||||
self.stateList.append(self.states[2])
|
||||
elif self.currentState == "w":
|
||||
self.change = np.random.choice(self.transitionName[1],
|
||||
replace=True,
|
||||
p=transitionMatrix[1])
|
||||
if self.change == "ww":
|
||||
self.prob = self.prob * 0.15
|
||||
self.currentState = 'w'
|
||||
self.stateList.append('w')
|
||||
self.stateList.append("w")
|
||||
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":
|
||||
# Fill in
|
||||
pass
|
||||
elif self.currentState == "W":
|
||||
# Fill in
|
||||
pass
|
||||
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")
|
||||
else:
|
||||
self.prob = self.prob * 0.15
|
||||
self.currentState = "w"
|
||||
self.stateList.append("w")
|
||||
i += 1
|
||||
print(f'Possible states: {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'Probability of all the possible sequence of states:'
|
||||
f' {self.prob}\n')
|
||||
print(f'Probability of this specific path:'
|
||||
f' {self.prob:.4f} or {self.prob:.2%}\n')
|
||||
return self.stateList
|
||||
|
||||
|
||||
def checker(item, name):
|
||||
try:
|
||||
item is not None
|
||||
except Exception:
|
||||
raise Exception(f'{name} is not set - set it and try again.')
|
||||
|
||||
|
||||
def main(*args, **kwargs):
|
||||
global startingState
|
||||
try:
|
||||
@@ -163,18 +207,21 @@ def main(*args, **kwargs):
|
||||
stepTime).forecast())
|
||||
for list in list_state:
|
||||
if(list[-1] == f'{endState!s}'):
|
||||
print(True, list)
|
||||
print(f'SUCCESS - path ended in the requested state {endState!s}'
|
||||
f':', list)
|
||||
count += 1
|
||||
else:
|
||||
print(False, list)
|
||||
print(f'FAILURE - path did not end in the requested state'
|
||||
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%}')
|
||||
if p_steps:
|
||||
print(f'P_{stepTime} is '
|
||||
f'{markov.p_steps(transitionMatrix, initial_dist, stepTime)}')
|
||||
f' in {endState} after {stepTime} steps is {(count / simNum):.2%}\n')
|
||||
if transition_matrix_step:
|
||||
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)}')
|
||||
|
||||
|
||||
|
||||
@@ -1,942 +0,0 @@
|
||||
{
|
||||
"auto_complete":
|
||||
{
|
||||
"selected_items":
|
||||
[
|
||||
[
|
||||
"mea",
|
||||
"mean_absolute_error\tfunction"
|
||||
],
|
||||
[
|
||||
"roc",
|
||||
"roc_auc_score\tfunction"
|
||||
],
|
||||
[
|
||||
"mean",
|
||||
"mean_squared_error\tfunction"
|
||||
],
|
||||
[
|
||||
"r",
|
||||
"r2_score\tfunction"
|
||||
],
|
||||
[
|
||||
"prec",
|
||||
"precision_score\tfunction"
|
||||
],
|
||||
[
|
||||
"re",
|
||||
"recall_score\tfunction"
|
||||
],
|
||||
[
|
||||
"lin",
|
||||
"lin_reg\tstatement"
|
||||
],
|
||||
[
|
||||
"X",
|
||||
"X_test\tstatement"
|
||||
],
|
||||
[
|
||||
"dec",
|
||||
"dec_tree\tstatement"
|
||||
],
|
||||
[
|
||||
"yt",
|
||||
"y_train\tstatement"
|
||||
],
|
||||
[
|
||||
"y_",
|
||||
"y_train\tstatement"
|
||||
],
|
||||
[
|
||||
"ran",
|
||||
"ran_for\tstatement"
|
||||
],
|
||||
[
|
||||
"Random",
|
||||
"RandomForestRegressor\tclass"
|
||||
],
|
||||
[
|
||||
"Dec",
|
||||
"DecisionTreeRegressor\tclass"
|
||||
],
|
||||
[
|
||||
"instant",
|
||||
"instaniated_models\tstatement"
|
||||
],
|
||||
[
|
||||
"i",
|
||||
"i"
|
||||
],
|
||||
[
|
||||
"train",
|
||||
"train_test_split\tfunction"
|
||||
],
|
||||
[
|
||||
"tes",
|
||||
"train_test_split\tfunction"
|
||||
],
|
||||
[
|
||||
"model",
|
||||
"model_selection\tmodule"
|
||||
],
|
||||
[
|
||||
"min",
|
||||
"min_samples_leaf\tparam"
|
||||
],
|
||||
[
|
||||
"max",
|
||||
"max_depth\tparam"
|
||||
],
|
||||
[
|
||||
"accur",
|
||||
"accuracy_score\tfunction"
|
||||
],
|
||||
[
|
||||
"y",
|
||||
"y_pred\tstatement"
|
||||
],
|
||||
[
|
||||
"reg",
|
||||
"reg_coef\tstatement"
|
||||
],
|
||||
[
|
||||
"V",
|
||||
"Var_Y"
|
||||
],
|
||||
[
|
||||
"Var",
|
||||
"Var_X"
|
||||
],
|
||||
[
|
||||
"li",
|
||||
"bmi_life_model"
|
||||
],
|
||||
[
|
||||
"cla",
|
||||
"classmethod\tclass"
|
||||
],
|
||||
[
|
||||
"tester",
|
||||
"tester1"
|
||||
],
|
||||
[
|
||||
"clas",
|
||||
"class\tNew Class"
|
||||
],
|
||||
[
|
||||
"int",
|
||||
"int\tclass"
|
||||
],
|
||||
[
|
||||
"set",
|
||||
"setattr\tfunction"
|
||||
],
|
||||
[
|
||||
"st",
|
||||
"Standalone"
|
||||
],
|
||||
[
|
||||
"M",
|
||||
"MyClass\tclass"
|
||||
],
|
||||
[
|
||||
"m",
|
||||
"my_printer\tstatement"
|
||||
],
|
||||
[
|
||||
"from",
|
||||
"fromBirthYear\tfunction"
|
||||
],
|
||||
[
|
||||
"class",
|
||||
"classmethod\tclass"
|
||||
],
|
||||
[
|
||||
"par",
|
||||
"partialmethod\tclass"
|
||||
],
|
||||
[
|
||||
"db",
|
||||
"initialize_db"
|
||||
],
|
||||
[
|
||||
"get",
|
||||
"getattr\tfunction"
|
||||
],
|
||||
[
|
||||
"name",
|
||||
"__name__"
|
||||
],
|
||||
[
|
||||
"__get",
|
||||
"__getattribute__\tfunction"
|
||||
],
|
||||
[
|
||||
"kwargs",
|
||||
"kwargs"
|
||||
],
|
||||
[
|
||||
"func",
|
||||
"functools\tmodule"
|
||||
],
|
||||
[
|
||||
"wra",
|
||||
"wrapper_slow_down\tfunction"
|
||||
],
|
||||
[
|
||||
"slo",
|
||||
"slowDownDecorator\tclass"
|
||||
],
|
||||
[
|
||||
"slow",
|
||||
"slowDown\tclass"
|
||||
],
|
||||
[
|
||||
"init",
|
||||
"init"
|
||||
],
|
||||
[
|
||||
"call",
|
||||
"callable"
|
||||
],
|
||||
[
|
||||
"Slo",
|
||||
"Slow_Down_Decorator\tclass"
|
||||
],
|
||||
[
|
||||
"Slow",
|
||||
"Slow_Down\tclass"
|
||||
],
|
||||
[
|
||||
"partial",
|
||||
"partial\tclass"
|
||||
],
|
||||
[
|
||||
"coun",
|
||||
"count_down\tfunction"
|
||||
],
|
||||
[
|
||||
"num",
|
||||
"num_calls\tstatement"
|
||||
],
|
||||
[
|
||||
"sum",
|
||||
"sum_squares"
|
||||
],
|
||||
[
|
||||
"wrapper",
|
||||
"wrapper_count_calls\tfunction"
|
||||
],
|
||||
[
|
||||
"n",
|
||||
"num_calls"
|
||||
],
|
||||
[
|
||||
"str",
|
||||
"str\tclass"
|
||||
],
|
||||
[
|
||||
"repea",
|
||||
"repeat_partial\tfunction"
|
||||
],
|
||||
[
|
||||
"parti",
|
||||
"repeat_partial_wrapper"
|
||||
],
|
||||
[
|
||||
"repeat",
|
||||
"repeatN\tfunction"
|
||||
],
|
||||
[
|
||||
"wrapper_rep",
|
||||
"wrapper_repeatN\tfunction"
|
||||
],
|
||||
[
|
||||
"de",
|
||||
"decorator_repeat\tfunction"
|
||||
],
|
||||
[
|
||||
"Call",
|
||||
"callable\tfunction"
|
||||
],
|
||||
[
|
||||
"R",
|
||||
"ReferenceError\tclass"
|
||||
],
|
||||
[
|
||||
"gree",
|
||||
"greeter_func\tstatement"
|
||||
],
|
||||
[
|
||||
"wrap",
|
||||
"wrapper_debug\tfunction"
|
||||
],
|
||||
[
|
||||
"__",
|
||||
"__name__"
|
||||
],
|
||||
[
|
||||
"kw",
|
||||
"kwargs_repr\tstatement"
|
||||
],
|
||||
[
|
||||
"args",
|
||||
"args_repr"
|
||||
],
|
||||
[
|
||||
"ge",
|
||||
"get_type\tfunction"
|
||||
],
|
||||
[
|
||||
"no",
|
||||
"not_during_the_night\tfunction"
|
||||
],
|
||||
[
|
||||
"ex",
|
||||
"executable\tinstance"
|
||||
],
|
||||
[
|
||||
"script",
|
||||
"script_file\tstatement"
|
||||
],
|
||||
[
|
||||
"sc",
|
||||
"script_file\tstatement"
|
||||
],
|
||||
[
|
||||
"Ke",
|
||||
"KeyError\tclass"
|
||||
],
|
||||
[
|
||||
"folder",
|
||||
"folder"
|
||||
],
|
||||
[
|
||||
"ar",
|
||||
"arguments\tstatement"
|
||||
],
|
||||
[
|
||||
"local",
|
||||
"locals_val\tstatement"
|
||||
],
|
||||
[
|
||||
"arguments",
|
||||
"arguments"
|
||||
],
|
||||
[
|
||||
"sctr",
|
||||
"script2_main\tfunction"
|
||||
],
|
||||
[
|
||||
"f",
|
||||
"f"
|
||||
],
|
||||
[
|
||||
"read_",
|
||||
"read_excel\tfunction"
|
||||
],
|
||||
[
|
||||
"read",
|
||||
"read_csv\tfunction"
|
||||
],
|
||||
[
|
||||
"is",
|
||||
"is_tasty"
|
||||
],
|
||||
[
|
||||
"prin",
|
||||
"print_sound"
|
||||
],
|
||||
[
|
||||
"w",
|
||||
"wheels"
|
||||
],
|
||||
[
|
||||
"pr",
|
||||
"print_sound"
|
||||
],
|
||||
[
|
||||
"date",
|
||||
"datetime\tclass"
|
||||
],
|
||||
[
|
||||
"p",
|
||||
"print_wheels\tfunction"
|
||||
],
|
||||
[
|
||||
"File",
|
||||
"FileNotFoundError\tclass"
|
||||
],
|
||||
[
|
||||
"borg",
|
||||
"borg"
|
||||
],
|
||||
[
|
||||
"log",
|
||||
"log"
|
||||
],
|
||||
[
|
||||
"conver",
|
||||
"convert_to_uppercase\tfunction"
|
||||
],
|
||||
[
|
||||
"hell",
|
||||
"hellow_world\tstatement"
|
||||
],
|
||||
[
|
||||
"__m",
|
||||
"__name__\tinstance"
|
||||
],
|
||||
[
|
||||
"fu",
|
||||
"func\tparam"
|
||||
],
|
||||
[
|
||||
"fun",
|
||||
"functools\tmodule"
|
||||
],
|
||||
[
|
||||
"funct",
|
||||
"function_call\tfunction"
|
||||
],
|
||||
[
|
||||
"d",
|
||||
"double_volume\tfunction"
|
||||
],
|
||||
[
|
||||
"s",
|
||||
"surface_area\tfunction"
|
||||
],
|
||||
[
|
||||
"new",
|
||||
"new_length\tparam"
|
||||
],
|
||||
[
|
||||
"Cube",
|
||||
"Cube"
|
||||
],
|
||||
[
|
||||
"area",
|
||||
"area_2\tfunction"
|
||||
],
|
||||
[
|
||||
"tri",
|
||||
"tri_area\tfunction"
|
||||
],
|
||||
[
|
||||
"triangle",
|
||||
"triangle_area\tstatement"
|
||||
],
|
||||
[
|
||||
"bas",
|
||||
"base_area\tstatement"
|
||||
],
|
||||
[
|
||||
"sl",
|
||||
"slant_height\tparam"
|
||||
],
|
||||
[
|
||||
"heigh",
|
||||
"height_tripled\tfunction"
|
||||
],
|
||||
[
|
||||
"h",
|
||||
"height_doubled\tfunction"
|
||||
],
|
||||
[
|
||||
"__in",
|
||||
"__init__\tfunction"
|
||||
],
|
||||
[
|
||||
"ba",
|
||||
"base_area"
|
||||
],
|
||||
[
|
||||
"garb",
|
||||
"garbage_check\tfunction"
|
||||
],
|
||||
[
|
||||
"len",
|
||||
"length\tparam"
|
||||
],
|
||||
[
|
||||
"__init",
|
||||
"__init_subclass__\tfunction"
|
||||
],
|
||||
[
|
||||
"Square",
|
||||
"Square\tclass"
|
||||
],
|
||||
[
|
||||
"se",
|
||||
"self"
|
||||
],
|
||||
[
|
||||
"list",
|
||||
"list_of_ips\tstatement"
|
||||
],
|
||||
[
|
||||
"su",
|
||||
"sub_list\tstatement"
|
||||
],
|
||||
[
|
||||
"def",
|
||||
"def\tFunction"
|
||||
]
|
||||
]
|
||||
},
|
||||
"buffers":
|
||||
[
|
||||
],
|
||||
"build_system": "",
|
||||
"build_system_choices":
|
||||
[
|
||||
],
|
||||
"build_varint": "",
|
||||
"command_palette":
|
||||
{
|
||||
"height": 0.0,
|
||||
"last_filter": "",
|
||||
"selected_items":
|
||||
[
|
||||
[
|
||||
"replv",
|
||||
"SublimeREPL: Python - virtualenv"
|
||||
],
|
||||
[
|
||||
"ac",
|
||||
"Virtualenv: Activate"
|
||||
],
|
||||
[
|
||||
"activ",
|
||||
"Virtualenv: Activate"
|
||||
],
|
||||
[
|
||||
"repl v",
|
||||
"SublimeREPL: Python - virtualenv"
|
||||
],
|
||||
[
|
||||
"virtualen",
|
||||
"Virtualenv: Activate"
|
||||
],
|
||||
[
|
||||
"repl p",
|
||||
"SublimeREPL: Python - virtualenv"
|
||||
],
|
||||
[
|
||||
"repl py",
|
||||
"SublimeREPL: Python - virtualenv"
|
||||
],
|
||||
[
|
||||
"repel p",
|
||||
"SublimeREPL: Python - virtualenv"
|
||||
],
|
||||
[
|
||||
"error",
|
||||
"SublimeLinter: Show All Errors"
|
||||
],
|
||||
[
|
||||
"show",
|
||||
"SublimeLinter: Show All Errors"
|
||||
],
|
||||
[
|
||||
"rep",
|
||||
"SublimeREPL: Python - virtualenv"
|
||||
],
|
||||
[
|
||||
"python",
|
||||
"SublimeREPL: Python - virtualenv"
|
||||
],
|
||||
[
|
||||
"virtual",
|
||||
"Virtualenv: Activate"
|
||||
],
|
||||
[
|
||||
"python vi",
|
||||
"SublimeREPL: Python - virtualenv"
|
||||
],
|
||||
[
|
||||
"repl python",
|
||||
"SublimeREPL: Python - virtualenv"
|
||||
],
|
||||
[
|
||||
"virtua",
|
||||
"Virtualenv: Activate"
|
||||
],
|
||||
[
|
||||
"sublimerepl",
|
||||
"Virtualenv: SublimeREPL - Python"
|
||||
],
|
||||
[
|
||||
"install",
|
||||
"Package Control: Install Package"
|
||||
],
|
||||
[
|
||||
"virt",
|
||||
"Virtualenv: Activate"
|
||||
],
|
||||
[
|
||||
"virtu",
|
||||
"Virtualenv: Activate"
|
||||
],
|
||||
[
|
||||
"packa",
|
||||
"Package Control: Install Package"
|
||||
],
|
||||
[
|
||||
"vir",
|
||||
"Virtualenv: New (venv)"
|
||||
],
|
||||
[
|
||||
"install pa",
|
||||
"Package Control: Install Package"
|
||||
],
|
||||
[
|
||||
"insta",
|
||||
"Package Control: Install Package"
|
||||
],
|
||||
[
|
||||
"ins",
|
||||
"Package Control: Install Package"
|
||||
],
|
||||
[
|
||||
"brow",
|
||||
"Preferences: Browse Packages"
|
||||
],
|
||||
[
|
||||
"browse",
|
||||
"Preferences: Browse Packages"
|
||||
],
|
||||
[
|
||||
"remove",
|
||||
"Package Control: Remove Package"
|
||||
],
|
||||
[
|
||||
"Snippet: ",
|
||||
"Snippet: For Loop"
|
||||
],
|
||||
[
|
||||
"remo",
|
||||
"Package Control: Remove Channel"
|
||||
],
|
||||
[
|
||||
"show all",
|
||||
"SublimeLinter: Show All Errors"
|
||||
],
|
||||
[
|
||||
"prefs",
|
||||
"Preferences: SublimeLinter Settings"
|
||||
],
|
||||
[
|
||||
"package in",
|
||||
"Package Control: Install Package"
|
||||
],
|
||||
[
|
||||
"install pack",
|
||||
"Package Control: Install Package"
|
||||
],
|
||||
[
|
||||
"ayu: Activate theme",
|
||||
"ayu: Activate theme"
|
||||
],
|
||||
[
|
||||
"Browse Pack",
|
||||
"Preferences: Browse Packages"
|
||||
],
|
||||
[
|
||||
"Package Control: insta",
|
||||
"Package Control: Install Package"
|
||||
]
|
||||
],
|
||||
"width": 0.0
|
||||
},
|
||||
"console":
|
||||
{
|
||||
"height": 250.0,
|
||||
"history":
|
||||
[
|
||||
"sublime.cache_path()",
|
||||
"import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)"
|
||||
]
|
||||
},
|
||||
"distraction_free":
|
||||
{
|
||||
"menu_visible": true,
|
||||
"show_minimap": false,
|
||||
"show_open_files": false,
|
||||
"show_tabs": false,
|
||||
"side_bar_visible": false,
|
||||
"status_bar_visible": false
|
||||
},
|
||||
"expanded_folders":
|
||||
[
|
||||
],
|
||||
"file_history":
|
||||
[
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/Regression Metrics/regression.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/Regression Metrics/tests2.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/3. Testing your models/tests.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/3. Testing your models/quiz.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/3. Testing your models/classification_metrics.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/3. Testing your models/data.csv",
|
||||
"/home/dtomlinson/git-repos/sublime/packages",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Ensemble Methods/spam_ensembles.py",
|
||||
"/home/dtomlinson/git-repos/sublime/licesne",
|
||||
"/home/dtomlinson/git-repos/sublime/.config/sublime-text-3/Packages/User/SublimeLinter.sublime-settings",
|
||||
"/home/dtomlinson/git-repos/sublime/Projects/base.sublime-project",
|
||||
"/home/dtomlinson/sublime/base.sublime-project",
|
||||
"/home/dtomlinson/git-repos/sublime/.config/sublime-text-3/Packages/Preferences.sublime-settings",
|
||||
"/home/dtomlinson/git-repos/sublime/Preferences.sublime-settings",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/.gitignore",
|
||||
"/home/dtomlinson/projects/temp/test.py",
|
||||
"/home/dtomlinson/projects/markov/markov.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Support Vector Machines/data.csv",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Support Vector Machines/quiz.py",
|
||||
"/home/dtomlinson/docker-commands/wordpress",
|
||||
"/home/dtomlinson/projects/.gitignore",
|
||||
"/home/dtomlinson/projects/README.md",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/README.md",
|
||||
"/home/dtomlinson/projects/reddit/comment_scrape.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/19. Titanic Exploration/titanic.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Naive Bayes/Bayesian_Inference.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/19. Titanic Exploration/titanic_solution.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/18. Decision Trees in sklearn/titanic.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/18. Decision Trees in sklearn/data.csv",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/18. Decision Trees in sklearn/quiz.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/Calculating information gain on a dataset.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Perceptron Algoritm/9. Perceptron Algorithm/data.csv",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Perceptron Algoritm/9. Perceptron Algorithm/perceptron.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Perceptron Algoritm/7 Perceptrons as Logical Operators/perceptron_not.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Perceptron Algoritm/7 Perceptrons as Logical Operators/perceptron_and.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/28. Feature Scaling/feature_scaling.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/28. Feature Scaling/data.csv",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/27. Regularization/regularization.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/27. Regularization/data.csv",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/25. Polynomial Regression/data.csv",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/25. Polynomial Regression/regression.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/25. Polynomial Regression/solution.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/20. Multiple Linear Regression/quiz.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/18. Linear Regression in scikit-learn/data.csv",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/18. Linear Regression in scikit-learn/regression.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/16 Quiz: Mini-Batch Gradient Descent/solution.py",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/16 Quiz: Mini-Batch Gradient Descent/data.csv",
|
||||
"/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/16 Quiz: Mini-Batch Gradient Descent/batch_graddesc.py",
|
||||
"/home/dtomlinson/projects/learning/property/property.py",
|
||||
"/home/dtomlinson/projects/learning/descriptors/test.py",
|
||||
"/home/dtomlinson/projects/learning/decorators/decorator.py",
|
||||
"/home/dtomlinson/projects/learning/decorators/classes_with_args.py",
|
||||
"/home/dtomlinson/projects/learning/decorators/test.py",
|
||||
"/tmp/mozilla_dtomlinson0/borg-docker-plex-do.sh",
|
||||
"/home/dtomlinson/.config/sublime-text-3/Packages/User/SublimeREPL.sublime-settings",
|
||||
"/home/dtomlinson/projects/learning/learning.sublime-project",
|
||||
"/home/dtomlinson/projects/boto3/blocking_suggestions/.history.txt",
|
||||
"/home/dtomlinson/.bash_profile",
|
||||
"/home/dtomlinson/projects/learning/singletons/client_a.py",
|
||||
"/home/dtomlinson/projects/learning/singletons/client_b.py",
|
||||
"/home/dtomlinson/projects/learning/singletons/db.py",
|
||||
"/home/dtomlinson/projects/learning/decorators/function_decorators.py",
|
||||
"/home/dtomlinson/projects/learning/decorators/classes_decorater.py",
|
||||
"/home/dtomlinson/.config/sublime-text-3/Packages/SublimeREPL/SublimeREPL.sublime-settings",
|
||||
"/home/dtomlinson/projects/learning/decorators/script1.py",
|
||||
"/home/dtomlinson/projects/learning/decorators/decorators.py",
|
||||
"/home/dtomlinson/projects/learning/decorators/__init__.py",
|
||||
"/home/dtomlinson/projects/learning/decorators/pathfix.py",
|
||||
"/home/dtomlinson/projects/learning/import-args/script1.py",
|
||||
"/home/dtomlinson/projects/learning/super/class_1.py",
|
||||
"/home/dtomlinson/projects/learning/super/temp.py",
|
||||
"/home/dtomlinson/projects/learning/import-args/script2.py",
|
||||
"/home/dtomlinson/projects/learning/import-args/script3.py",
|
||||
"/home/dtomlinson/projects/bayes-learning/lesson1.py",
|
||||
"/home/dtomlinson/.virtualenvs/bayes-learning/lib/python3.7/site-packages/pandas/core/series.py",
|
||||
"/home/dtomlinson/.virtualenvs/bayes-learning/lib/python3.7/site-packages/pandas/io/api.py",
|
||||
"/home/dtomlinson/.virtualenvs/bayes-learning/lib/python3.7/site-packages/pandas/io/excel.py",
|
||||
"/home/dtomlinson/docker-commands/bookstack-temp.txt",
|
||||
"/home/dtomlinson/projects/learning/super/triangle.py",
|
||||
"/home/dtomlinson/projects/learning/super/rectangle-square.py",
|
||||
"/home/dtomlinson/projects/learning/class-and-object-orianted-python/lesson1.py",
|
||||
"/usr/lib64/python3.7/datetime.py",
|
||||
"/home/dtomlinson/projects/bayes-learning/seaborn.py",
|
||||
"/home/dtomlinson/.virtualenvs/bayes-learning/lib/python3.7/site-packages/pandas/core/frame.py",
|
||||
"/home/dtomlinson/Downloads/borg-backup-plex-do.sh",
|
||||
"/home/dtomlinson/Downloads/borg-docker-plex-do.sh",
|
||||
"/home/dtomlinson/Downloads/tmp/borg-docker-plex-do.sh",
|
||||
"/home/dtomlinson/Downloads/tmp/borg-backup-plex-do.sh",
|
||||
"/home/dtomlinson/Downloads/borg-docker-plex-server.sh",
|
||||
"/home/dtomlinson/Downloads/borg-docker-plex-download.sh",
|
||||
"/home/dtomlinson/Downloads/borg-backup-plex-download.sh",
|
||||
"/home/dtomlinson/Downloads/borg-backup-plex-server.sh",
|
||||
"/home/dtomlinson/Downloads/credentials.csv",
|
||||
"/home/dtomlinson/requirements.txt",
|
||||
"/home/dtomlinson/.config/sublime-text-3/Packages/Anaconda/Anaconda.sublime-settings",
|
||||
"/home/dtomlinson/.config/sublime-text-3/Packages/User/Anaconda.sublime-settings",
|
||||
"/home/dtomlinson/projects/learning/test.py",
|
||||
"/home/dtomlinson/projects/temp/temp_1.py",
|
||||
"/home/dtomlinson/projects/learning/class-and-object-orianted-python/links.txt",
|
||||
"/home/dtomlinson/projects/base/base.sublime-project",
|
||||
"/home/dtomlinson/projects/temp/temp_new.sublime-project",
|
||||
"/home/dtomlinson/projects/temp/temp.sublime-project",
|
||||
"/home/dtomlinson/.config/sublime-text-3/Packages/User/ayu-mirage.sublime-theme",
|
||||
"/home/dtomlinson/.config/sublime-text-3/Packages/User/ayu-dark.sublime-theme",
|
||||
"/home/dtomlinson/.config/sublime-text-3/Packages/SideBarEnhancements/Side Bar.sublime-settings",
|
||||
"/home/dtomlinson/.config/sublime-text-3/Packages/User/Python.sublime-settings"
|
||||
],
|
||||
"find":
|
||||
{
|
||||
"height": 29.0
|
||||
},
|
||||
"find_in_files":
|
||||
{
|
||||
"height": 0.0,
|
||||
"where_history":
|
||||
[
|
||||
]
|
||||
},
|
||||
"find_state":
|
||||
{
|
||||
"case_sensitive": false,
|
||||
"find_history":
|
||||
[
|
||||
"r2",
|
||||
"import",
|
||||
",",
|
||||
"= ",
|
||||
" =",
|
||||
"= ",
|
||||
" i",
|
||||
"i",
|
||||
"f1_score",
|
||||
"recall_score",
|
||||
"precision_score",
|
||||
"accuracy_score",
|
||||
"fit",
|
||||
"- ",
|
||||
"movie",
|
||||
"movie is",
|
||||
"r",
|
||||
",",
|
||||
"0",
|
||||
"__x",
|
||||
"x",
|
||||
"arg",
|
||||
"fibonacci",
|
||||
"self",
|
||||
"decorator",
|
||||
"self"
|
||||
],
|
||||
"highlight": true,
|
||||
"in_selection": false,
|
||||
"preserve_case": false,
|
||||
"regex": false,
|
||||
"replace_history":
|
||||
[
|
||||
],
|
||||
"reverse": false,
|
||||
"show_context": true,
|
||||
"use_buffer2": true,
|
||||
"whole_word": true,
|
||||
"wrap": false
|
||||
},
|
||||
"groups":
|
||||
[
|
||||
{
|
||||
"sheets":
|
||||
[
|
||||
]
|
||||
}
|
||||
],
|
||||
"incremental_find":
|
||||
{
|
||||
"height": 29.0
|
||||
},
|
||||
"input":
|
||||
{
|
||||
"height": 51.0
|
||||
},
|
||||
"layout":
|
||||
{
|
||||
"cells":
|
||||
[
|
||||
[
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1
|
||||
]
|
||||
],
|
||||
"cols":
|
||||
[
|
||||
0.0,
|
||||
1.0
|
||||
],
|
||||
"rows":
|
||||
[
|
||||
0.0,
|
||||
1.0
|
||||
]
|
||||
},
|
||||
"menu_visible": true,
|
||||
"output.SublimeLinter":
|
||||
{
|
||||
"height": 0.0
|
||||
},
|
||||
"output.exec":
|
||||
{
|
||||
"height": 132.0
|
||||
},
|
||||
"output.find_results":
|
||||
{
|
||||
"height": 0.0
|
||||
},
|
||||
"pinned_build_system": "Packages/Virtualenv/Python + Virtualenv.sublime-build",
|
||||
"project": "/home/dtomlinson/sublime/base.sublime-project",
|
||||
"replace":
|
||||
{
|
||||
"height": 54.0
|
||||
},
|
||||
"save_all_on_build": true,
|
||||
"select_file":
|
||||
{
|
||||
"height": 0.0,
|
||||
"last_filter": "",
|
||||
"selected_items":
|
||||
[
|
||||
],
|
||||
"width": 0.0
|
||||
},
|
||||
"select_project":
|
||||
{
|
||||
"height": 500.0,
|
||||
"last_filter": "",
|
||||
"selected_items":
|
||||
[
|
||||
[
|
||||
"",
|
||||
"~/projects/bayes-learning/seaborn.sublime-workspace"
|
||||
]
|
||||
],
|
||||
"width": 380.0
|
||||
},
|
||||
"select_symbol":
|
||||
{
|
||||
"height": 0.0,
|
||||
"last_filter": "",
|
||||
"selected_items":
|
||||
[
|
||||
],
|
||||
"width": 0.0
|
||||
},
|
||||
"selected_group": 0,
|
||||
"settings":
|
||||
{
|
||||
},
|
||||
"show_minimap": true,
|
||||
"show_open_files": true,
|
||||
"show_tabs": true,
|
||||
"side_bar_visible": true,
|
||||
"side_bar_width": 276.0,
|
||||
"status_bar_visible": true,
|
||||
"template_settings":
|
||||
{
|
||||
"max_columns": 2
|
||||
}
|
||||
}
|
||||
@@ -29,18 +29,26 @@ transitionMatrix = np.array([[0.6, 0.1, 0.3],
|
||||
|
||||
# Starting state
|
||||
startingState = 'w'
|
||||
initial_dist = np.array([0.3, 0.3, 0.4])
|
||||
initial_dist = np.array([0, 1, 0])
|
||||
# initial_dist = None
|
||||
|
||||
# Steps to run
|
||||
stepTime = 2
|
||||
stepTime = 100
|
||||
# End state you want to find probabilites of
|
||||
endState = 'W'
|
||||
|
||||
# Get P_steps
|
||||
p_steps = False
|
||||
""" Find the transition matrix for the given number of steps
|
||||
This finds the probabilities of going from step i to step j
|
||||
in N number of steps, where N is your step size
|
||||
E.g 10 steps would give P_10 which is prob of going from step i
|
||||
to step j in exactly 10 steps """
|
||||
transition_matrix_step = True
|
||||
|
||||
# Get Stationary Dist
|
||||
stat_dist = False
|
||||
""" Get Stationary distribution of the Markov Chain
|
||||
This tells you what % of time you spend in each state if you
|
||||
simulated the Markov Chain for a high number of steps
|
||||
THIS NEEDS THE INTIIAL DISTRIBUTION SETTING """
|
||||
stat_dist = True
|
||||
|
||||
|
||||
# A class that implements the Markov chain to forecast the state/mood:
|
||||
@@ -73,20 +81,22 @@ class markov(object):
|
||||
return np.random.seed(num)
|
||||
|
||||
@staticmethod
|
||||
def p_steps(transitionMatrix, initial_dist, steps):
|
||||
def transition_matrix_step(transitionMatrix, steps):
|
||||
for _ in itertools.repeat(None, steps):
|
||||
initial_dist = transitionMatrix.T.dot(initial_dist)
|
||||
return initial_dist
|
||||
step_mat = np.matmul(transitionMatrix, transitionMatrix)
|
||||
return step_mat
|
||||
|
||||
@staticmethod
|
||||
def stationary_dist(transitionMatrix, initial_dist, steps):
|
||||
for _ in itertools.repeat(None, steps):
|
||||
initial_dist = transitionMatrix.T.dot(initial_dist)
|
||||
return initial_dist
|
||||
w, v = np.linalg.eig(transitionMatrix.T)
|
||||
j_stationary = np.argmin(abs(w - 1.0))
|
||||
p_stationary = v[:, j_stationary].real
|
||||
p_stationary /= p_stationary.sum()
|
||||
return p_stationary
|
||||
|
||||
@functools.lru_cache(maxsize=128)
|
||||
def forecast(self):
|
||||
print(f'Start state: {self.currentState}')
|
||||
print(f'Start state: {self.currentState}\n')
|
||||
# Shall store the sequence of states taken
|
||||
self.stateList = [self.currentState]
|
||||
i = 0
|
||||
@@ -142,13 +152,20 @@ class markov(object):
|
||||
self.currentState = "w"
|
||||
self.stateList.append("w")
|
||||
i += 1
|
||||
print(f'Possible states: {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'Probability of all the possible sequence of states:'
|
||||
f' {self.prob}\n')
|
||||
print(f'Probability of this specific path:'
|
||||
f' {self.prob:.4f} or {self.prob:.2%}\n')
|
||||
return self.stateList
|
||||
|
||||
|
||||
def checker(item, name):
|
||||
try:
|
||||
item is not None
|
||||
except Exception:
|
||||
raise Exception(f'{name} is not set - set it and try again.')
|
||||
|
||||
|
||||
def main(*args, **kwargs):
|
||||
global startingState
|
||||
try:
|
||||
@@ -190,18 +207,21 @@ def main(*args, **kwargs):
|
||||
stepTime).forecast())
|
||||
for list in list_state:
|
||||
if(list[-1] == f'{endState!s}'):
|
||||
print(True, list)
|
||||
print(f'SUCCESS - path ended in the requested state {endState!s}'
|
||||
f':', list)
|
||||
count += 1
|
||||
else:
|
||||
print(False, list)
|
||||
print(f'FAILURE - path did not end in the requested state'
|
||||
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%}')
|
||||
if p_steps:
|
||||
print(f'P_{stepTime} is '
|
||||
f'{markov.p_steps(transitionMatrix, initial_dist, stepTime)}')
|
||||
f' in {endState} after {stepTime} steps is {(count / simNum):.2%}\n')
|
||||
if transition_matrix_step:
|
||||
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)}')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user