bayesflow.sensitivity module#

bayesflow.sensitivity.misspecification_experiment(trainer, generator, first_config_dict, second_config_dict, error_function=<function aggregated_error>, n_posterior_samples=500, n_sim=200, configurator=None)[source]#

Performs a systematic sensitivity analysis with regard to two misspecification factors across different values of the factors provided in the config dictionaries.

Parameters:
trainerbayesflow.trainers.Trainer

A Trainer instance (usually after converged training).

generatorcallable with signature p1: float, p2, float -> simulation.GenerativeModel

A callable that takes two misspecification factors and returns a generative model for forward sampling responsible for generating n_sim simulations.

first_config_dictdict

Configuration for the first misspecification factor fields: name (str), values (1D np.ndarray)

second_config_dictdict

Configuration for the second misspecification factor fields: name (str), values (1D np.ndarray)

error_functioncallable, default: bayesflow.computational_utilities.aggregated_error

A callable that computes an error metric on the approximate posterior samples

n_posterior_samplesint, optional, default: 500

Number of samples from the approximate posterior per data set

n_simint, optional, default: 200

Number of simulated data sets per configuration

configuratorcallable or None, optional, default: None

An optional configurator for the misspecified simulations. If None provided (default), Trainer.configurator will be used.

Returns:
posterior_error_dict: {P1, P2, value} - dictionary with misspecification grid (P1, P2) and posterior error results (values)
summary_mmd: {P1, P2, values} - dictionary with misspecification grid (P1, P2) and summary MMD results (values)
bayesflow.sensitivity.plot_model_misspecification_sensitivity(results_dict, first_config_dict, second_config_dict, plot_config=None)[source]#

Visualizes the results from a sensitivity analysis via a colored 2D grid.

Parameters:
results_dictdict

The results from sensitivity.misspecification_experiment(), Alternatively, a dictionary with mandatory keys: P1, P2, values

first_config_dictdict

see parameter first_config_dict in sensitivity.misspecification_experiment() Important: Needs additional key well_specified_value

second_config_dictdict

see parameter second_config_dict in sensitivity.misspecification_experiment() Important: Needs additional key well_specified_value

plot_configdict or None, optional, default: None

Optional plot configuration dictionary, fields: xticks, yticks, vmin, vmax, cmap, cbar_title

Returns:
fplt.Figure - the figure instance for optional saving
bayesflow.sensitivity.plot_color_grid(x_grid, y_grid, z_grid, cmap='viridis', vmin=None, vmax=None, xlabel='x', ylabel='y', cbar_title='z', xticks=None, yticks=None, hline_location=None, vline_location=None)[source]#

Plots a 2-dimensional color grid.

Parameters:
x_gridnp.ndarray

meshgrid of x values

y_gridnp.ndarray

meshgrid of y values

z_gridnp.ndarray

meshgrid of z values (coded by color in the plot)

cmapstr, default: viridis

color map for the fill

vminfloat, default: None

lower limit of the color map, None results in dynamic limit

vmaxfloat, default: None

upper limit of the color map, None results in dynamic limit

xlabelstr, default: x

x label text

ylabelstr, default: y

y label text

cbar_titlestr, default: z

title of the color bar legend

xtickslist, default: None

list of x ticks, None results in dynamic ticks

ytickslist, default: None

list of y ticks, None results in dynamic ticks

hline_locationfloat, default: None

(optional) horizontal dashed line

vline_locationfloat, default: None

(optional) vertical dashed line

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