def plot_eta_earth_histogram(eta_earths, outdir=None): pu.plot_histogram_posterior(eta_earths.flatten(), normed=True, histtype='step', color='k') pp.xlabel(r'$\eta_\oplus$') pp.ylabel(r'$p\left(\eta_\oplus\right)$') pp.axvline(np.percentile(eta_earths, 5), color='k') pp.axvline(np.percentile(eta_earths, 95), color='k') if outdir is not None: pp.savefig(op.join(outdir, 'eta-earth.pdf'))
def plot_1d(chain, inj, name, lname, outdir, show): pp.clf() pu.plot_histogram_posterior(chain[name].view(float).flatten(), normed=True, histtype='step') if inj is not None: pp.axvline(inj[name]) pp.xlabel('$' + lname + '$') pp.ylabel(r'$p\left( ' + lname + r' \right)$') pp.savefig(op.join(outdir, '{0:s}.pdf'.format(name))) if show: pp.show()
def plot_nplanets_histogram(chain, outdir=None): tau = ac.autocorrelation_length_estimate(np.mean(chain[:,:,0], axis=0)) if tau is None: nps = chain[:,:,0].flatten() else: nps = chain[:,::tau,0].flatten() pu.plot_histogram_posterior(nps, normed=True, color='k', histtype='step') pp.xlabel(r'$\Lambda_\mathrm{pl}$') pp.ylabel(r'$p(\Lambda_\mathrm{pl})$') pp.axvline(np.percentile(nps, 5), color='k') pp.axvline(np.percentile(nps, 95), color='k') if outdir is not None: pp.savefig(op.join(outdir, 'npl.pdf'))
def plot_correlation_coefficient(logpost, chain, outdir=None): fchain = chain.reshape((-1, chain.shape[2])) rs = [] for p in fchain: cm = logpost.covariance_matrix(p) rs.append(cm[0,1] / np.sqrt(cm[0,0]*cm[1,1])) rs = np.array(rs) pu.plot_histogram_posterior(rs, normed=True, histtype='step', color='k') pp.xlabel(r'$r$') pp.ylabel(r'$p(r)$') pp.axvline(np.percentile(rs, 5), color='k') pp.axvline(np.percentile(rs, 95), color='k') if outdir is not None: pp.savefig(op.join(outdir, 'corr.pdf'))
def plot_parameters(logpost, chain, eta_earths, outdir=None): pp.subplot(2,1,1) pu.plot_histogram_posterior(eta_earths.flatten(), normed=True, histtype='step', color='k') pp.xlabel(r'$\eta_\oplus$') pp.ylabel(r'$p\left(\eta_\oplus\right)$') pp.axvline(np.percentile(eta_earths, 5), color='k') pp.axvline(np.percentile(eta_earths, 95), color='k') pp.subplot(2,1,2) nps = chain[:,:,0].flatten() pu.plot_histogram_posterior(nps, normed=True, color='k', histtype='step') pp.xlabel(r'$\Lambda_\mathrm{pl}$') pp.ylabel(r'$p(\Lambda_\mathrm{pl})$') pp.axvline(np.percentile(nps, 5), color='k') pp.axvline(np.percentile(nps, 95), color='k') pp.tight_layout() if outdir is not None: pp.savefig(op.join(outdir, 'pars.pdf')) ea_col = fits.Column(name='eta_earth', format='E', array=eta_earths.flatten()) npl_col = fits.Column(name='Lambda_pl', format='E', array=nps) cols = fits.ColDefs([ea_col, npl_col]) tbhdu = fits.BinTableHDU.from_columns(cols) hdr = fits.Header() hdr['COMMENT'] = 'Farr, et al (2015) Figure 2' hdr['COMMENT'] = 'Posterior samples from eta_earth and Lambda_pl' hdr['COMMENT'] = 'eta_earth is the number density of planets per star' hdr['COMMENT'] = 'per natural-logarithmic interval in P and R' hdr['COMMENT'] = 'at P = 1 yr and R = 1 REarth' hdr['COMMENT'] = 'Lambda_pl is the Poisson mean number of planets' hdr['COMMENT'] = 'per star' prihdu = fits.PrimaryHDU(header=hdr) hdus = fits.HDUList([prihdu, tbhdu]) hdus.writeto(op.join(outdir, 'pars.fits'), clobber=True)
def plot_selection(logpost, chain, Ndraw=100, outdir=None): fchain = chain.reshape((-1, chain.shape[2])) all_draws = [] draws = [] psel = 0 for p in fchain[np.random.randint(fchain.shape[0], size=Ndraw), :]: dcands = logpost.draw(p, logpost.systems['Kepler_ID'], logpost.systems['Mass'], logpost.systems['Radius'], logpost.systems['SNR0']) all_draws.append(dcands) draws.append(dcands[np.random.random(size=dcands.shape[0]) < 1.0/Ndraw]) psel += logpost.ptransit(logpost.candidates['Period'], logpost.candidates['Stellar_Mass'], logpost.candidates['Stellar_Radius']) * \ logpost.pdetect(p, logpost.candidates['Period'], logpost.candidates['Radius'], logpost.candidates['SNR0']) psel /= Ndraw all_candidates = np.concatenate(all_draws) candidates = np.concatenate(draws) pp.subplot(2,2,1) pu.plot_histogram_posterior(logpost.candidates['Radius'], color='k', normed=True, histtype='step', log=True) pu.plot_histogram_posterior(all_candidates['Radius'], color='b', normed=True, histtype='step', log=True) pp.yscale('log') pp.axis(xmin=min(np.min(candidates['Radius']), np.min(logpost.candidates['Radius'])), xmax=max(np.max(candidates['Radius']), np.max(logpost.candidates['Radius'])), ymin=1e-4) pp.xlabel(r'$R$ ($R_\oplus$)') pp.ylabel(r'$p(\ln R)$') pp.subplot(2,2,2) pp.scatter(logpost.candidates['Period'], logpost.candidates['Radius'], color='k', alpha=0.05) pp.scatter(candidates['Period'], candidates['Radius'], color='b', alpha=0.05) pp.axis(xmin=min(np.min(candidates['Period']), np.min(logpost.candidates['Period'])), xmax=max(np.max(candidates['Period']), np.max(logpost.candidates['Period'])), ymin=min(np.min(candidates['Radius']), np.min(logpost.candidates['Radius'])), ymax=max(np.max(candidates['Radius']), np.max(logpost.candidates['Radius']))) pp.ylabel(r'$R$ ($R_\oplus$)') pp.xlabel(r'$P$ ($\mathrm{yr}$)') pp.xscale('log') pp.yscale('log') pp.subplot(2,2,3) pp.scatter(logpost.candidates['Period'], logpost.candidates['Radius'], c=psel, norm=mc.LogNorm()) pp.axis(xmin=min(np.min(candidates['Period']), np.min(logpost.candidates['Period'])), xmax=max(np.max(candidates['Period']), np.max(logpost.candidates['Period'])), ymin=min(np.min(candidates['Radius']), np.min(logpost.candidates['Radius'])), ymax=max(np.max(candidates['Radius']), np.max(logpost.candidates['Radius']))) pp.xscale('log') pp.yscale('log') pp.ylabel(r'$R$ ($R_\oplus$)') pp.xlabel(r'$P$ ($\mathrm{yr}$)') pp.colorbar() pp.subplot(2,2,4) pu.plot_histogram_posterior(logpost.candidates['Period'], color='k', normed=True, histtype='step', log=True) pu.plot_histogram_posterior(all_candidates['Period'], color='b', normed=True, histtype='step', log=True) pp.yscale('log') pp.axis(xmin=min(np.min(candidates['Period']), np.min(logpost.candidates['Period'])), xmax=max(np.max(candidates['Period']), np.max(logpost.candidates['Period'])), ymin=1e-4) pp.xlabel(r'$P$ ($\mathrm{yr}$)') pp.ylabel(r'$p(\ln P)$') pp.tight_layout() if outdir is not None: pp.savefig(op.join(outdir, 'selection.pdf')) hdr = fits.Header() hdr['COMMENT'] = 'Farr et al (2015) Figure 4' hdr['COMMENT'] = 'Posterior predictive distribution of observed periods and radii' hdr['COMMENT'] = 'from the model in that paper fit to the candidate data set.' hdr['COMMENT'] = 'Also, the inferred selection function at each of the candidates.' hdu = fits.PrimaryHDU(header=hdr) hdulist = fits.HDUList([hdu]) pers = fits.Column(name='P', format='E', unit='yr', array=all_candidates['Period']) rads = fits.Column(name='R', format='E', unit='REarth', array=all_candidates['Radius']) hdulist.append(fits.BinTableHDU.from_columns(fits.ColDefs([pers, rads]))) ids = fits.Column(name='ID', format='D', array=logpost.candidates['Kepler_ID']) pers = fits.Column(name='P', format='E', unit='yr', array=logpost.candidates['Period']) rads = fits.Column(name='R', format='E', unit='REarth', array=logpost.candidates['Radius']) pselect = fits.Column(name='psel', format='E', array=psel) hdulist.append(fits.BinTableHDU.from_columns(fits.ColDefs([ids, pers, rads, pselect]))) hdulist.writeto(op.join(outdir, 'selection.fits'), clobber=True)
# parameters you are interested in. ys = zeros(xs.shape) for mu, sigma in zip(pchain['mu'].flatten(), pchain['sigma'].flatten()): ys += ss.norm.pdf(xs, loc=mu, scale=sigma) ys /= 12000.0 # Here's the plot plot(xs, ys, '-b') xlabel(r'$\mu_j$') ylablel(r'$p\left( \mu_j \right)$') ylabel(r'$p\left( \mu_j \right)$') savefig('muj.pdf') # Now just plot some posterior distributions of the mu and sigma # parameters. pu.plot_histogram_posterior(pchain['mu'].flatten(), normed=True, histtype='step') axis(ymax=1) axis(ymax=0.2 ) axis(ymax=0.07) axis(ymax=0.06) axvline(p0['mu']) axvline(p0['mu'], color='k') xlabel(r'$\mu$') ylabel(r'$p(\mu)$') savefig('mu.pdf') pu.plot_histogram_posterior(pchain['sigma'].flatten(), normed=True, histtype='step') axis(ymax=0.07) axis(ymax=0.1) axis(ymax=0.09) axvline(p0['sigma'])