Debian Codegraph @

tests/model-inversion/bk-7/varmodel.mod

raw
1 // --+ options: stochastic +--
2
3 /* © 2022 Dynare Team
4 *
5 * This file is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * It is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with the file. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 var y1 y2 y3 ;
20
21 varexo e1 e2 e3 u1 u2 u3 ;
22
23 parameters a11 a12 a13 a21 a22 a23 a31 a32 a33 b11 b12 b13 b22 b23 b33 ;
24
25 /*
26 ** Simulate the elements of the first order autoregressive matrix (we impose stability of the model, note that
27 ** inversion fails if the model is explosive, ie the autoregressive matrix has at least one root greater than
28 ** one in modulus) probably because of the propagation of roundoff errors.
29 */
30
31 D = diag([.9 .7 .8]);
32 P = randn(3,3);
33 A = P*D*inv(P);
34 B = [.1 -3 .5; 0 .2 -.5; 0 0 .1];
35
36 a11 = A(1,1);
37 a12 = A(1,2);
38 a13 = A(1,3);
39 a21 = A(2,1);
40 a22 = A(2,2);
41 a23 = A(2,3);
42 a31 = A(3,1);
43 a32 = A(3,2);
44 a33 = A(3,3);
45 b11 = B(1,1);
46 b12 = B(1,2);
47 b13 = B(1,3);
48 b22 = B(2,2);
49 b23 = B(2,3);
50 b33 = B(3,3);
51
52 model(linear);
53 y1 = a11*y1(-1) + a12*y2(-1) + a13*y3(-1) + b11*e1 + b12*e2 + b13*e3 + u1 ;
54 y2 = a21*y1(-1) + a22*y2(-1) + a23*y3(-1) + b22*e2 + b23*e3 + u2 ;
55 y3 = a31*y1(-1) + a32*y2(-1) + a33*y3(-1) + b33*e3 + u3 - .1*u3(-1);
56 end;
57
58 histval;
59 y1(0) = .1;
60 y2(0) = .2;
61 y3(0) = .3;
62 u3(0) = -.1;
63 end;
64
65 shocks;
66 var e1 = 0.01;
67 var e2 = 0.01;
68 var e3 = 0.01;
69 var u1 = 0.01;
70 var u2 = 0.01;
71 var u3 = 0.01;
72 end;
73
74 steady;
75
76 check;
77
78 TrueData = simul_backward_model([], 200);
79
80 // Set the periods where some of the endogenous variables will be constrained.
81 subsample = 2Y:10Y;
82
83 // Copy the generated data
84 SimulatedData = copy(TrueData);
85
86 // Set the constrained paths for the endogenous variables.
87 constrainedpaths = SimulatedData{'y1','y2','y3'}(subsample);
88
89 // Set the instruments (innovations used to control the paths for the endogenous variables).
90 exogenousvariables = dseries([NaN(200, 3) TrueData{'u1','u2','u3'}.data(2:201,:)], '2Y', M_.exo_names);
91
92 /* REMARK
93 **
94 ** Here we will control y1, y2, and y3 with e1, e2 and e3, u1, u2 and u3 are treated as observed
95 ** exogenous variables.
96 */
97
98 // Invert the model by calling the model_inversion routine.
99 [endogenousvariables, exogenousvariables] = model_inversion(constrainedpaths, exogenousvariables, SimulatedData, M_, options_, oo_);
100
101 // Check that all the constraints are satisfied.
102 if max(abs(constrainedpaths(subsample).y1.data-endogenousvariables(subsample).y1.data))>1e-12
103 error('Constraint on y1 path is not satisfied!')
104 end
105
106 if max(abs(constrainedpaths(subsample).y2.data-endogenousvariables(subsample).y2.data))>1e-12
107 error('Constraint on y2 path is not satisfied!')
108 end
109
110 if max(abs(constrainedpaths(subsample).y3.data-endogenousvariables(subsample).y3.data))>1e-12
111 error('Constraint on y3 path is not satisfied!')
112 end
113
114 if max(abs(exogenousvariables(subsample).u1.data-SimulatedData(subsample).u1.data))>1e-12
115 error('Constraint on u1 path is not satisfied!')
116 end
117
118 if max(abs(exogenousvariables(subsample).u2.data-SimulatedData(subsample).u2.data))>1e-12
119 error('Constraint on u2 path is not satisfied!')
120 end
121
122 if max(abs(exogenousvariables(subsample).u3.data-SimulatedData(subsample).u3.data))>1e-12
123 error('Constraint on u3 path is not satisfied!')
124 end
125
126 // Check consistency of the results.
127 if max(abs(exogenousvariables(subsample).e1.data-SimulatedData(subsample).e1.data))>1e-12
128 error('Model inversion is not consistent with true innovations (e1)')
129 end
130
131 if max(abs(exogenousvariables(subsample).e2.data-SimulatedData(subsample).e2.data))>1e-12
132 error('Model inversion is not consistent with true innovations (e2)')
133 end
134
135 if max(abs(exogenousvariables(subsample).e3.data-SimulatedData(subsample).e3.data))>1e-12
136 error('Model inversion is not consistent with true innovations (e3)')
137 end