def fit_hmm(Xs, Xtest, N_samples=100): model = MultinomialHMM(K, D) for X in Xs: model.add_data(X) samples = [] lls = [] test_lls = [] pis = [] 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()) # lls.append(model.log_likelihood_fixed_z()) test_lls.append(model.log_likelihood(Xtest)) # pis.append(testmodel.pis()[0]) zs.append(model.stateseqs[0]) lls = np.array(lls) test_lls = np.array(test_lls) pis = np.array(pis) zs = np.array(zs) timestamps = np.array(timestamps) timestamps -= timestamps[0] return model, lls, test_lls, pis, zs, timestamps
def fit_hmm(Xs, Xtest, D_hmm, N_samples=100): print("Fitting HMM with %d states" % D_hmm) model = MultinomialHMM(K, D_hmm) for X in Xs: model.add_data(X) init_results = (0, None, model.log_likelihood(), model.log_likelihood(Xtest), (model.log_likelihood(np.vstack((Xs[0], Xtest))) - model.log_likelihood(Xs[0]))) def resample(): tic = time.time() model.resample_model() toc = time.time() - tic return toc, None, model.log_likelihood(), \ model.log_likelihood(Xtest), \ (model.log_likelihood(np.vstack((Xs[0], Xtest))) - model.log_likelihood(Xs[0])) 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_hmm(Xs, Xtest, D_hmm, N_samples=100): print("Fitting HMM with %d states" % D_hmm) model = MultinomialHMM(K, D_hmm) for X in Xs: model.add_data(X) init_results = (0, None, model.log_likelihood(), model.log_likelihood(Xtest), (model.log_likelihood(np.vstack( (Xs[0], Xtest))) - model.log_likelihood(Xs[0]))) def resample(): tic = time.time() model.resample_model() toc = time.time() - tic return toc, None, model.log_likelihood(), \ model.log_likelihood(Xtest), \ (model.log_likelihood(np.vstack((Xs[0], Xtest))) - model.log_likelihood(Xs[0])) 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_hmm(Xs, Xtest, D_hmm, N_samples=100): print("Fitting HMM with %d states" % D_hmm) model = MultinomialHMM(K, D_hmm, alpha_0=10.0) for X in Xs: model.add_data(X) compute_pred_ll = lambda: sum([model.log_likelihood(np.vstack((Xs[i], Xtest[i]))) - model.log_likelihood(Xs[i]) for i,Xt in enumerate(Xtest)]) init_results = (0, None, model.log_likelihood(), model.log_likelihood(Xtest), 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)
def fit_hmm(Xs, Xtest, D_hmm, N_samples=100): print("Fitting HMM with %d states" % D_hmm) model = MultinomialHMM(K, D_hmm, alpha_0=10.0) for X in Xs: model.add_data(X) compute_pred_ll = lambda: sum([ model.log_likelihood(np.vstack( (Xs[i], Xtest[i]))) - model.log_likelihood(Xs[i]) for i, Xt in enumerate(Xtest) ]) init_results = (0, None, model.log_likelihood(), model.log_likelihood(Xtest), 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)