Example #1
0
 def test_true_param_cred_value(self):
     self.assertEqual(
         e.get_true_estimator_values(e.ParamCred(0.5), self.settings), 0)
     self.assertAlmostEqual(e.get_true_estimator_values(
         e.ParamCred(0.84), self.settings),
                            9.8952257789120635e-01,
                            places=10)
Example #2
0
 def test_true_r_cred_value(self):
     self.assertTrue(
         np.isnan(e.get_true_estimator_values(e.RCred(0.84),
                                              self.settings)))
     # Test with a list argument as well to cover list version of true
     # get_true_estimator_values
     self.assertTrue(
         np.isnan(
             e.get_true_estimator_values([e.RCred(0.84)],
                                         self.settings)[0]))
Example #3
0
def plot_parameter_logx_diagram(settings, ftheta, **kwargs):
    """
    Plots parameter estimation diagram of the type described in Section 3.1 and
    shown in Figure 3 of "Sampling errors in nested sampling parameter
    estimation" (Higson et al., 2018).

    Parameters
    ----------
    settings: PerfectNSSettings object
    ftheta: estimator object
        function of parameters to plot
    ymin: float, optional
        y axis (ftheta) min.
    ymax: float, optional
        y axis (ftheta) max.
    ylabel: str, optional
        y axis (ftheta) label.
    logx_min: float, optional
        Lower limit of logx axis.
    x_points: int, optional
        How many logx points to use in the plots.
    y_points: int, optional
    figsize: tuple, optional
        Size of figure in inches.

    Returns
    -------
    fig: matplotlib figure
    """
    logx_min = kwargs.pop('logx_min', -16.0)
    figsize = kwargs.pop('figsize', (6, 2.5))
    x_points = kwargs.pop('x_points', 300)
    y_points = kwargs.pop('y_points', 300)
    ylab_def = r'$f(\theta)=' + ftheta.latex_name[1:]
    ylab_def = ylab_def.replace(r'\\overline', '').replace(r'\overline', '')
    ylabel = kwargs.pop('ylabel', ylab_def)
    # estimator specific defaults:
    ymax = kwargs.pop('ymax', 10)
    if hasattr(ftheta, 'min_value'):
        ymin = kwargs.pop('ymin', ftheta.min_value)
    else:
        ymin = kwargs.pop('ymin', -ymax)
    if kwargs:
        raise TypeError('Unexpected **kwargs: {0}'.format(kwargs))
    # Plotting settings
    max_sigma = 3.5
    contour_line_levels = [1, 2, 3]
    contour_linewidths = 0.5
    color_scheme = plt.get_cmap('Reds_r')
    contour_color_levels = np.arange(0, 4, 0.075)
    darkred = (200 / 255, 0, 0)  # match the color of the tikz evidence picture
    # Initialise figure
    # -----------------
    fig = plt.figure(figsize=figsize)
    gs = gridspec.GridSpec(2, 4, width_ratios=[1, 1, 40, 1],
                           height_ratios=[1, 4])
    gs.update(wspace=0.1, hspace=0.1)
    # plot weights
    # ------------
    weight = plt.subplot(gs[0, -2])
    x_setup = np.linspace(logx_min, 0, num=x_points)
    # Calculate posterior weight as a function of logx
    logw = settings.logl_given_logx(x_setup) + x_setup
    w_rel = np.exp(logw - logw.max())
    w_rel[0] = 0.0  # to make fill work
    w_rel[-1] = 0.0  # to make fill work
    plt.fill(x_setup, w_rel, color=darkred)
    weight.set_xticklabels([])
    weight.set_yticklabels([])
    weight.set_yticks([])
    weight.set_ylim([0.0, 1.1])
    weight.set_xlim([logx_min, 0])
    w_patch = matplotlib.patches.Patch(color=darkred,
                                       label='relative posterior mass')
    weight.legend(handles=[w_patch], loc=2 if settings.n_dim <= 4 else 1)
    # plot contours
    # -------------
    y_setup = np.linspace(ymin, ymax, num=y_points)
    x_grid, y_grid = np.meshgrid(x_setup, y_setup)
    # label with negative index for adding/removing extra posterior mean plot
    fgivenx = plt.subplot(gs[1, -2])
    cdf_fgivenx = cdf_given_logx(ftheta, y_grid, x_grid, settings)
    assert cdf_fgivenx.min() >= 0
    assert cdf_fgivenx.max() <= 1
    sigma_fgivenx = sigma_given_cdf(cdf_fgivenx)
    if not np.all(np.isinf(sigma_fgivenx)):
        # Plot the filled contours onto the axis ax
        cs1 = fgivenx.contourf(x_grid, y_grid, sigma_fgivenx,
                               cmap=color_scheme, levels=contour_color_levels,
                               vmin=0, vmax=max_sigma)
        for col in cs1.collections:  # remove annoying white lines
            col.set_edgecolor('face')
        # Plot some sigma-based contour lines
        cs2 = fgivenx.contour(
            x_grid, y_grid, sigma_fgivenx,
            colors='k',
            linewidths=contour_linewidths,
            levels=contour_line_levels, vmin=0, vmax=max_sigma
        )
    fgivenx.set_xlabel(r'$\mathrm{log} \, X $')
    fgivenx.set_yticklabels([])
    # Add ftilde if it is available
    if hasattr(ftheta, 'ftilde'):
        ftilde = ftheta.ftilde(x_setup, settings)
        fgivenx.plot(x_setup, ftilde, color='k', linestyle='--', dashes=(2, 3),
                     linewidth=1, label='$\\tilde{f}(X)$')
    # set y limits as grid goes below limits to make cdf to pdf calc work
    fgivenx.set_ylim([ymin, ymax])
    fgivenx.set_xlim([logx_min, 0])
    # plot posterior
    # --------------
    post_cdf, support = posterior_cdf(ftheta, y_setup, x_setup, settings)
    x_posterior, y_posterior = np.meshgrid(np.linspace(0, 1, num=2),
                                           support)
    z_posterior = sigma_given_cdf(np.vstack((post_cdf, post_cdf)).T)
    posterior = plt.subplot(gs[1, -3])
    # Plot the filled contours onto the axis ax
    cs1 = posterior.contourf(
        x_posterior, y_posterior, z_posterior,
        cmap=color_scheme,
        levels=contour_color_levels, vmin=0, vmax=max_sigma
    )
    for col in cs1.collections:  # remove annoying white lines
        col.set_edgecolor('face')
    # Plot some sigma-based contour lines
    cs2 = posterior.contour(x_posterior, y_posterior, z_posterior, colors='k',
                            linewidths=contour_linewidths,
                            levels=contour_line_levels, vmin=0, vmax=max_sigma)
    # add the posterior expectation if it exists
    posterior_mean = e.get_true_estimator_values(ftheta, settings)
    if not np.isnan(posterior_mean):
        fgivenx.plot([logx_min, 0], [posterior_mean, posterior_mean],
                     color='k', linestyle=':', dashes=(0.5, 1), linewidth=1,
                     label=r'$\mathrm{E}[f(\theta)|\mathcal{L},\pi]$')
        posterior.plot([0, 1], [posterior_mean, posterior_mean], color='k',
                       linestyle=':', linewidth=1,
                       label=r'$\mathrm{E}[f(\theta)|\mathcal{L},\pi]$')
        fgivenx.legend(loc=2)
    posterior.set_xticklabels([])
    posterior.set_xticks([])
    # add labelpad to avoid clash with xtick labels
    posterior.set_xlabel('posterior', labelpad=18)
    posterior.set_ylabel(ylabel)
    posterior.set_ylim([ymin, ymax])
    # plot Colorbar key
    # ----------------
    # place in seperate subplot
    colorbar_plot = plt.subplot(gs[1, -1])
    colorbar = plt.colorbar(cs1, cax=colorbar_plot, ticks=[1, 2, 3])
    colorbar.solids.set_edgecolor('face')
    colorbar.ax.set_yticklabels(
        [r'$1\sigma$', r'$2\sigma$', r'$3\sigma$'])
    colorbar.add_lines(cs2)
    return fig
Example #4
0
 def test_true_r_mean_value(self):
     self.assertAlmostEqual(e.get_true_estimator_values(
         e.RMean(), self.settings),
                            1.2470645289408879e+00,
                            places=10)
Example #5
0
 def test_true_param_mean_squared_value(self):
     self.assertAlmostEqual(e.get_true_estimator_values(
         e.ParamSquaredMean(), self.settings),
                            9.9009851517647807e-01,
                            places=10)
Example #6
0
 def test_true_param_mean_value(self):
     self.assertEqual(
         e.get_true_estimator_values(e.ParamMean(), self.settings), 0)
Example #7
0
 def test_true_z_value(self):
     self.assertAlmostEqual(e.get_true_estimator_values(
         e.Z(), self.settings),
                            1.5757915157613399e-03,
                            places=10)
Example #8
0
 def test_true_logz_value(self):
     self.assertAlmostEqual(e.get_true_estimator_values(
         e.LogZ(), self.settings),
                            -6.4529975832506050,
                            places=10)