Debian Codegraph @

tests/kalman_skewed/ireland2004_ml.mod

raw
1 % Various tests within Maximum Likelihood estimation
2 % with skew normally distributed shocks
3 % -------------------------------------------------------------------------
4
5 % Copyright © 2025 Dynare Team
6 %
7 % This file is part of Dynare.
8 %
9 % Dynare is free software: you can redistribute it and/or modify
10 % it under the terms of the GNU General Public License as published by
11 % the Free Software Foundation, either version 3 of the License, or
12 % (at your option) any later version.
13 %
14 % Dynare is distributed in the hope that it will be useful,
15 % but WITHOUT ANY WARRANTY; without even the implied warranty of
16 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 % GNU General Public License for more details.
18 %
19 % You should have received a copy of the GNU General Public License
20 % along with Dynare. If not, see <https://www.gnu.org/licenses/>.
21
22 @#include "ireland2004_model.inc"
23
24 estimated_params;
25 stderr eta_a, 3.01673, 0, 10;
26 stderr eta_e, 0.02476, 0, 10;
27 stderr eta_z, 0.88648, 0, 10;
28 stderr eta_r, 0.27903, 0, 10;
29 OMEGA, 0.05809, 0, 1;
30 RHO_PI, 0.38648, 0, 1;
31 RHO_G, 0.39601, 0, 1;
32 RHO_X, 0.16539, 0, 1;
33 RHO_A, 0.90480, 0, 1;
34 RHO_E, 0.99067, 0, 1;
35 end;
36
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 % TEST 1: Gaussian and pruned skewed kalman filter should run without error %
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40
41 % use Gaussian Kalman filter
42 estimation(datafile = 'ireland2004_post1980.csv'
43 ,mode_compute = 8
44 ,silent_optimizer
45 ,lik_init = 1 % initialize Kalman filter at Gaussian steady-state distribution
46 ,kalman_algo = 1 % use Gaussian Kalman filter
47 );
48 struct0 = cell2struct([struct2cell(oo_.mle_mode.parameters); struct2cell(oo_.mle_mode.shocks_std); oo_.posterior.optimization.log_density], ...
49 [fieldnames(oo_.mle_mode.parameters); [strcat('stderr_', fieldnames(oo_.mle_mode.shocks_std)); {'loglik'}]]);
50 smoothed_shocks_0 = oo_.SmoothedShocks;
51
52 % use Pruned Skewed Kalman filter as Gaussian distribution is special case of CSN distribution
53 estimation(datafile = 'ireland2004_post1980.csv'
54 ,mode_compute = 8
55 ,silent_optimizer
56 ,lik_init = 1 % initialize Kalman filter at Gaussian steady-state distribution
57 ,kalman_algo = 5 % use pruned skewed Kalman filter routine even in Gaussian case for comparability;
58 % note that Dynare's implementation is much faster because it switches to the steady-state Kalman filter which we have not implemented yet for the PSKF
59 ,skewed_kalman_mvnlogcdf = 'mvncdf' % test whether the option is passed correctly, in Gaussian case there is no curse of increasing skewness dimension (as there is no need to compute Gaussian cdfs)
60 );
61 struct5 = cell2struct([struct2cell(oo_.mle_mode.parameters); struct2cell(oo_.mle_mode.shocks_std); oo_.posterior.optimization.log_density], ...
62 [fieldnames(oo_.mle_mode.parameters); [strcat('stderr_', fieldnames(oo_.mle_mode.shocks_std)); {'loglik'}]]);
63 smoothed_shocks_5 = oo_.SmoothedShocks;
64
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 % TEST 2: skewed_kalman_mvnlogcdf option should have been set to 'mvncdf' %
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 if ~strcmp(options_.skewed_kalman.mvnlogcdf,'mvncdf')
69 error('option ''skewed_kalman_mvnlogcdf'' was not set correctly')
70 end
71
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 % TEST 3: Estimation of Gaussian model with either Gaussian %
74 % or Pruned Skewed Kalman filter should be numerically %
75 % equal to each other %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 fprintf('Comparing estimation results:\n');
78 fprintf('%-15s %15s %15s %15s %10s\n', 'Parameter', 'Gaussian KF', 'Skewed KF', 'Max Abs Dev', 'Status');
79 fprintf('%s\n', repmat('-', 1, 85));
80 status = nan(length(fieldnames(struct0)),1);
81 for i = 1:length(fieldnames(struct0))
82 field_name = fieldnames(struct0);
83 max_abs_dev = max(abs(struct0.(field_name{i}) - struct5.(field_name{i})));
84 status(i) = (max_abs_dev > 1e-4);
85 if status(i); status_str = 'FAIL'; else status_str = 'PASS'; end
86 fprintf('%-15s %15f %15f %15.6e %10s\n', field_name{i}, struct0.(field_name{i}), struct5.(field_name{i}), max_abs_dev, status_str);
87 end
88 if any(status)
89 error('Estimates with Gaussian KF (kalman_algo=0) are not numerically equal to Pruned Skewed KF (kalman_algo=5), something is wrong!')
90 end
91
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % TEST 4: Smoothed shock values of Gaussian model with either Gaussian %
94 % or Pruned Skewed Kalman smoother should be numerically %
95 % equal to each other %
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 if ~isoctave % array2table is not available in Octave
98 fprintf('\n<strong>Smoothed Gaussian eta_r</strong>\n');
99 disp(array2table([smoothed_shocks_0.eta_r smoothed_shocks_5.eta_r (smoothed_shocks_0.eta_r-smoothed_shocks_5.eta_r)], ...
100 'RowNames', "t="+string(1:options_.nobs), ...
101 'VariableNames', ["kalman_algo=0", "kalman_algo=5", "dev"]));
102 fprintf('\n<strong>Smoothed Gaussian eta_z</strong>\n');
103 disp(array2table([smoothed_shocks_0.eta_z smoothed_shocks_5.eta_z (smoothed_shocks_0.eta_z-smoothed_shocks_5.eta_z)], ...
104 'RowNames', "t="+string(1:options_.nobs), ...
105 'VariableNames', ["kalman_algo=0", "kalman_algo=5", "dev"]));
106 fprintf('\n<strong>Smoothed Gaussian eta_e</strong>\n');
107 disp(array2table([smoothed_shocks_0.eta_e smoothed_shocks_5.eta_e (smoothed_shocks_0.eta_e-smoothed_shocks_5.eta_e)], ...
108 'RowNames', "t="+string(1:options_.nobs), ...
109 'VariableNames', ["kalman_algo=0", "kalman_algo=5", "dev"]));
110 fprintf('\n<strong>Smoothed Gaussian eta_a</strong>\n');
111 disp(array2table([smoothed_shocks_0.eta_a smoothed_shocks_5.eta_a (smoothed_shocks_0.eta_a-smoothed_shocks_5.eta_a)], ...
112 'RowNames', "t="+string(1:options_.nobs), ...
113 'VariableNames', ["kalman_algo=0", "kalman_algo=5", "dev"]));
114
115 end
116
117 if any(abs(smoothed_shocks_0.eta_r - smoothed_shocks_5.eta_r) > 1e-3) || ...
118 any(abs(smoothed_shocks_0.eta_z - smoothed_shocks_5.eta_z) > 1e-3) || ...
119 any(abs(smoothed_shocks_0.eta_e - smoothed_shocks_5.eta_e) > 1e-3) || ...
120 any(abs(smoothed_shocks_0.eta_a - smoothed_shocks_5.eta_a) > 1e-3)
121 error('Smoothed values should be numerically equal to each other')
122 end
123
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % TEST 3: Estimation of skewness coefficients can be successfully done with %
126 % Pruned Skewed Kalman filter, estimation should run without error %
127 % and estimation options should have be correctly changed %
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 OMEGA = 0.15356;
130 RHO_PI = 0.28761;
131 RHO_G = 0.33795;
132 RHO_X = 0.28325;
133 RHO_A = 0.91704;
134 RHO_E = 0.98009;
135
136 shocks;
137 var eta_a; stderr 2.5232;
138 var eta_e; stderr 0.021228;
139 var eta_z; stderr 0.79002;
140 var eta_r; stderr 0.28384;
141 skew eta_z = -0.9950;
142 end;
143
144 estimated_params(overwrite);
145 OMEGA, 0.15356;
146 stderr eta_e, 0.021228;
147 skew eta_a, -0.1948;
148 skew eta_e, -0.2140;
149 skew eta_r, 0.8128;
150 end;
151
152 % should run without error
153 estimation(datafile = 'ireland2004_post1980.csv'
154 ,mode_compute = 5
155 ,silent_optimizer
156 ,kalman_algo = 5 % use pruned skewed Kalman filter
157 ,lik_init = 1 % initialize Kalman filter at Gaussian steady-state distribution
158 ,skewed_kalman_prune_tol = 0.02
159 ,skewed_kalman_rank_deficiency_transform
160 ,skewed_kalman_mvnlogcdf = 'gaussian_log_mvncdf_mendell_elston'
161 ,tex
162 );
163
164 % check whether options have been correctly passed by the preprocessor
165 if (options_.skewed_kalman.prune_tol ~= 0.02) || ...
166 (options_.skewed_kalman.rank_deficiency_transform ~= true) || ...
167 ~strcmp(options_.skewed_kalman.mvnlogcdf, 'gaussian_log_mvncdf_mendell_elston')
168 error('skewed kalman options were not passed correctly')
169 end
170
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % TEST 4: Likelihood with skew normal model should be higher than with Gaussian model %
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 loglik_csn = oo_.posterior.optimization.log_density; % expected to be approx 1215.68
175 if (loglik_csn-1215.6815424) > 1e-5
176 error('Something wrong with CSN likelihood')
177 end
178 loglik_gauss = struct5.loglik; % expected to be approx 1207.56
179 if (loglik_gauss-1207.5618755) > 1e-5
180 error('Something wrong with Gaussian likelihood')
181 end
182 lr_stat = 2 * (loglik_csn - loglik_gauss);
183 lr_pval = 1 - chi2cdf(lr_stat, 4); % 4 additional shock parameters with CSN (only if one would have estimated all parameters, here we don't do this to speed up testsuite)
184 lr_str = sprintf('test statistic of $%.2f$ and a p-value of $%.4f$.', lr_stat, lr_pval);
185 fprintf('\n%s\n* LIKELIHOOD RATIO TEST *\n%s\n', repmat('*',1,25), repmat('*',1,25));
186 fprintf('- unconstrained log-likelihood (CSN): %.4f\n', loglik_csn);
187 fprintf('- constrained log-likelihood (Gaussian): %.4f\n', loglik_gauss);
188 fprintf('- %s\n\n',lr_str);