Debian Codegraph @

tests/shock_decomposition/shock_decomp_backward.mod

raw
1 // --+ options: json=compute, stochastic +--
2
3 /* This file tests the matlab/backward/shock_decomposition_backward.m routine,
4 used for shock decomposition of nonlinear backward models */
5
6 var x1 x2 x3 x1bar x2bar z y x u v s;
7
8 varexo ex1
9 ex2
10 ex1bar (used='estimationonly')
11 ex2bar (used='estimationonly')
12 ez
13 ey
14 ex
15 eu
16 ev
17 es;
18
19 parameters
20 rho_1 rho_2 rho_3 rho_4
21 a_x1_0 a_x1_1 a_x1_2 a_x1_x2_1 a_x1_x2_2
22 a_x2_0 a_x2_1 a_x2_2 a_x2_x1_1 a_x2_x1_2
23 e_c_m c_z_1 c_z_2 c_z_dx2 c_z_u c_z_dv c_z_s cx cy beta
24 lambda
25 px3;
26
27 rho_1 = .9;
28 rho_2 = -.2;
29 rho_3 = .4;
30 rho_4 = -.3;
31
32
33 a_x1_0 = -.9;
34 a_x1_1 = .4;
35 a_x1_2 = .3;
36 a_x1_x2_1 = .1;
37 a_x1_x2_2 = .2;
38
39 a_x2_0 = -.9;
40 a_x2_1 = .2;
41 a_x2_2 = -.1;
42 a_x2_x1_1 = -.1;
43 a_x2_x1_2 = .2;
44
45 beta = .2;
46 e_c_m = .5;
47 c_z_1 = .2;
48 c_z_2 = -.1;
49 c_z_dx2 = .3;
50 c_z_u = .3;
51 c_z_dv = .4;
52 c_z_s = -.2;
53 cx = 1.0;
54 cy = 1.0;
55
56
57 lambda = 0.5; // Share of optimizing agents.
58
59 px3 = -.1;
60
61 trend_component_model(model_name=toto, eqtags=['eq:x1', 'eq:x2', 'eq:x1bar', 'eq:x2bar'], targets=['eq:x1bar', 'eq:x2bar']);
62
63 pac_model(auxiliary_model_name=toto, discount=beta, model_name=pacman);
64
65 model;
66
67 [name='eq:s']
68 s = .3*s(-1) - .1*s(-2) + es;
69
70 [name='eq:diff(v)']
71 diff(v) = .5*diff(v(-1)) + ev;
72
73 [name='eq:u']
74 u = .5*u(-1) - .2*u(-2) + eu;
75
76 [name='eq:y']
77 y = rho_1*y(-1) + rho_2*y(-2) + ey;
78
79 [name='eq:x']
80 x = rho_3*x(-1) + rho_4*x(-2) + ex;
81
82 [name='eq:x1']
83 diff(x1) = a_x1_0*(x1(-1)-x1bar(-1)) + a_x1_1*diff(x1(-1)) + a_x1_2*diff(x1(-2)) + a_x1_x2_1*diff(x2(-1)) + a_x1_x2_2*diff(x2(-2)) + ex1;
84
85 [name='eq:x2']
86 diff(x2) = a_x2_0*(x2(-1)-x2bar(-1)) + a_x2_1*diff(x1(-1)) + a_x2_2*diff(x1(-2)) + a_x2_x1_1*diff(x2(-1)) + a_x2_x1_2*diff(x2(-2)) + ex2;
87
88 [name='eq:x3']
89 x3 = px3*x + y ;
90
91 [name='eq:x1bar']
92 x1bar = x1bar(-1) + ex1bar;
93
94 [name='eq:x2bar']
95 x2bar = x2bar(-1) + ex2bar;
96
97 [name='zpac']
98 diff(z) = lambda*(e_c_m*(x1(-1)-z(-1)) + c_z_1*diff(z(-1)) + c_z_2*diff(z(-2)) + pac_expectation(pacman) + c_z_s*s + c_z_dv*diff(v) ) + (1-lambda)*( cy*y + cx*x) + c_z_u*u + c_z_dx2*diff(x2) + ez;
99
100 end;
101
102 shocks;
103 var ex1 = 1.0;
104 var ex2 = 1.0;
105 var ex1bar = 1.0;
106 var ex2bar = 1.0;
107 var ez = 1.0;
108 var ey = 0.1;
109 var ex = 0.1;
110 var eu = 0.05;
111 var ev = 0.05;
112 var es = 0.07;
113 end;
114
115 shock_groups;
116 g1 = ex, ey, ez;
117 g2 = eu, ev;
118 end;
119
120
121 // Initialize the PAC model (build the Companion VAR representation for the auxiliary model).
122 pac.initialize('pacman');
123
124 // Update the parameters of the PAC expectation model (h0 and h1 vectors).
125 pac.update.expectation('pacman');
126
127 // Set initial conditions to zero for non logged variables, and one for logged variables
128 init = zeros(10, M_.endo_nbr+M_.exo_nbr);
129 initialconditions = dseries(init, 2000Q1, vertcat(M_.endo_names,M_.exo_names));
130
131 // Simulate the model for 500 periods
132 TrueData = simul_backward_model(initialconditions, 500);
133 options_.TeX=true;
134 decomposition = shock_decomposition_backward(TrueData, initialconditions, { 'g1', 'g2', 'es' }, { 'z', 'y', 'u'});
135
136 % Verify that y is only influenced by g1 (which contains ey)
137 y_idx = find(strcmp(M_.endo_names, 'y'));
138 if ~(all(all(decomposition(y_idx, 2:3, :) == 0)) && all(all(decomposition(y_idx, 1, :) ~= 0)))
139 error('Wrong decomposition for y')
140 end
141
142 % Verify that u is only influenced by g2 (which contains eu)
143 u_idx = find(strcmp(M_.endo_names, 'u'));
144 if ~(all(all(decomposition(u_idx, [1 3], :) == 0)) && all(all(decomposition(u_idx, 2, :) ~= 0)))
145 error('Wrong decomposition for u')
146 end