def _summary(name, param, posterior, chi_sq): """ Find summary statistics for a single parameter. :param name: Name of parameter :type name: string :param param: Data column of parameter :type param: :param posterior: :type posterior: :param chi_sq: :type chi_sq: :returns: List of summary statistics for a particular parameter :rtype: list """ # Best-fit point bestfit = stats.best_fit(chi_sq, param) # Posterior mean post_mean = stats.posterior_mean(posterior, param) # Credible regions pdf_data = one_dim.posterior_pdf(param, posterior, nbins=default("nbins"), bin_limits=default("bin_limits") ) lower_credible_region = one_dim.credible_region(pdf_data.pdf, pdf_data.bin_centers, alpha=default("alpha")[1], region="lower") upper_credible_region = one_dim.credible_region(pdf_data.pdf, pdf_data.bin_centers, alpha=default("alpha")[1], region="upper") summary = [name, bestfit, post_mean, lower_credible_region, upper_credible_region ] return summary
def figure(self): fig, ax = self._new_plot() opt = self.plot_options summary = [] # Autoscale the y-axis. ax.autoscale(axis='y') # Posterior PDF. Norm by area if not showing profile likelihood, # otherwise norm max value to one. pdf_data = one_dim.posterior_pdf( self.xdata, self.posterior, nbins=opt.nbins, bin_limits=opt.bin_limits, norm_area=False if opt.show_prof_like else True) if opt.show_posterior_pdf: pm.plot_data(pdf_data.bin_centers, pdf_data.pdf, schemes.posterior) # Profile likelihood prof_data = one_dim.prof_data(self.xdata, self.chisq, nbins=opt.nbins, bin_limits=opt.bin_limits) if opt.show_prof_like: pm.plot_data(prof_data.bin_centers, prof_data.prof_like, schemes.prof_like) # Height to plot the best-fit and posterior-mean points point_height = 0.02 * pdf_data.pdf.max() # Best-fit point best_fit = stats.best_fit(self.chisq, self.xdata) summary.append("Best-fit point: {}".format(best_fit)) if opt.show_best_fit: pm.plot_data(best_fit, point_height, schemes.best_fit) # Posterior mean posterior_mean = stats.posterior_mean(self.posterior, self.xdata) summary.append("Posterior mean: {}".format(posterior_mean)) if opt.show_posterior_mean: pm.plot_data(posterior_mean, point_height, schemes.posterior_mean) # Credible region lower_credible_region = [ one_dim.credible_region(pdf_data.pdf, pdf_data.bin_centers, alpha=aa, region="lower") for aa in opt.alpha ] upper_credible_region = [ one_dim.credible_region(pdf_data.pdf, pdf_data.bin_centers, alpha=aa, region="upper") for aa in opt.alpha ] summary.append( "Lower credible region: {}".format(lower_credible_region)) summary.append( "Upper credible region: {}".format(upper_credible_region)) if opt.show_credible_regions: # Plot the credible region line @ +10% of the max PDF value cr_height = 1.1 * pdf_data.pdf.max() for lower, upper, scheme in zip(lower_credible_region, upper_credible_region, schemes.credible_regions): pm.plot_data([lower, upper], [cr_height, cr_height], scheme) # Confidence interval conf_intervals = [ one_dim.conf_interval(prof_data.prof_chi_sq, prof_data.bin_centers, alpha=aa) for aa in opt.alpha ] for intervals, scheme in zip(conf_intervals, schemes.conf_intervals): if opt.show_conf_intervals: # Plot the CI line @ the max PDF value pm.plot_data(intervals, [pdf_data.pdf.max()] * int(opt.nbins), scheme) summary.append("{}:".format(scheme.label)) for interval in intervals: summary.append(str(interval)) # Add plot legend pm.legend(opt.leg_title, opt.leg_position) # Override y-axis label. This prevents the y axis from taking its # label from the 'y-axis variable' selction in the GUI. if opt.show_posterior_pdf and not opt.show_prof_like: plt.ylabel(schemes.posterior.label) elif opt.show_prof_like and not opt.show_posterior_pdf: plt.ylabel(schemes.prof_like.label) else: plt.ylabel("") return self.plot_data(figure=fig, summary=summary)
def figure(self): fig, ax = self._new_plot() opt = self.plot_options summary = [] # Autoscale the y-axis. ax.autoscale(axis='y') # Posterior PDF. Norm by area if not showing profile likelihood, # otherwise norm max value to one. pdf_data = one_dim.posterior_pdf(self.xdata, self.posterior, nbins=opt.nbins, bin_limits=opt.bin_limits, norm_area= False if opt.show_prof_like else True) if opt.show_posterior_pdf: pm.plot_data(pdf_data.bin_centers, pdf_data.pdf, schemes.posterior) # Profile likelihood prof_data = one_dim.prof_data(self.xdata, self.chisq, nbins=opt.nbins, bin_limits=opt.bin_limits) if opt.show_prof_like: pm.plot_data(prof_data.bin_centers, prof_data.prof_like, schemes.prof_like) # Height to plot the best-fit and posterior-mean points point_height = 0.02 * pdf_data.pdf.max() # Best-fit point best_fit = stats.best_fit(self.chisq, self.xdata) summary.append("Best-fit point: {}".format(best_fit)) if opt.show_best_fit: pm.plot_data(best_fit, point_height, schemes.best_fit) # Posterior mean posterior_mean = stats.posterior_mean(self.posterior, self.xdata) summary.append("Posterior mean: {}".format(posterior_mean)) if opt.show_posterior_mean: pm.plot_data(posterior_mean, point_height, schemes.posterior_mean) # Credible region lower_credible_region = [one_dim.credible_region(pdf_data.pdf, pdf_data.bin_centers, alpha=aa, region="lower") for aa in opt.alpha] upper_credible_region = [one_dim.credible_region(pdf_data.pdf, pdf_data.bin_centers, alpha=aa, region="upper") for aa in opt.alpha] summary.append("Lower credible region: {}".format(lower_credible_region)) summary.append("Upper credible region: {}".format(upper_credible_region)) if opt.show_credible_regions: # Plot the credible region line @ +10% of the max PDF value cr_height = 1.1 * pdf_data.pdf.max() for lower, upper, scheme in zip(lower_credible_region, upper_credible_region, schemes.credible_regions): pm.plot_data([lower, upper], [cr_height, cr_height], scheme) # Confidence interval conf_intervals = [one_dim.conf_interval(prof_data.prof_chi_sq, prof_data.bin_centers, alpha=aa) for aa in opt.alpha] for intervals, scheme in zip(conf_intervals, schemes.conf_intervals): if opt.show_conf_intervals: # Plot the CI line @ the max PDF value pm.plot_data(intervals, [pdf_data.pdf.max()] * int(opt.nbins), scheme) summary.append("{}:".format(scheme.label)) for interval in intervals: summary.append(str(interval)) # Add plot legend pm.legend(opt.leg_title, opt.leg_position) # Override y-axis label. This prevents the y axis from taking its # label from the 'y-axis variable' selction in the GUI. if opt.show_posterior_pdf and not opt.show_prof_like: plt.ylabel(schemes.posterior.label) elif opt.show_prof_like and not opt.show_posterior_pdf: plt.ylabel(schemes.prof_like.label) else: plt.ylabel("") return self.plot_data(figure=fig, summary=summary)