def backward(self, obs, start=0, length=0): if length == 0: length = obs.shape[0] start = length-1 beta = [] for i in range(start, start - length, -1): message = Message() # change p_obs = Potential.from_observation(obs[i, :], self.m, self.n) pot_change = p_obs.copy() if len(beta) > 0: temp = Message() for p in beta[-1].potentials: temp.add_potential(p * self.prior) pot_change.log_c += self.log_p1 + temp.log_likelihood() message.add_potential(pot_change) # no change if len(beta) > 0: for p in beta[-1].potentials: p2 = p * p_obs p2.log_c += self.log_p0 message.add_potential(p2) beta.append(message) beta.reverse() return beta
def update(self, predict, obs): m = Message() p_obs = Potential.from_observation(obs, self.m, self.n) for p in predict.potentials: m.add_potential(p * p_obs) return m