def test_post_likelihood(): """ Swaps likelihood "gaussian" for "target". It also tests aggregated chi2's by removing and adding a likelihood to an existing type. """ # Generate original chain orig_interval = OutputOptions.output_inteveral_s try: OutputOptions.output_inteveral_s = 0 info_params_local = deepcopy(info_params) info_params_local["dummy"] = 0 dummy_loglike_add = 0.1 dummy_loglike_remove = 0.01 info = { "output": None, "force": True, "params": info_params_local, "sampler": info_sampler, "likelihood": { "gaussian": { "external": sampled_pdf, "type": "A" }, "dummy": { "external": lambda dummy: 1, "type": "BB" }, "dummy_remove": { "external": lambda dummy: dummy_loglike_add, "type": "BB" } } } info_out, sampler = run(info) samples_in = mpi.gather(sampler.products()["sample"]) if mpi.is_main_process(): mcsamples_in = MCSamplesFromCobaya(info_out, samples_in) else: mcsamples_in = None info_out.update({ "post": { "suffix": "foo", "remove": { "likelihood": { "gaussian": None, "dummy_remove": None } }, "add": { "likelihood": { "target": { "external": target_pdf, "type": "A", "output_params": ["cprime"] }, "dummy_add": { "external": lambda dummy: dummy_loglike_remove, "type": "BB" } } } } }) info_post_out, products_post = post(info_out, sampler.products()["sample"]) samples = mpi.gather(products_post["sample"]) # Load with GetDist and compare if mcsamples_in: target_mean, target_cov = mpi.share(_get_targets(mcsamples_in)) mcsamples = MCSamplesFromCobaya(info_post_out, samples, name_tag="sample") new_mean = mcsamples.mean(["a", "b"]) new_cov = mcsamples.getCovMat().matrix mpi.share((new_mean, new_cov)) else: target_mean, target_cov = mpi.share() new_mean, new_cov = mpi.share() assert np.allclose(new_mean, target_mean) assert np.allclose(new_cov, target_cov) assert allclose(products_post["sample"]["chi2__A"], products_post["sample"]["chi2__target"]) assert allclose( products_post["sample"]["chi2__BB"], products_post["sample"]["chi2__dummy"] + products_post["sample"]["chi2__dummy_add"]) finally: OutputOptions.output_inteveral_s = orig_interval
# Export the results to GetDist from getdist.mcsamples import MCSamplesFromCobaya gd_sample = MCSamplesFromCobaya(updated_info, products["sample"]) # Analyze and plot mean = gd_sample.getMeans()[:2] covmat = gd_sample.getCovMat().matrix[:2, :2] print("Mean:") print(mean) print("Covariance matrix:") print(covmat) # %matplotlib inline # uncomment if running from the Jupyter notebook import getdist.plots as gdplt gdplot = gdplt.getSubplotPlotter() gdplot.triangle_plot(gd_sample, ["a", "b"], filled=True)