Debian Codegraph @

tests/pac/trend-component-28/example5.mod

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