arviz.apply_test_function(idata, func, group='both', var_names=None, pointwise=False, out_data_shape=None, out_pp_shape=None, out_name_data='T', out_name_pp=None, func_args=None, func_kwargs=None, ufunc_kwargs=None, wrap_data_kwargs=None, wrap_pp_kwargs=None, inplace=True, overwrite=None)[source]

Apply a Bayesian test function to an InferenceData object.

idata: InferenceData

arviz.InferenceData object on which to apply the test function. This function will add new variables to the InferenceData object to store the result without modifying the existing ones.

func: callable

Callable that calculates the test function. It must have the following call signature func(y, theta, *args, **kwargs) (where y is the observed data or posterior predictive and theta the model parameters) even if not all the arguments are used.

group: str, optional

Group on which to apply the test function. Can be observed_data, posterior_predictive or both.

var_names: dict group -> var_names, optional

Mapping from group name to the variables to be passed to func. It can be a dict of strings or lists of strings. There is also the option of using both as key, in which case, the same variables are used in observed data and posterior predictive groups

pointwise: bool, optional

If True, apply the test function to each observation and sample, otherwise, apply test function to each sample.

out_data_shape, out_pp_shape: tuple, optional

Output shape of the test function applied to the observed/posterior predictive data. If None, the default depends on the value of pointwise.

out_name_data, out_name_pp: str, optional

Name of the variables to add to the observed_data and posterior_predictive datasets respectively. out_name_pp can be None, in which case will be taken equal to out_name_data.

func_args: sequence, optional

Passed as is to func

func_kwargs: mapping, optional

Passed as is to func

wrap_data_kwargs, wrap_pp_kwargs: mapping, optional

kwargs passed to wrap_xarray_ufunc(). By default, some suitable input_core_dims are used.

inplace: bool, optional

If True, add the variables inplace, otherwise, return a copy of idata with the variables added.

overwrite: bool, optional

Overwrite data in case out_name_data or out_name_pp are already variables in dataset. If None it will be the opposite of inplace.

idata: InferenceData

Output InferenceData object. If inplace=True, it is the same input object modified inplace.

See also


Plot Bayesian p-value for observed data and Posterior/Prior predictive.


This function is provided for convenience to wrap scalar or functions working on low dims to inference data object. It is not optimized to be faster nor as fast as vectorized computations.


Use apply_test_function to wrap numpy.min for illustration purposes. And plot the results.

>>> import arviz as az
>>> idata = az.load_arviz_data("centered_eight")
>>> az.apply_test_function(idata, lambda y, theta: np.min(y))
>>> T = np.asscalar(idata.observed_data.T)
>>> az.plot_posterior(idata, var_names=["T"], group="posterior_predictive", ref_val=T)