observations = [ "diagonal_gaussian", "gaussian", "diagonal_t", "studentst", "diagonal_ar", "ar", "diagonal_robust_ar", "robust_ar" ] # Fit with both SGD and EM methods = ["sgd", "em"] results = {} for obs in observations: for method in methods: print("Fitting {} HMM with {}".format(obs, method)) model = HMM(K, D, observations=obs) train_lls = model.fit(y, method=method) test_ll = model.log_likelihood(y_test) smoothed_y = model.smooth(y) # Permute to match the true states model.permute(find_permutation(z, model.most_likely_states(y))) smoothed_z = model.most_likely_states(y) results[(obs, method)] = (model, train_lls, test_ll, smoothed_z, smoothed_y) # Plot the inferred states fig, axs = plt.subplots(len(observations) + 1, 1, figsize=(12, 8)) # Plot the true states plt.sca(axs[0]) plt.imshow(z[None, :], aspect="auto", cmap="jet") plt.title("true") plt.xticks()
# Make an HMM with the true parameters true_hmm = HMM(K, D, observations="gaussian") z, y = true_hmm.sample(T) z_test, y_test = true_hmm.sample(T) true_ll = true_hmm.log_probability(y) # Fit models N_sgd_iters = 1000 N_em_iters = 100 print("Fitting HMM with SGD") hmm = HMM(K, D, observations="gaussian") hmm_sgd_lls = hmm.fit(y, method="sgd", num_iters=N_sgd_iters) hmm_sgd_test_ll = hmm.log_probability(y_test) hmm_sgd_smooth = hmm.smooth(y) print("Fitting HMM with EM") hmm = HMM(K, D, observations="gaussian") hmm_em_lls = hmm.fit(y, method="em", num_em_iters=N_em_iters) hmm_em_test_ll = hmm.log_probability(y_test) hmm_em_smooth = hmm.smooth(y) print("Fitting Student's t HMM with SGD") thmm = HMM(K, D, observations="studentst") thmm_sgd_lls = thmm.fit(y, method="sgd", num_iters=N_sgd_iters) thmm_sgd_test_ll = thmm.log_probability(y_test) thmm_sgd_smooth = thmm.smooth(y) print("Fitting Student's t HMM with EM") thmm = HMM(K, D, observations="studentst")
plt.imshow(z[None, :], aspect="auto", cmap=cmap, vmin=0, vmax=len(colors) - 1) plt.xlim(0, T) plt.ylabel("$z_{\\mathrm{true}}$") plt.yticks([]) plt.subplot(212) plt.imshow(hmm_z[None, :], aspect="auto", cmap=cmap, vmin=0, vmax=len(colors) - 1) plt.xlim(0, T) plt.ylabel("$z_{\\mathrm{inferred}}$") plt.yticks([]) plt.xlabel("time") plt.tight_layout() # In[34]: # Use the HMM to "smooth" the data hmm_y = hmm.smooth(y) plt.figure(figsize=(8, 4)) plt.plot(y + 3 * np.arange(D), '-k', lw=2) plt.plot(hmm_y + 3 * np.arange(D), '-', lw=2) plt.xlim(0, T) plt.ylabel("$y$") # plt.yticks([]) plt.xlabel("time")