| 1 |
// --+ options: json=compute, stochastic +-- |
| 2 |
|
| 3 |
var x1 x2 x1bar x2bar z y gg; |
| 4 |
|
| 5 |
varexo ex1 ex2 ex1bar ex2bar ez ey g; |
| 6 |
|
| 7 |
parameters |
| 8 |
rho_1 rho_2 |
| 9 |
a_x1_0 a_x1_1 a_x1_2 a_x1_x2_1 a_x1_x2_2 |
| 10 |
a_x2_0 a_x2_1 a_x2_2 a_x2_x1_1 a_x2_x1_2 |
| 11 |
e_c_m c_z_1 c_z_2 beta ; |
| 12 |
|
| 13 |
rho_1 = .9; |
| 14 |
rho_2 = -.2; |
| 15 |
|
| 16 |
a_x1_0 = -.9; |
| 17 |
a_x1_1 = .4; |
| 18 |
a_x1_2 = .3; |
| 19 |
a_x1_x2_1 = .1; |
| 20 |
a_x1_x2_2 = .2; |
| 21 |
|
| 22 |
a_x2_0 = -.9; |
| 23 |
a_x2_1 = .2; |
| 24 |
a_x2_2 = -.1; |
| 25 |
a_x2_x1_1 = -.1; |
| 26 |
a_x2_x1_2 = .2; |
| 27 |
|
| 28 |
beta = .2; |
| 29 |
e_c_m = .5; |
| 30 |
c_z_1 = .2; |
| 31 |
c_z_2 = -.1; |
| 32 |
|
| 33 |
trend_component_model(model_name=toto, eqtags=['eq:x1', 'eq:x2', 'eq:x1bar', 'eq:x2bar'], targets=['eq:x1bar', 'eq:x2bar']); |
| 34 |
|
| 35 |
pac_model(auxiliary_model_name=toto, discount=beta, growth=0.5*gg(-1)+beta+ex1, model_name=pacman); |
| 36 |
|
| 37 |
model; |
| 38 |
|
| 39 |
[name='eq:gg'] |
| 40 |
gg = g; |
| 41 |
|
| 42 |
[name='eq:y'] |
| 43 |
y = rho_1*y(-1) + rho_2*y(-2) + ey; |
| 44 |
|
| 45 |
[name='eq:x1'] |
| 46 |
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; |
| 47 |
|
| 48 |
[name='eq:x2'] |
| 49 |
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; |
| 50 |
|
| 51 |
[name='eq:x1bar'] |
| 52 |
x1bar = x1bar(-1) + ex1bar; |
| 53 |
|
| 54 |
[name='eq:x2bar'] |
| 55 |
x2bar = x2bar(-1) + ex2bar; |
| 56 |
|
| 57 |
[name='zpac'] |
| 58 |
diff(z) = e_c_m*(x1(-1)-z(-1)) + c_z_1*diff(z(-1)) + c_z_2*diff(z(-2)) + pac_expectation(pacman) + ez; |
| 59 |
|
| 60 |
end; |
| 61 |
|
| 62 |
// Initialize the PAC model (build the Companion VAR representation for the auxiliary model). |
| 63 |
pac.initialize('pacman'); |
| 64 |
|
| 65 |
// Update the parameters of the PAC expectation model (h0 and h1 vectors). |
| 66 |
pac.update.expectation('pacman'); |
| 67 |
|
| 68 |
// Set initial conditions to zero. Please use more sensible values if any... |
| 69 |
initialconditions = dseries(zeros(10, M_.endo_nbr+M_.exo_nbr), 2000Q1, vertcat(M_.endo_names,M_.exo_names)); |
| 70 |
|
| 71 |
// Simulate the model for 5000 periods |
| 72 |
TrueData = simul_backward_model(initialconditions, 5000); |
| 73 |
|
| 74 |
// NLS estimation |
| 75 |
// Define a structure describing the parameters to be estimated (with initial conditions). |
| 76 |
clear eparams |
| 77 |
eparams.e_c_m = .5; |
| 78 |
eparams.c_z_1 = .2; |
| 79 |
eparams.c_z_2 =-.1; |
| 80 |
|
| 81 |
edata = TrueData; |
| 82 |
edata.ez = dseries(NaN(TrueData.nobs, 1), 2000Q1, 'ez'); |
| 83 |
pac.estimate.nls('zpac', eparams, edata, 2005Q1:2000Q1+200, 'lsqnonlin'); |
| 84 |
|
| 85 |
e_c_m_nls = M_.params(strmatch('e_c_m', M_.param_names, 'exact')); |
| 86 |
c_z_1_nls = M_.params(strmatch('c_z_1', M_.param_names, 'exact')); |
| 87 |
c_z_2_nls = M_.params(strmatch('c_z_2', M_.param_names, 'exact')); |
| 88 |
resid_nls = oo_.pac.pacman.residual; |
| 89 |
|
| 90 |
fprintf('Estimate of e_c_m: %f \n', e_c_m_nls) |
| 91 |
fprintf('Estimate of c_z_1: %f \n', c_z_1_nls) |
| 92 |
fprintf('Estimate of c_z_2: %f \n', c_z_2_nls) |
| 93 |
|
| 94 |
skipline(2) |
| 95 |
|
| 96 |
// Iterative OLS estimation using estimates from NLS |
| 97 |
// Define a structure describing the parameters to be estimated (with initial conditions). |
| 98 |
clear eparams |
| 99 |
eparams.e_c_m = e_c_m_nls; |
| 100 |
eparams.c_z_1 = c_z_1_nls; |
| 101 |
eparams.c_z_2 = c_z_2_nls; |
| 102 |
|
| 103 |
// Define the dataset used for estimation |
| 104 |
edata = TrueData; |
| 105 |
edata.ez = dseries(NaN(TrueData.nobs, 1), 2000Q1, 'ez'); |
| 106 |
pac.estimate.iterative_ols('zpac', eparams, edata, 2005Q1:2000Q1+200); |
| 107 |
|
| 108 |
// Test printing of PAC expectations |
| 109 |
pac.print('pacman','zpac'); |
| 110 |
|
| 111 |
// Print equations where the variable appears in |
| 112 |
search('x1bar') |
| 113 |
search('x2bar', 'withparamvalues') |
| 114 |
|
| 115 |
e_c_m_iterative_ols = M_.params(strmatch('e_c_m', M_.param_names, 'exact')); |
| 116 |
c_z_1_iterative_ols = M_.params(strmatch('c_z_1', M_.param_names, 'exact')); |
| 117 |
c_z_2_iterative_ols = M_.params(strmatch('c_z_2', M_.param_names, 'exact')); |
| 118 |
resid_iterative_ols = oo_.pac.pacman.residual; |
| 119 |
|
| 120 |
fprintf('Estimate of e_c_m: %f \n', e_c_m_iterative_ols) |
| 121 |
fprintf('Estimate of c_z_1: %f \n', c_z_1_iterative_ols) |
| 122 |
fprintf('Estimate of c_z_2: %f \n', c_z_2_iterative_ols) |
| 123 |
|
| 124 |
difference = abs(resid_nls-resid_iterative_ols); |
| 125 |
if any(difference.data > 1e-4) |
| 126 |
error('Iterative OLS and NLS do not provide consistent results.') |
| 127 |
end |