def test_coin_sample_post(self): """Test sampling from posterior distribution""" outdir = 'test/tmp/test_hmm/test_coin_sample_post/' make_clean_dir(outdir) model = make_coin_model() # sample states and data ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) data = list(hmm.sample_hmm_data(model, states)) model.prob_emission = ( lambda pos, state: model.prob_emission_data(state, data[pos])) p = Gnuplot() p.enableOutput(False) p.plot(states, style="lines") probs = hmm.get_posterior_probs(model, len(data)) states2 = [exp(probs[i][1]) for i in xrange(len(data))] p.plot(util.vadds(states2, 1.5), style="lines", miny=-1, maxy=12) for i in range(2, 10): states2 = hmm.sample_posterior(model, ndata) self.assertTrue(stats.corr(states, states2) > .5) p.plot(util.vadds(states2, 1.5 * i), style="lines", miny=-1, maxy=12) p.enableOutput(True) p.save(outdir + 'plot.png')
def test_coin_sample_post(self): """Test sampling from posterior distribution""" outdir = 'test/tmp/test_hmm/test_coin_sample_post/' make_clean_dir(outdir) model = make_coin_model() # sample states and data ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) data = list(hmm.sample_hmm_data(model, states)) model.prob_emission = (lambda pos, state: model.prob_emission_data(state, data[pos])) p = Gnuplot() p.enableOutput(False) p.plot(states, style="lines") probs = hmm.get_posterior_probs(model, len(data)) states2 = [exp(probs[i][1]) for i in xrange(len(data))] p.plot(util.vadds(states2, 1.5), style="lines", miny=-1, maxy=12) for i in range(2, 10): states2 = hmm.sample_posterior(model, ndata) self.assertTrue(stats.corr(states, states2) > .5) p.plot(util.vadds(states2, 1.5*i), style="lines", miny=-1, maxy=12) p.enableOutput(True) p.save(outdir + 'plot.png')
def test_post_c(self): k = 3 n = 1e4 rho = 1.5e-8 * 30 mu = 2.5e-8 * 100 length = 100 arg = arglib.sample_arg(k, n, rho, start=0, end=length) arg.prune() muts = arglib.sample_arg_mutations(arg, mu) seqs = arglib.make_alignment(arg, muts) print arglib.get_recomb_pos(arg) print "muts", len(muts) print "recomb", len(arglib.get_recomb_pos(arg)) times = arghmm.get_time_points(ntimes=10) arghmm.discretize_arg(arg, times) tree = arg.get_marginal_tree(0) print tree.root.age treelib.draw_tree_names(tree.get_tree(), minlen=5, scale=4e-4) # remove chrom keep = ["n%d" % i for i in range(k - 1)] arglib.subarg_by_leaf_names(arg, keep) model = arghmm.ArgHmm(arg, seqs, new_name="n%d" % (k - 1), times=times, rho=rho, mu=mu) print "states", len(model.states[0]) util.tic("C") probs1 = list(arghmm.get_posterior_probs(model, length, verbose=True)) util.toc() util.tic("python") probs2 = list(hmm.get_posterior_probs(model, length, verbose=True)) util.toc() print "probs1" pc(probs1) print "probs2" pc(probs2) for col1, col2 in izip(probs1, probs2): for a, b in izip(col1, col2): fequal(a, b)
def test_post_c(self): k = 3 n = 1e4 rho = 1.5e-8 * 30 mu = 2.5e-8 * 100 length = 100 arg = arglib.sample_arg(k, n, rho, start=0, end=length) arg.prune() muts = arglib.sample_arg_mutations(arg, mu) seqs = arglib.make_alignment(arg, muts) print arglib.get_recomb_pos(arg) print "muts", len(muts) print "recomb", len(arglib.get_recomb_pos(arg)) times = arghmm.get_time_points(ntimes=10) arghmm.discretize_arg(arg, times) tree = arg.get_marginal_tree(0) print tree.root.age treelib.draw_tree_names(tree.get_tree(), minlen=5, scale=4e-4) # remove chrom keep = ["n%d" % i for i in range(k-1)] arglib.subarg_by_leaf_names(arg, keep) model = arghmm.ArgHmm(arg, seqs, new_name="n%d" % (k-1), times=times, rho=rho, mu=mu) print "states", len(model.states[0]) util.tic("C") probs1 = list(arghmm.get_posterior_probs(model, length, verbose=True)) util.toc() util.tic("python") probs2 = list(hmm.get_posterior_probs(model, length, verbose=True)) util.toc() print "probs1" pc(probs1) print "probs2" pc(probs2) for col1, col2 in izip(probs1, probs2): for a, b in izip(col1, col2): fequal(a, b)
def test_coin_post(self): """Test that posterior decoding.""" model = make_coin_model() # sample states and data ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) data = list(hmm.sample_hmm_data(model, states)) model.prob_emission = ( lambda pos, state: model.prob_emission_data(state, data[pos])) probs = hmm.get_posterior_probs(model, len(data)) for col in probs: p = sum(map(exp, col)) self.assertAlmostEqual(p, 1.0)
def test_coin_post(self): """Test that posterior decoding.""" model = make_coin_model() # sample states and data ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) data = list(hmm.sample_hmm_data(model, states)) model.prob_emission = (lambda pos, state: model.prob_emission_data(state, data[pos])) probs = hmm.get_posterior_probs(model, len(data)) for col in probs: p = sum(map(exp, col)) self.assertAlmostEqual(p, 1.0)
def test_coin(self): """Test that viterbi and posterior coding work well.""" outdir = 'test/tmp/test_hmm/test_coin/' make_clean_dir(outdir) model = make_coin_model() # sample states ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) p = Gnuplot() p.enableOutput(False) p.plot(states, style="lines") # sample data data = list(hmm.sample_hmm_data(model, states)) # viterbi model.prob_emission = ( lambda pos, state: model.prob_emission_data(state, data[pos])) states2 = hmm.viterbi(model, len(data)) # posterior probs = hmm.get_posterior_probs(model, len(data)) states3 = [exp(probs[i][1]) for i in xrange(len(data))] # assert that inferences correlates with true state self.assertTrue(stats.corr(states, states2) > .5) self.assertTrue(stats.corr(states, states3) > .5) # plot inference p.plot(util.vadds(states2, 1.5), style="lines", miny=-1, maxy=4) p.plot(util.vadds(states3, 2.5), style="lines", miny=-1, maxy=4) p.enableOutput(True) p.save(outdir + 'plot.png')
def test_coin(self): """Test that viterbi and posterior coding work well.""" outdir = 'test/tmp/test_hmm/test_coin/' make_clean_dir(outdir) model = make_coin_model() # sample states ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) p = Gnuplot() p.enableOutput(False) p.plot(states, style="lines") # sample data data = list(hmm.sample_hmm_data(model, states)) # viterbi model.prob_emission = (lambda pos, state: model.prob_emission_data(state, data[pos])) states2 = hmm.viterbi(model, len(data)) # posterior probs = hmm.get_posterior_probs(model, len(data)) states3 = [exp(probs[i][1]) for i in xrange(len(data))] # assert that inferences correlates with true state self.assertTrue(stats.corr(states, states2) > .5) self.assertTrue(stats.corr(states, states3) > .5) # plot inference p.plot(util.vadds(states2, 1.5), style="lines", miny=-1, maxy=4) p.plot(util.vadds(states3, 2.5), style="lines", miny=-1, maxy=4) p.enableOutput(True) p.save(outdir + 'plot.png')