0001 function bvar_irf(nlags,identification)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 global options_ oo_ M_
0032
0033 if nargin==1
0034 identification = 'Cholesky';
0035 end
0036
0037 [ny, nx, posterior, prior] = bvar_toolbox(nlags);
0038
0039 S_inv_upper_chol = chol(inv(posterior.S));
0040
0041
0042
0043 XXi_lower_chol = chol(posterior.XXi)';
0044
0045 k = ny*nlags+nx;
0046
0047
0048 Companion_matrix = diag(ones(ny*(nlags-1),1),-ny);
0049
0050
0051 p = 0;
0052
0053
0054 sampled_irfs = NaN(ny, ny, options_.irf, options_.bvar_replic);
0055
0056 for draw=1:options_.bvar_replic
0057
0058
0059 Sigma = rand_inverse_wishart(ny, posterior.df, S_inv_upper_chol);
0060 Sigma_upper_chol = chol(Sigma);
0061 Sigma_lower_chol = transpose(Sigma_upper_chol);
0062
0063
0064 Phi = rand_matrix_normal(k, ny, posterior.PhiHat, Sigma_lower_chol, XXi_lower_chol);
0065
0066
0067 Companion_matrix(1:ny,:) = transpose(Phi(1:ny*nlags,:));
0068
0069
0070
0071 test = (abs(eig(Companion_matrix)));
0072 if any(test>1.0000000000001)
0073 p = p+1;
0074 end
0075
0076 if strcmpi(identification,'Cholesky')
0077 StructuralMat = Sigma_lower_chol;
0078 elseif strcmpi(identification,'SquareRoot')
0079 StructuralMat = sqrtm(Sigma);
0080 end
0081
0082
0083 lags_data = zeros(ny,ny*nlags) ;
0084 sampled_irfs(:,:,1,draw) = Sigma_lower_chol ;
0085 lags_data(:,1:ny) = Sigma_lower_chol ;
0086 for t=2:options_.irf
0087 sampled_irfs(:,:,t,draw) = lags_data(:,:)*Phi(1:ny*nlags,:) ;
0088 lags_data(:,ny+1:end) = lags_data(:,1:end-ny) ;
0089 lags_data(:,1:ny) = sampled_irfs(:,:,t,draw) ;
0090 end
0091
0092 end
0093
0094 if p > 0
0095 disp(' ')
0096 disp(['Some of the VAR models sampled from the posterior distribution'])
0097 disp(['were found to be explosive (' int2str(p) ' samples).'])
0098 disp(' ')
0099 end
0100
0101 posterior_mean_irfs = mean(sampled_irfs,4);
0102 posterior_variance_irfs = var(sampled_irfs, 1, 4);
0103
0104 sorted_irfs = sort(sampled_irfs,4);
0105 sort_idx = round((0.5 + [-options_.conf_sig, options_.conf_sig, .0]/2) * options_.bvar_replic);
0106
0107 posterior_down_conf_irfs = sorted_irfs(:,:,:,sort_idx(1));
0108 posterior_up_conf_irfs = sorted_irfs(:,:,:,sort_idx(2));
0109 posterior_median_irfs = sorted_irfs(:,:,:,sort_idx(3));
0110
0111 number_of_columns = fix(sqrt(ny));
0112 number_of_rows = ceil(ny / number_of_columns) ;
0113
0114
0115 for shock=1:ny
0116 figure('Name',['Posterior BVAR Impulse Responses (shock in equation ' int2str(shock) ').']);
0117 for variable=1:ny
0118 subplot(number_of_rows,number_of_columns,variable);
0119 h1 = area(1:options_.irf,squeeze(posterior_up_conf_irfs(shock,variable,:)));
0120 set(h1,'BaseValue',min([min(posterior_up_conf_irfs(shock,variable,:)),min(posterior_down_conf_irfs(shock,variable,:))]))
0121 set(h1,'FaceColor',[.9 .9 .9])
0122 hold on
0123 h2 = area(1:options_.irf,squeeze(posterior_down_conf_irfs(shock,variable,:)));
0124 set(h2,'BaseValue',min([min(posterior_up_conf_irfs(shock,variable,:)),min(posterior_down_conf_irfs(shock,variable,:))]))
0125 set(h2,'FaceColor',[1 1 1])
0126 plot(1:options_.irf,squeeze(posterior_median_irfs(shock,variable,:)),'-k','linewidth',2)
0127 axis tight
0128 hold off
0129 end
0130 end
0131
0132
0133 DirectoryName = [ M_.fname '/bvar_irf' ];
0134 if ~isdir(DirectoryName)
0135 mkdir('.',DirectoryName);
0136 end
0137 save([ DirectoryName '/simulations.mat'], 'sampled_irfs');
0138
0139
0140 for i=1:ny
0141 shock_name = options_.varobs(i, :);
0142 for j=1:ny
0143 variable_name = options_.varobs(j, :);
0144 eval(['oo_.bvar.irf.Mean.' variable_name '.' shock_name ' = posterior_mean_irfs(' int2str(j) ',' int2str(i) ',:);'])
0145 eval(['oo_.bvar.irf.Median.' variable_name '.' shock_name ' = posterior_median_irfs(' int2str(j) ',' int2str(i) ',:);'])
0146 eval(['oo_.bvar.irf.Var.' variable_name '.' shock_name ' = posterior_variance_irfs(' int2str(j) ',' int2str(i) ',:);'])
0147 eval(['oo_.bvar.irf.Upper_bound.' variable_name '.' shock_name ' = posterior_up_conf_irfs(' int2str(j) ',' int2str(i) ',:);'])
0148 eval(['oo_.bvar.irf.Lower_bound.' variable_name '.' shock_name ' = posterior_down_conf_irfs(' int2str(j) ',' int2str(i) ',:);'])
0149 end
0150 end