predictive.predict_in_sample()
Sample the in-sample posterior predictive of the obs site.
Usage
predictive.predict_in_sample(
rng_key,
model,
posterior,
covariates,
*,
batch_size=None,
parallel=True,
device=None
)Runs Predictive with the in-sample covariates and the supplied posterior latent draws. Unlike forecast() there is no forecast horizon: covariates span only the observed window, so the model’s obs site is sampled at every step. The number of predictive samples equals the leading (sample) axis of posterior (see draw_posterior()). The model gets data=None, so the draws have the axes that the model gives obs, and the driver has no data to check them against: a predictor shared by n series without a size-n plate at dim=-1 draws one column. forecast() and to_datatree() get data and raise ValueError in that case.
Parameters
rng_key: Array-
PRNG key.
model: ForecastModel-
The forecasting model callable (the same one that produced
posterior). posterior: Mapping[str, ArrayLike]-
Posterior samples of the latent sites of the training run, sample axis leading (
mcmc.get_samples()of the fit, or draw_posterior()). The output of adevice="host"stage (CPU-committed jax leaves or NumPy leaves) is accepted directly. Forecast-horizon keys follow the rule of forecast(): one with values raisesValueError, because it marks the output of anumpyro.infer.Predictivecall over a horizon, and one with an empty leaf is dropped. An"obs"key is dropped too, so the draws come from the latent sites:Predictivesubstitutes every posterior sample site by name, and an"obs"key (the output of aPredictivecall withdata, or of a prior predictive call) would make every draw equal to it. Only"obs"is dropped: keep the other sites that the model observes when it getsdata(a second likelihood) out ofposterior, or they are substituted. covariates: Array-
Covariates with time at axis
-2spanning the observed window. Its time length must match the data theposteriorwas fit on, since the in-sample latent sites are sized to that window. An innovations() site of another length raisesValueErrorhowever the model slices its"obs"site. For other latents the driver checks only that the"obs"draws have one step per row, and a markov_series() latent replays a shorter posterior as a prefix and draws the remaining steps from the prior (a longer one raises NumPyro’sRuntimeError). batch_size: int | None = None-
Optional chunk size for sampling (caps peak memory).
parallel: bool = True-
Whether
Predictivevectorizes over the sample axis withvmap(True, faster, higher peak memory) or maps it serially withlax.map(False). See forecast() for how this interacts withbatch_size. device: jax.Device | str | None = None-
Where each chunk of draws is placed as soon as it is drawn and where the stitched result lives; the same placement contract as the
deviceargument of draw_posterior() ("host"for pageable host memory as a CPU-committedjax.Array, or a NumPy array when no CPU backend is initialized;"numpy";"pinned_host"; ajax.Deviceor platform name;Nonefor the default device), including its mixing rules for host-committed results. Withbatch_sizeset on an accelerator, any host target bounds accelerator memory by a single chunk instead of the full(sample, time, obs)array; the draw values are unchanged, only where the result lives. The bound requiresbatch_sizestrictly below the sample count: at or above it, the single-shot path runs and the full array is materialized on the default device before the one transfer. The result feeds straight into to_datatree() and thebatch_size-chunked evaluation metrics inevaluate, which accept host-resident draws.
Returns
Num[Array, " sample *batch time obs"]-
In-sample posterior-predictive draws of the
obssite (withdevice="host"committed to the CPU device, or a NumPy array when no CPU backend is initialized).
Raises
ValueError-
If
posterioris empty, aposteriorleaf has no sample axis, or the leaves disagree on the sample-axis length, if a forecast-horizon key ofposterior(one that ends in"_future"or contains"_future_", or"forecast") has a non-empty leaf, if"obs"is the only key ofposterior, ifcovariateshas fewer than two axes (time must be at axis-2) or no time steps, if aposteriorvalue of an innovations() site does not have one step percovariatesrow (a posterior drawn for a different training length), or if the"obs"draws do not have time at axis-2with one step percovariatesrow. HostMemoryKindError-
If
device="pinned_host"is requested on a device that exposes no host memory kind (see_host_memory_kind()). DevicePlatformError-
If
devicenames a platform whose backend is not initialized (seeresolve_device()).
Warns
UserWarning-
If
device="cpu"is requested and the JAX CPU backend is not initialized, so the draws take the NumPy path of"host"instead.
Notes
Like forecast(), each call compiles the predictive once (shared by every batch_size chunk) and keeps no compiled program afterwards, so the draws reflect the model as it is at call time and model need not be hashable.