K = 5 # number of discrete states D = 2 # number of observed dimensions # 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)
plt.yticks(lim * np.arange(D), ["$y_{}$".format(d + 1) for d in range(D)]) plt.title("Simulated data from an HMM") plt.tight_layout() if save_figures: plt.savefig("hmm_2.pdf") # # Fit an HMM to this synthetic data # In[7]: N_iters = 50 hmm = HMM(K, D, observations="gaussian") hmm_lls = hmm.fit(y, method="em", num_em_iters=N_iters, verbose=True) plt.plot(hmm_lls, label="EM") plt.plot([0, N_iters], true_ll * np.ones(2), ':k', label="True") plt.xlabel("EM Iteration") plt.ylabel("Log Probability") plt.legend(loc="lower right") # In[8]: # Find a permutation of the states that best matches the true and inferred states hmm.permute(find_permutation(z, hmm.most_likely_states(y))) # In[11]: # Plot the true and inferred discrete states
# A bunch of observation models that all include the # diagonal Gaussian as a special case. 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")
else: trialdata_ses2=[latents_ses2[i,:,:] for i in np.arange(latents_ses2.shape[0])] if regularize: arhmm_ses1 = HMM(K,N, observations="ar", transitions="sticky", transition_kwargs=dict(kappa=10), observation_kwargs=dict(regularization_params=dict(type='l2',lambda_A=lambda_reg))) regularizestring='_regl2' else: arhmm_ses1 = HMM(K,N, observations="ar", transitions="sticky", transition_kwargs=dict(kappa=10)) regularizestring='' arhmm_em_lls_ses1 = arhmm_ses1.fit(trialdata_ses1, method="em", num_em_iters=numiters) # Get the inferred states for session 1 trialdata_ses1_z=[arhmm_ses1.most_likely_states(trial) for trial in trialdata_ses1] trialdata_ses1_z=np.asarray(trialdata_ses1_z) As_ses1=[None]*K; for k in np.arange(K): As_ses1[k]=arhmm_ses1.params[2][0][k,:,:]; fig = plt.figure(figsize=(8, 10)) plt.imshow(trialdata_ses1_z, aspect="auto", vmin=0, vmax=K) plt.plot([54,54],[0,trialdata_ses1_z.shape[0]],'-r') plt.ylim([0,trialdata_ses1_z.shape[0]]) fig.savefig(os.path.join(resultsfolder,'ses1'+cca_str+constring+'K'+str(K)+whitenstring+regularizestring+'.png')) fig = plt.figure(figsize=(8*K,8))
true_hsmm = HSMM(K, D, observations="gaussian") true_hsmm.transitions.rs z, y = true_hsmm.sample(T) z_test, y_test = true_hsmm.sample(T) true_ll = true_hsmm.log_probability(y) # Fit an HSMM N_em_iters = 100 print("Fitting Gaussian HSMM with EM") hsmm = HSMM(K, D, observations="gaussian") hsmm_em_lls = hsmm.fit(y, method="em", num_em_iters=N_em_iters) print("Fitting Gaussian HMM with EM") hmm = HMM(K, D, observations="gaussian") hmm_em_lls = hmm.fit(y, method="em", num_em_iters=N_em_iters) # Plot log likelihoods (fit model is typically better) plt.figure() plt.plot(hsmm_em_lls, ls='-', label="HSMM (EM)") plt.plot(hmm_em_lls, ls='-', label="HMM (EM)") plt.plot(true_ll * np.ones(N_em_iters), ':', label="true") plt.legend(loc="lower right") # Print the test likelihoods (true model is typically better) print("Test log likelihood") print("True HSMM: ", true_hsmm.log_likelihood(y_test)) print("Fit HSMM: ", hsmm.log_likelihood(y_test)) print("Fit HMM: ", hmm.log_likelihood(y_test)) # Plot the true and inferred states
# shuffle the trials sequence = [i for i in range(len(trialdata))] npr.shuffle(sequence) sequence=np.array(sequence) # Divide into training and testing (I didn't really end up using the testing - but can check log likelihoods to decide K) traintrials=[trialdata[j] for j in sequence[:int(np.ceil(0.8*len(trialdata)))]] testtrials=[trialdata[j] for j in sequence[int(np.ceil(0.8*len(trialdata))):]] print(len(traintrials)); print(len(testtrials)) # Run the ARHMM arhmm = HMM(K,N, observations="ar", transitions="sticky", transition_kwargs=dict(kappa=kappa)) arhmm_em_lls = arhmm.fit(traintrials, method="em", num_em_iters=numiters) # Get the inferred states for train and test trials traintrials_z=[arhmm.most_likely_states(traintrial) for traintrial in traintrials] traintrials_z=np.asarray(traintrials_z) testtrials_z=[arhmm.most_likely_states(testtrial) for testtrial in testtrials] testtrials_z=np.asarray(testtrials_z) As=[None]*K; maxvals=[None]*K for k in np.arange(K): As[k]=arhmm.params[2][0][k,:,:]; maxvals[k]=np.var(As[k]) # Tried to permute the states so that it would be 'no movement' --> 'movement', based on the variance of the values in the A matrix (didn't really work) # permute the states sortorder=np.argsort(maxvals)
plt.xlim(0, T) plt.ylabel("observation") plt.yticks(np.arange(C)) # In[4]: # Now create a new HMM and fit it to the data with EM N_iters = 50 hmm = HMM(K, D, M, observations="categorical", observation_kwargs=dict(C=C), transitions="inputdriven") # Fit hmm_lps = hmm.fit(y, inputs=inpt, method="em", num_em_iters=N_iters) # In[5]: # Find a permutation of the states that best matches the true and inferred states hmm.permute(find_permutation(z, hmm.most_likely_states(y, input=inpt))) z_inf = hmm.most_likely_states(y, input=inpt) # In[6]: # Plot the log probabilities of the true and fit models plt.plot(hmm_lps, label="EM")
# In[7]: mog = MixtureOfGaussians(100, D) mog.fit(x) # In[8]: plt.plot(x[:, 0], x[:, 1]) for mu in mog.observations.mus: plt.plot(mu[0], mu[1], 'o') # In[9]: arhmm = HMM(K=8, D=D, observations="ar") arhmm.fit(x) # In[10]: z_smpl, x_smpl = arhmm.sample(T=3000) plt.plot(x_smpl[:, 0], x_smpl[:, 1]) # In[11]: def sample(T=3000, num_samples=25, num_burnin=100, schedule=None, filename="samples.mp4"):