def fit_lds_model(Xs, Xtest, D, N_samples=100): model = MultinomialLDS(K, D, init_dynamics_distn=GaussianFixed(mu=np.zeros(D), sigma=1 * np.eye(D)), dynamics_distn=AutoRegression(nu_0=D + 1, S_0=1 * np.eye(D), M_0=np.zeros((D, D)), K_0=1 * np.eye(D)), sigma_C=0.01) for X in Xs: model.add_data(X) model.resample_parameters() init_results = (0, model, model.log_likelihood(), model.heldout_log_likelihood(Xtest, M=1), model.predictive_log_likelihood(Xtest, M=1000)) def resample(): tic = time.time() model.resample_model() toc = time.time() - tic return toc, None, model.log_likelihood(), \ model.heldout_log_likelihood(Xtest, M=1), \ model.predictive_log_likelihood(Xtest, M=1000) times, samples, lls, test_lls, pred_lls = \ map(np.array, zip(*([init_results] + [resample() for _ in progprint_xrange(N_samples)]))) timestamps = np.cumsum(times) return Results(lls, test_lls, pred_lls, samples, timestamps)
def fit_lds_model(Xs, Xtest, D, N_samples=100): model = MultinomialLDS(K, D, init_dynamics_distn=GaussianFixed(mu=np.zeros(D), sigma=1*np.eye(D)), dynamics_distn=AutoRegression(nu_0=D+1,S_0=1*np.eye(D),M_0=np.zeros((D,D)),K_0=1*np.eye(D)), sigma_C=0.01 ) for X in Xs: model.add_data(X) model.resample_parameters() init_results = (0, model, model.log_likelihood(), model.heldout_log_likelihood(Xtest, M=1), model.predictive_log_likelihood(Xtest, M=1000)) def resample(): tic = time.time() model.resample_model() toc = time.time() - tic return toc, None, model.log_likelihood(), \ model.heldout_log_likelihood(Xtest, M=1), \ model.predictive_log_likelihood(Xtest, M=1000) times, samples, lls, test_lls, pred_lls = \ map(np.array, zip(*([init_results] + [resample() for _ in progprint_xrange(N_samples)]))) timestamps = np.cumsum(times) return Results(lls, test_lls, pred_lls, samples, timestamps)
def fit_lds_model(Xs, Xtest, D, N_samples=100): Nx = len(Xs) assert len(Xtest) == Nx model = MultinomialLDS(K, D, init_dynamics_distn=GaussianFixed(mu=np.zeros(D), sigma=1 * np.eye(D)), dynamics_distn=AutoRegression(nu_0=D + 1, S_0=1 * np.eye(D), M_0=np.zeros((D, D)), K_0=1 * np.eye(D)), sigma_C=1.) for X in Xs: model.add_data(X) model.resample_parameters() compute_pred_ll = lambda: sum([ model.predictive_log_likelihood(Xt, data_index=i, M=10)[0] for i, Xt in enumerate(Xtest) ]) init_results = ( 0, None, model.log_likelihood(), # model.heldout_log_likelihood(Xtest, M=1), np.nan, compute_pred_ll()) def resample(): tic = time.time() model.resample_model() toc = time.time() - tic return toc, None, model.log_likelihood(), \ np.nan,\ compute_pred_ll() times, samples, lls, test_lls, pred_lls = \ list(map(np.array, list(zip(*([init_results] + [resample() for _ in progprint_xrange(N_samples, perline=5)]))))) timestamps = np.cumsum(times) return Results(lls, test_lls, pred_lls, samples, timestamps)
def fit_lds_model(Xs, Xtest, N_samples=100): model = MultinomialLDS(K, D, init_dynamics_distn=Gaussian(mu_0=np.zeros(D), sigma_0=np.eye(D), kappa_0=1.0, nu_0=D + 1.0), dynamics_distn=AutoRegression(nu_0=D + 1, S_0=np.eye(D), M_0=np.zeros((D, D)), K_0=np.eye(D)), sigma_C=1) for X in Xs: model.add_data(X) data = model.data_list[0] samples = [] lls = [] test_lls = [] mc_test_lls = [] pis = [] psis = [] zs = [] timestamps = [time.time()] for smpl in progprint_xrange(N_samples): model.resample_model() timestamps.append(time.time()) samples.append(model.copy_sample()) # TODO: Use log_likelihood() to marginalize over z lls.append(model.log_likelihood()) # test_lls.append(model.heldout_log_likelihood(Xtest, M=50)[0]) mc_test_lls.append(model._mc_heldout_log_likelihood(Xtest, M=1)[0]) pis.append(model.pi(data)) psis.append(model.psi(data)) zs.append(data["states"].stateseq) lls = np.array(lls) test_lls = np.array(test_lls) pis = np.array(pis) psis = np.array(psis) zs = np.array(zs) timestamps = np.array(timestamps) timestamps -= timestamps[0] return model, lls, test_lls, mc_test_lls, pis, psis, zs, timestamps
def fit_lds_model(Xs, Xtest, N_samples=100): model = MultinomialLDS(K, D, init_dynamics_distn=Gaussian(mu_0=np.zeros(D), sigma_0=np.eye(D), kappa_0=1.0, nu_0=D+1.0), dynamics_distn=AutoRegression(nu_0=D+1,S_0=np.eye(D),M_0=np.zeros((D,D)),K_0=np.eye(D)), sigma_C=1 ) for X in Xs: model.add_data(X) data = model.data_list[0] samples = [] lls = [] test_lls = [] mc_test_lls = [] pis = [] psis = [] zs = [] timestamps = [time.time()] for smpl in progprint_xrange(N_samples): model.resample_model() timestamps.append(time.time()) samples.append(model.copy_sample()) # TODO: Use log_likelihood() to marginalize over z lls.append(model.log_likelihood()) # test_lls.append(model.heldout_log_likelihood(Xtest, M=50)[0]) mc_test_lls.append(model._mc_heldout_log_likelihood(Xtest, M=1)[0]) pis.append(model.pi(data)) psis.append(model.psi(data)) zs.append(data["states"].stateseq) lls = np.array(lls) test_lls = np.array(test_lls) pis = np.array(pis) psis = np.array(psis) zs = np.array(zs) timestamps = np.array(timestamps) timestamps -= timestamps[0] return model, lls, test_lls, mc_test_lls, pis, psis, zs, timestamps
def fit_lds_model(Xs, Xtest, D, N_samples=100): Nx = len(Xs) assert len(Xtest) == Nx model = MultinomialLDS(K, D, init_dynamics_distn=GaussianFixed(mu=np.zeros(D), sigma=1*np.eye(D)), dynamics_distn=AutoRegression(nu_0=D+1,S_0=1*np.eye(D),M_0=np.zeros((D,D)),K_0=1*np.eye(D)), sigma_C=1. ) for X in Xs: model.add_data(X) model.resample_parameters() compute_pred_ll = lambda: sum([model.predictive_log_likelihood(Xt, data_index=i, M=10)[0] for i,Xt in enumerate(Xtest)]) init_results = (0, None, model.log_likelihood(), # model.heldout_log_likelihood(Xtest, M=1), np.nan, compute_pred_ll()) def resample(): tic = time.time() model.resample_model() toc = time.time() - tic return toc, None, model.log_likelihood(), \ np.nan,\ compute_pred_ll() times, samples, lls, test_lls, pred_lls = \ map(np.array, zip(*([init_results] + [resample() for _ in progprint_xrange(N_samples, perline=5)]))) timestamps = np.cumsum(times) return Results(lls, test_lls, pred_lls, samples, timestamps)
################### # inference # ################### testmodel = MultinomialLDS(K, D, init_dynamics_distn=Gaussian(mu_0=mu_init, sigma_0=sigma_init, kappa_0=1.0, nu_0=3.0), dynamics_distn=AutoRegression(nu_0=D + 1, S_0=np.eye(D), M_0=np.zeros((D, D)), K_0=np.eye(D)), sigma_C=1) testmodel.add_data(data["x"]) testdata = testmodel.data_list[0] N_samples = 100 samples = [] lls = [] pis = [] psis = [] zs = [] for smpl in range(N_samples): print("Iteration ", smpl) testmodel.resample_model() samples.append(testmodel.copy_sample()) lls.append(testmodel.log_likelihood()) pis.append(testmodel.pi(testdata))
C=C ) data = truemodel.generate(T=T) ################### # inference # ################### testmodel = MultinomialLDS(K, D, init_dynamics_distn=Gaussian(mu_0=mu_init, sigma_0=sigma_init, kappa_0=1.0, nu_0=3.0), dynamics_distn=AutoRegression(nu_0=D+1,S_0=np.eye(D),M_0=np.zeros((D,D)),K_0=np.eye(D)), sigma_C=1 ) testmodel.add_data(data["x"]) testdata = testmodel.data_list[0] N_samples = 100 samples = [] lls = [] pis = [] psis = [] zs = [] for smpl in xrange(N_samples): print("Iteration ", smpl) testmodel.resample_model() samples.append(testmodel.copy_sample()) lls.append(testmodel.log_likelihood()) pis.append(testmodel.pi(testdata))