arviz_stats.weight_predictions

arviz_stats.weight_predictions#

arviz_stats.weight_predictions(dts, weights=None, group='posterior_predictive', sample_dims=None, random_seed=None)[source]#

Generate weighted posterior predictive samples.

Parameters:
dtslist[xarray.DataTree] or list[InferenceData]

Elements in the list should contain the groups posterior_predictive and observed_data. Observations should be the same for all models.

weightsarray_like, optional

Individual weights for each model. Weights should be positive. If they do not sum up to 1, they will be normalized. Default, same weight for each model. Weights can be computed using many different methods including those in arviz.compare.

groupstr, optional

Group from which to extract predictions to weight. Default to posterior_predictive.

sample_dimsiterable of hashable, optional

Dimensions to be considered sample dimensions and are to be reduced. Default rcParams["data.sample_dims"].

random_seedint, optional

Random seed for reproducibility. Default is None.

Returns:
dt: xarray.DataTree

DataTree object with the groups posterior_predictive and observed_data.

See also

compare

Compare models based on PSIS-LOO-CV loo

Examples

In [1]: import arviz_base as azb
   ...: import arviz_stats as azs
   ...: m0 = azb.load_arviz_data("crabs_poisson")
   ...: m1 = azb.load_arviz_data("crabs_hurdle_nb")
   ...: model_dict = {"m0": m0, "m1": m1}
   ...: comp = azs.compare({"m0": m0, "m1": m1})
   ...: azs.weight_predictions(
   ...:     [model_dict[name] for name in comp.index],
   ...:     weights=comp["weight"],
   ...: )
   ...: 
Out[1]: 
<xarray.DataTree>
Group: /
├── Group: /posterior_predictive
│       Dimensions:    (__obs__: 173, sample: 4000)
│       Coordinates:
│         * __obs__    (__obs__) int64 1kB 0 1 2 3 4 5 6 ... 166 167 168 169 170 171 172
│         * sample     (sample) object 32kB MultiIndex
│         * chain      (sample) int64 32kB 3 0 1 1 1 2 2 1 2 2 3 ... 1 2 0 1 3 0 2 0 0 3
│         * draw       (sample) int64 32kB 761 957 350 951 323 ... 267 893 280 145 315
│       Data variables:
│           satellite  (__obs__, sample) int64 6MB 0 2 5 0 0 0 0 1 0 ... 2 1 2 1 1 2 6 3
└── Group: /observed_data
        Dimensions:    (__obs__: 173)
        Coordinates:
          * __obs__    (__obs__) int64 1kB 0 1 2 3 4 5 6 ... 166 167 168 169 170 171 172
        Data variables:
            satellite  (__obs__) int64 1kB 8 0 9 0 4 0 0 0 0 0 0 ... 0 7 0 0 2 3 4 0 0 0
        Attributes:
            created_at:                  2025-04-03T07:20:40.992917+00:00
            arviz_version:               0.21.0
            inference_library:           pymc
            inference_library_version:   5.21.1
            modeling_interface:          bambi
            modeling_interface_version:  0.12.1.dev79+g649a304a.d20250403