32. Storytelling
The storytelling layer takes an estimated, data-fed model and answers the questions an empirical paper actually has to answer:
Which structural shocks were active in each period?
What was the most likely regime path – and how confident are we?
What would the path have looked like without the monetary shock? Without the fiscal regime change?
How much credit do we give each shock for each datapoint?
Three layers do the work, in order: the smoother (filter), the
classical historical decomposition (historical_decomposition),
and the modern resimulation surface (rise.engine.resim_bridge).
This chapter is the modern entry-point reference. The legacy chapter Resimulation and counterfactuals remains canonical for the algorithm details and the per-method API and is scheduled to fold into this tree pending review.
32.1. filter
Everything downstream starts from the smoother. filter(m) runs
the Kalman filter (or, for switching models, the regime-conditional
Kalman filter; for nonlinear models, a sigma-point filter: the
divided-difference filter by default, or the unscented or cubature
filter). It returns the smoothed states, smoothed shocks, smoothed
regime probabilities, and the filter likelihood:
[filt, LogLik, Incr, retcode, m] = filter(m_est);
assert(retcode == 0, decipher(retcode));
The five outputs:
Output |
Meaning |
|---|---|
|
Struct of |
|
Sample log-likelihood at the parameter vector currently bound to |
|
Per-period likelihood increments, useful for diagnostics. |
|
Return code; |
|
The model with the filter output attached. Required by |
The fields of filt follow a predictable naming pattern: each
quantity (variables, shocks, measurement errors, regime
probabilities, state probabilities) comes in three flavours –
filtered (one-step prediction), updated (conditional on the
contemporaneous observation), and smoothed (conditional on the
whole sample) – plus an Expected_* companion that averages
over regime uncertainty. The most-used entries:
Field |
Content |
|---|---|
|
Smoothed endogenous variables (struct of |
|
Smoothed structural shocks (struct of |
|
One |
|
One |
|
The one-step and contemporaneous counterparts. |
|
When the model declares them. |
For a model with a mon chain (active/passive monetary) and a
fis chain (active/passive fiscal) the smoothed marginal
probability of an active monetary regime is the sum of the
composite-regime probabilities consistent with that marginal:
p_active_mon = filt.smoothed_regime_probabilities.regime_1 ...
+ filt.smoothed_regime_probabilities.regime_3;
(The composite-regime numbering follows m.markov_chains.regimes.)
32.2. Historical decomposition
historical_decomposition splits each datapoint into the
contributions of each structural shock plus an initial conditions
term:
hd = historical_decomposition(m);
The output is a struct of ts keyed by endogenous-variable name.
Each ts carries one column per contributing series; the
varnames property lists them. The contributions sum to the
realized path: for any variable y and any date t:
y_t == sum_k hd.y(t, k) over all columns k
The columns include every declared exogenous shock plus an init
column that absorbs the effect of initial conditions and the steady
state. A VAR (rfvar_model, svar_model, …) with complete data
has an exact decomposition with separate columns for the initial
conditions (init), the constant (const) and each deterministic
variable, besides its structural shocks; see
Reduced-form VAR Modeling.
For linear constant-parameter models the decomposition is
classical and exact. For switching or nonlinear models the
decomposition is approximated by a Monte Carlo integration over the
regime distribution; the integration size is controlled by the
nsim argument to historical_decomposition (default
100 for switching, automatically 1 for linear constant-
parameter). An empty nsim or sim_engine takes the model’s
option histdec_nsim (default 100) or histdec_sim_engine
(default @utils.filtering.sim_engine), so both can be set once with
set. counterfactual reads ctrf_nsim, ctrf_sim_engine
and ctrf_shock_list the same way.
Grouping shocks is supported via the fourth argument:
groups = struct( ...
demand = {'eps_g'}, ...
monetary = {'eps_m'}, ...
fiscal = {'eps_f'});
hd = historical_decomposition(m, [], [], groups);
Each group becomes a single column in the output ts; unlisted
shocks are pooled under others.
32.3. rise.engine.resim_bridge – the resimulation surface
The classical historical decomposition tells you who’s responsible
for each datapoint. The rise.engine.resim_bridge package answers the more
specific questions: which regime path was the economy really on,
what would have happened without a specific shock, what is the
exactly-additive credit assignment across shocks.
The package’s full name is rise.engine.resim_bridge; the code below
calls it by that name, and the text shortens it to resim_bridge.
Five reconstruction methods, three counterfactual modes, and three decomposition utilities sit on top of the same adapter object.
32.3.1. Adapter
rise.engine.resim_bridge.adapt packages a solved-and-filtered model into
the canonical tuple used by every downstream method:
adapter = rise.engine.resim_bridge.adapt(m, filt);
You rarely call this directly – run, counterfactual,
shock_decomp, shapley, and var_decomp all call it
internally. Direct access is useful when writing a custom
reconstruction or counterfactual.
The adapter exposes Tfunc(x, eps, r) (the one-step state
propagator), Pfunc(x) (the state-dependent regime transition
matrix, constant for exogenous Markov chains), the smoothed
regime-conditional means and probabilities (smoothed.x,
smoothed.eps, smoothed.pi), the regime likelihoods of the
observations (smoothed.lik) and the updated regime probabilities
(alpha).
The reconstruction covers the second to the last date of the sample.
The filter does not export the state before the sample, so the
smoothed state of the first date is the initial condition
(adapter.x0) and the shocks are replayed from the second date on;
adapter.dates (and the dates field of every run output)
gives the dates covered. Every series is aligned by date.
32.3.2. Reconstruction
rise.engine.resim_bridge.run(m, filt, method, opts) reconstructs the latent
regime path consistent with the smoother output. Four methods:
|
What it returns |
|---|---|
|
The most-likely regime path (single deterministic sequence), its log posterior probability ( |
|
|
|
|
|
Deterministic mixture across regimes: each regime carries the mean of the re-simulated state given that regime, mixed with the backward transition probabilities of the regime chain, and the dates are weighted by |
With an exogenous Markov chain these probabilities are those of the
filter itself. With endogenous transition probabilities, Pfunc is
evaluated at the smoothed states, which makes the reconstruction an
approximation.
The most common pairing is Viterbi for headline plots and FFBS for uncertainty bands:
viterbi = rise.engine.resim_bridge.run(m, filt, 'viterbi');
ffbs = rise.engine.resim_bridge.run(m, filt, 'ffbs', struct(M = 1000));
32.3.3. Counterfactuals
rise.engine.resim_bridge.counterfactual(m, filt, mode, spec) reconstructs a
deterministic baseline (Viterbi by default) and re-simulates under
one perturbation:
|
|
Effect |
|---|---|---|
|
|
Zeroes (or replaces) the listed shocks. A Viterbi baseline keeps its regime path; a Rao-Blackwell baseline is recomputed as the same mixture with the shocks changed in every regime, so unchanged shocks change nothing. |
|
|
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. baseline and counter are
structs of numeric arrays, not databases: r_path (the regime
path), eps (the shocks, one row per shock in the order of
get(m, 'exo_list')) and x (the endogenous variables, one row
per variable in the order of get(m, 'endo_list')), with one column
per period of the filtered sample. spec.shock_ids takes positions
in the shock list, not names. A what-if on monetary shocks looks
like:
exo=get(m,'exo_list');
endo=get(m,'endo_list');
spec=struct(shock_ids=find(strcmp(exo,'eps_m')));
cf=rise.engine.resim_bridge.counterfactual(m,filt,'shock',spec);
ipi=find(strcmp(endo,'pi'));
plot([cf.baseline.x(ipi,:);cf.counter.x(ipi,:)].');
title('Inflation: baseline vs counterfactual without monetary shocks');
32.3.4. Shock decompositions
Two flavours, both along the reconstructed regime path.
rise.engine.resim_bridge.shock_decomp(m, filt) is the leave-one-out
decomposition:
delta_k(t) = x_t^full - x_t^(-k)
It is exact for linear models; for nonlinear models the interaction residual is not allocated.
rise.engine.resim_bridge.shapley(m, filt) is the Shapley decomposition:
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
so that the residual is at machine precision (smoke runs report
residual = 1.11e-16).
Exact mode enumerates all \(2^{n_e}\) subsets and runs for
ne <= opts.exact_threshold (default 10). For ne above
the threshold the sampled estimator of Castro, Gomez, and Tejada
(2009) is used.
32.3.5. Ergodic variance decomposition
rise.engine.resim_bridge.var_decomp(m, kind) works at the model’s ergodic
distribution; it does not consume a filter output and is therefore
a property of the solved model alone. Three games:
|
Decomposition |
|---|---|
|
Shapley allocation of total variance across the |
|
Shapley allocation across the shocks and a |
|
Between-vs-within decomposition via the law of total variance: |
32.4. A worked story
The empirical section of a paper, in five calls, on the estimated fiscal-monetary switching NK from Estimation:
% 1. filter
[filt, ~, ~, rc, m_est] = filter(m_est);
assert(rc == 0, decipher(rc));
% 2. who's responsible for each datapoint?
hd = historical_decomposition(m_est);
% 3. most-likely regime path
viterbi = rise.engine.resim_bridge.run(m_est, filt, 'viterbi');
plot(viterbi.r_path); ylim([0.5, m_est.markov_chains.regimes_number + 0.5]);
title('Most-likely composite-regime path');
% 4. what if there had been no monetary shocks?
exo=get(m_est,'exo_list');
endo=get(m_est,'endo_list');
spec=struct(shock_ids=find(strcmp(exo,'eps_m')));
cf=rise.engine.resim_bridge.counterfactual(m_est,filt,'shock',spec);
ipi=find(strcmp(endo,'pi'));
plot([cf.baseline.x(ipi,:);cf.counter.x(ipi,:)].');
legend('baseline','no monetary shocks');
% 5. Shapley credit assignment, exactly additive
sh = rise.engine.resim_bridge.shapley(m_est, filt);
These five calls answer “what drove the data”, “what regime were we in”, “what would have happened without X”, and “how much credit does each shock get” – the standard menu of an empirical paper’s results section.
32.5. Where to look next
Resimulation and counterfactuals (legacy DSGE chapter) – the long-form algorithm description plus per-method API references for
rise.engine.resim_bridge.adapt,run,counterfactual,shock_decomp,shapley,var_decomp. Canonical; fold pending review.Master filtering (legacy DSGE chapter) – the full surface of
filteroptions, including the choice of nonlinear filter (regime-switching divided-difference, unscented, cubature), the initialization options, and the OBC variants.Forecasting and simulation –
simulateandforecast, used for forward extensions of a Viterbi baseline.