plot_quantity#

bayesflow.diagnostics.plot_quantity(values: Mapping[str, ndarray] | ndarray | Callable, targets: Mapping[str, ndarray] | ndarray, *, variable_keys: Sequence[str] = None, variable_names: Sequence[str] = None, estimates: Mapping[str, ndarray] | ndarray | None = None, test_quantities: dict[str, Callable] = None, figsize: Sequence[int] = None, label_fontsize: int = 16, title_fontsize: int = 18, tick_fontsize: int = 12, color: str = '#132a70', markersize: float = 25.0, marker: str = 'o', alpha: float = 0.5, xlabel: str = 'Ground truth', ylabel: str = '', num_col: int = None, num_row: int = None, default_name: str = 'v') Figure[source]#

Plot a quantity as a function of a variable for each variable key.

The function supports the following different combinations to pass or compute the values:

  1. pass values as an array of shape (num_datasets,) or (num_datasets, num_variables)

  2. pass values as a dictionary with the keys ‘values’, ‘metric_name’ and ‘variable_names’ as provided by the metrics functions. Note that the functions have to be called without aggregation to obtain value per dataset.

  3. pass a function to values, as well as estimates. The function should have the signature fn(estimates, targets, [aggregation]) and return an object like the values described in the previous options.

Parameters:
valuesdict[str, np.ndarray] | np.ndarray | Callable,

The value of the quantity to plot. One of the following:

  1. an array of shape (num_datasets,) or (num_datasets, num_variables)

  2. a dictionary with the keys ‘values’, ‘metric_name’ and ‘variable_names’ as provided by the metrics functions. Note that the functions have to be called without aggregation to obtain value per dataset.

  3. a callable, requires passing estimates as well. The function should have the signature fn(estimates, targets, [aggregation]) and return an object like the ones described in the previous options.

targetsdict[str, np.ndarray] | np.ndarray,

The parameter values plotted on the axis.

variable_keyslist or None, optional, default: None

Select keys from the dictionary provided in samples. By default, select all keys.

variable_nameslist or None, optional, default: None

The parameter names for nice plot titles. Inferred if None

estimatesnp.ndarray of shape (n_data_sets, n_post_draws, n_params), optional, default: None

The posterior draws obtained from n_data_sets. Can only be supplied if values is of type Callable.

test_quantitiesdict or None, optional, default: None

A dict that maps plot titles to functions that compute test quantities based on estimate/target draws. Can only be supplied if values is a function.

The dict keys are automatically added to variable_keys and variable_names. Test quantity functions are expected to accept a dict of draws with shape (batch_size, ...) as the first (typically only) positional argument and return an NumPy array of shape (batch_size,). The functions do not have to deal with an additional sample dimension, as appropriate reshaping is done internally.

figsizetuple or None, optional, defaultNone

The figure size passed to the matplotlib constructor. Inferred if None.

label_fontsizeint, optional, default: 16

The font size of the y-label text

title_fontsizeint, optional, default: 18

The font size of the title text

tick_fontsizeint, optional, default: 12

The font size of the axis ticklabels

colorstr, optional, default: ‘#8f2727’

The color for the true vs. estimated scatter points and error bars

markersizefloat, optional, default: 25.0

The marker size in points**2 for the scatter plot.

markerstr, optional, default: ‘o’

The marker for the scatter plot.

alphafloat, default: 0.5

The opacity for the scatter plot

num_rowint, optional, default: None

The number of rows for the subplots. Dynamically determined if None.

num_colint, optional, default: None

The number of columns for the subplots. Dynamically determined if None.

default_namestr, optional (default = “v”)

The default name to use for estimates if None provided

Returns:
fplt.Figure - the figure instance for optional saving
Raises:
ShapeError

If there is a deviation from the expected shapes of estimates and targets.