arviz.psens#
- arviz.psens(data, *, component='prior', component_var_names=None, component_coords=None, var_names=None, coords=None, filter_vars=None, delta=0.01, dask_kwargs=None)[source]#
Compute power-scaling sensitivity diagnostic.
Power-scales the prior or likelihood and calculates how much the posterior is affected.
- Parameters:
- data
obj Any object that can be converted to an
arviz.InferenceDataobject. Refer to documentation ofarviz.convert_to_dataset()for details. For ndarray: shape = (chain, draw). For n-dimensional ndarray transform first to dataset withaz.convert_to_dataset.- component{“prior”, “likelihood”}, default “prior”
When
componentis “likelihood”, the log likelihood values are retrieved from thelog_likelihoodgroup as pointwise log likelihood and added together. With “prior”, the log prior values are retrieved from thelog_priorgroup.- component_var_names
str, optional Name of the prior or log likelihood variables to use
- component_coords
dict, optional Coordinates defining a subset over the component element for which to compute the prior sensitivity diagnostic.
- var_names
listofstr, optional Names of posterior variables to include in the power scaling sensitivity diagnostic
- coords
dict, optional Coordinates defining a subset over the posterior. Only these variables will be used when computing the prior sensitivity.
- filter_vars: {None, “like”, “regex”}, default None
If
None(default), interpret var_names as the real variables names. If “like”, interpret var_names as substrings of the real variables names. If “regex”, interpret var_names as regular expressions on the real variables names.- delta
float Value for finite difference derivative calculation.
- dask_kwargs
dict, optional Dask related kwargs passed to
wrap_xarray_ufunc().
- data
- Returns:
xarray.DatasetReturns dataset of power-scaling sensitivity diagnostic values. Higher sensitivity values indicate greater sensitivity. Prior sensitivity above 0.05 indicates informative prior. Likelihood sensitivity below 0.05 indicates weak or nonin-formative likelihood.
Notes
The diagnostic is computed by power-scaling the specified component (prior or likelihood) and determining the degree to which the posterior changes as described in [1]. It uses Pareto-smoothed importance sampling to avoid refitting the model.
References
[1]Kallioinen et al, Detecting and diagnosing prior and likelihood sensitivity with power-scaling, 2022, https://arxiv.org/abs/2107.14054
Examples
Compute the likelihood sensitivity for the non centered eight model:
In [1]: import arviz as az ...: data = az.load_arviz_data("non_centered_eight") ...: az.psens(data, component="likelihood") ...: Out[1]: <xarray.Dataset> Size: 656B Dimensions: (school: 8) Coordinates: * school (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon' Data variables: mu float64 8B 0.09673 theta_t (school) float64 64B 0.03804 0.01805 0.01727 ... 0.04732 0.007672 tau float64 8B 0.03095 theta (school) float64 64B 0.07747 0.08618 0.03315 ... 0.08787 0.03992 Attributes: created_at: 2022-10-13T14:37:26.351883 arviz_version: 0.13.0.dev0 inference_library: pymc inference_library_version: 4.2.2 sampling_time: 4.738754749298096 tuning_steps: 1000
To compute the prior sensitivity, we need to first compute the log prior at each posterior sample. In our case, we know mu has a normal prior \(N(0, 5)\), tau is a half cauchy prior with scale/beta parameter 5, and theta has a standard normal as prior. We add this information to the
log_priorgroup before computing powerscaling check withpsensIn [2]: from xarray_einstats.stats import XrContinuousRV ...: from scipy.stats import norm, halfcauchy ...: post = data.posterior ...: log_prior = { ...: "mu": XrContinuousRV(norm, 0, 5).logpdf(post["mu"]), ...: "tau": XrContinuousRV(halfcauchy, scale=5).logpdf(post["tau"]), ...: "theta_t": XrContinuousRV(norm, 0, 1).logpdf(post["theta_t"]), ...: } ...: data.add_groups({"log_prior": log_prior}) ...: az.psens(data, component="prior") ...: Out[2]: <xarray.Dataset> Size: 656B Dimensions: (school: 8) Coordinates: * school (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon' Data variables: mu float64 8B 0.1106 theta_t (school) float64 64B 0.1282 0.08115 0.06867 ... 0.1025 0.08952 tau float64 8B 0.06421 theta (school) float64 64B 0.1684 0.07522 0.06509 ... 0.1197 0.09038 Attributes: created_at: 2022-10-13T14:37:26.351883 arviz_version: 0.13.0.dev0 inference_library: pymc inference_library_version: 4.2.2 sampling_time: 4.738754749298096 tuning_steps: 1000