Home > matlab > bvar_irf.m

bvar_irf

PURPOSE ^

builds IRFs for a bvar model

SYNOPSIS ^

function bvar_irf(nlags,identification)

DESCRIPTION ^

 builds IRFs for a bvar model

 INPUTS
    nlags            [integer]     number of lags for the bvar
    identification   [string]      identification scheme ('Cholesky' or 'SquareRoot')

 OUTPUTS
    none

 SPECIAL REQUIREMENTS
    none

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function bvar_irf(nlags,identification)
0002 % builds IRFs for a bvar model
0003 %
0004 % INPUTS
0005 %    nlags            [integer]     number of lags for the bvar
0006 %    identification   [string]      identification scheme ('Cholesky' or 'SquareRoot')
0007 %
0008 % OUTPUTS
0009 %    none
0010 %
0011 % SPECIAL REQUIREMENTS
0012 %    none
0013 
0014 % Copyright (C) 2007-2009,2012 Dynare Team
0015 %
0016 % This file is part of Dynare.
0017 %
0018 % Dynare is free software: you can redistribute it and/or modify
0019 % it under the terms of the GNU General Public License as published by
0020 % the Free Software Foundation, either version 3 of the License, or
0021 % (at your option) any later version.
0022 %
0023 % Dynare is distributed in the hope that it will be useful,
0024 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0025 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0026 % GNU General Public License for more details.
0027 %
0028 % You should have received a copy of the GNU General Public License
0029 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
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 % Option 'lower' of chol() not available in old versions of
0042 % Matlab, so using transpose
0043 XXi_lower_chol = chol(posterior.XXi)';
0044 
0045 k = ny*nlags+nx;
0046 
0047 % Declaration of the companion matrix
0048 Companion_matrix = diag(ones(ny*(nlags-1),1),-ny);
0049 
0050 % Number of explosive VAR models sampled
0051 p = 0;
0052 
0053 % Initialize a four dimensional array.
0054 sampled_irfs = NaN(ny, ny, options_.irf, options_.bvar_replic);
0055 
0056 for draw=1:options_.bvar_replic
0057     
0058     % Get a covariance matrix from an inverted Wishart distribution.
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     % Get the Autoregressive matrices from a matrix variate distribution.
0064     Phi = rand_matrix_normal(k, ny, posterior.PhiHat, Sigma_lower_chol, XXi_lower_chol);
0065     
0066     % Form the companion matrix.
0067     Companion_matrix(1:ny,:) = transpose(Phi(1:ny*nlags,:)); 
0068     
0069     % All the eigenvalues of the companion matrix have to be on or
0070     % inside the unit circle to rule out explosive time series.
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     % Build the IRFs...
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 % Plots of the IRFs
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 % Save intermediate results
0133 DirectoryName = [ M_.fname '/bvar_irf' ];
0134 if ~isdir(DirectoryName)
0135     mkdir('.',DirectoryName);
0136 end
0137 save([ DirectoryName '/simulations.mat'], 'sampled_irfs');
0138 
0139 % Save results in oo_
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

Generated on Mon 21-May-2012 02:42:43 by m2html © 2005