def runplot(self, **kwargs): try: dyplot.runplot(results=self.samples.results, **kwargs) except ValueError: pass self.output.to_figure(structure=None, auto_filename="runplot")
def runplot(self, **kwargs): if conf.instance["general"]["test"]["test_mode"]: return None try: dyplot.runplot(results=self.samples.results, **kwargs) except ValueError: pass self.output.to_figure(structure=None, auto_filename="runplot") self.mat_plot_1d.figure.close()
def test_gaussian(): logz_tol = 1 sampler = dynesty.NestedSampler(loglikelihood_gau, prior_transform_gau, ndim_gau, nlive=nlive) sampler.run_nested(print_progress=printing) # add samples # check continuation behavior sampler.run_nested(dlogz=0.1, print_progress=printing) # get errors nerr = 2 for i in range(nerr): sampler.reset() sampler.run_nested(print_progress=False) results = sampler.results pos = results.samples wts = np.exp(results.logwt - results.logz[-1]) mean, cov = dyfunc.mean_and_cov(pos, wts) logz = results.logz[-1] assert (np.abs(logz - logz_truth_gau) < logz_tol) # check summary res = sampler.results res.summary() # check plots dyplot.runplot(sampler.results) plt.close() dyplot.traceplot(sampler.results) plt.close() dyplot.cornerpoints(sampler.results) plt.close() dyplot.cornerplot(sampler.results) plt.close() dyplot.boundplot(sampler.results, dims=(0, 1), it=3000, prior_transform=prior_transform_gau, show_live=True, span=[(-10, 10), (-10, 10)]) plt.close() dyplot.cornerbound(sampler.results, it=3500, prior_transform=prior_transform_gau, show_live=True, span=[(-10, 10), (-10, 10)]) plt.close()
def plot_current_state(self): import matplotlib.pyplot as plt if self.check_point_plot: import dynesty.plotting as dyplot labels = [ label.replace('_', ' ') for label in self.search_parameter_keys ] try: filename = "{}/{}_checkpoint_trace.png".format( self.outdir, self.label) fig = dyplot.traceplot(self.sampler.results, labels=labels)[0] fig.tight_layout() fig.savefig(filename) except (RuntimeError, np.linalg.linalg.LinAlgError, ValueError, OverflowError, Exception) as e: logger.warning(e) logger.warning( 'Failed to create dynesty state plot at checkpoint') finally: plt.close("all") try: filename = "{}/{}_checkpoint_run.png".format( self.outdir, self.label) fig, axs = dyplot.runplot(self.sampler.results, logplot=False, use_math_text=False) fig.tight_layout() plt.savefig(filename) except (RuntimeError, np.linalg.linalg.LinAlgError, ValueError) as e: logger.warning(e) logger.warning( 'Failed to create dynesty run plot at checkpoint') finally: plt.close('all') try: filename = "{}/{}_checkpoint_stats.png".format( self.outdir, self.label) fig, axs = plt.subplots(nrows=3, sharex=True) for ax, name in zip(axs, ["boundidx", "nc", "scale"]): ax.plot(getattr(self.sampler, "saved_{}".format(name)), color="C0") ax.set_ylabel(name) axs[-1].set_xlabel("iteration") fig.tight_layout() plt.savefig(filename) except (RuntimeError, ValueError) as e: logger.warning(e) logger.warning( 'Failed to create dynesty stats plot at checkpoint') finally: plt.close('all')
def plot_current_state(self): import matplotlib.pyplot as plt if self.check_point_plot: import dynesty.plotting as dyplot labels = [label.replace('_', ' ') for label in self.search_parameter_keys] try: filename = "{}/{}_checkpoint_trace.png".format(self.outdir, self.label) fig = dyplot.traceplot(self.sampler.results, labels=labels)[0] fig.tight_layout() fig.savefig(filename) except (RuntimeError, np.linalg.linalg.LinAlgError, ValueError, OverflowError, Exception) as e: logger.warning(e) logger.warning('Failed to create dynesty state plot at checkpoint') finally: plt.close("all") try: filename = "{}/{}_checkpoint_trace_unit.png".format(self.outdir, self.label) from copy import deepcopy temp = deepcopy(self.sampler.results) temp["samples"] = temp["samples_u"] fig = dyplot.traceplot(temp, labels=labels)[0] fig.tight_layout() fig.savefig(filename) except (RuntimeError, np.linalg.linalg.LinAlgError, ValueError, OverflowError, Exception) as e: logger.warning(e) logger.warning('Failed to create dynesty unit state plot at checkpoint') finally: plt.close("all") try: filename = "{}/{}_checkpoint_run.png".format(self.outdir, self.label) fig, axs = dyplot.runplot( self.sampler.results, logplot=False, use_math_text=False) fig.tight_layout() plt.savefig(filename) except (RuntimeError, np.linalg.linalg.LinAlgError, ValueError) as e: logger.warning(e) logger.warning('Failed to create dynesty run plot at checkpoint') finally: plt.close('all') try: filename = "{}/{}_checkpoint_stats.png".format(self.outdir, self.label) fig, axs = dynesty_stats_plot(self.sampler) fig.tight_layout() plt.savefig(filename) except (RuntimeError, ValueError) as e: logger.warning(e) logger.warning('Failed to create dynesty stats plot at checkpoint') finally: plt.close('all')
def plot_unweighted(weightedouput, population): """ Produces the corner, trace and runplot for the results out of the bayesian analysis using the individual populations without normalisation. input: weightedoutput: Result dictionary from the bayesian analysis. population: Label of the population to used. """ fig1, _ = dyplot.cornerplot(weightedouput, color='blue', \ truth_color='black', show_titles=True, max_n_ticks=3, quantiles=None) fig2, _ = dyplot.traceplot(weightedouput, truth_color='black', \ show_titles=True, trace_cmap='viridis') fig3, _ = dyplot.runplot(weightedouput, lnz_error=False) fig1.savefig("./output/"+population+"_corner_unweighted.png") fig2.savefig("./output/"+population+"_trace_unweighted.png") fig3.savefig("./output/"+population+"_runplot_unweighted.png")
def dynesty_plots(system, truthvals = None, outname = None): if system.results == None: raise ValueError("No retrieval found in your system object!") if outname is not None: names = [outname + '_cornerplot.png', outname + '_traceplot.png', outname + '_runplot.png'] #cornerplot plt.figure(figsize = (20, 20)) cfig, caxes = dyplot.cornerplot(system.results, color = 'blue', max_n_ticks = 3, labels = system.fit_param_names, truths = truthvals) if outname == None: plt.show() else: plt.savefig(names[0]) plt.close('all') #traceplot plt.figure(figsize = (20, 20)) tfig, taxes = dyplot.traceplot(system.results, truths = truthvals, show_titles = True, trace_cmap = 'viridis', labels = system.fit_param_names) if outname == None: plt.show() else: plt.savefig(names[1]) plt.close('all') #runplot try: plt.figure(figsize = (20, 20)) rfig, raxes = dyplot.runplot(system.results) if outname == None: plt.show() else: plt.savefig(names[2]) plt.close('all') return names except ValueError: plt.close('all') print("Axis limits error on runplot; internal to dynesty") pass return None
def drun(self, dlogz=0.1, ndim=4): """ simple main function for sampling. """ # initialize our nested sampler sampler = dynesty.NestedSampler(self.log_likelihood, likemod.pt_te, ndim) sampler.run_nested(dlogz=dlogz) self.res = sampler.results self.res.summary() fig, _ = dyplot.runplot(self.res, lnz_error=False) fig1, _ = dyplot.traceplot(self.res, truths=np.zeros(ndim), \ truth_color='black', show_titles=True, \ trace_cmap='viridis', connect=True, \ connect_highlight=range(10)) fig2, _ = dyplot.cornerplot(self.res, color='blue', \ truth_color='black', show_titles=True, \ max_n_ticks=3, quantiles=None) fig.savefig('./output/evidence.png') fig1.savefig('./output/tracers.png') fig2.savefig('./output/cornerplot.png')
def plot_current_state(sampler, search_parameter_keys, outdir, label): labels = [label.replace("_", " ") for label in search_parameter_keys] try: filename = "{}/{}_checkpoint_trace.png".format(outdir, label) fig = dyplot.traceplot(sampler.results, labels=labels)[0] fig.tight_layout() fig.savefig(filename) except ( AssertionError, RuntimeError, np.linalg.linalg.LinAlgError, ValueError, ) as e: logger.warning(e) logger.warning("Failed to create dynesty state plot at checkpoint") finally: plt.close("all") try: filename = "{}/{}_checkpoint_run.png".format(outdir, label) fig, axs = dyplot.runplot(sampler.results) fig.tight_layout() plt.savefig(filename) except (RuntimeError, np.linalg.linalg.LinAlgError, ValueError) as e: logger.warning(e) logger.warning("Failed to create dynesty run plot at checkpoint") finally: plt.close("all") try: filename = "{}/{}_checkpoint_stats.png".format(outdir, label) fig, axs = plt.subplots(nrows=3, sharex=True) for ax, name in zip(axs, ["boundidx", "nc", "scale"]): ax.plot(getattr(sampler, f"saved_{name}"), color="C0") ax.set_ylabel(name) axs[-1].set_xlabel("iteration") fig.tight_layout() plt.savefig(filename) except (RuntimeError, ValueError) as e: logger.warning(e) logger.warning("Failed to create dynesty stats plot at checkpoint") finally: plt.close("all")
nlive_init=1000, nlive_batch=100, maxiter=maxiter, use_stop=False, # wt_kwargs={'pfrac': 1.0} ) res = sampler.results pickle.dump(res, open(chain_file, "wb")) t_run = (time.time() - t0) / 60. print('\n============================================') print("Sampling took {0:.10f} mins".format(t_run)) print('============================================') # ===================================================================== # Plots if plot: rfig, raxes = dyplot.runplot( res, span=[0.0, (0., 1.1), 0.0, (0., 1.05 * np.exp(np.nanmax(res.logz)))]) plt.savefig(chain_file.replace('.pickle', '_runplot.png')) tfig, taxes = dyplot.traceplot(res, labels=labels) plt.savefig(chain_file.replace('.pickle', '_traceplot.png')) cfig, caxes = dyplot.cornerplot(res, labels=labels, show_titles=True) plt.savefig(chain_file.replace('.pickle', '_cornerplot.png')) del res
def pyorbit_dynesty(config_in, input_datasets=None, return_output=None): output_directory = './' + config_in['output'] + '/dynesty/' mc = ModelContainerDynesty() pars_input(config_in, mc, input_datasets) if mc.nested_sampling_parameters['shutdown_jitter']: for dataset in mc.dataset_dict.itervalues(): dataset.shutdown_jitter() mc.model_setup() mc.create_variables_bounds() mc.initialize_logchi2() mc.create_starting_point() results_analysis.results_resumen(mc, None, skip_theta=True) mc.output_directory = output_directory print() print('Reference Time Tref: ', mc.Tref) print() print('*************************************************************') print() import dynesty # "Standard" nested sampling. sampler = dynesty.NestedSampler(mc.dynesty_call, mc.dynesty_priors, mc.ndim) sampler.run_nested() results = sampler.results # "Dynamic" nested sampling. dsampler = dynesty.DynamicNestedSampler(mc.dynesty_call, mc.dynesty_priors, mc.ndim) dsampler.run_nested() dresults = dsampler.results from dynesty import plotting as dyplot # Plot a summary of the run. rfig, raxes = dyplot.runplot(results) # Plot traces and 1-D marginalized posteriors. tfig, taxes = dyplot.traceplot(results) # Plot the 2-D marginalized posteriors. cfig, caxes = dyplot.cornerplot(results) from dynesty import utils as dyfunc # Extract sampling results. samples = results.samples # samples weights = np.exp(results.logwt - results.logz[-1]) # normalized weights # Compute 5%-95% quantiles. quantiles = dyfunc.quantile(samples, [0.05, 0.95], weights=weights) # Compute weighted mean and covariance. mean, cov = dyfunc.mean_and_cov(samples, weights) # Resample weighted samples. samples_equal = dyfunc.resample_equal(samples, weights) # Generate a new set of results with statistical+sampling uncertainties. results_sim = dyfunc.simulate_run(results) """ A dummy file is created to let the cpulimit script to proceed with the next step""" nested_sampling_create_dummy_file(mc) if return_output: return mc else: return
def plot_runplot(dsampler): fig, axes = dyplot.runplot(dsampler.results, fig=plt.subplots(4, 1, figsize=(8, 8))) plt.tight_layout() plt.savefig(PLOT_DIR / Path('test_runplot_3gauss.pdf')) plt.close('all')
def test_gaussian(dynamic, periodic, ndim, bound): rstate = get_rstate() g = Gaussian(ndim=ndim) if periodic: periodic = [0] else: periodic = None if dynamic: sampler = dynesty.DynamicNestedSampler(g.loglikelihood, g.prior_transform, g.ndim, nlive=nlive, rstate=rstate, periodic=periodic, bound=bound) else: sampler = dynesty.NestedSampler(g.loglikelihood, g.prior_transform, g.ndim, nlive=nlive, rstate=rstate, periodic=periodic, bound=bound) sampler.run_nested(print_progress=printing) results = sampler.results # check plots dyplot.runplot(results) dyplot.runplot(results, span=[(0., 10.), 0.001, 0.2, (5., 6.)]) dyplot.runplot(results, logplot=True) dyplot.runplot(results, fig=(plt.gcf(), plt.gcf().axes), max_x_ticks=0, max_y_ticks=0) plt.close() dyplot.traceplot(results) dyplot.traceplot(results, smooth=[10] * ndim) dyplot.traceplot(results, connect=True) dyplot.traceplot(results, fig=(plt.gcf(), plt.gcf().axes), show_titles=True, truths=np.zeros(ndim), verbose=True, max_n_ticks=0) plt.close() truths = np.zeros(ndim) truths[0] = -.1 span = [[-10, 10]] * ndim if ndim > 1: truths[1] = .1 span[1] = .9 dyplot.cornerplot(results, show_titles=True, truths=truths) dyplot.cornerplot(results, smooth=10, verbose=True, hist2d_kwargs=dict(plot_datapoints=True), max_n_ticks=0) plt.close() if ndim != 1: # cornerbound dyplot.cornerbound(results, it=500, prior_transform=g.prior_transform, show_live=True, span=span) dyplot.cornerbound(results, it=500, show_live=True, span=span, fig=(plt.gcf(), plt.gcf().axes)) plt.close() # boundplot dyplot.boundplot(results, dims=(0, 1)[:min(ndim, 2)], it=1000, prior_transform=g.prior_transform, show_live=True, span=span) dyplot.boundplot(results, dims=(0, 1)[:min(ndim, 2)], it=1000) plt.close() # cornerpoints dyplot.cornerpoints(results) plt.close() dyplot.cornerpoints(results, span=span, truths=truths, max_n_ticks=0) plt.close()
pfile = None # transform to preferred model variables res['chain'], parnames = transform_chain(res['chain'],res['model']) # mimic dynesty outputs res['logwt'] = (np.log(res['weights'])+res['logz'][-1]).astype(np.float64) res['logl'] = (res['lnlikelihood']).astype(np.float64) res['samples'] = (res['chain']).astype(np.float64) res['nlive'] = res['run_params']['nested_nlive_init'] font_kwargs = {'fontsize': fs} # Plot a summary of the run. if plt_summary: print 'making SUMMARY plot' rfig, raxes = dyplot.runplot(res, mark_final_live=False, label_kwargs=font_kwargs) for ax in raxes: ax.xaxis.set_tick_params(labelsize=tick_fs) ax.yaxis.set_tick_params(labelsize=tick_fs) ax.yaxis.get_offset_text().set_size(fs) rfig.tight_layout() rfig.savefig(outfolder+objname+'_dynesty_summary.pdf',dpi=200) # Plot traces and 1-D marginalized posteriors. if plt_trace: print 'making TRACE plot' tfig, taxes = dyplot.traceplot(res, labels=parnames,label_kwargs=font_kwargs) for ax in taxes.ravel(): ax.xaxis.set_tick_params(labelsize=tick_fs) ax.yaxis.set_tick_params(labelsize=tick_fs) tfig.tight_layout()
# "Dynamic" nested sampling. dsampler = dynesty.DynamicNestedSampler(loglike, ptform, ndim, nlive=500) dsampler.run_nested() dresults = dsampler.results from dynesty import utils as dyfunc # Combine results from "Static" and "Dynamic" runs. results = dyfunc.merge_runs([sresults, dresults]) from dynesty import plotting as dyplot # Plot a summary of the run. rfig, raxes = dyplot.runplot(results) # Plot traces and 1-D marginalized posteriors. tfig, taxes = dyplot.traceplot(results) # Plot the 2-D marginalized posteriors. cfig, caxes = dyplot.cornerplot(results) # we can post-process results # Extract sampling results. samples = results.samples # samples weights = np.exp(results.logwt - results.logz[-1]) # normalized weights # Compute 10%-90% quantiles.
p = np.array([(self.data[n, :] - mu) / sigma for n in range(self.N)]) return np.sum(-0.5 * (np.sum(p**2) + self.N * np.log(2 * np.pi * sigma**2))) likelihood = MultidimGaussianLikelihood(data, dim) priors = bilby.core.prior.PriorDict() priors.update({ "mu_{0}".format(i): bilby.core.prior.Uniform(-5, 5, 'mu') for i in range(dim) }) priors.update({ "sigma_{0}".format(i): bilby.core.prior.LogUniform(0.2, 5, 'sigma') for i in range(dim) }) # And run sampler result = bilby.run_sampler(likelihood=likelihood, priors=priors, sampler='dynesty', npoints=500, walks=10, outdir=outdir, label=label) res = result #result.plot_corner() # Analytic evidence solution logz_real = dim * np.log(2 * 5) fig, axes = dyplot.runplot(res, lnz_truth=logz_real)
def plot_ecc_results(results, pdfs, pdf_weights, suffix): samples = results.samples weights = np.exp(results.logwt - results.logz[-1]) samples_equal = dyfunc.resample_equal(samples, weights) # results.summary() mean, cov = dyfunc.mean_and_cov(samples, weights) errors = np.diagonal(cov)**0.5 maxL_index = results['logl'].argmax() maxL_params = samples[maxL_index] param_names = ['alpha', 'beta'] labels = ['$\\alpha$', '$\\beta$'] for ii in range(len(mean)): print('{0:5s} = {1:5.2f} +/- {2:5.2f}, maxL = {3:5.2f}'.format( param_names[ii], mean[ii], errors[ii], maxL_params[ii])) plt.close('all') dyplot.runplot(results) plt.savefig('dnest_ecc_run_' + suffix + '.png') dyplot.traceplot(results, labels=labels) plt.savefig('dnest_ecc_trace_' + suffix + '.png') dyplot.cornerplot(results, labels=labels) plt.savefig('dnest_ecc_corner_' + suffix + '.png') # Make a plot of the resulting distributions. # Note these bins have to match what we used to make the PDFs in the first place. e_bin = np.arange(0, 1, 0.01) # Calculate the "best-fit" PDF. # p_e = scipy.stats.beta.pdf(e_bin, mean[0], mean[1]) p_e = scipy.stats.beta.pdf(e_bin, maxL_params[0], maxL_params[1]) # Make samples drawn from the posteriors. N_samp = 1000 p_e_nk = np.zeros((len(e_bin), N_samp), dtype=float) for ss in range(N_samp): p_e_nk[:, ss] = scipy.stats.beta.pdf(e_bin, samples_equal[ss][0], samples_equal[ss][1]) fix, ax = plt.subplots(2, 1, sharex=True) plt.subplots_adjust(hspace=0) for ss in range(N_samp): ax[0].plot(e_bin, p_e_nk[:, ss], 'r-', linewidth=1, alpha=0.05) ax[0].plot(e_bin, p_e, 'r-', linewidth=5) # Plot the individual star PDFs e_bin_edges = np.append(e_bin, 1.0) e_bin_widths = np.diff(e_bin_edges) for ss in range(pdfs.shape[0]): # # instantiate and fit the KDE model # kde = KernelDensity(bandwidth=1e-2, kernel='gaussian') # kde.fit(pdfs[ss][:, None], sample_weight=pdf_weights[ss]) # # score_samples returns the log of the probability density # e_bin_kde = np.arange(0, 1.0, 5e-3) # logprob = kde.score_samples(e_bin_kde[:, None]) # prob = np.exp(logprob) # prob *= pdf_weights[ss].sum() # ax[1].plot(e_bin_kde, prob, 'k-', color='green', linewidth=2, alpha=0.5) en, eb = np.histogram(pdfs[ss], bins=e_bin_edges, weights=pdf_weights[ss], density=False) en /= e_bin_widths ax[1].plot(e_bin + (e_bin_widths / 2.0), en, 'k-', linewidth=2, alpha=0.5) ax[1].set_xlabel('Eccentricity') ax[1].set_ylabel('PDF') ax[0].set_ylabel('PDF') # ax[0].set_ylim(0, 5) ylim1 = ax[1].get_ylim() ax[1].set_ylim(0, ylim1[1]) plt.savefig('dnest_ecc_dist_' + suffix + '.png') return
sys.stderr.write('\n') lz_tol, m_tol, c_tol = (np.std(logzs), np.std(means, axis=0), np.std(covs, axis=0)) sys.stderr.write('logz_tol: {}\n'.format(lz_tol)) sys.stderr.write('mean_tol: {}\n'.format(m_tol)) sys.stderr.write('cov_tol: {}\n'.format(c_tol)) # check summary sys.stderr.write('\nResults\n') res = sampler.results res.summary() # check plots sys.stderr.write('\nPlotting\n') sys.stderr.write('Summary/Run Plot\n') dyplot.runplot(sampler.results) plt.close() sys.stderr.write('Trace Plot\n') dyplot.traceplot(sampler.results) plt.close() sys.stderr.write('Sub-Corner Plot (Points)\n') dyplot.cornerpoints(sampler.results) plt.close() sys.stderr.write('Corner Plot (Contours)\n') dyplot.cornerplot(sampler.results) plt.close() sys.stderr.write('2-D Bound Plot\n') dyplot.boundplot(sampler.results, dims=(0, 1), it=3000, prior_transform=prior_transform,
def MCMC_diagnostic(path, ndim, p, loglike, ptform, galname, nlive, **pdict): pdict = pdict['pdict'] start = time.time() pdict['start'] = start if ndim == 10: nparams = '_10P' sampler = NestedSampler(loglike, ptform, ndim=ndim, nlive=nlive, sample='unif', bound='multi', logl_kwargs=pdict, update_interval=.8, dlogz=0.5, first_update={ 'min_ncall': nlive, 'min_eff': 50. }, pool=p) sampler.run_nested(maxiter=15000, maxcall=50000) res1 = sampler.results # Save nested data # obtain KL divergence klds = [] for i in range(500): kld = dyfunc.kld_error(res1, error='simulate') klds.append(kld[-1]) print(np.mean(klds)) res1['KLval'] = np.mean(klds) with open(path + '/result_nested_P' + '{}'.format(nlive) + '.json', 'w') as ff: ff.write(json.dumps(res1, cls=NumpyEncoder)) lnz_truth = 10 * -np.log(2 * 30.) fig, axes = dyplot.runplot(res1, lnz_truth=lnz_truth) plt.savefig(path + '/runplot_' + galname + nparams + '.png') plt.close() fig, axes = dyplot.traceplot( res1, truths=np.array([ pdict['vrot'][0], pdict['vrot'][1], pdict['vrot'][2], pdict['vrot'][3], pdict['vdisp'][0], pdict['vdisp'][1], pdict['vdisp'][2], pdict['vdisp'][3], pdict['inc'], pdict['phi'] ]), truth_color='black', show_titles=True, trace_cmap='viridis', connect=True, smooth=0.02, connect_highlight=range(8), labels=[ r'$v_{rot,225}$', r'$v_{rot,450}$', r'$v_{rot,675}$', r'$v_{rot,900}$', r'$\sigma_{225}$', r'$\sigma_{450}$', r'$\sigma_{675}$', r'$\sigma_{900}$', r'$i$', r'$\phi$' ]) plt.savefig(path + '/traceplot_' + galname + nparams + '.png') plt.close() # initialize figure fig, axes = plt.subplots(2, 3, figsize=(15, 10)) # plot 6 snapshots over the course of the run for i, a in enumerate(axes.flatten()): it = int((i + 1) * res1.niter / 8.) # overplot the result onto each subplot temp = dyplot.boundplot(res1, dims=(0, 1), it=it, prior_transform=ptform, max_n_ticks=3, show_live=True, span=[(70, 150), (70, 150)], fig=(fig, a)) a.set_title('Iteration {0}'.format(it), fontsize=26) fig.tight_layout() plt.savefig(path + '/boundplot_' + galname + nparams + '.png') plt.close() fg, ax = dyplot.cornerplot( res1, color='blue', truths=np.array([ pdict['vrot'][0], pdict['vrot'][1], pdict['vrot'][2], pdict['vrot'][3], pdict['vdisp'][0], pdict['vdisp'][1], pdict['vdisp'][2], pdict['vdisp'][3], pdict['inc'], pdict['phi'] ]), # 91.8,98.3,8.88,6.5,60,60 truth_color='black', show_titles=True, smooth=0.02, max_n_ticks=5, quantiles=None, labels=[ r'$v_{rot,225}$', r'$v_{rot,450}$', r'$v_{rot,675}$', r'$v_{rot,900}$', r'$\sigma_{225}$', r'$\sigma_{450}$', r'$\sigma_{675}$', r'$\sigma_{900}$', r'$i$', r'$\phi$' ]) plt.savefig(path + '/cornerplot_' + galname + nparams + '.png') plt.close() with open(path + '/' + galname + '.txt', 'w+') as f: f.write('Running took: {} hours'.format( (time.time() - start) / 3600)) # Save the model data samples, weights = res1.samples, np.exp(res1.logwt - res1.logz[-1]) mean, cov = dyfunc.mean_and_cov(samples, weights) MaP = res1['samples'][res1['logl'].tolist().index( max(res1['logl'].tolist()))] quantiles = [ dyfunc.quantile(samps, [0.025, 0.5, 0.975], weights=weights) for samps in samples.T ] print(quantiles) # vrotsigma sigmavrot1_l = [i for i in samples[:, 0] if (i - MaP[0]) < 0] sigmavrot1_r = [i for i in samples[:, 0] if (i - MaP[0]) > 0] sigmavrot2_l = [i for i in samples[:, 1] if (i - MaP[1]) < 0] sigmavrot2_r = [i for i in samples[:, 1] if (i - MaP[1]) > 0] sigmavrot3_l = [i for i in samples[:, 2] if (i - MaP[2]) < 0] sigmavrot3_r = [i for i in samples[:, 2] if (i - MaP[2]) > 0] sigmavrot4_l = [i for i in samples[:, 3] if (i - MaP[3]) < 0] sigmavrot4_r = [i for i in samples[:, 3] if (i - MaP[3]) > 0] if len(sigmavrot1_l) == 0: sigmavrot1_l.append(0) if len(sigmavrot1_r) == 0: sigmavrot1_r.append(0) if len(sigmavrot2_l) == 0: sigmavrot2_l.append(0) if len(sigmavrot2_r) == 0: sigmavrot2_r.append(0) if len(sigmavrot3_l) == 0: sigmavrot3_l.append(0) if len(sigmavrot3_r) == 0: sigmavrot3_r.append(0) if len(sigmavrot4_l) == 0: sigmavrot4_l.append(0) if len(sigmavrot4_r) == 0: sigmavrot4_r.append(0) # vdispsigma sigmavdisp1_l = [i for i in samples[:, 4] if (i - MaP[4]) < 0] sigmavdisp1_r = [i for i in samples[:, 4] if (i - MaP[4]) > 0] sigmavdisp2_l = [i for i in samples[:, 5] if (i - MaP[5]) < 0] sigmavdisp2_r = [i for i in samples[:, 5] if (i - MaP[5]) > 0] sigmavdisp3_l = [i for i in samples[:, 6] if (i - MaP[6]) < 0] sigmavdisp3_r = [i for i in samples[:, 6] if (i - MaP[6]) > 0] sigmavdisp4_l = [i for i in samples[:, 7] if (i - MaP[7]) < 0] sigmavdisp4_r = [i for i in samples[:, 7] if (i - MaP[7]) > 0] if len(sigmavdisp1_l) == 0: sigmavdisp1_l.append(0) if len(sigmavdisp1_r) == 0: sigmavdisp1_r.append(0) if len(sigmavdisp2_l) == 0: sigmavdisp2_l.append(0) if len(sigmavdisp2_r) == 0: sigmavdisp2_r.append(0) if len(sigmavdisp3_l) == 0: sigmavdisp3_l.append(0) if len(sigmavdisp3_r) == 0: sigmavdisp3_r.append(0) if len(sigmavdisp4_l) == 0: sigmavdisp4_l.append(0) if len(sigmavdisp4_r) == 0: sigmavdisp4_r.append(0) pdict['sigmavrot'] = [(np.std(sigmavrot1_l), np.std(sigmavrot1_r)), (np.std(sigmavrot2_l), np.std(sigmavrot2_r)), (np.std(sigmavrot3_l), np.std(sigmavrot3_r)), (np.std(sigmavrot4_l), np.std(sigmavrot4_r))] pdict['sigmavdisp'] = [(np.std(sigmavdisp1_l), np.std(sigmavdisp1_r)), (np.std(sigmavdisp2_l), np.std(sigmavdisp2_r)), (np.std(sigmavdisp3_l), np.std(sigmavdisp3_r)), (np.std(sigmavdisp4_l), np.std(sigmavdisp4_r))] if len(MaP) == 8: pdict['vrot'] = MaP[0:4] pdict['vdisp'] = MaP[4:8] if len(MaP) == 9: pdict['vrot'] = MaP[0:4] pdict['vdisp'] = MaP[4:8] pdict['inc'] = MaP[8] # inc sigmainc_l = [i for i in samples[:, 8] if (i - MaP[8]) < 0] sigmainc_r = [i for i in samples[:, 8] if (i - MaP[8]) > 0] if len(sigmainc_l) == 0: sigmainc_l.append(0) if len(sigmainc_r) == 0: sigmainc_r.append(0) pdict['sigmainc'] = [(np.std(sigmainc_l), np.std(sigmainc_r))] if len(MaP) == 10: pdict['vrot'] = MaP[0:4] pdict['vdisp'] = MaP[4:8] pdict['inc'] = MaP[8] pdict['phi'] = MaP[9] # inc sigmainc_l = [i for i in samples[:, 8] if (i - MaP[8]) < 0] sigmainc_r = [i for i in samples[:, 8] if (i - MaP[8]) > 0] if len(sigmainc_l) == 0: sigmainc_l.append(0) if len(sigmainc_r) == 0: sigmainc_r.append(0) pdict['sigmainc'] = [(np.std(sigmainc_l), np.std(sigmainc_r))] # phi sigmaphi_l = [i for i in samples[:, 9] if (i - MaP[9]) < 0] sigmaphi_r = [i for i in samples[:, 9] if (i - MaP[9]) > 0] if len(sigmaphi_l) == 0: sigmaphi_l.append(0) if len(sigmaphi_r) == 0: sigmaphi_r.append(0) pdict['sigmaphi'] = [(np.std(sigmaphi_l), np.std(sigmaphi_r))] # We don't need data entry pdict['Data'] = None with open(path + '/params_model.json', 'w') as f: f.write(json.dumps(pdict, cls=NumpyEncoder))
def runMCMC(path, ndim, p, loglike, ptform, galname, **pdict): pdict = pdict['pdict'] start = time.time() pdict['start'] = start if ndim == 8: nparams = '_8P' sampler = NestedSampler(loglike, ptform, ndim=ndim, nlive=250, sample='unif', bound='multi', logl_kwargs=pdict, update_interval=0.8, dlogz=0.5, first_update={ 'min_ncall': 300, 'min_eff': 50. }, pool=p) sampler.run_nested(maxiter=15000, maxcall=50000) res1 = sampler.results with open(path + '/result_nested_P' + '{}'.format(ndim) + '.json', 'w') as ff: ff.write(json.dumps(res1, cls=NumpyEncoder)) lnz_truth = 10 * -np.log(2 * 30.) fig, axes = dyplot.runplot(res1, lnz_truth=lnz_truth) plt.savefig(path + '/runplot_' + galname + nparams + '.png') plt.close() fig, axes = dyplot.traceplot(res1, truths=np.array([ pdict['vrot'][0], pdict['vrot'][1], pdict['vrot'][2], pdict['vrot'][3], pdict['vdisp'][0], pdict['vdisp'][1], pdict['vdisp'][2], pdict['vdisp'][3] ]), truth_color='black', show_titles=True, trace_cmap='viridis', connect=True, smooth=0.02, connect_highlight=range(8), labels=[ r'$v_{rot,225}$', r'$v_{rot,450}$', r'$v_{rot,675}$', r'$v_{rot,900}$', r'$\sigma_{225}$', r'$\sigma_{450}$', r'$\sigma_{675}$', r'$\sigma_{900}$' ]) plt.savefig(path + '/traceplot_' + galname + nparams + '.png') plt.close() # plot 6 snapshots over the course of the run for i, a in enumerate(axes.flatten()): it = int((i + 1) * res1.niter / 8.) # overplot the result onto each subplot temp = dyplot.boundplot(res1, dims=(0, 1), it=it, prior_transform=ptform, max_n_ticks=5, show_live=True, span=[(70, 150), (70, 150)], fig=(fig, a)) a.set_title('Iteration {0}'.format(it), fontsize=26) fig.tight_layout() plt.savefig(path + '/boundplot_' + galname + nparams + '.png') plt.close() matplotlib.rcParams.update({'font.size': 16}) fig, axes = dyplot.cornerplot( res1, color='blue', truths=np.array([ pdict['vrot'][0], pdict['vrot'][1], pdict['vrot'][2], pdict['vrot'][3], pdict['vdisp'][0], pdict['vdisp'][1], pdict['vdisp'][2], pdict['vdisp'][3], pdict['inc'], pdict['phi'] ]), truth_color='black', show_titles=True, smooth=0.02, max_n_ticks=5, quantiles=[0.16, 0.5, 0.84], labels=[ r'$V_{225}[km/s]$', r'$V_{450}[km/s]$', r'$V_{675}[km/s]$', r'$V_{900}[km/s]$', r'$\sigma_{gas,225}[km/s]$', r'$\sigma_{gas,450}[km/s]$', r'$\sigma_{gas,675}[km/s]$', r'$\sigma_{gas,900}[km/s]$', r'$i[deg]$', r'$\phi[deg]$' ]) # Save the model data samples, weights = res1.samples, np.exp(res1.logwt - res1.logz[-1]) mean, cov = dyfunc.mean_and_cov(samples, weights) MaP = res1['samples'][res1['logl'].tolist().index( max(res1['logl'].tolist()))] quantiles = [ dyfunc.quantile(samps, [0.16, 0.5, 0.84], weights=weights) for samps in samples.T ] labels = [ r'$V_{225}$', r'$V_{450}$', r'$V_{675}$', r'$V_{900}$', r'$\sigma_{gas,225}$', r'$\sigma_{gas,450}$', r'$\sigma_{gas,675}$', r'$\sigma_{gas,900}$', r'$i$', r'$\phi$' ] units = [ ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [deg]', ' [deg]' ] for i in range(ndim): ax = axes[i, i] q5 = np.round(quantiles[i][1], 2) q14 = np.round(quantiles[i][0], 2) q84 = np.round(quantiles[i][2], 2) ax.set_title(r"$%.2f_{%.2f}^{+%.2f}$" % (q5, -1 * abs(q5 - q14), abs(q5 - q84)) + units[i]) # Loop over the histograms for yi in range(ndim): axes[yi, 0].set_ylabel(labels[yi] + units[yi], labelpad=30, fontsize=20) axes[-1, yi].set_xlabel(labels[yi] + units[yi], labelpad=30, fontsize=20) axes[yi, 0].tick_params(axis='y', which='major', labelsize=14) axes[-1, yi].tick_params(axis='x', which='major', labelsize=14) fig.tight_layout() plt.savefig(path + '/cornerplot_' + galname + nparams + '.pdf') plt.close() with open(path + '/' + galname + '.txt', 'w+') as f: f.write('Running took: {} hours'.format( (time.time() - start) / 3600)) elif ndim == 9: nparams = '_9P' sampler = NestedSampler(loglike, ptform, ndim=ndim, nlive=250, sample='unif', bound='multi', logl_kwargs=pdict, update_interval=0.8, dlogz=0.5, first_update={ 'min_ncall': 300, 'min_eff': 50. }, pool=p) sampler.run_nested(maxiter=15000, maxcall=50000) res1 = sampler.results with open(path + '/result_nested_P' + '{}'.format(ndim) + '.json', 'w') as ff: ff.write(json.dumps(res1, cls=NumpyEncoder)) lnz_truth = 10 * -np.log(2 * 30.) fig, axes = dyplot.runplot(res1, lnz_truth=lnz_truth) plt.savefig(path + '/runplot_' + galname + nparams + '.png') plt.close() fig, axes = dyplot.traceplot( res1, truths=np.array([ pdict['vrot'][0], pdict['vrot'][1], pdict['vrot'][2], pdict['vrot'][3], pdict['vdisp'][0], pdict['vdisp'][1], pdict['vdisp'][2], pdict['vdisp'][3], pdict['inc'] ]), truth_color='black', show_titles=True, trace_cmap='viridis', connect=True, smooth=0.02, connect_highlight=range(8), labels=[ r'$v_{rot,225}$', r'$v_{rot,450}$', r'$v_{rot,675}$', r'$v_{rot,900}$', r'$\sigma_{225}$', r'$\sigma_{450}$', r'$\sigma_{675}$', r'$\sigma_{900}$', r'$i$' ]) plt.savefig(path + '/traceplot_' + galname + nparams + '.png') plt.close() # initialize figure fig, axes = plt.subplots(2, 3, figsize=(15, 10)) # plot 6 snapshots over the course of the run for i, a in enumerate(axes.flatten()): it = int((i + 1) * res1.niter / 8.) # overplot the result onto each subplot temp = dyplot.boundplot(res1, dims=(0, 1), it=it, prior_transform=ptform, max_n_ticks=3, show_live=True, span=[(70, 150), (70, 150)], fig=(fig, a)) a.set_title('Iteration {0}'.format(it), fontsize=26) fig.tight_layout() plt.savefig(path + '/boundplot_' + galname + nparams + '.png') plt.close() matplotlib.rcParams.update({'font.size': 16}) fig, axes = dyplot.cornerplot( res1, color='blue', truths=np.array([ pdict['vrot'][0], pdict['vrot'][1], pdict['vrot'][2], pdict['vrot'][3], pdict['vdisp'][0], pdict['vdisp'][1], pdict['vdisp'][2], pdict['vdisp'][3], pdict['inc'] ]), truth_color='black', show_titles=True, smooth=0.02, max_n_ticks=5, quantiles=[0.16, 0.5, 0.84], labels=[ r'$V_{225}[km/s]$', r'$V_{450}[km/s]$', r'$V_{675}[km/s]$', r'$V_{900}[km/s]$', r'$\sigma_{gas,225}[km/s]$', r'$\sigma_{gas,450}[km/s]$', r'$\sigma_{gas,675}[km/s]$', r'$\sigma_{gas,900}[km/s]$', r'$i[deg]$' ]) # Save the model data samples, weights = res1.samples, np.exp(res1.logwt - res1.logz[-1]) mean, cov = dyfunc.mean_and_cov(samples, weights) MaP = res1['samples'][res1['logl'].tolist().index( max(res1['logl'].tolist()))] quantiles = [ dyfunc.quantile(samps, [0.16, 0.5, 0.84], weights=weights) for samps in samples.T ] labels = [ r'$V_{225}$', r'$V_{450}$', r'$V_{675}$', r'$V_{900}$', r'$\sigma_{gas,225}$', r'$\sigma_{gas,450}$', r'$\sigma_{gas,675}$', r'$\sigma_{gas,900}$', r'$i$', r'$\phi$' ] units = [ ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [deg]', ' [deg]' ] for i in range(ndim): ax = axes[i, i] q5 = np.round(quantiles[i][1], 2) q14 = np.round(quantiles[i][0], 2) q84 = np.round(quantiles[i][2], 2) ax.set_title(r"$%.2f_{%.2f}^{+%.2f}$" % (q5, -1 * abs(q5 - q14), abs(q5 - q84)) + units[i]) # Loop over the histograms for yi in range(ndim): axes[yi, 0].set_ylabel(labels[yi] + units[yi], labelpad=30, fontsize=20) axes[-1, yi].set_xlabel(labels[yi] + units[yi], labelpad=30, fontsize=20) axes[yi, 0].tick_params(axis='y', which='major', labelsize=14) axes[-1, yi].tick_params(axis='x', which='major', labelsize=14) fig.tight_layout() plt.savefig(path + '/cornerplot_' + galname + nparams + '.pdf') plt.close() with open(path + '/' + galname + '.txt', 'w+') as f: f.write('Running took: {} hours'.format( (time.time() - start) / 3600)) elif ndim == 10: nparams = '_10P' sampler = NestedSampler(loglike, ptform, ndim=ndim, nlive=250, sample='unif', bound='multi', logl_kwargs=pdict, update_interval=.8, dlogz=0.5, first_update={ 'min_ncall': 300, 'min_eff': 50. }, pool=p) sampler.run_nested(maxiter=15000, maxcall=50000) res1 = sampler.results with open(path + '/result_nested_P' + '{}'.format(ndim) + '.json', 'w') as ff: ff.write(json.dumps(res1, cls=NumpyEncoder)) lnz_truth = 10 * -np.log(2 * 30.) fig, axes = dyplot.runplot(res1, lnz_truth=lnz_truth) plt.savefig(path + '/runplot_' + galname + nparams + '.png') plt.close() fig, axes = dyplot.traceplot( res1, truths=np.array([ pdict['vrot'][0], pdict['vrot'][1], pdict['vrot'][2], pdict['vrot'][3], pdict['vdisp'][0], pdict['vdisp'][1], pdict['vdisp'][2], pdict['vdisp'][3], pdict['inc'], pdict['phi'] ]), truth_color='black', show_titles=True, trace_cmap='viridis', connect=True, smooth=0.02, connect_highlight=range(8), labels=[ r'$v_{rot,225}$', r'$v_{rot,450}$', r'$v_{rot,675}$', r'$v_{rot,900}$', r'$\sigma_{225}$', r'$\sigma_{450}$', r'$\sigma_{675}$', r'$\sigma_{900}$', r'$i$', r'$\phi$' ]) plt.savefig(path + '/traceplot_' + galname + nparams + '.png') plt.close() # initialize figure fig, axes = plt.subplots(2, 3, figsize=(15, 10)) # plot 6 snapshots over the course of the run for i, a in enumerate(axes.flatten()): it = int((i + 1) * res1.niter / 8.) # overplot the result onto each subplot temp = dyplot.boundplot(res1, dims=(0, 1), it=it, prior_transform=ptform, max_n_ticks=3, show_live=True, span=[(70, 150), (70, 150)], fig=(fig, a)) a.set_title('Iteration {0}'.format(it), fontsize=26) fig.tight_layout() plt.savefig(path + '/boundplot_' + galname + nparams + '.png') plt.close() matplotlib.rcParams.update({'font.size': 16}) fig, axes = dyplot.cornerplot( res1, color='blue', truths=np.array([ pdict['vrot'][0], pdict['vrot'][1], pdict['vrot'][2], pdict['vrot'][3], pdict['vdisp'][0], pdict['vdisp'][1], pdict['vdisp'][2], pdict['vdisp'][3], pdict['inc'], pdict['phi'] ]), truth_color='black', show_titles=True, smooth=0.02, max_n_ticks=5, quantiles=[0.16, 0.5, 0.84], labels=[ r'$V_{225}[km/s]$', r'$V_{450}[km/s]$', r'$V_{675}[km/s]$', r'$V_{900}[km/s]$', r'$\sigma_{gas,225}[km/s]$', r'$\sigma_{gas,450}[km/s]$', r'$\sigma_{gas,675}[km/s]$', r'$\sigma_{gas,900}[km/s]$', r'$i[deg]$', r'$\phi[deg]$' ]) # Save the model data samples, weights = res1.samples, np.exp(res1.logwt - res1.logz[-1]) mean, cov = dyfunc.mean_and_cov(samples, weights) MaP = res1['samples'][res1['logl'].tolist().index( max(res1['logl'].tolist()))] quantiles = [ dyfunc.quantile(samps, [0.16, 0.5, 0.84], weights=weights) for samps in samples.T ] labels = [ r'$V_{225}$', r'$V_{450}$', r'$V_{675}$', r'$V_{900}$', r'$\sigma_{gas,225}$', r'$\sigma_{gas,450}$', r'$\sigma_{gas,675}$', r'$\sigma_{gas,900}$', r'$i$', r'$\phi$' ] units = [ ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [km/s]', ' [deg]', ' [deg]' ] for i in range(ndim): ax = axes[i, i] q5 = np.round(quantiles[i][1], 2) q14 = np.round(quantiles[i][0], 2) q84 = np.round(quantiles[i][2], 2) ax.set_title(r"$%.2f_{%.2f}^{+%.2f}$" % (q5, -1 * abs(q5 - q14), abs(q5 - q84)) + units[i]) # Loop over the histograms for yi in range(ndim): axes[yi, 0].set_ylabel(labels[yi] + units[yi], labelpad=30, fontsize=20) axes[-1, yi].set_xlabel(labels[yi] + units[yi], labelpad=30, fontsize=20) axes[yi, 0].tick_params(axis='y', which='major', labelsize=14) axes[-1, yi].tick_params(axis='x', which='major', labelsize=14) fig.tight_layout() plt.savefig(path + '/cornerplot_' + galname + nparams + '.pdf') plt.close() with open(path + '/' + galname + '.txt', 'w+') as f: f.write('Running took: {} hours'.format( (time.time() - start) / 3600)) # Save the model data samples, weights = res1.samples, np.exp(res1.logwt - res1.logz[-1]) mean, cov = dyfunc.mean_and_cov(samples, weights) MaP = res1['samples'][res1['logl'].tolist().index( max(res1['logl'].tolist()))] quantiles = [ dyfunc.quantile(samps, [0.16, 0.5, 0.84], weights=weights) for samps in samples.T ] pdict['sigmavrot'] = [(quantiles[0][0], quantiles[0][2]), (quantiles[1][0], quantiles[1][2]), (quantiles[2][0], quantiles[2][2]), (quantiles[3][0], quantiles[3][2])] pdict['sigmavdisp'] = [(quantiles[4][0], quantiles[4][2]), (quantiles[5][0], quantiles[5][2]), (quantiles[6][0], quantiles[6][2]), (quantiles[7][0], quantiles[7][2])] pdict['vrot'] = [ quantiles[0][1], quantiles[1][1], quantiles[2][1], quantiles[3][1] ] pdict['vdisp'] = [ quantiles[4][1], quantiles[5][1], quantiles[6][1], quantiles[7][1] ] if len(quantiles) == 9: pdict['inc'] = quantiles[8][1] pdict['sigmainc'] = [(quantiles[8][0], quantiles[8][2])] if len(quantiles) == 10: pdict['inc'] = quantiles[8][1] pdict['sigmainc'] = [(quantiles[8][0], quantiles[8][2])] pdict['phi'] = quantiles[9][1] pdict['sigmaphi'] = [(quantiles[9][0], quantiles[9][2])] # We don't need data entry, waste of space pdict['Data'] = None with open(path + '/params_model.json', 'w') as f: f.write(json.dumps(pdict, cls=NumpyEncoder))
dlogz_init=0.01, wt_kwargs={'pfrac': 1.0}) ##ANALYSIS print "Analysing Dynesty Results" dres = dsampler.results # store results with open( "LSQ12dlf_dynesty_Plim550_DopLens%g_Zoom%g.pkl" % (DopLens_Fit, full_ecc), "wb") as out: pickle.dump(dres, out) # diagnostic summary plot from dynesty import plotting as dyplot print "Summary Plot" fig, ax = dyplot.runplot(dres, logplot=True) fig.tight_layout() #fig.savefig("dyntest_sumry_DopLens%g_Zoom%g.png" %(DopLens_Fit, full_ecc)) fig.savefig("dplots/dyntest_sumry_DopLens%g_Zoom%g_seconly.png" % (DopLens_Fit, full_ecc)) # trace plot print "Trace Plot" fig1, ax1 = dyplot.traceplot(dres, labels=param_names, show_titles=True) fig1.tight_layout() fig1.savefig("dplots/dyntest_traceDopLens%g_Zoom%g_seconly.png" % (DopLens_Fit, full_ecc)) #fig1.savefig("dyntest_traceDopLens%g_Zoom%g.png" %(DopLens_Fit, full_ecc))
def plot(n, alpha, beta, dlogz_val=.1, interloper=False, d=1): x_k = np.round(generator(n, alpha, beta, d), 2) #round up #append values if interloper is true if (interloper == True): interloper_x_k = np.round(generator(n, alpha + 3, beta, d), 2) x_k = np.append(x_k, interloper_x_k) #feed parameters for alpha and beta def lighthouse_logl(params): return log_likelihood(x_k, params[0], params[1]) #prior transform, using documentation for range -1000, 1000 def prior_transform(u): return [2000 * u[0] - 1000, 1000 * u[1]] ndim = 2 sampler = dynesty.NestedSampler(lighthouse_logl, prior_transform, ndim, bound='single', nlive=200) sampler.run_nested(dlogz=dlogz_val, print_progress=False) results = sampler.results #runplot dyplot.runplot(results) #plt.savefig('int_run_plot' + str(dlogz_val)+".png", dpi =300) plt.show() #cornerpoints plot fig = plt.subplots(1, 1, figsize=(10, 10)) dyplot.cornerpoints(results, fig=fig, cmap='plasma', truths=np.zeros(ndim), kde=False) fig[1].set_ylabel('$\\beta$') fig[1].set_xlabel('$\\alpha$') plt.tight_layout() plt.xlim(-10, 10) plt.ylim(0, 10) plt.savefig('int_corner1' + str(dlogz_val) + ".png", dpi=300) plt.show() #traceplot fig = plt.subplots(2, 2, figsize=(15, 10)) dyplot.traceplot(results, fig=fig, truth_color='black', trace_cmap='viridis', connect=True, connect_highlight=range(5), show_titles=True) fig[1][1, 1].set_xlabel('$\\beta$') fig[1][0, 1].set_xlabel('$\\alpha$') fig[1][1, 0].set_ylabel('$\\beta$') fig[1][0, 0].set_ylabel('$\\alpha$') plt.tight_layout() #plt.savefig('int_trace' + str(dlogz_val)+".png", dpi =300) plt.show() print(results.samples[-1])
def run_multinest(self, transit_bins, transit_depths, transit_errors, eclipse_bins, eclipse_depths, eclipse_errors, fit_info, include_condensation=True, plot_best=False, maxiter=None, maxcall=None, nlive=100, **dynesty_kwargs): '''Runs nested sampling to retrieve atmospheric parameters. Parameters ---------- transit_bins : array_like, shape (N,2) Wavelength bins, where wavelength_bins[i][0] is the start wavelength and wavelength_bins[i][1] is the end wavelength for bin i. transit_depths : array_like, length N Measured transit depths for the specified wavelength bins transit_errors : array_like, length N Errors on the aforementioned transit depths eclipse_bins : array_like, shape (N,2) Wavelength bins, where wavelength_bins[i][0] is the start wavelength and wavelength_bins[i][1] is the end wavelength for bin i. eclipse_depths : array_like, length N Measured eclipse depths for the specified wavelength bins eclipse_errors : array_like, length N Errors on the aforementioned eclipse depths fit_info : :class:`.FitInfo` object Tells us what parameters to freely vary, and in what range those parameters can vary. Also sets default values for the fixed parameters. include_condensation : bool, optional When determining atmospheric abundances, whether to include condensation. plot_best : bool, optional If True, plots the best fit model with the data nlive : int Number of live points to use for nested sampling **dynesty_kwargs : keyword arguments to pass to dynesty's NestedSampler Returns ------- result : Result object This returns dynesty's NestedSampler 'results' field, slightly modified. The object is dictionary-like and has many useful items. For example, result.samples (or alternatively, result["samples"]) are the parameter values of each sample, result.logwt contains the log(weights), result.weights contains the normalized weights (this is added by PLATON), result.logl contains the ln likelihoods, and result.logp contains the ln posteriors (this is added by PLATON). result.logz is the natural logarithm of the evidence. ''' transit_calc = TransitDepthCalculator( include_condensation=include_condensation) transit_calc.change_wavelength_bins(transit_bins) eclipse_calc = EclipseDepthCalculator() eclipse_calc.change_wavelength_bins(eclipse_bins) self._validate_params(fit_info, transit_calc) def transform_prior(cube): new_cube = np.zeros(len(cube)) for i in range(len(cube)): new_cube[i] = fit_info._from_unit_interval(i, cube[i]) return new_cube def multinest_ln_like(cube): ln_like = self._ln_like(cube, transit_calc, eclipse_calc, fit_info, transit_depths, transit_errors, eclipse_depths, eclipse_errors) if np.random.randint(100) == 0: print("\nEvaluated params: {}".format( self.pretty_print(fit_info))) return ln_like num_dim = fit_info._get_num_fit_params() sampler = NestedSampler(multinest_ln_like, transform_prior, num_dim, bound='multi', sample='rwalk', update_interval=float(num_dim), nlive=nlive, **dynesty_kwargs) sampler.run_nested(maxiter=maxiter, maxcall=maxcall) result = sampler.results result.logp = result.logl + np.array( [fit_info._ln_prior(params) for params in result.samples]) best_params_arr = result.samples[np.argmax(result.logp)] normalized_weights = np.exp(result.logwt) / np.sum(np.exp( result.logwt)) result.weights = normalized_weights write_param_estimates_file( dynesty.utils.resample_equal(result.samples, normalized_weights), best_params_arr, np.max(result.logp), fit_info.fit_param_names) if plot_best: self._ln_prob(best_params_arr, transit_calc, eclipse_calc, fit_info, transit_depths, transit_errors, eclipse_depths, eclipse_errors, plot=True) plt.figure(3) dyplot.runplot(result) plt.savefig("dyplot_runplot.png") plt.figure(4) dyplot.traceplot(result) plt.savefig("dyplot_traceplot.png") return result