2.14. Resimulation and counterfactuals
Given a solved model and a filter output, the resim_bridge package
reconstructs the latent regime path and structural shock sequence
consistent with the observed data, then re-simulates under user-chosen
perturbations. Five reconstruction methods are exposed; three
decomposition utilities consume the reconstructed baseline.
The bridge does not modify filter(m), the solver, or any class
method. It reads the filter output and dispatches into the underlying
+resim package (also under classes/utils/).
2.14.1. Adapter
resim_bridge.adapt packages the model and filter output into the
canonical tuple used by every reconstruction method:
adapter = resim_bridge.adapt(m, f);
The returned struct exposes:
Field |
Meaning |
|---|---|
|
One-step state propagator on the full endogenous vector. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2.14.2. Reconstruction methods
resim_bridge.run(m, f, method, opts) dispatches over four
deterministic / stochastic reconstruction methods:
|
What it returns |
|---|---|
|
The MAP regime path (single deterministic sequence) and the state path obtained by forward-simulating along it with the regime-conditional smoothed shocks. |
|
|
|
|
|
Deterministic pi-weighted mixture across regimes. Exact for linear models; biased for nonlinear ones, with bias growing in regime uncertainty. |
The 'particle' method requires a particle-filter output rather
than a Kalman smoother; run raises resim_bridge:run:notWired
in that case. Call resim.particle directly with a particle
container.
2.14.3. Counterfactuals
resim_bridge.counterfactual(m, f, mode, spec) reconstructs a
deterministic baseline and re-simulates under one of three
perturbations:
|
|
Effect |
|---|---|---|
|
|
Zeroes (or replaces) the listed shocks on the reconstructed baseline; the regime path is held fixed. |
|
|
Replaces the reconstructed regime path with a user-supplied sequence; shocks held fixed. |
|
|
Re-sets parameters, re-solves, re-filters on the same data, and re-runs the reconstruction. |
The return struct carries baseline, counter, and delta
= counter.x - baseline.x.
2.14.4. Shock decompositions
Two flavours, both along the reconstructed regime path:
resim_bridge.shock_decomp(m, f) – leave-one-out:
delta_k(t) = x_t^full - x_t^(-k)
Exact for linear models; the interaction residual is not allocated.
resim_bridge.shapley(m, f) – Shapley:
phi_k(t) = (1/ne!) sum_sigma [ v(S U {k}, t) - v(S, t) ]
The Shapley decomposition is exactly additive even for nonlinear models:
sum_k phi_k(t) = x_t^full - x_t^base
Exact mode enumerates all \(2^{n_e}\) subsets and runs for
ne <= opts.exact_threshold (default 10). Sampled mode
([Castro et al., 2009]) is used otherwise.
2.14.5. Ergodic variance decomposition
resim_bridge.var_decomp(m, kind) works at the model’s ergodic
distribution; it does not need f. Three games:
|
Decomposition |
|---|---|
|
Shapley allocation of total variance across the |
|
Shapley allocation across the |
|
Between-vs-within decomposition via the law of total variance: Var(x) = W + B
where |
A worked example using a switching New-Keynesian model is provided in
Chapter 22 - Nonlinear filtering under regime switching of the
RISE lecture notes (T22_6_Resimulation.m).