VariationalPosterior.to_arviz

VariationalPosterior.to_arviz#

VariationalPosterior.to_arviz(n_samples=1000, *, var_names=None, orig_flag=True, variables=None, dims=None, coords=None)[source]#

Export independent posterior draws as an ArviZ DataTree.

Parameters:
n_samplesint, optional

Positive number of independent draws, default 1000, as any scalar that holds a whole number (see sample); a boolean is refused.

var_namessequence of str, optional

Unique names for the D scalar parameters, default x_0, x_1, and so on. Names must differ from chain and draw. Cannot be used with variables.

orig_flagbool, optional

Draw in original coordinates (default True), or in the internal unconstrained coordinates if False.

variablesmapping of str to int or sequence of int, optional

Structured variable layout. Mapping order assigns consecutive columns of each draw to each variable. An integer defines a one-dimensional shape and () defines a scalar. Variable sizes must sum to D.

dimsmapping of str to sequence of str, optional

Event dimension names for structured variables. Each sequence must have one name per variable axis. Requires variables.

coordsmapping of str to one-dimensional array-like, optional

Coordinate values for structured dimensions. Each coordinate must match the length of every axis using that dimension. Requires variables.

Returns:
dataxarray.DataTree

A posterior group with one chain. Without variables, it has one scalar variable per parameter. Structured variables retain their event dimensions after chain and draw.

Raises:
ImportError

If Python is older than 3.12 or ArviZ’s current data API is absent; use Python 3.12+ and install pyvbmc[arviz].

ValueError

If the sample count, names, structured layout, dimensions, or coordinates are invalid.

Notes

This method advances self.rng exactly as a call to self.sample(n_samples, orig_flag=orig_flag) does. It creates no likelihood or sampler-diagnostic groups. These are draws from a variational approximation: MCMC diagnostics on them do not assess how well the approximation represents the target posterior.

A structured layout takes consecutive sample columns in mapping order and reshapes each block in C order. The exported arrays are independent of the posterior’s parameter arrays afterwards. The posterior group’s parameter_space attribute is "original" or "internal" according to orig_flag; "model" denotes exports assembled in model-variable coordinates by an integration.

See ArviZ DataTree for a short example and installation details in Optional integrations. The worked PyVBMC Example 9: Torch and JAX models and posterior exports shows posterior summaries for a fitted model.