1function oo_=disp_moments(y,var_list,M_,options_,oo_) 2% function disp_moments(y,var_list,M_,options_,oo_) 3% Displays moments of simulated variables 4% INPUTS 5% y [double] nvar*nperiods vector of simulated variables. 6% var_list [char] nvar character array with names of variables. 7% M_ [structure] Dynare's model structure 8% oo_ [structure] Dynare's results structure 9% options_ [structure] Dynare's options structure 10% 11% OUTPUTS 12% oo_ [structure] Dynare's results structure, 13 14% Copyright (C) 2001-2019 Dynare Team 15% 16% This file is part of Dynare. 17% 18% Dynare is free software: you can redistribute it and/or modify 19% it under the terms of the GNU General Public License as published by 20% the Free Software Foundation, either version 3 of the License, or 21% (at your option) any later version. 22% 23% Dynare is distributed in the hope that it will be useful, 24% but WITHOUT ANY WARRANTY; without even the implied warranty of 25% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26% GNU General Public License for more details. 27% 28% You should have received a copy of the GNU General Public License 29% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 30 31warning_old_state = warning; 32warning off 33 34if isempty(var_list) 35 var_list = M_.endo_names(1:M_.orig_endo_nbr); 36end 37 38nvar = length(var_list); 39ivar=zeros(nvar,1); 40for i=1:nvar 41 i_tmp = strmatch(var_list{i}, M_.endo_names, 'exact'); 42 if isempty(i_tmp) 43 error ('One of the variable specified does not exist') ; 44 else 45 ivar(i) = i_tmp; 46 end 47end 48 49y = y(ivar,options_.drop+1:end)'; 50 51ME_present=0; 52if ~all(M_.H==0) 53 if (isoctave && octave_ver_less_than('6')) || (~isoctave && matlab_ver_less_than('8.1')) 54 [observable_pos_requested_vars, index_subset, index_observables] = intersect_stable(ivar, options_.varobs_id); 55 else 56 [observable_pos_requested_vars, index_subset, index_observables] = intersect(ivar, options_.varobs_id, 'stable'); 57 end 58 if ~isempty(observable_pos_requested_vars) 59 ME_present=1; 60 i_ME = setdiff([1:size(M_.H,1)],find(diag(M_.H) == 0)); % find ME with 0 variance 61 chol_S = chol(M_.H(i_ME,i_ME)); %decompose rest 62 shock_mat=zeros(options_.periods,size(M_.H,1)); %initialize 63 shock_mat(:,i_ME)=randn(length(i_ME),options_.periods)'*chol_S; 64 y_ME = y(:,index_subset)+shock_mat(options_.drop+1:end,index_observables); 65 y_ME_only = shock_mat(options_.drop+1:end,index_observables); 66 m_ME = mean(y_ME); 67 y_ME=get_filtered_time_series(y_ME,m_ME,options_); 68 y_ME_only_filtered=get_filtered_time_series(y_ME_only,mean(y_ME_only),options_); 69 s2_ME = mean(y_ME.*y_ME); 70 s_ME = sqrt(s2_ME); 71 zero_variance_ME_var_index=index_subset(abs(s_ME')<options_.zero_moments_tolerance); 72 end 73end 74 75 76m = mean(y); 77 78% filter series 79y=get_filtered_time_series(y,m,options_); 80 81s2 = mean(y.*y); 82s = sqrt(s2); 83oo_.mean = transpose(m); 84oo_.var = y'*y/size(y,1); 85oo_.skewness = (mean(y.^3)./s2.^1.5)'; 86oo_.kurtosis = (mean(y.^4)./(s2.*s2)-3)'; 87 88zero_variance_var_index=find(abs(s)<options_.zero_moments_tolerance); 89oo_.skewness(zero_variance_var_index)=NaN; 90oo_.kurtosis(zero_variance_var_index)=NaN; 91s(zero_variance_var_index)=0; 92s2(zero_variance_var_index)=0; 93oo_.var(zero_variance_var_index,:)=0; 94oo_.var(:,zero_variance_var_index)=0; 95 96 97labels = M_.endo_names(ivar); 98labels_TeX = M_.endo_names_tex(ivar); 99 100if ~options_.nomoments 101 z = [ m' s' s2' oo_.skewness oo_.kurtosis ]; 102 title='MOMENTS OF SIMULATED VARIABLES'; 103 title=add_filter_subtitle(title, options_); 104 headers = {'VARIABLE'; 'MEAN'; 'STD. DEV.'; 'VARIANCE'; 'SKEWNESS'; 'KURTOSIS'}; 105 dyntable(options_, title, headers, labels, z, cellofchararraymaxlength(labels)+2, 16, 6); 106 if options_.TeX 107 dyn_latex_table(M_, options_, title, 'sim_moments', headers, labels_TeX, z, cellofchararraymaxlength(labels)+2, 16, 6); 108 end 109end 110 111if ~options_.nocorr 112 corr = (y'*y/size(y,1))./(s'*s); 113 corr(zero_variance_var_index,:)=NaN; 114 corr(:,zero_variance_var_index)=NaN; 115 if options_.contemporaneous_correlation 116 oo_.contemporaneous_correlation = corr; 117 end 118 if ~options_.noprint 119 title = 'CORRELATION OF SIMULATED VARIABLES'; 120 title=add_filter_subtitle(title,options_); 121 headers = vertcat('VARIABLE', M_.endo_names(ivar)); 122 dyntable(options_, title, headers, labels, corr, cellofchararraymaxlength(labels)+2, 8, 4); 123 if options_.TeX 124 headers = vertcat('VARIABLE', M_.endo_names_tex(ivar)); 125 lh = cellofchararraymaxlength(labels)+2; 126 dyn_latex_table(M_, options_, title, 'sim_corr_matrix', headers, labels_TeX, corr, lh, 8,4); 127 end 128 end 129end 130 131if ~options_.noprint && length(options_.conditional_variance_decomposition) 132 fprintf('\nSTOCH_SIMUL: conditional_variance_decomposition requires theoretical moments, i.e. periods=0.\n') 133end 134 135ar = options_.ar; 136if ar > 0 137 autocorr = []; 138 for i=1:ar 139 oo_.autocorr{i} = y(ar+1:end,:)'*y(ar+1-i:end-i,:)./((size(y,1)-ar)*std(y(ar+1:end,:))'*std(y(ar+1-i:end-i,:))); 140 oo_.autocorr{i}(zero_variance_var_index,:)=NaN; 141 oo_.autocorr{i}(:,zero_variance_var_index)=NaN; 142 autocorr = [ autocorr diag(oo_.autocorr{i}) ]; 143 end 144 if ~options_.noprint 145 title = 'AUTOCORRELATION OF SIMULATED VARIABLES'; 146 title=add_filter_subtitle(title,options_); 147 headers = vertcat('VARIABLE', cellstr(int2str([1:ar]'))); 148 dyntable(options_, title, headers, labels, autocorr, cellofchararraymaxlength(labels)+2, 8, 4); 149 if options_.TeX 150 headers = vertcat('VARIABLE', cellstr(int2str([1:ar]'))); 151 lh = cellofchararraymaxlength(labels)+2; 152 dyn_latex_table(M_, options_, title, 'sim_autocorr_matrix', headers, labels_TeX, autocorr, cellofchararraymaxlength(labels_TeX)+2, 8, 4); 153 end 154 end 155 156end 157 158 159if ~options_.nodecomposition 160 if M_.exo_nbr == 1 161 oo_.variance_decomposition = 100*ones(nvar,1); 162 else 163 oo_.variance_decomposition=zeros(nvar,M_.exo_nbr); 164 %get starting values 165 if isempty(M_.endo_histval) 166 y0 = oo_.dr.ys; 167 else 168 if options_.loglinear 169 y0 = log_variable(1:M_.endo_nbr,M_.endo_histval,M_); 170 else 171 y0 = M_.endo_histval; 172 end 173 end 174 %back out shock matrix used for generating y 175 i_exo_var = setdiff([1:M_.exo_nbr],find(diag(M_.Sigma_e) == 0)); % find shocks with 0 variance 176 chol_S = chol(M_.Sigma_e(i_exo_var,i_exo_var)); %decompose rest 177 shock_mat=zeros(options_.periods,M_.exo_nbr); %initialize 178 shock_mat(:,i_exo_var)=oo_.exo_simul(:,i_exo_var)/chol_S; %invert construction of oo_.exo_simul from simult.m 179 180 for shock_iter=1:length(i_exo_var) 181 temp_shock_mat=zeros(size(shock_mat)); 182 temp_shock_mat(:,i_exo_var(shock_iter))=shock_mat(:,i_exo_var(shock_iter)); 183 temp_shock_mat(:,i_exo_var) = temp_shock_mat(:,i_exo_var)*chol_S; 184 y_sim_one_shock = simult_(M_,options_,y0,oo_.dr,temp_shock_mat,options_.order); 185 y_sim_one_shock=y_sim_one_shock(ivar,1+options_.drop+1:end)'; 186 y_sim_one_shock=get_filtered_time_series(y_sim_one_shock,mean(y_sim_one_shock),options_); 187 oo_.variance_decomposition(:,i_exo_var(shock_iter))=var(y_sim_one_shock)./s2*100; 188 end 189 oo_.variance_decomposition(zero_variance_var_index,:)=NaN; 190 if ME_present 191 oo_.variance_decomposition_ME=oo_.variance_decomposition(index_subset,:)... 192 .*repmat((s2(index_subset)./s2_ME)',1,length(i_exo_var)); 193 oo_.variance_decomposition_ME(:,end+1)=var(y_ME_only_filtered)./s2_ME*100; 194 oo_.variance_decomposition_ME(ismember(observable_pos_requested_vars,intersect(zero_variance_ME_var_index,zero_variance_var_index)),:)=NaN; 195 oo_.variance_decomposition_ME(ismember(observable_pos_requested_vars,setdiff(zero_variance_var_index,zero_variance_ME_var_index)),1:end-1)=0; 196 oo_.variance_decomposition_ME(ismember(observable_pos_requested_vars,setdiff(zero_variance_var_index,zero_variance_ME_var_index)),end)=1; 197 end 198 if ~options_.noprint %options_.nomoments == 0 199 skipline() 200 title='VARIANCE DECOMPOSITION SIMULATING ONE SHOCK AT A TIME (in percent)'; 201 title=add_filter_subtitle(title,options_); 202 headers = M_.exo_names; 203 headers(M_.exo_names_orig_ord) = headers; 204 headers = vertcat(' ', headers); 205 lh = cellofchararraymaxlength(M_.endo_names(ivar))+2; 206 dyntable(options_, title, vertcat(headers, 'Tot. lin. contr.'), ... 207 M_.endo_names(ivar), [oo_.variance_decomposition sum(oo_.variance_decomposition,2)], lh, 8, 2); 208 if ME_present 209 headers_ME = vertcat(headers, 'ME'); 210 dyntable(options_, [title,' WITH MEASUREMENT ERROR'], vertcat(headers_ME, 'Tot. lin. contr.'), M_.endo_names(ivar(index_subset)), ... 211 [oo_.variance_decomposition_ME sum(oo_.variance_decomposition_ME, 2)], lh, 8, 2); 212 end 213 if options_.TeX 214 headers = M_.exo_names_tex; 215 headers = vertcat(' ', headers); 216 labels = M_.endo_names_tex(ivar); 217 lh = cellofchararraymaxlength(labels)+2; 218 dyn_latex_table(M_, options_, title, 'sim_var_decomp', vertcat(headers, 'Tot. lin. contr.'), ... 219 labels_TeX, [oo_.variance_decomposition sum(oo_.variance_decomposition, 2)], lh, 8, 2); 220 if ME_present 221 headers_ME = vertcat(headers, 'ME'); 222 dyn_latex_table(M_, options_, [title, ' WITH MEASUREMENT ERROR'], 'sim_var_decomp_ME', ... 223 vertcat(headers_ME, 'Tot. lin. contr.'), ... 224 labels_TeX(index_subset), ... 225 [oo_.variance_decomposition_ME sum(oo_.variance_decomposition_ME, 2)], lh, 8, 2); 226 end 227 end 228 229 if options_.order == 1 230 fprintf('Note: numbers do not add up to 100 due to non-zero correlation of simulated shocks in small samples\n\n') 231 else 232 fprintf('Note: numbers do not add up to 100 due to i) non-zero correlation of simulated shocks in small samples and ii) nonlinearity\n\n') 233 end 234 end 235 236 end 237end 238 239warning(warning_old_state); 240end 241 242function y = get_filtered_time_series(y, m, options_) 243 244if options_.hp_filter && ~options_.one_sided_hp_filter && ~options_.bandpass.indicator 245 [hptrend,y] = sample_hp_filter(y,options_.hp_filter); 246elseif ~options_.hp_filter && options_.one_sided_hp_filter && ~options_.bandpass.indicator 247 [hptrend,y] = one_sided_hp_filter(y,options_.one_sided_hp_filter); 248elseif ~options_.hp_filter && ~options_.one_sided_hp_filter && options_.bandpass.indicator 249 data_temp=dseries(y,'0q1'); 250 data_temp=baxter_king_filter(data_temp,options_.bandpass.passband(1),options_.bandpass.passband(2),options_.bandpass.K); 251 y=data_temp.data; 252elseif ~options_.hp_filter && ~options_.one_sided_hp_filter && ~options_.bandpass.indicator 253 y = bsxfun(@minus, y, m); 254else 255 error('disp_moments:: You cannot use more than one filter at the same time') 256end 257 258end 259