Esempio n. 1
0
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
Esempio n. 2
0
    def __init__(self, data, plot_options):
        super(TwoDimPlot, self).__init__(data, plot_options)
        opt = self.plot_options

        # If the user didn't specify bin or plot limits,
        # we find the extent of the data and use that to set them.
        extent = np.zeros(4)
        extent[0] = min(self.xdata)
        extent[1] = max(self.xdata)
        extent[2] = min(self.ydata)
        extent[3] = max(self.ydata)

        if self.plot_options.bin_limits is None:
            self.plot_options = self.plot_options._replace(
                bin_limits=[[extent[0], extent[1]], [extent[2], extent[3]]])
        if self.plot_options.plot_limits is None:
            self.plot_options = self.plot_options._replace(plot_limits=extent)

        # Posterior PDF
        if opt.kde_pdf:

            # KDE estimate of PDF
            self.pdf_data = two_dim.kde_posterior_pdf(
                self.xdata,
                self.ydata,
                self.posterior,
                bw_method=opt.bw_method,
                bin_limits=opt.bin_limits)
        else:

            # Binned estimate of PDF
            self.pdf_data = two_dim.posterior_pdf(self.xdata,
                                                  self.ydata,
                                                  self.posterior,
                                                  nbins=opt.nbins,
                                                  bin_limits=opt.bin_limits)

        # Profile likelihood
        self.prof_data = two_dim.profile_like(self.xdata,
                                              self.ydata,
                                              self.chisq,
                                              nbins=opt.nbins,
                                              bin_limits=opt.bin_limits)

        # As with the 1D plots we use raw data for the best-fit point,
        # and binned data for the mean and mode.

        # Best-fit point
        self.best_fit_x = stats.best_fit(self.chisq, self.xdata)
        self.best_fit_y = stats.best_fit(self.chisq, self.ydata)
        self.summary.append("Best-fit point (x,y): {}, {}".format(
            self.best_fit_x, self.best_fit_y))

        # Posterior mean
        self.posterior_mean_x = stats.posterior_mean(
            np.sum(self.pdf_data.pdf, axis=1), self.pdf_data.bin_centers_x)
        self.posterior_mean_y = stats.posterior_mean(
            np.sum(self.pdf_data.pdf, axis=0), self.pdf_data.bin_centers_y)
        self.summary.append("Posterior mean (x,y): {}, {}".format(
            self.posterior_mean_x, self.posterior_mean_y))

        # Posterior mode
        self.posterior_modes = two_dim.posterior_mode(*self.pdf_data)
        self.summary.append("Posterior modes/s (x,y): {}".format(
            self.posterior_modes))

        # Posterior median
        self.posterior_median_x = one_dim.posterior_median(
            np.sum(self.pdf_data.pdf, axis=1), self.pdf_data.bin_centers_x)
        self.posterior_median_y = one_dim.posterior_median(
            np.sum(self.pdf_data.pdf, axis=0), self.pdf_data.bin_centers_y)
        self.summary.append("Posterior median (x,y): {}, {}".format(
            self.posterior_median_x, self.posterior_median_y))
Esempio n. 3
0
    def __init__(self, data, plot_options):
        super(OneDimPlot, self).__init__(data, plot_options)
        opt = self.plot_options

        # If the user didn't specify bin or plot limits,
        # we find the extent of the data and use that to set them.
        extent = np.zeros(4)
        extent[0] = min(self.xdata)
        extent[1] = max(self.xdata)
        extent[2] = 0
        extent[3] = 1.2

        # Downside of using named tuple is they're immutable
        # so changing options is (justifiably) annoying.
        # If this happens a lot (it shouldn't), consider
        # using a mutable type instead...
        if self.plot_options.bin_limits is None:
            self.plot_options = self.plot_options._replace(
                bin_limits=[extent[0], extent[1]])
        if self.plot_options.plot_limits is None:
            self.plot_options = self.plot_options._replace(plot_limits=extent)

        # Posterior PDF. Norm by area if not showing profile likelihood,
        # otherwise norm max value to one.
        if opt.kde_pdf:

            # KDE estimate of PDF
            self.pdf_data = one_dim.kde_posterior_pdf(
                self.xdata,
                self.posterior,
                bin_limits=opt.bin_limits,
                norm_area=not opt.show_prof_like,
                bw_method=opt.bw_method)
        else:

            # Binned estimate of PDF
            self.pdf_data = one_dim.posterior_pdf(
                self.xdata,
                self.posterior,
                nbins=opt.nbins,
                bin_limits=opt.bin_limits,
                norm_area=not opt.show_prof_like)

        # Profile likelihood
        self.prof_data = one_dim.prof_data(self.xdata,
                                           self.chisq,
                                           nbins=opt.nbins,
                                           bin_limits=opt.bin_limits)

        # Note the best-fit point is calculated using the raw data,
        # while the mean, median and mode use the binned PDF.

        # Best-fit point
        self.best_fit = stats.best_fit(self.chisq, self.xdata)
        self.summary.append("Best-fit point: {}".format(self.best_fit))

        # Posterior mean
        self.posterior_mean = stats.posterior_mean(*self.pdf_data)
        self.summary.append("Posterior mean: {}".format(self.posterior_mean))

        # Posterior median
        self.posterior_median = one_dim.posterior_median(*self.pdf_data)
        self.summary.append("Posterior median: {}".format(
            self.posterior_median))

        # Posterior mode
        self.posterior_modes = one_dim.posterior_mode(*self.pdf_data)
        self.summary.append("Posterior mode/s: {}".format(
            self.posterior_modes))
Esempio n. 4
0
    def __init__(self, data, plot_options):
        super(TwoDimPlot, self).__init__(data, plot_options)
        opt = self.plot_options

        # If the user didn't specify bin or plot limits,
        # we find the extent of the data and use that to set them.
        extent = np.zeros(4)
        extent[0] = min(self.xdata)
        extent[1] = max(self.xdata)
        extent[2] = min(self.ydata)
        extent[3] = max(self.ydata)

        if self.plot_options.bin_limits is None:
            self.plot_options = self.plot_options._replace(bin_limits=[[extent[0], extent[1]], [extent[2], extent[3]]])
        if self.plot_options.plot_limits is None:
            self.plot_options = self.plot_options._replace(plot_limits=extent)

        # Posterior PDF
        if opt.kde_pdf:

            # KDE estimate of PDF
            self.pdf_data = two_dim.kde_posterior_pdf(
                self.xdata, self.ydata, self.posterior, bw_method=opt.bw_method, bin_limits=opt.bin_limits
            )
        else:

            # Binned estimate of PDF
            self.pdf_data = two_dim.posterior_pdf(
                self.xdata, self.ydata, self.posterior, nbins=opt.nbins, bin_limits=opt.bin_limits
            )

        # Profile likelihood
        self.prof_data = two_dim.profile_like(
            self.xdata, self.ydata, self.chisq, nbins=opt.nbins, bin_limits=opt.bin_limits
        )

        # As with the 1D plots we use raw data for the best-fit point,
        # and binned data for the mean and mode.

        # Best-fit point
        self.best_fit_x = stats.best_fit(self.chisq, self.xdata)
        self.best_fit_y = stats.best_fit(self.chisq, self.ydata)
        self.summary.append("Best-fit point (x,y): {}, {}".format(self.best_fit_x, self.best_fit_y))

        # Posterior mean
        self.posterior_mean_x = stats.posterior_mean(np.sum(self.pdf_data.pdf, axis=1), self.pdf_data.bin_centers_x)
        self.posterior_mean_y = stats.posterior_mean(np.sum(self.pdf_data.pdf, axis=0), self.pdf_data.bin_centers_y)
        self.summary.append("Posterior mean (x,y): {}, {}".format(self.posterior_mean_x, self.posterior_mean_y))

        # Posterior mode
        self.posterior_modes = two_dim.posterior_mode(*self.pdf_data)
        self.summary.append("Posterior modes/s (x,y): {}".format(self.posterior_modes))

        # Posterior median
        self.posterior_median_x = one_dim.posterior_median(
            np.sum(self.pdf_data.pdf, axis=1), self.pdf_data.bin_centers_x
        )
        self.posterior_median_y = one_dim.posterior_median(
            np.sum(self.pdf_data.pdf, axis=0), self.pdf_data.bin_centers_y
        )
        self.summary.append("Posterior median (x,y): {}, {}".format(self.posterior_median_x, self.posterior_median_y))
Esempio n. 5
0
    def __init__(self, data, plot_options):
        super(OneDimPlot, self).__init__(data, plot_options)
        opt = self.plot_options

        # If the user didn't specify bin or plot limits,
        # we find the extent of the data and use that to set them.
        extent = np.zeros(4)
        extent[0] = min(self.xdata)
        extent[1] = max(self.xdata)
        extent[2] = 0
        extent[3] = 1.2

        # Downside of using named tuple is they're immutable
        # so changing options is (justifiably) annoying.
        # If this happens a lot (it shouldn't), consider
        # using a mutable type instead...
        if self.plot_options.bin_limits is None:
            self.plot_options = self.plot_options._replace(bin_limits=[extent[0], extent[1]])
        if self.plot_options.plot_limits is None:
            self.plot_options = self.plot_options._replace(plot_limits=extent)

        # Posterior PDF. Norm by area if not showing profile likelihood,
        # otherwise norm max value to one.
        if opt.kde_pdf:

            # KDE estimate of PDF
            self.pdf_data = one_dim.kde_posterior_pdf(
                self.xdata,
                self.posterior,
                bin_limits=opt.bin_limits,
                norm_area=not opt.show_prof_like,
                bw_method=opt.bw_method,
            )
        else:

            # Binned estimate of PDF
            self.pdf_data = one_dim.posterior_pdf(
                self.xdata, self.posterior, nbins=opt.nbins, bin_limits=opt.bin_limits, norm_area=not opt.show_prof_like
            )

        # Profile likelihood
        self.prof_data = one_dim.prof_data(self.xdata, self.chisq, nbins=opt.nbins, bin_limits=opt.bin_limits)

        # Note the best-fit point is calculated using the raw data,
        # while the mean, median and mode use the binned PDF.

        # Best-fit point
        self.best_fit = stats.best_fit(self.chisq, self.xdata)
        self.summary.append("Best-fit point: {}".format(self.best_fit))

        # Posterior mean
        self.posterior_mean = stats.posterior_mean(*self.pdf_data)
        self.summary.append("Posterior mean: {}".format(self.posterior_mean))

        # Posterior median
        self.posterior_median = one_dim.posterior_median(*self.pdf_data)
        self.summary.append("Posterior median: {}".format(self.posterior_median))

        # Posterior mode
        self.posterior_modes = one_dim.posterior_mode(*self.pdf_data)
        self.summary.append("Posterior mode/s: {}".format(self.posterior_modes))