def single_chain_run(): basic_chain(start_x=starting_trees[0], summaries=summaries, posterior_function=posterior, proposal=mp[0], post=None, N=sum(sim_lengths), sample_verbose_scheme=summary_verbose_scheme[0], overall_thinning=int(options.thinning_coef), i_start_from=0, temperature=1.0, proposal_update=None, multiplier=multiplier, appending_result_file=options.result_file, appending_result_frequency=sim_lengths[0])
def test_prior_model_no_admixes(start_tree, sim_length=100000, summaries=None, thinning_coef=1): posterior = initialize_prior_as_posterior() if summaries is None: summaries = [ s_variable('posterior'), s_variable('mhr'), s_no_admixes() ] proposal = adaptive_proposal_no_admix() #proposal.props=proposal.props[2:] #a little hack under the hood #proposal.params=proposal.params[2:] #a little hack under the hood. sample_verbose_scheme = {summary.name: (1, 0) for summary in summaries} final_tree, final_posterior, results, _ = basic_chain( start_tree, summaries, posterior, proposal, post=None, N=sim_length, sample_verbose_scheme=sample_verbose_scheme, overall_thinning=int(thinning_coef + sim_length / 60000), i_start_from=0, temperature=1.0, proposal_update=None, check_trees=False) save_to_csv(results, summaries) return results
def test_prior_model(start_tree, sim_length=100000, summaries=None, thinning_coef=1): posterior = initialize_prior_as_posterior() if summaries is None: summaries = [ s_variable('posterior'), s_variable('mhr'), s_no_admixes() ] proposal = adaptive_proposal() #basic_meta_proposal() sample_verbose_scheme = {summary.name: (1, 0) for summary in summaries} sample_verbose_scheme['posterior'] = (1, 1000) final_tree, final_posterior, results, _ = basic_chain( start_tree, summaries, posterior, proposal, post=None, N=sim_length, sample_verbose_scheme=sample_verbose_scheme, overall_thinning=int(thinning_coef + sim_length / 60000), i_start_from=0, temperature=1.0, proposal_update=None, check_trees=True) print results save_to_csv(results, summaries) return results
def run_chain(self, p): start_tree, post, N, sample_verbose_scheme, overall_thinning, i_start_from, temperature, proposal_update, multiplier = p #print 'run_chain proposal.node_naming.n', self.proposal.node_naming.n return basic_chain(start_tree, self.summaries, self.posterior_function, self.proposal, post, N, sample_verbose_scheme, overall_thinning, i_start_from, temperature, proposal_update, multiplier)
def mcmc_search(likelihood, pops, generations, short_to_full, init=None, N=1000): ''' This function wraps the MCMC call, and takes the MCMC output and finds the maximum posterior value. ''' if init is None: init = choice(pops, 2**generations) init = ''.join(map(str, init)) summaries = [basic_summary('posterior'), basic_summary('x')] summary_verbose_scheme = {summary.name: (1, 1) for summary in summaries} proposal_function = MetaProposal(pops=pops) _, _, s = basic_chain(init, summaries, likelihood, proposal_function, post=None, N=N, sample_verbose_scheme=summary_verbose_scheme, overall_thinning=1, i_start_from=0, temperature=1.0) res = list(s) #print(res) #we know that the first column is iteration number, the second posterior, and the last is the configuration x. res_dic = { find_smallest_equivalence_class(x): p for x, p in zip(res[2], res[1]) } xs, posteriors = map(list, list(zip(*list(res_dic.items())))) list_sorted = sorted(enumerate(posteriors), key=lambda elem: elem[1]) i = list_sorted[-1][0] #print(xs[i], posteriors[i]) x_to_return = [] p_to_return = [] for i_tup in list_sorted[-10:][::-1]: i = i_tup[0] x_to_return.append(xs[i]) p_to_return.append(posteriors[i]) return list(zip(x_to_return, p_to_return))
def mcmc(observed_covariance, df, outgroup=False): nodes=['s'+str(i+1) for i in range(observed_covariance.shape[0])] start_x=identifier_to_tree_clean(simulate_tree(4,0)),0 summaries=[summary.s_posterior(), summary.s_basic_tree_statistics(tree_statistics.unique_identifier_and_branch_lengths, 'tree', output='string'), summary.s_variable('add', output='double'), summary.s_no_admixes(),] options=options_object(outgroup) proposal=make_proposal(options)[0] posterior_function=posterior_class(observed_covariance, M=df, nodes=nodes) sample_verbose_scheme={'posterior':(1,200), 'tree':(1,0),'add':(1,200),'no_admixes':(1,200)} a=basic_chain(start_x, summaries, posterior_function, proposal, post=None, N=5000, sample_verbose_scheme=sample_verbose_scheme, overall_thinning=100, i_start_from=0, temperature=1.0, proposal_update=None, multiplier=None, check_trees=False, appending_result_file=None, appending_result_frequency=10) return a[2]
def func(nstart_tree): n, start_tree = nstart_tree final_tree, final_posterior, results, _ = basic_chain( start_tree, summaries, posterior, proposal, post=None, N=sim_length, sample_verbose_scheme=sample_verbose_scheme, overall_thinning=int(thinning_coef + sim_length / 60000), i_start_from=0, temperature=1.0, proposal_update=None, check_trees=True) save_to_csv(results, summaries, filename='results_' + str(n + 1) + 'csv', origin_layer=(n + 1, 1))
def trivial_simulation(start_val, reps, thinning_coef=1): posterior = initialize_trivial_posterior() summaries = [Trivial_Summary()] proposal = trivial_proposal() sample_verbose_scheme = {summary.name: (1, 0) for summary in summaries} final_tree, final_posterior, results, _ = basic_chain( start_val, summaries, posterior, proposal, post=None, N=reps, sample_verbose_scheme=sample_verbose_scheme, overall_thinning=int(thinning_coef + reps / 60000), i_start_from=0, temperature=1.0, proposal_update=None, check_trees=False) print results save_to_csv(results, summaries) return results
def test_posterior_model(true_tree=None, start_tree=None, sim_length=100000, summaries=None, thinning_coef=19, admixtures_of_true_tree=None, no_leaves_true_tree=4, filename='results.csv', sim_from_wishart=False, wishart_df=None, sap_sim=False, sap_ana=False, resimulate_regrafted_branch_length=False, emp_cov=None, big_posterior=False, rescale_empirical_cov=False): if true_tree is None: if admixtures_of_true_tree is None: admixtures_of_true_tree = geom.rvs(p=0.5) - 1 true_tree = generate_phylogeny(no_leaves_true_tree, admixtures_of_true_tree, skewed_admixture_prior=sap_sim) else: no_leaves_true_tree = get_no_leaves(true_tree) admixtures_of_true_tree = get_number_of_admixes(true_tree) true_x = (true_tree, 0) m = make_covariance(true_tree, get_trivial_nodes(no_leaves_true_tree)) if start_tree is None: start_tree = true_tree start_x = (start_tree, 0) if wishart_df is None: wishart_df = n_mark(m) if sim_from_wishart: r = m.shape[0] print m m = wishart.rvs(df=r * wishart_df - 1, scale=m / (r * wishart_df)) print m if emp_cov is not None: m = emp_cov if big_posterior: posterior = initialize_big_posterior(m, wishart_df, use_skewed_distr=sap_ana) else: posterior = initialize_posterior(m, wishart_df, use_skewed_distr=sap_ana, rescale=rescale_empirical_cov) print 'true_tree=', unique_identifier_and_branch_lengths(true_tree) post_ = posterior(true_x) print 'likelihood(true_tree)', post_[0] print 'prior(true_tree)', post_[1] print 'posterior(true_tree)', sum(post_[:2]) if summaries is None: summaries = [s_posterior(), s_variable('mhr'), s_no_admixes()] proposal = adaptive_proposal( resimulate_regrafted_branch_length=resimulate_regrafted_branch_length) #proposal.props=proposal.props[2:] #a little hack under the hood #proposal.params=proposal.params[2:] #a little hack under the hood. sample_verbose_scheme = {summary.name: (1, 0) for summary in summaries} sample_verbose_scheme['posterior'] = (1, 1) sample_verbose_scheme['no_admixes'] = (1, 1) final_tree, final_posterior, results, _ = basic_chain( start_x, summaries, posterior, proposal, post=None, N=sim_length, sample_verbose_scheme=sample_verbose_scheme, overall_thinning=int(max(thinning_coef, sim_length / 60000)), i_start_from=0, temperature=1.0, proposal_update=None, check_trees=False) save_to_csv(results, summaries, filename=filename) return true_tree
def _basic_chain_unpacker(args): return basic_chain(*args)