arviz_stats.bayes_factor

Contents

arviz_stats.bayes_factor#

arviz_stats.bayes_factor(data, var_names, ref_vals=0, return_ref_vals=False, prior=None, circular=False)[source]#

Compute Bayes factor using Savage–Dickey ratio.

Parameters:
dataxarray.DataTree, or InferenceData

The data object containing the posterior and optionally the prior distributions.

var_namesstr or list of str

Names of the variables for which the Bayes factor should be computed.

ref_valsfloat or list of float, default 0

Reference value for each variable. Must match var_names in length if list.

return_ref_valsbool, default False

If True, return the reference density values for the posterior and prior.

priordict, optional

Dictionary with prior distributions for each variable of interest. If not provided, the prior will be taken from the prior group in the data object.

circularbool, default False

Whether the variables are circular (e.g. angles). This affects KDE computation, which is used to estimate the density at the reference value.

Returns:
xr.Dataset

Dataset with one variable per requested variable. Each DataArray has a bf_type dimension with coordinates ["BF10", "BF01"], plus any non-sample coordinates of the original variable (e.g. school).

References

[1]

Heck DW. A caveat on the Savage-Dickey density ratio: The case of computing Bayes factors for regression parameters. Br J Math Stat Psychol, 72. (2019) https://doi.org/10.1111/bmsp.12150

Examples

Compute Bayes factor for a home and intercept variable in a rugby dataset using a reference value of 0.15 for home and 3 for intercept.

In [1]: from arviz_base import load_arviz_data
   ...: from arviz_stats import bayes_factor
   ...: dt = load_arviz_data("rugby")
   ...: bayes_factor(dt, var_names=["home", "intercept"], ref_vals=[0.15, 3])
   ...: 
Out[1]: 
<xarray.Dataset> Size: 64B
Dimensions:    (bf_type: 2)
Coordinates:
  * bf_type    (bf_type) <U4 32B 'BF10' 'BF01'
Data variables:
    home       (bf_type) float64 16B 0.04563 21.92
    intercept  (bf_type) float64 16B 1.513 0.6611