Debian Codegraph @

tests/model-inversion/bk-4-nlsolve/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
35 a11 = A(1,1);
36 a12 = A(1,2);
37 a13 = A(1,3);
38 a21 = A(2,1);
39 a22 = A(2,2);
40 a23 = A(2,3);
41 a31 = A(3,1);
42 a32 = A(3,2);
43 a33 = A(3,3);
44 b11 = .10;
45 b12 = -.30;
46 b13 = .05;
47 b22 = .20;
48 b23 = -.05;
49 b33 = .10;
50
51 model;
52 y1 = a11*y1(-1) + a12*y2(-1) + a13*y3(-1) + b11*e1 + b12*e2 + b13*e3 + u1 ;
53 y2 = a21*y1(-1) + a22*y2(-1) + a23*y3(-1) + b22*e2 + b23*e3 + u2 ;
54 y3 = a31*y1(-1) + a32*y2(-1) + a33*y3(-1) + b33*e3 + u3 ;
55 end;
56
57 histval;
58 y1(0) = 0;
59 y2(0) = 0;
60 y3(0) = 0;
61 end;
62
63 shocks;
64 var e1 = 1.0;
65 var e2 = 1.0;
66 var e3 = 1.0;
67 var u1 = .1;
68 var u2 = .3;
69 var u3 = .2;
70 end;
71
72 steady;
73
74 check;
75
76 TrueData = simul_backward_model([], 200);
77
78 // Set the periods where some of the endogenous variables will be constrained.
79 subsample = 2Y:100Y;
80
81 // Load the generated data
82 SimulatedData = copy(TrueData);
83
84 // Set the constrained paths for the endogenous variables (Output and PhysicalCapitalStock).
85 constrainedpaths = SimulatedData{'y1','y2','y3'}(subsample);
86
87 // Set the instruments (innovations used to control the paths for the endogenous variables).
88 exogenousvariables = dseries([NaN(100, 3) TrueData{'u1','u2','u3'}.data(1:100,:)], '1Y', M_.exo_names);
89
90 /* REMARK
91 **
92 ** Here we will control y1, y2, and y3 with e1, e2 and e3, u1, u2 and u3 are treated as observed
93 ** exogenous variables.
94 */
95
96 // Invert the model by calling the model_inversion routine.
97 options_.dynatol.f = 1e-8;
98 [endogenousvariables, exogenousvariables] = model_inversion(constrainedpaths, exogenousvariables, SimulatedData, M_, options_, oo_);
99
100 // Check that all the constraints are satisfied.
101 if max(abs(constrainedpaths(subsample).y1.data-endogenousvariables(subsample).y1.data))>1e-12
102 error('Constraint on y1 path is not satisfied!')
103 end
104
105 if max(abs(constrainedpaths(subsample).y2.data-endogenousvariables(subsample).y2.data))>1e-12
106 error('Constraint on y2 path is not satisfied!')
107 end
108
109 if max(abs(constrainedpaths(subsample).y3.data-endogenousvariables(subsample).y3.data))>1e-12
110 error('Constraint on y3 path is not satisfied!')
111 end
112
113 if max(abs(exogenousvariables(subsample).u1.data-SimulatedData(subsample).u1.data))>1e-12
114 error('Constraint on u1 path is not satisfied!')
115 end
116
117 if max(abs(exogenousvariables(subsample).u2.data-SimulatedData(subsample).u2.data))>1e-12
118 error('Constraint on u2 path is not satisfied!')
119 end
120
121 if max(abs(exogenousvariables(subsample).u3.data-SimulatedData(subsample).u3.data))>1e-12
122 error('Constraint on u3 path is not satisfied!')
123 end
124
125 // Check consistency of the results.
126 if max(abs(exogenousvariables(subsample).e1.data-SimulatedData(subsample).e1.data))>1e-12
127 error('Model inversion is not consistent with true innovations (e1)')
128 end
129
130 if max(abs(exogenousvariables(subsample).e2.data-SimulatedData(subsample).e2.data))>1e-12
131 error('Model inversion is not consistent with true innovations (e3)')
132 end
133
134 if max(abs(exogenousvariables(subsample).e3.data-SimulatedData(subsample).e3.data))>1e-12
135 error('Model inversion is not consistent with true innovations (e3)')
136 end