1.2. Monte Carlo Filtering
1.2.1. Description
Monte Carlo filtering is a method for analyzing the sensitivity of a model’s characteristics to its parameters. The approach involves generating draws of parameters from their distribution (e.g. prior, posterior, support) and examining the model’s behavior for each draw.
By comparing the empirical marginal distributions of the draws that lead to desired behavior versus those that do not, key parameters that drive the model’s behavior can be identified.
Monte Carlo filtering is useful for locating the most important parameters of a model that contribute to its behavior.
A key reference in Global sensitivity analysis is [Ratto, 2008].
1.2.2. Quick start examples
A very simple example
1 x=randn(2^13,8);
2
3 y=x(:,1)+x(:,2).^2+x(:,1).*x(:,2);
4
5 check_behavior=y<0;
6
7 obj=mcf(check_behavior(:).',x.')
8 %%
9 cdf_plot(obj)
10 %%
11 scatter(obj)
12 %%
13 correlation_patterns_plot(obj,[],'behave')
In this small example, we have a function y=y(x1,x2) and we interested in finding the combinations of parameters/covariates x1, x2, x3, x4, x5, x6, x7 and x8 for which y<0. In this controlled example, clearly, we know a priori that the only covariates that matter for the result are x1 and x2. But in general we may not know that in advance.
We first sample values for all the covariates and for each combination (x1(i), x2(i), x3(i), x4(i), x5(i), x6(i), x7(i), x8(i)), for i=1,2,…,2^13, we compute y(i) and set a flag telling us whether y(i) is negative for combination i.
Then we proceed to investigate whether there are some patterns among the different covariates both in situations where y<0 and in situations where y>=0.
A simple DSGE example
1 %% A DSGE model
2
3 simpleModel={
4 '%% three AR(1) processes with switching volatilities'
5 '@endogenous X1, X2, X3'
6 '@exogenous E1, E2, E3'
7 '@parameters a1, a2, a3, vol_tp_1_2, vol_tp_2_1'
8 '@parameters(vol,2) s1, s2, s3'
9 '@model'
10 'X1=a1*X1{-1}+s1*E1;'
11 'X2=a2*X2{-1}+s2*E2;'
12 'X3=a3*X3{-1}+s3*E3;'
13 };
14
15 m=dsge_model(simpleModel);
16
17 %% The parameters to vary and their ranges
18
19 plist={'a1','a2','a3',...
20 'vol_tp_1_2','vol_tp_2_1',...
21 's1_vol_1','s2_vol_1','s3_vol_1',...
22 's1_vol_2','s2_vol_2','s3_vol_2'};
23
24 bounds=[0.5,0.9;0.5,0.9;0.5,0.9;0,1;0,1;0,0.5;0,0.5;0,0.5;0.5,2;0.5,2;0.5,2];
25
26 %% The behavior: the model solves and, in regime 2, the autocorrelations
27 % of X2 of orders 3 to 8 all exceed 0.01
28
29 props=mcfprop(m,plist,'solve','all(corr{X2,X2,3:8,2}>0.01)');
30
31 Behavior=create_handle(props,true);
32
33 %% Run the behavior through the mcf on 500 Latin-hypercube draws
34
35 nsim=500;
36
37 obj=mcf(Behavior,nsim,bounds(:,1),bounds(:,2),plist,'latin_hypercube');
38
39 assert(isequal(size(obj.samples),[numel(plist),nsim]))
40
41 assert(any(obj.is_behaved) && ~all(obj.is_behaved),...
42 'the behavior does not split the draws')
43
44 %% Smirnov test for equality of distributions
45
46 hcdf=cdf_plot(obj);
47
48 %% Correlation patterns in the full, behave and non-behave samples
49
50 hall=correlation_patterns_plot(obj,[],'');
51
52 hgood=correlation_patterns_plot(obj,[],'behave');
53
54 hbad=correlation_patterns_plot(obj,[],'non-behave');
55
56 %% Scatter plots of the significant correlations
57
58 hsc=scatter(obj,[],'',1/100);
59
60 hscbad=scatter(obj,[],'non-behave');
This second example transposes the workflow above to a DSGE model with
regime-switching shock volatilities. The model has three AR(1)
endogenous variables X1, X2, X3, three shocks E1,
E2, E3, and two volatility regimes (vol) governed by the
transition probabilities vol_tp_1_2 and vol_tp_2_1. The
standard deviations s1, s2, s3 are declared switching
over the vol chain, so each one carries two values in the
parameter table (s1_vol_1 / s1_vol_2, etc.).
The parameter list plist collects every parameter the analyst
wants to vary: the AR(1) persistences a1-a3, the two
transition probabilities, and both regime values of each volatility.
bounds gives the lower and upper sampling range for each.
Behavior is a function of the sampled parameter vector that
returns true when the vector produces the model behavior we want
to single out: here, that the model solves and that the
autocorrelations of X2 of orders 3 to 8 in regime 2 all exceed
0.01, which fails when a2 is low. It is built by mcfprop (see
Support facility for filtration below); a hand-written function
handle @(x) ... returning a logical works the same way.
mcf draws nsim parameter vectors between the bounds and
evaluates Behavior on each. The sampling procedure is
'uniform' (the default), 'latin_hypercube', 'sobol' or
'halton', or a function handle @(lb,ub,nsim) returning a
matrix with one draw per column. The two assertions check that the
draws have the expected size and that both groups are non-empty, which
the comparisons need. Then cdf_plot reports the marginal
distribution of each parameter conditional on behavior vs.
non-behavior; correlation_patterns_plot highlights pairwise
patterns within the behave / non-behave / full samples; scatter
shows the joint distribution of the most informative pairs. Each
returns the handles of the figures it draws.
1.2.3. Details on the mcf class
Contents:
1.2.4. Support facility for filtration : the mcfprop class
An example
1 %% construct the support object
2
3 m=dsge_model(simpleModel);
4
5 self=mcfprop(m,plist);
6
7 %% Add a few properties
8
9 self=self.add('solve'); % the model solves
10
11 self=self.add('all(corr{X2,X2,3:8,2}>0.01)'); % correlations of orders 3 to 8 in regime 2
12
13 self=self.add('abs(corr{X3,X3,0,2}-1)<1e-9'); % correlation of order 0 in regime 2
14
15 self=self.add('irf{X1,E1}>0.01'); % irf in period 1
16
17 self=self.add('irf{X1,E1,3}>0.01'); % irf in period 3
18
19 self=self.add('all(irf{X2,E2,3:5,2}>1e-3)'); % irf in periods 3 to 5 in regime 2
20
21 self=self.add('Tz{X1,E1}>0'); % impact of E1 on X1, regime 1
22
23 self=self.add('abs(Tz{X1,@sig,2})<1e-8'); % impact of the perturbation parameter, regime 2
24
25 %% Create a function handle for the behavior
26
27 n=size(bounds,1);
28
29 propfun=create_handle(self); % one output per property
30
31 Behavior=create_handle(self,true); % one output: the joint truth of all the properties
32
33 %% Evaluate one parameter vector
34
35 x=bounds(:,1)+rand(n,1).*(bounds(:,2)-bounds(:,1));
36
37 eval_=cell(1,self.nprops);
38
39 [eval_{1:self.nprops}]=propfun(x);
40
41 is_behaved=Behavior(x);
mcfprop is the builder side of mcf: it lets the user assemble
a behavior-checking function from named model properties without
writing it by hand. Each call to self.add(...) registers one
property to evaluate; properties can also be given to the constructor,
after the parameter list. The shortcuts are 'solve' (the model
solved successfully at the candidate parameter vector) and the
failure flags 'pb_sstate', 'pb_trans_matrix', 'pb_maxiter',
'pb_multi_sol', 'pb_nan_sol', 'pb_explosive_sol' and
'pb_unstable'. The other properties are conditions on RISE’s
post-solve accessors:
corr{Xi,Xj,k,r}/cov{Xi,Xj,k,r}– the correlation / covariance of orderk(or a rangek:l) between variablesXiandXj, in regimer(defaults: order 0, regime 1).irf{Xi,Ej,k,r}– the impulse response ofXito shockEjat horizonk(or a range), in regimer(defaults: horizon 1, regime 1).vd{Xi,Ej,k,r}– the variance decomposition (defaults: horizoninf, regime 1).Tz{Xi,Ej,r}/Tz{Xi,Xj{-1},r}/Tz{Xi,@sig,r}– direct entries of the policy function in regimer(impact of shockEj, of the lag ofXj, or of the perturbation parameter@sig).
Such a property must contain a relational operator (==, <,
>, <=, >=), and may sit inside a MATLAB expression such as
all(...) or abs(...), so that it evaluates to true or false; a
range of orders or horizons gives a vector, which all or any
reduces to one value. A property can also be a function handle of the
solved model and the filtering output, @(m,filtr) ..., returning
true or false; give it as {handle,level} when it needs the
predicted (level 1), updated (2) or smoothed (3) output of the
filter, which requires data on the model.
create_handle(self) returns a function of the parameter vector
with one logical output per property, in the order they were
added, as in the evaluation above. create_handle(self,true)
collapses them into a single output, the joint truth of all the
properties: true only if each of them holds. That is the handle the
Behavior slot of mcf expects when the behavior is “all of the
properties” – closing the loop from a higher-level statement of
“model properties of interest” to a low-level boolean signal driving
the sensitivity analysis.
mcf classifies a draw as behaving or not on the first output of
its handle alone and stores the others in its user_outputs
property: given create_handle(self) and the properties above, a
draw that solves but fails every other test counts as behaving. Use
that form only when the first property is the behavior and the others
are to be recorded.
Hint
With the handle/behavior in hand we can go ahead and pass it to the mcf class as above