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:
dataobj

Any object that can be converted to an arviz.InferenceData object. Refer to documentation of arviz.convert_to_dataset() for details. For ndarray: shape = (chain, draw). For n-dimensional ndarray transform first to dataset with az.convert_to_dataset.

component{“prior”, “likelihood”}, default “prior”

When component is “likelihood”, the log likelihood values are retrieved from the log_likelihood group as pointwise log likelihood and added together. With “prior”, the log prior values are retrieved from the log_prior group.

component_var_namesstr, optional

Name of the prior or log likelihood variables to use

component_coordsdict, optional

Coordinates defining a subset over the component element for which to compute the prior sensitivity diagnostic.

var_nameslist of str, optional

Names of posterior variables to include in the power scaling sensitivity diagnostic

coordsdict, 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.

deltafloat

Value for finite difference derivative calculation.

dask_kwargsdict, optional

Dask related kwargs passed to wrap_xarray_ufunc().

Returns:
xarray.Dataset

Returns 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_prior group before computing powerscaling check with psens

In [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