21. X at risk
Growth at risk is a low quantile of the forecast of output growth, typically the 5th percentile. Inflation at risk is a high quantile of the forecast of inflation. Both are read off the distribution of a forecast, and both are tracked through history: how bad could it get, seen from each date? The empirical literature estimates these quantiles by quantile regression on financial conditions (Adrian, Boyarchenko and Giannone, 2019, “Vulnerable Growth”, American Economic Review). A structural model implies them.
rise.engine.numerics.x_at_risk.x_at_risk computes the distribution of the
forecast of every variable of a model, at every date of the data:
m = solve(set(dsge_model('nk3_vol.rs'), 'data', db));
fk = rise.engine.numerics.x_at_risk.x_at_risk(m, 4, 1000, 'seed', 1);
gar = quantile(fk.ygap.data(:,4,:), 0.05, 3); % output gap at risk, 4 quarters ahead, every date
21.1. What it computes
At every date t of the data, the model is filtered with the data up to
t. Each draw then
takes one parameter vector;
draws the regime of
tfrom the updated regime probabilities oft;starts from the updated state of that regime;
simulates
nfperiods ahead: the regime of each period is drawn from the transition matrix, and the shocks are drawn standard normal.
Together, the draws are the distribution, seen from date t, of every
variable h = 1, ..., nf periods ahead. Its quantiles are the risk
measures.
Three sources of uncertainty enter: the shocks, the regimes and, when the
draws carry different parameters, the parameters. The state of date t
starts at its updated mean: the uncertainty about the current state is not
drawn.
21.2. The regime-switching angle
In a constant-parameter linear model with Gaussian shocks the forecast is normal. Its tails are symmetric. Its width does not change from one date to the next, so growth at risk moves only with the mean: it is the mean minus 1.645 standard deviations.
With regimes, the forecast is a mixture. Take a calm regime 1 and a crisis regime 2 in which the shocks are more volatile. One period ahead,
P(y(t+1) <= x | data up to t) = w1(t)*Phi((x-mu(t))/s1) + w2(t)*Phi((x-mu(t))/s2)
[w1(t) w2(t)] = p(t)' * Q
with p(t) the updated regime probabilities, Q the transition matrix,
mu(t) the mean and s1, s2 the standard deviations of the two
regimes. The weight of the crisis, w2(t), moves the tail even when the mean
does not move. At longer horizons the mixture runs over the paths of regimes.
So two things drive growth at risk in a switching model besides the shocks:
the probability of being in the crisis now, which the data update at every
date, and the probability of entering it, a parameter of the model. The
output fk.regime records the regime path of every draw, which says how
much of the tail comes from the crisis:
y = reshape(fk.ygap.data(end,4,:), 1, []); % the last date, 4 quarters ahead
crisis = reshape(any(fk.regime.data(end,2:end,:)==2, 2), 1, []);
tail = y <= quantile(y, 0.05);
[mean(crisis), mean(crisis(tail))] % in all the draws, in the tail
The unit tests of the feature check the simulated 5th percentile against the quantile of the mixture above, at every date, as the probability of entering the crisis rises from 0.02 to 0.6.
21.3. The call
[fkst, retcode] = rise.engine.numerics.x_at_risk.x_at_risk(m, nf, draws)
[fkst, retcode] = rise.engine.numerics.x_at_risk.x_at_risk(..., 'seed', s)
[fkst, retcode] = rise.engine.numerics.x_at_risk.x_at_risk(..., 'partitions', p)
m is a solvable model with data (set(m, 'data', db)) and nf the
forecast horizon. draws gives one parameter vector per draw:
N N draws at the parameters of m (shocks and regimes only)
struct array, 1 x N one parameter structure per draw, as in set(m,'parameters',p)
struct array with x and f a posterior sample of the samplers (x the estimated parameters)
Consecutive draws with the same parameters are solved and filtered once. The options:
seed seed of the draws of regimes and shocks; default: one draw from
the global stream, so that rng(s) before the call reproduces the run
partitions number of blocks the draws are split into, one parfor iteration
each; default: the workers of the current pool (one block without one)
Draw i takes substream i of a stream of its own. The result therefore
does not depend on the number of partitions or workers, and a run with fewer
draws gives the first draws of a longer one.
The outputs:
fkst.(name) one ts per endogenous variable and per shock, dates x nf x N:
row t the forecasts made at date t, column h the forecast h
periods ahead, page i draw i (variables in logs come in levels)
fkst.regime dates x (nf+1) x N: column 1 the regime drawn at t,
column h+1 the regime of period t+h
retcode 1 x N, 0 for a draw that solved and filtered
A draw whose parameters do not solve has NaN paths, shocks and regimes, and
one warning counts such draws. MATLAB’s quantile and the plots below
leave the NaN draws out.
21.4. The plots
The package rise.engine.numerics.x_at_risk.plot draws the outputs. Its
functions take one of two inputs.
A time series of T dates and N draws (T x N):
fan_chart quantile bands and the median over time
fan_chartx the same, from central intervals in percent, with palettes
density_surface the density at every date, as a surface
distribution_snapshot the density at one date, with its median and 5th percentile
quantiles the quantile function at each date
risk_map the quantile of one variable conditional on another
Two slices of the forecast give such a series: the history of the forecast
h periods ahead, and the forecast made at one date over its horizons:
[T, nf, N] = size(fk.ygap.data);
hist4 = ts(fk.ygap.start, reshape(fk.ygap.data(:,4,:), T, N)); % 4 quarters ahead, every date
rise.engine.numerics.x_at_risk.plot.fan_chart(hist4)
d = fk.ygap.dates;
last = ts(date2serial(d(end))+1, reshape(fk.ygap.data(end,:,:), nf, N)); % from the last date
rise.engine.numerics.x_at_risk.plot.fan_chart(last, [0.05 0.25 0.5 0.75 0.95])
Scenarios, a structure of x_at_risk outputs (each variable T x nf x N),
aggregated over the horizon by its mean or its end:
compare_tail_risk one quantile per scenario, through time
quantify_scenario_bands the quantile bands of each scenario
interconnected_tail_map the tail quantile of several variables, as a heat map
sims = struct('rare', fk_rare, 'frequent', fk_frequent);
rise.engine.numerics.x_at_risk.plot.compare_tail_risk(sims, {'ygap'}, ...
'tau', 0.05, 'h_agg', "end", 'show_diff', true)
fan_chart, fan_chartx, quantiles, risk_map,
density_surface and distribution_snapshot take one weight per draw.
Entropic tilting gives such weights: the paths are
reweighted to respect outside information, and the plots show the tilted
distribution.
ens = struct('ygap', reshape(fk.ygap.data(end,:,:), nf, N));
out = rise.scenario.tilt(ens, {"ygap", 4, "mean", 0});
rise.engine.numerics.x_at_risk.plot.fan_chart(last, [], out.Weights)
Without weights, a quantile is that of MATLAB’s quantile. With weights, it
is the smallest value whose cumulated weight reaches the level
(utils.entropic_tilting.weighted_quantile).
21.5. A worked example
The course library teaches the feature in its chapter Risk analysis. The
lecture demo lecture/demo_risk.m computes output gap at risk and inflation
at risk on US data with nk3.rs and checks them against the exact quantiles
of the linear Gaussian model. The lab draws the history of output gap at
risk and checks every date against x_at_risk. The demo
lecture/demo_risk_regimes.m adds a crisis regime (nk3_vol.rs: in
regime 2 every shock is twice as volatile):
db = us_data();
m = solve(set(dsge_model('nk3_vol.rs'), 'data', db));
for p = [0.02 0.1 0.3 0.6]
mk = solve(set(m, 'parameters', struct('vol_tp_1_2', p)));
fk = rise.engine.numerics.x_at_risk.x_at_risk(mk, 4, 1000, 'seed', 9);
y = 100*reshape(fk.ygap.data(end,4,:), 1, []);
fprintf('%.2f: 5th percentile %.2f, median %.2f\n', p, quantile(y,0.05), median(y));
end
0.02: 5th percentile -3.65, median 0.18
0.10: 5th percentile -4.46, median 0.18
0.30: 5th percentile -5.82, median 0.17
0.60: 5th percentile -6.37, median 0.26
These are the forecasts made on the last date of the data, four quarters
ahead, in percent. As the probability of entering the crisis rises, the 5th
percentile falls and the median stays where it is: the regime moves the tail,
not the center. The same seed gives every run the same numbers, so the runs
differ by the parameter alone. The regime paths say why: with an entry
probability of 0.1, 36% of all the draws meet the crisis within the four
quarters, and 70% of those in the tail do. With the two regimes made equal
(sv_vol_2 = 1), the 5th percentile is that of nk3.rs: -3.40% from 2000
draws against the exact -3.54%.
21.6. Limits
The shocks are unanticipated. A model whose agents anticipate shocks is refused (
rise:x_at_risk:anticipatedShocks).Observed exogenous variables stay at zero over the forecast.
The state of each date starts at its updated mean; its uncertainty is not drawn.
The jump-off dates are all the dates of the data. To look at fewer dates, give the model less data.
The cost is one forecast step per date, horizon and draw, plus one solve and one filter per distinct parameter vector. The draws run in parallel when a pool is open.