def short_state_trace(): fbhmm = ForwardBackwardHMM(pxk_xkm1, pyk_xk, px0, y_obs_long) probs, alphas, betas = fbhmm.forward_backward() probs_eln, logalphas, logbetas = fbhmm.forward_backward_eln() state_trace = fbhmm.max_likelihood_state_estimate(probs) eln_state_trace = fbhmm.max_likelihood_state_estimate(probs_eln) K = [i for i in range(0, len(y_obs_long) + 1)] plt.plot(K, state_trace, marker='+', markersize='12', linestyle='-.', color='b', linewidth=2, drawstyle='steps-mid', label='Regular FB') plt.plot(K, eln_state_trace, marker='x', markersize='12', linestyle=':', color='r', linewidth=2, drawstyle='steps-mid', label='ELN FB') yint = range(1, 5) plt.yticks(yint) plt.legend(handlelength=4, loc='upper right') plt.title("State transition chart for Long Dataset") plt.ylabel("State") plt.xlabel("Xk") plt.show()
def posterior_chart(): fbhmm = ForwardBackwardHMM(pxk_xkm1, pyk_xk, px0, y_obs_short) probs, alphas, betas = fbhmm.forward_backward() print( tabulate(probs, headers=[ "K1", "K2", "K3", "K4", "K5", "K6", "K7", "K8", "K9", "K11", "K11", "K12", "K13", "K14", "K15" ], tablefmt="latex"))
def posterior_chart(): fbhmm = ForwardBackwardHMM(pxk_xkm1, pyk_xk, px0, y_obs_long) probs, alphas, betas = fbhmm.forward_backward() probs = np.concatenate((probs.transpose()[:5], probs.transpose()[-5:]), axis=0).transpose() print( tabulate(probs, headers=[ "X1", "X2", "X3", "X4", "X5", "X42", "X43", "X44", "X45", "X46" ], tablefmt="latex"))
def _e_step(self): """ For each observation sequence, we store the elngamma and elnxi values. Thus we are running forward-backward with the current set of parameters for every sequence and storing the resulting log probabilities and log state-state+1 probabilities for each. These elngamma's and elnxi's are then passed to the maximization step. """ elnxis = [] xis = [] elngammas = [] gammas = [] for idx, obs_seq in enumerate(self.sequences): elngamma, gamma, elnalpha, elnbeta = ForwardBackwardHMM( self.init, self.trans, self.emis, obs_seq).forward_backward_eln() elngammas.append(elngamma[:, 1:]) gammas.append(gamma[:, 1:]) elnxi, xi = self._eln_xi(elnalpha[:, 1:], elnbeta[:, 1:], obs_seq) elnxis.append(elnxi) xis.append(xi) return elnxis, xis, elngammas, gammas
def state_trace(): fbhmm = ForwardBackwardHMM(pxk_xkm1, pyk_xk, px0, y_obs_short) probs_eln, logalphas, logbetas = fbhmm.forward_backward_eln() exact_state_trace = fbhmm.max_likelihood_state_estimate(probs_eln) vhmm = ViterbiHMM(pxk_xkm1, pyk_xk, px0, y_obs_short) path, log_probs = vhmm.viterbi_path() K = [i for i in range(0, len(y_obs_short) + 1)] plt.plot(K, exact_state_trace, marker='+', markersize='12', linestyle='-.', color='b', linewidth=2, drawstyle='steps-mid', label='Exact Inference') plt.plot(K, path, marker='x', markersize='12', linestyle=':', color='r', linewidth=2, drawstyle='steps-mid', label='Viterbi') yint = range(1, 5) plt.yticks(yint) plt.legend(handlelength=6) plt.title( "State Transition Comparing Exact vs. Viterbi MAP Inference | Short HMM" ) plt.ylabel("State") plt.xlabel("Xk") plt.show() fbhmm = ForwardBackwardHMM(pxk_xkm1, pyk_xk, px0, y_obs_long) probs_eln, logalphas, logbetas = fbhmm.forward_backward_eln() exact_state_trace = fbhmm.max_likelihood_state_estimate(probs_eln) vhmm = ViterbiHMM(pxk_xkm1, pyk_xk, px0, y_obs_long) path, log_probs = vhmm.viterbi_path() K = [i for i in range(0, len(y_obs_long) + 1)] plt.plot(K, exact_state_trace, marker='+', markersize='12', linestyle='-.', color='b', linewidth=2, drawstyle='steps-mid', label='Exact Inference') plt.plot(K, path, marker='x', markersize='12', linestyle=':', color='r', linewidth=2, drawstyle='steps-mid', label='Viterbi') yint = range(1, 5) plt.yticks(yint) plt.legend(handlelength=6) plt.title( "State Transition Comparing Exact vs. Viterbi MAP Inference | Long HMM" ) plt.ylabel("State") plt.xlabel("Xk") plt.show()
def data_likelihood(): fbhmm = ForwardBackwardHMM(pxk_xkm1, pyk_xk, px0, y_obs_long) probs, alphas, betas = fbhmm.forward_backward() print(eln(sum(alphas[:, len(y_obs_long)])))
def state_trace(): lli = LikelihoodSamplingInference(pxk_xkm1, pyk_xk, px0, y_obs_short) print("Running approximate inference with 100 samples...") state_trace_100 = lli.max_likelihood_state_estimate(lli.run_inference(100)) print("Running approximate inference with 1000 samples...") state_trace_1000 = lli.max_likelihood_state_estimate( lli.run_inference(1000)) print("Running approximate inference with 10000 samples...") state_trace_10000 = lli.max_likelihood_state_estimate( lli.run_inference(10000)) print("Running exact inference...") fbhmm = ForwardBackwardHMM(pxk_xkm1, pyk_xk, px0, y_obs_short) probs_eln, logalphas, logbetas = fbhmm.forward_backward_eln() exact_state_trace = fbhmm.max_likelihood_state_estimate(probs_eln) K = [i for i in range(0, len(y_obs_short) + 1)] print( "Plotting 'State Transition Comparing Exact vs. 100 Sample Approximate Inference'..." ) plt.plot(K, exact_state_trace, marker='+', markersize='12', linestyle='-.', color='b', linewidth=2, drawstyle='steps-mid', label='Exact Inference') plt.plot(K, state_trace_100, marker='x', markersize='12', linestyle=':', color='r', linewidth=2, drawstyle='steps-mid', label='LW - 100 Samples') yint = range(1, 5) plt.yticks(yint) plt.legend(handlelength=6) plt.title( "State Transition Comparing Exact vs. 100 Sample Approximate Inference" ) plt.ylabel("State") plt.xlabel("Xk") plt.show() print( "Plotting 'State Transition Comparing Exact vs. 1000 Sample Approximate Inference'..." ) plt.plot(K, exact_state_trace, marker='+', markersize='12', linestyle='-.', color='b', linewidth=2, drawstyle='steps-mid', label='Exact Inference') plt.plot(K, state_trace_1000, marker='x', markersize='12', linestyle=':', color='r', linewidth=2, drawstyle='steps-mid', label='LW - 1000 Samples') yint = range(1, 5) plt.yticks(yint) plt.legend(handlelength=6) plt.title( "State Transition Comparing Exact vs. 1000 Sample Approximate Inference" ) plt.ylabel("State") plt.xlabel("Xk") plt.show() print( "Plotting 'State Transition Comparing Exact vs. 10000 Sample Approximate Inference'..." ) plt.plot(K, exact_state_trace, marker='+', markersize='12', linestyle='-.', color='b', linewidth=2, drawstyle='steps-mid', label='Exact Inference') plt.plot(K, state_trace_10000, marker='x', markersize='12', linestyle=':', color='r', linewidth=2, drawstyle='steps-mid', label='LW - 10000 Samples') yint = range(1, 5) plt.yticks(yint) plt.legend(handlelength=6) plt.title( "State Transition Comparing Exact vs. 10000 Sample Approximate Inference" ) plt.ylabel("State") plt.xlabel("Xk") plt.show()
def _e_step(self, obs_seq): return ForwardBackwardHMM(self.init, self.trans, self.emis, obs_seq).forward_backward_eln()