convert.add_forecast_groups()
Attach out-of-sample forecast groups to a copy of tree.
Usage
convert.add_forecast_groups(
tree,
forecast_samples,
covariates_future,
*,
time_coord=None,
covariate_dims=None
)Adds a predictions group (the forecast obs draws) and a predictions_constant_data group (the future covariates). The forecast time coordinate continues the in-sample one: integer continuation by default, or explicit values via time_coord. This is the step-by-step route for draws you produced yourself; to_datatree() attaches the same groups automatically when its covariates extend beyond data. Both routes give the forecast groups the (chain, draw) layout of the posterior and the dimension names and labels of the in-sample obs and covariates on every non-time axis, so xarray aligns the forecast with the rest of the tree draw by draw and label by label.
Parameters
tree: xarray.DataTree-
A tree from to_datatree(). The forecast continues its
observed_datatime coordinate, takes the(chain, draw)layout and labels of itsposteriorgroup, and copies the labels of the non-time axes ofobserved_data["obs"]andconstant_data["covariates"]to the same axes ofpredictionsandpredictions_constant_data. forecast_samples: Num[ArrayLike, " sample *batch future obs"]-
Forecast draws shaped
(num_samples, *batch, future, obs)from forecast(), with the layout of the tree’s in-sampleobs, one per draw of the tree’sposteriorgroup as it is now, in the order oftree["posterior"].stack(sample=("chain", "draw"))(for a tree straight from to_datatree(), the order of the flattened posterior it was built from). They are split into the(chain, draw)layout of that group; a group without achaindimension (for example aftertree.isel(chain=0)) counts as one chain. A tree with noposteriorgroup uses itsposterior_predictivegroup instead, and a tree with neither gets one pseudo-chain. Thepredictionsobstakes the dimension names and the non-time coordinate labels of the tree’sobserved_data["obs"](set byto_datatree(obs_dims=..., coords=...)), so each non-time axis must have the size of the stored one. If the tree’s storedobsis missing or has notimeaxis at-2(anobserved_datasqueezed to one series), it takes the default names of to_datatree() and no labels instead. covariates_future: Array-
Future covariates shaped
(future, covariate_dim), or any layout with time at axis-2whencovariate_dimsnames the axes, with one time step per forecast step. time_coord: Collection[Any] | None = None-
Optional explicit forecast time coordinate; defaults to integer continuation of the in-sample time. Required when the in-sample time coordinate is non-integer (e.g. datetime64): auto-continuing would have to guess the frequency, so explicit values are demanded instead. Any one-dimensional collection is accepted (a list, a NumPy array, a pandas index) and stored as
np.asarray(time_coord). A timezone-awarepd.DatetimeIndexkeeps its zone, as an object array of aware timestamps that xarray does not treat as a datetime. The forecast times must be of the same type as the in-sample ones (numbers,datetime64, or aware times in the same zone); seetime_coordof to_datatree() for the costs and the alternatives. covariate_dims: Collection[str] | None = None-
Optional dimension names for
covariates_future, one per axis, as any ordered one-dimensional collection ofstr(a list, a tuple, a NumPy array, a pandas index). When omitted, the names are inherited from the tree’sconstant_data["covariates"]variable (falling back to("time", "covariate_dim")if the tree carries no stored covariates), so the forecast covariates always share the in-sample axis names. When given explicitly, the names must match the stored ones. The future covariates also take the non-time coordinate labels of the stored covariates. See to_datatree().
Returns
xarray.DataTree-
A new tree with the
predictionsandpredictions_constant_datagroups added.
Raises
ValueError-
If
forecast_samplesdoes not have one draw per draw of the tree’s posterior (to attach fewer draws, subset the tree first, for example withtree.isel(draw=...)), if it does not have one axis per inheritedobsdimension name after the sample axis, if a non-time axis offorecast_samplesorcovariates_futurediffers in size from the same axis of the tree’s in-sampleobsor covariates, ifcovariates_futuredoes not have one time step per forecast step, iftime_coordis not a one-dimensional collection of labels (a set, a dict, a scalar, a 2-D array, ragged input) or is apd.MultiIndex, if it mixes time zones, if its time type (numbers,datetime64, aware times and their zone) differs from that of the in-sample time coordinate, ifcovariate_dimsis not an ordered one-dimensional collection (a set, a dict, a singlestr), iftime_coordis given but its length differs from the forecast horizon, or if it is omitted while the in-sample time coordinate is non-integer. TypeError-
If a name in
covariate_dimsis not astr. CovariateDimsError-
If the resolved
covariate_dims(explicit or inherited) do not name everycovariates_futureaxis, or if explicit names disagree with the dimension names already stored on the tree’sconstant_data["covariates"].