|
| 1 | +from collections.abc import Sequence, Mapping |
| 2 | + |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from bayesflow.utils import prepare_plot_data, add_titles_and_labels, prettify_subplots, compute_empirical_coverage |
| 7 | + |
| 8 | + |
| 9 | +def coverage( |
| 10 | + estimates: Mapping[str, np.ndarray] | np.ndarray, |
| 11 | + targets: Mapping[str, np.ndarray] | np.ndarray, |
| 12 | + difference: bool = False, |
| 13 | + variable_keys: Sequence[str] = None, |
| 14 | + variable_names: Sequence[str] = None, |
| 15 | + figsize: Sequence[int] = None, |
| 16 | + label_fontsize: int = 16, |
| 17 | + title_fontsize: int = 18, |
| 18 | + tick_fontsize: int = 12, |
| 19 | + color: str = "#132a70", |
| 20 | + num_col: int = None, |
| 21 | + num_row: int = None, |
| 22 | +) -> plt.Figure: |
| 23 | + """ |
| 24 | + Creates coverage plots showing empirical coverage of posterior credible intervals. |
| 25 | +
|
| 26 | + The empirical coverage shows the coverage (proportion of true variable values that fall within the interval) |
| 27 | + of the central posterior credible intervals. |
| 28 | + A well-calibrated model would have coverage exactly match interval width (i.e. 95% |
| 29 | + credible interval contains the true value 95% of the time) as shown by the diagonal line. |
| 30 | +
|
| 31 | + The coverage is accompanied by credible intervals for the coverage (gray ribbon). |
| 32 | + These are computed via the (conjugate) Beta-Binomial model for binomial proportions with a uniform prior. |
| 33 | + For more details on the Beta-Binomial model, see Chapter 2 of Bayesian Data Analysis (2013, 3rd ed.) by |
| 34 | + Gelman A., Carlin J., Stern H., Dunson D., Vehtari A., & Rubin D. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + estimates : np.ndarray of shape (num_datasets, num_post_draws, num_params) |
| 39 | + The posterior draws obtained from num_datasets |
| 40 | + targets : np.ndarray of shape (num_datasets, num_params) |
| 41 | + The true parameter values used for generating num_datasets |
| 42 | + difference : bool, optional, default: False |
| 43 | + If True, plots the difference between empirical coverage and ideal coverage |
| 44 | + (coverage - width), making deviations from ideal calibration more visible. |
| 45 | + If False, plots the standard coverage plot. |
| 46 | + variable_keys : list or None, optional, default: None |
| 47 | + Select keys from the dictionaries provided in estimates and targets. |
| 48 | + By default, select all keys. |
| 49 | + variable_names : list or None, optional, default: None |
| 50 | + The parameter names for nice plot titles. Inferred if None |
| 51 | + figsize : tuple or None, optional, default: None |
| 52 | + The figure size passed to the matplotlib constructor. Inferred if None. |
| 53 | + label_fontsize : int, optional, default: 16 |
| 54 | + The font size of the y-label and x-label text |
| 55 | + title_fontsize : int, optional, default: 18 |
| 56 | + The font size of the title text |
| 57 | + tick_fontsize : int, optional, default: 12 |
| 58 | + The font size of the axis ticklabels |
| 59 | + color : str, optional, default: '#132a70' |
| 60 | + The color for the coverage line |
| 61 | + num_row : int, optional, default: None |
| 62 | + The number of rows for the subplots. Dynamically determined if None. |
| 63 | + num_col : int, optional, default: None |
| 64 | + The number of columns for the subplots. Dynamically determined if None. |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + f : plt.Figure - the figure instance for optional saving |
| 69 | +
|
| 70 | + Raises |
| 71 | + ------ |
| 72 | + ShapeError |
| 73 | + If there is a deviation from the expected shapes of ``estimates`` and ``targets``. |
| 74 | +
|
| 75 | + """ |
| 76 | + |
| 77 | + # Gather plot data and metadata into a dictionary |
| 78 | + plot_data = prepare_plot_data( |
| 79 | + estimates=estimates, |
| 80 | + targets=targets, |
| 81 | + variable_keys=variable_keys, |
| 82 | + variable_names=variable_names, |
| 83 | + num_col=num_col, |
| 84 | + num_row=num_row, |
| 85 | + figsize=figsize, |
| 86 | + ) |
| 87 | + |
| 88 | + estimates = plot_data.pop("estimates") |
| 89 | + targets = plot_data.pop("targets") |
| 90 | + |
| 91 | + # Determine widths to compute coverage for |
| 92 | + num_draws = estimates.shape[1] |
| 93 | + widths = np.arange(0, num_draws + 2) / (num_draws + 1) |
| 94 | + |
| 95 | + # Compute empirical coverage with default parameters |
| 96 | + coverage_data = compute_empirical_coverage( |
| 97 | + estimates=estimates, |
| 98 | + targets=targets, |
| 99 | + widths=widths, |
| 100 | + prob=0.95, |
| 101 | + interval_type="central", |
| 102 | + ) |
| 103 | + |
| 104 | + # Plot coverage for each parameter |
| 105 | + for i, ax in enumerate(plot_data["axes"].flat): |
| 106 | + if i >= plot_data["num_variables"]: |
| 107 | + break |
| 108 | + |
| 109 | + width_rep = coverage_data["width_represented"][:, i] |
| 110 | + coverage_est = coverage_data["coverage_estimates"][:, i] |
| 111 | + coverage_low = coverage_data["coverage_lower"][:, i] |
| 112 | + coverage_high = coverage_data["coverage_upper"][:, i] |
| 113 | + |
| 114 | + if difference: |
| 115 | + # Compute differences for coverage difference plot |
| 116 | + diff_est = coverage_est - width_rep |
| 117 | + diff_low = coverage_low - width_rep |
| 118 | + diff_high = coverage_high - width_rep |
| 119 | + |
| 120 | + # Plot confidence ribbon |
| 121 | + ax.fill_between( |
| 122 | + width_rep, |
| 123 | + diff_low, |
| 124 | + diff_high, |
| 125 | + color="grey", |
| 126 | + alpha=0.33, |
| 127 | + label="95% Credible Interval", |
| 128 | + ) |
| 129 | + |
| 130 | + # Plot ideal coverage difference line (y = 0) |
| 131 | + ax.axhline(y=0, color="skyblue", linewidth=2.0, label="Ideal Coverage") |
| 132 | + |
| 133 | + # Plot empirical coverage difference |
| 134 | + ax.plot(width_rep, diff_est, color=color, alpha=1.0, label="Coverage Difference") |
| 135 | + |
| 136 | + # Set axis limits |
| 137 | + ax.set_xlim(0, 1) |
| 138 | + |
| 139 | + # Add legend to first subplot |
| 140 | + if i == 0: |
| 141 | + ax.legend(fontsize=tick_fontsize, loc="upper right") |
| 142 | + else: |
| 143 | + # Plot confidence ribbon |
| 144 | + ax.fill_between( |
| 145 | + width_rep, |
| 146 | + coverage_low, |
| 147 | + coverage_high, |
| 148 | + color="grey", |
| 149 | + alpha=0.33, |
| 150 | + label="95% Credible Interval", |
| 151 | + ) |
| 152 | + |
| 153 | + # Plot ideal coverage line (y = x) |
| 154 | + ax.plot([0, 1], [0, 1], color="skyblue", linewidth=2.0, label="Ideal Coverage") |
| 155 | + |
| 156 | + # Plot empirical coverage |
| 157 | + ax.plot(width_rep, coverage_est, color=color, alpha=1.0, label="Empirical Coverage") |
| 158 | + |
| 159 | + # Set axis limits |
| 160 | + ax.set_xlim(0, 1) |
| 161 | + ax.set_ylim(0, 1) |
| 162 | + |
| 163 | + # Add legend to first subplot |
| 164 | + if i == 0: |
| 165 | + ax.legend(fontsize=tick_fontsize, loc="upper left") |
| 166 | + |
| 167 | + prettify_subplots(plot_data["axes"], num_subplots=plot_data["num_variables"], tick_fontsize=tick_fontsize) |
| 168 | + |
| 169 | + # Add labels, titles, and set font sizes |
| 170 | + ylabel = "Observed coverage difference" if difference else "Observed coverage" |
| 171 | + add_titles_and_labels( |
| 172 | + axes=plot_data["axes"], |
| 173 | + num_row=plot_data["num_row"], |
| 174 | + num_col=plot_data["num_col"], |
| 175 | + title=plot_data["variable_names"], |
| 176 | + xlabel="Central interval width", |
| 177 | + ylabel=ylabel, |
| 178 | + title_fontsize=title_fontsize, |
| 179 | + label_fontsize=label_fontsize, |
| 180 | + ) |
| 181 | + |
| 182 | + plot_data["fig"].tight_layout() |
| 183 | + return plot_data["fig"] |
0 commit comments