working on forward/backward procedure

This commit is contained in:
aj 2020-12-23 20:12:08 +00:00
parent 5c1ee8ed41
commit 08c130f020
4 changed files with 241 additions and 26 deletions

File diff suppressed because one or more lines are too long

View File

@ -21,19 +21,83 @@ class MarkovModel:
def __init__(self, states: list, observations: list = list(), state_transitions: list = list()): def __init__(self, states: list, observations: list = list(), state_transitions: list = list()):
self.observations = observations self.observations = observations
self.state_transitions = state_transitions self.state_transitions = state_transitions # use state number not state index, is padded by entry and exit probs
self.states = states # number of states self.states = states # number of states
# self.timesteps = list() # self.timesteps = list()
self.forward = np.zeros((len(states), len(observations))) self.forward = np.zeros((len(states), len(observations)))
self.backward = np.zeros((len(states), len(observations))) self.backward = np.zeros((len(states), len(observations)))
def get_other_state_index(self, state_in):
"""For when state changes, get other index for retrieving state transitions (FOR 0 INDEXING)"""
if state_in == 0:
return 1
elif state_in == 1:
return 0
else:
print(f"invalid state index provided, ({state_in})")
def get_other_state_number(self, state_in):
"""For when state changes, get other number for retrieving state transitions (FOR 1 INDEXING)"""
return self.get_other_state_index(state_in - 1) + 1
def populate_forward(self): def populate_forward(self):
for t, observation in enumerate(self.observations): # iterate through observations (time) for t, observation in enumerate(self.observations): # iterate through observations (time)
for state_number, state in enumerate(self.states): for state_index, state in enumerate(self.states):
state_number = state_index + 1 # for easier reading (arrays 0-indexed, numbers start at 1)
if t == 0: # calcualte initial if t == 0: # calcualte initial
self.forward[state_number, t] = self.state_transitions[0, state_number + 1] * gaussian(observation, state.mean, state.std_dev) self.forward[state_index, t] = self.state_transitions[0, state_number] * gaussian(observation, state.mean, state.std_dev)
else: else:
self.forward[state_number, t] = gaussian(observation, state.mean, state.std_dev) # each state for each time has two paths leading to
other_index = self.get_other_state_index(state_index)
other_number = other_index + 1 # for 1 indexing
# previous value prob of changing from previous state to current
this_to_this = self.forward[state_index, t - 1] * self.state_transitions[state_number, state_number]
other_to_this = self.forward[other_index, t - 1] * self.state_transitions[other_number, state_number]
self.forward[state_index, t] = (this_to_this + other_to_this) * gaussian(observation, state.mean, state.std_dev)
@property
def p_observations_forward(self):
sum = 0
for state_index, final_likelihood in enumerate(self.forward[:, -1]):
sum += final_likelihood * self.state_transitions[state_index + 1, -1] # get exit prob from state transitions
return sum
#TODO finish
def populate_backward(self):
# initialise from exit probabilities
self.backward[:, -1] = self.state_transitions[1:len(self.states) + 1, -1]
for t, observation in list(enumerate(self.observations[1:]))[::-1]: # iterate backwards through observations (time)
print(t, observation)
for state_index, state in enumerate(self.states):
state_number = state_index + 1 # for easier reading (arrays 0-indexed, numbers start at 1)
other_index = self.get_other_state_index(state_index)
other_number = other_index + 1 # for 1 indexing
# previous value prob of changing from previous state to current
this_to_this = self.backward[state_index, t + 1] * self.state_transitions[state_number, state_number]
other_to_this = self.backward[other_index, t + 1] * self.state_transitions[other_number, state_number]
self.backward[state_index, t] = (this_to_this + other_to_this) * gaussian(observation, state.mean, state.std_dev)
#TODO finish
@property
def p_observations_backward(self):
sum = 0
for state_index, initial_likelihood in enumerate(self.backward[:, 0]):
sum += self.state_transitions[0, state_index + 1] * gaussian(self.observations[0], self.states[state_index].mean, self.states[state_index].std_dev) * initial_likelihood
return sum

View File

@ -136,7 +136,7 @@ Andy Pack
\begin_layout Standard \begin_layout Standard
\align center \align center
\begin_inset Graphics \begin_inset Graphics
filename ../../../matlab/lpss/report/surrey.png filename surrey.png
lyxscale 15 lyxscale 15
width 40col% width 40col%
@ -323,6 +323,17 @@ lstparams "language=Python,breaklines=true,frame=tb,otherkeywords={self},emph={S
\end_inset \end_inset
\end_layout
\begin_layout Standard
\begin_inset CommandInset include
LatexCommand lstinputlisting
filename "../markov.py"
lstparams "language=Python,breaklines=true,frame=tb,otherkeywords={self},emph={State},emphstyle={\\ttb\\color{darkred}},basicstyle={\\ttfamily},commentstyle={\\color{commentgreen}\\itshape},keywordstyle={\\color{darkblue}},emphstyle={\\color{red}},stringstyle={\\color{red}},caption={Markov model object defining forward and backward procedure},label={markov-listing}"
\end_inset
\end_layout \end_layout
\end_body \end_body

File diff suppressed because one or more lines are too long