contrib.dynestyx.state_space()
Condition, forecast or predict in-sample a dynestyx model over the horizon.
Usage
contrib.dynestyx.state_space(
h,
name,
y,
dynamics,
*,
controls=None,
)One dsx.sample call on the integer time grid 0, ..., h.duration - 1 (obs_times are the first h.t_obs steps, predict_times the rest, ctrl_times the whole grid), interpreted by the dynestyx handlers active around the model call. What the block registers depends on the mode of the call, exactly as the other building blocks decide it from h:
h.datagiven andh.future == 0(training): nothing. The activeFilter,SmootherorLatentPathBuilderadds its likelihood factor, which is whatMCMCandSVIfit.h.datagiven andh.future > 0(forecasting): the site"forecast", shape(future, obs), read from the rollout of aDiscreteTimeSimulator(n_simulations=1)placed outside theFilter,SmootherorLatentPathBuilder(the explicit simulator is the recommended form; dynestyx 0.8 adds one itself when none is active). The rollout starts from the conditioned state at the last observation: the filtering or smoothing distribution at the parameters of each posterior draw (the two coincide there), or the sampled final state of the path under aLatentPathBuilder, which therefore inherits the mixing of the path. The block asks the simulator forpredict_timesstarting at the last observation and drops that first row, because dynestyx places the rollout’s initial state at the first predict time without a transition.h.data is None(the in-sample predictive of predict_in_sample() and to_datatree()): the site"obs", shape(t_obs, obs), one observation draw per step fromdynamics.observation_model(x_t, u_t, t)at a draw of the in-sample states: step-wise from the smoothing marginals under aSmoother, the posterior path itself under aLatentPathBuilder. This is not a prior predictive: the block still conditions onyfrom the covariates, soPredictive(model, num_samples=...)(rng_key, covariates)returns draws conditioned on the observed window at prior parameters. Simulate a prior predictive withdsx.sampleandpredict_timesunder aSimulator, withoutobs_values.
Parameters
h: Horizon-
The current call’s horizon.
name: str-
Site prefix handed to
dsx.sample(dynestyx registers its own sites asf"{name}_..."). y: Float[Array, " time obs"]-
The observed window, shape
(h.t_obs, obs), read from the covariates by the caller (covariates[..., : h.t_obs, :1]for a series stored in column 0) so that it is present when the drivers call the model withdata=None. NaN entries are missing observations and must be iny(the likelihood never readsh.data). TheLatentPathBuilderand the Kalman and ensemble Kalman filters and smoothers of the cuthbert backend (KFConfig,EnKFConfig,KFSmootherConfig,EnRTSSmootherConfigwithfilter_source="cuthbert", and the defaultFilter(), an ensemble Kalman filter) skip them. The other configurations reject them, including the cd_dynamax backend (the default ofKFConfig) and the defaultSmoother(). The particle filter returns a NaN log likelihood (its warning disappears inside the jitted drivers), and dynestyx does not check the HMM filters. Under aLatentPathBuilder, a partly missing row of an observation family the builder cannot marginalize adds a latentf"{name}_missing_obs_values"site. dynamics: Any-
A
dynestyx.DynamicalModelwith a vector state (one row of the path per observed step). Itsobservation_model(x, u, t)must return a NumPyro distribution for one time step (dynestyx’sLinearGaussianObservationor any callable), which the in-sample predictive samples. controls: Float[Array, " duration control"] | None = None-
Optional control inputs over the whole horizon, shape
(h.duration, control); passed asctrl_valueswithctrl_timesequal to the full grid, and sliced to the observed window for the in-sample predictive.
Returns
StateSpaceResult-
x_futurewhile forecasting andx_in_samplewhen called without data,Noneotherwise.
Raises
ValueError-
If no
Filter,SmootherorLatentPathBuilderis active; ifyorcontrolsdo not span the horizon; if the forecast runs withn_simulations != 1; if the in-sample predictive runs under aFilter, receives no per-step marginals, or gets a state path that is not(t_obs, state); if a dynestyx site is missing (for example undernumpyro.handlers.scope). A freshLatentPathBuilderinside a jitted driver raises dynestyx’s ownValueErrorabout the missingness pattern. RuntimeError-
If the in-sample predictive runs without a seed handler.
jax.errors.JaxRuntimeError-
From dynestyx’s layout check, when a traced call evaluates the likelihood under a
LatentPathBuilderwhose cached missingness layout comes from a later fit with another missingness pattern (see Notes).
Notes
- The
Smootherin-sample draws are per-step marginals p(x_t \mid y_{1:T}), independent across steps: exact for per-step bands and metrics, not a joint path. TheLatentPathBuilderdraws are joint. The smoother’s marginals arrive as a Python list, so building the draws costs trace time that grows with the length of the window. - A
Filterand aSmootherthat run the same forward filter add the same marginal likelihood, so the draws of aFilterfit can be exported under thatSmootherwithout a refit: the Kalman filter with the Kalman (Rauch-Tung-Striebel) smoother, and an approximate filter with the smoother of the same configuration and seed. With an approximate filter, the posterior is only as good as that filter’s likelihood estimate. - Under a
LatentPathBuilderthe drivers need the posterior draws off"{name}_state_path_params", which only a fit under a builder has. A posterior from aFilterorSmootherfit has no path, and the builder then draws the path from its prior without an error: export such fits under aSmoother. - The
LatentPathBuilderaround a driver call must be the instance that fitted the model: it caches the observation missingness layout per site name and shape from the concrete observations of the fit, and the jitted drivers hand it traced arrays. A later fit with the same site name and shape and a different missingness pattern overwrites that layout. A traced call for the earlier model that evaluates the likelihood (a fit,log_density) then raises dynestyx’sJaxRuntimeError; the predictive drivers do not evaluate it and ran without error in our tests. Use one builder per fitted model when the patterns differ. A fresh builder needs its first model run eager. mcmc.get_samples()also holds the deterministic sites dynestyx records (f"{name}_filtered_states_mean"and similar). The drivers ignore deterministic sites, but to_datatree() stores them in the posterior group; drop them for a smaller tree, and keepf"{name}_state_path_params"under aLatentPathBuilder.
Examples
A local level model, fitted with the Kalman filter and exported with the smoother around the driver call:
def local_level(covariates, data=None):
h = Horizon.from_data(covariates, data)
y = covariates[..., : h.t_obs, :] # the series travels in the covariates
q = numpyro.sample("q", dist.HalfNormal(1.0))
r = numpyro.sample("r", dist.HalfNormal(1.0))
dynamics = DynamicalModel(
initial_condition=dist.MultivariateNormal(jnp.zeros(1), jnp.eye(1)),
state_evolution=LinearGaussianStateEvolution(A=jnp.eye(1), cov=q**2 * jnp.eye(1)),
observation_model=LinearGaussianObservation(H=jnp.eye(1), R=r**2 * jnp.eye(1)),
)
state_space(h, "f", y, dynamics)
rng_key, key_fit, key_export = random.split(rng_key, 3)
with Filter(filter_config=KFConfig(filter_source="cuthbert")):
mcmc.run(key_fit, series[:t_obs], series[:t_obs])
# the parameters only: the fit also recorded its filtered states (f_filtered_states_*)
posterior = {k: v for k, v in mcmc.get_samples().items() if not k.startswith("f_")}
smoother = Smoother(smoother_config=KFSmootherConfig(filter_source="cuthbert"))
with DiscreteTimeSimulator(n_simulations=1), smoother:
tree = to_datatree(key_export, local_level, posterior, series[:t_obs], series)