| 1 |
% Various tests for simulations with skew normally distributed shocks |
| 2 |
% ------------------------------------------------------------------------- |
| 3 |
|
| 4 |
% Copyright © 2025 Dynare Team |
| 5 |
% |
| 6 |
% This file is part of Dynare. |
| 7 |
% |
| 8 |
% Dynare is free software: you can redistribute it and/or modify |
| 9 |
% it under the terms of the GNU General Public License as published by |
| 10 |
% the Free Software Foundation, either version 3 of the License, or |
| 11 |
% (at your option) any later version. |
| 12 |
% |
| 13 |
% Dynare is distributed in the hope that it will be useful, |
| 14 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
% GNU General Public License for more details. |
| 17 |
% |
| 18 |
% You should have received a copy of the GNU General Public License |
| 19 |
% along with Dynare. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
@#include "ireland2004_model.inc" |
| 22 |
var rAnnualized piAnnualized; |
| 23 |
model; |
| 24 |
rAnnualized = 4*rhat; |
| 25 |
piAnnualized = 4*pihat; |
| 26 |
end; |
| 27 |
|
| 28 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 29 |
% TEST 1: check user-facing set_*_value functions % |
| 30 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 31 |
set_param_value('OMEGA', 0.15356); |
| 32 |
set_param_value('RHO_PI', 0.28761); |
| 33 |
set_param_value('RHO_G', 0.33795); |
| 34 |
set_param_value('RHO_X', 0.28325); |
| 35 |
set_param_value('RHO_A', 0.91704); |
| 36 |
set_param_value('RHO_E', 0.98009); |
| 37 |
set_shock_stderr_value('eta_a',2.5232); |
| 38 |
set_shock_stderr_value('eta_e',0.021228); |
| 39 |
set_shock_stderr_value('eta_z',0.79002); |
| 40 |
set_shock_stderr_value('eta_r',0.28384); |
| 41 |
set_shock_skew_value('eta_a',-0.1948); % single shock input |
| 42 |
set_shock_skew_value('eta_e','eta_e','eta_e',-0.21401); % triple shock input |
| 43 |
set_shock_skew_value('eta_z',-0.99527); |
| 44 |
set_shock_skew_value('eta_r',0.81275); |
| 45 |
|
| 46 |
if ~isequal(M_.params, [0.99; 0.1; 0; 0; 0.91704; 0.98009; 0.15356; 0.28761; 0.33795; 0.28325; 1]) |
| 47 |
error('set_param_value did not set values correctly') |
| 48 |
end |
| 49 |
if ~isequal(M_.Sigma_e,diag([2.5232^2; 0.021228^2; 0.79002^2; 0.28384^2])) |
| 50 |
error('set_shock_stderr_value did not set values correctly') |
| 51 |
end |
| 52 |
% Helper to look up skewness value from sparse Skew_e (sorts indices, returns 0 if not found) |
| 53 |
skew_lookup = @(S,i,j,k) sum(S(S(:,1)==min([i j k]) & S(:,2)==median([i j k]) & S(:,3)==max([i j k]), 4)); |
| 54 |
if ~isequal(skew_lookup(M_.Skew_e,1,1,1),-0.1948) || ~isequal(skew_lookup(M_.Skew_e,2,2,2),-0.21401) || ~isequal(skew_lookup(M_.Skew_e,3,3,3),-0.99527) || ~isequal(skew_lookup(M_.Skew_e,4,4,4),0.81275) |
| 55 |
error('set_shock_skew_value did not set values correctly') |
| 56 |
end |
| 57 |
|
| 58 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 59 |
% TEST 2: compute quantiles of CSN distribution and use for IRFs % |
| 60 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 61 |
% note that skew normal is special case of CSN |
| 62 |
csn = csn_update_specification(M_.Sigma_e, M_.Skew_e); |
| 63 |
csn_q16 = csn_quantile(0.16, csn.mu_e(4,1), csn.Sigma_e(4,4), csn.Gamma_e(4,4), 0, 1, 'mvncdf'); |
| 64 |
csn_q84 = csn_quantile(0.84, csn.mu_e(4,1), csn.Sigma_e(4,4), csn.Gamma_e(4,4), 0, 1, 'mvncdf'); |
| 65 |
csn_target=[-0.273121782371810, 0.283270501374202]; |
| 66 |
|
| 67 |
if isoctave |
| 68 |
tol=[1e-10 1e-06]; |
| 69 |
else |
| 70 |
tol=[1e-15 1e-15]; |
| 71 |
end |
| 72 |
|
| 73 |
if abs(csn_q16 - csn_target(1)) > tol(1) || abs(csn_q84 - csn_target(2)) > tol(2) |
| 74 |
error('CSN quantiles have not been computed correctly'); |
| 75 |
end |
| 76 |
|
| 77 |
% Note on IRF computations: |
| 78 |
% Dynare always computes irfs with respect to a one standard deviation shock, |
| 79 |
% which is provided by the user in the shocks block. |
| 80 |
% Therefore, we use the shocks block to set the stderr to the quantiles |
| 81 |
% and adjust the auxiliary parameter SIGN_SHOCKS in the mod file to get the |
| 82 |
% correct sign on shocks (whether positive or negative). |
| 83 |
|
| 84 |
% showcase how to simulate negative shocks, i.e. 16th quantiles |
| 85 |
SIGN_SHOCKS = -1; % flip sign of shocks for 16th quantiles as they are negative |
| 86 |
shocks; |
| 87 |
var eta_r; stderr (csn_q16); |
| 88 |
end; |
| 89 |
stoch_simul(order=1, periods=0, irf=15, nodecomposition, nomoments, nocorr, nofunctions, nograph) rAnnualized piAnnualized xhat; |
| 90 |
irfs_csn_neg = oo_.irfs; |
| 91 |
|
| 92 |
% showcase how to simulate positive shocks, i.e. 84th quantile |
| 93 |
SIGN_SHOCKS = 1; % don't flip sign of shocks for 84th quantiles as they are positive |
| 94 |
shocks; |
| 95 |
var eta_r; stderr (csn_q84); |
| 96 |
end; |
| 97 |
stoch_simul(order=1, periods=0, irf=15, nodecomposition, nomoments, nocorr, nofunctions, nograph) rAnnualized piAnnualized xhat; |
| 98 |
irfs_csn_pos = oo_.irfs; |
| 99 |
|
| 100 |
|
| 101 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 102 |
% TEST 3: simulate data with skew normally distributed shocks % |
| 103 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 104 |
@#for DISTRIB in ["N", "SN"] // N: Gaussian, SN: skew normal distribution |
| 105 |
shocks; |
| 106 |
var eta_a = 0.1^2; |
| 107 |
var eta_e = 0.2^2; |
| 108 |
var eta_z = 0.3^2; |
| 109 |
var eta_r = 0.4^2; |
| 110 |
@#if DISTRIB == "N" |
| 111 |
skew eta_a = 0; |
| 112 |
skew eta_e = 0; |
| 113 |
skew eta_z = 0; |
| 114 |
skew eta_r = 0; |
| 115 |
@#elseif DISTRIB == "SN" |
| 116 |
skew eta_a = -0.2; |
| 117 |
skew eta_e = -0.6; |
| 118 |
skew eta_z = -0.9; |
| 119 |
skew eta_r = +0.8; |
| 120 |
@#endif |
| 121 |
end; |
| 122 |
|
| 123 |
set_dynare_seed(132); |
| 124 |
stoch_simul(order=1,periods=500000,irf=0,nodecomposition,nomoments,nocorr,nofunctions,nograph); |
| 125 |
send_exogenous_variables_to_workspace; |
| 126 |
exo_@{DISTRIB} = oo_.exo_simul; |
| 127 |
if ~isoctave % histogram is not available in Octave |
| 128 |
hh_fig = dyn_figure(options_.nodisplay,'Name','Histogram of @{DISTRIB} shocks'); |
| 129 |
subplot(2,2,1); histogram(eta_a,'normalization','pdf'); title('\eta_a'); |
| 130 |
subplot(2,2,2); histogram(eta_e,'normalization','pdf'); title('\eta_e'); |
| 131 |
subplot(2,2,3); histogram(eta_z,'normalization','pdf'); title('\eta_z'); |
| 132 |
subplot(2,2,4); histogram(eta_r,'normalization','pdf'); title('\eta_r'); |
| 133 |
sgtitle(hh_fig.Name); |
| 134 |
end |
| 135 |
@#endfor |
| 136 |
|
| 137 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 138 |
% TEST 4: compare empirical statistics to theoretical moments for exogenous shocks % |
| 139 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 140 |
statNames = {'Mean', 'Median', 'StdDev', 'Variance', 'Skewness', 'Kurtosis', 'Min', 'Max'}; |
| 141 |
stats_N = nan(M_.exo_nbr, length(statNames)); |
| 142 |
stats_SN = nan(M_.exo_nbr, length(statNames)); |
| 143 |
for i = 1:M_.exo_nbr |
| 144 |
@#for DISTRIB in ["N", "SN"] |
| 145 |
stats_@{DISTRIB}(i, 1) = mean(exo_@{DISTRIB}(:, i)); |
| 146 |
stats_@{DISTRIB}(i, 2) = median(exo_@{DISTRIB}(:, i)); |
| 147 |
stats_@{DISTRIB}(i, 3) = std(exo_@{DISTRIB}(:, i)); |
| 148 |
stats_@{DISTRIB}(i, 4) = var(exo_@{DISTRIB}(:, i)); |
| 149 |
stats_@{DISTRIB}(i, 5) = skewness(exo_@{DISTRIB}(:, i)); |
| 150 |
stats_@{DISTRIB}(i, 6) = kurtosis(exo_@{DISTRIB}(:, i)); |
| 151 |
stats_@{DISTRIB}(i, 7) = min(exo_@{DISTRIB}(:, i)); |
| 152 |
stats_@{DISTRIB}(i, 8) = max(exo_@{DISTRIB}(:, i)); |
| 153 |
@#endfor |
| 154 |
end |
| 155 |
if ~isoctave % array2table is not available in Octave |
| 156 |
fprintf('<strong>DESCRIPTIVE STATISTICS FOR NORMAL SHOCKS</strong>\n'); |
| 157 |
disp(array2table(stats_N, 'VariableNames', statNames, 'RowNames', M_.exo_names)); |
| 158 |
fprintf('<strong>DESCRIPTIVE STATISTICS FOR SKEW NORMAL SHOCKS</strong>\n'); |
| 159 |
disp(array2table(stats_SN, 'VariableNames', statNames, 'RowNames', M_.exo_names)); |
| 160 |
end |
| 161 |
% error out if something is not close to theoretical value |
| 162 |
if any((abs(mean(exo_N)) > 1e-3)) |
| 163 |
error('Mean of normal shocks should be close to zero!') |
| 164 |
end |
| 165 |
if any(abs(mean(exo_SN)) > 1e-3) |
| 166 |
error('Mean of skew normal shocks should be close to zero!') |
| 167 |
end |
| 168 |
if any(abs(std(exo_N) - transpose(sqrt(diag(M_.Sigma_e)))) > 1e-3) |
| 169 |
error('Standard deviation of normal shocks should be close to theoretical values in M_.Sigme_e!') |
| 170 |
end |
| 171 |
if any(abs(std(exo_SN) - transpose(sqrt(diag(M_.Sigma_e)))) > 1e-3) |
| 172 |
error('Standard deviation of skew normal shocks should be close to theoretical values in M_.Sigme_e!') |
| 173 |
end |
| 174 |
if any(abs(skewness(exo_N)) > 1e-2) |
| 175 |
error('Skewness coefficient of normal shocks should be close to 0!') |
| 176 |
end |
| 177 |
sorted_Skew = sortrows(M_.Skew_e, 1); |
| 178 |
if any(abs(skewness(exo_SN)-transpose(sorted_Skew(:,4))) > 1e-2) |
| 179 |
error('Skewness coefficient of skew normal shocks should be close to theoretical values in M_.Skew_e!') |
| 180 |
end |