diff --git a/bvartools/OrderingIndexes.m b/bvartools/OrderingIndexes.m index 841fba6..389238b 100644 --- a/bvartools/OrderingIndexes.m +++ b/bvartools/OrderingIndexes.m @@ -30,6 +30,7 @@ if check == 1 %assignin('base', [newstrng 'index_' varordering{ii} ], ii); String = varordering{ii}; + String = replace(String,'-',''); iString = String(find(~isspace(String))); assignin('base', [newstrng 'i' iString ], ii); else diff --git a/bvartools/bvar_.m b/bvartools/bvar_.m index 32aa7d3..7c3fada 100644 --- a/bvartools/bvar_.m +++ b/bvartools/bvar_.m @@ -723,9 +723,9 @@ else % pooled units yy = []; XX = []; for nunit = 1 : nunits - [yy,XX] = YXB_(y(idx, :, nunit),lags,[nx timetrend]); - XX = [XX; XX]; - yy = [yy; yy]; + [yy_p,XX_p] = YXB_(y(idx, :, nunit),lags,[nx timetrend]); + XX = [XX; XX_p]; + yy = [yy; yy_p]; end end @@ -1517,8 +1517,8 @@ % inference and IRFs BVAR.Phi_draws = Phi_draws; % draws from the autoregressive part BVAR.Sigma_draws = Sigma_draws; % draws from the covarance matrix -BVAR.alpha_draws = Phi_draws; % Older name: draws from the autoregressive part -BVAR.sigma_draws = Sigma_draws; % Older name: draws from the covarance matrix +% BVAR.alpha_draws = Phi_draws; % Older name: draws from the autoregressive part +% BVAR.sigma_draws = Sigma_draws; % Older name: draws from the covarance matrix BVAR.Sigma_lower_chol_draw = Sigma_lower_chol_draw; BVAR.ir_draws = ir_draws; % draws from the IRF with cholesky BVAR.irlr_draws = irlr_draws; % draws from the IRF with Long Run @@ -1526,8 +1526,8 @@ BVAR.lags = lags; % lags BVAR.N = ny; % number of variables BVAR.e_draws = e_draws; % residuals -BVAR.e = e_draws; % backward compatible with earlier versions -BVAR.fe_draws = fe_draws; % backward compatible with earlier versions +% BVAR.e = e_draws; % backward compatible with earlier versions +BVAR.fe_draws = fe_draws; % insample forecast errors BVAR.posterior = posterior; BVAR.prior = prior; % priors used diff --git a/bvartools/max_fevd.m b/bvartools/max_fevd.m index 0b08afa..d52218f 100644 --- a/bvartools/max_fevd.m +++ b/bvartools/max_fevd.m @@ -5,18 +5,28 @@ if nargin < 6 Kappa = 1000; end +if length(i) ~= length(j) + error('each shock must have one target') +end N = size(Sigma,1); -crit = nan(Kappa,1); +crit = nan(Kappa,length(j)); Q = nan(N,N,Kappa); for k = 1 : Kappa Q(:,:,k) = generateQ(N); % generate an orthonormal matrix FEVD = fevd(h,Phi,Sigma,Q(:,:,k)); % compute the FEVD % Calculate the contribution of shock j, to variable i forecast error volatility, at horizon h - crit(k,1) = FEVD(i, j); + for j1 = 1 : length(j) + crit(k,j1) = FEVD(i(j1), j(j1)); + end end % Pick the maximum -[~,index] = max(crit); -Qbar = Q(:,:,index); +Qbar = nan(N,N,length(j)); +for j1 = 1 : length(j) + [~,index] = max(crit(:,j1)); + tmp_notindx = setdiff([1:N] , j(j1)); + Omega = Q(:,:,index); + Qbar(:,:,j1) = Omega(:,[j(j1) tmp_notindx]); +end end diff --git a/bvartools/u2nu.m b/bvartools/u2nu.m new file mode 100644 index 0000000..af4dac5 --- /dev/null +++ b/bvartools/u2nu.m @@ -0,0 +1,11 @@ +function [nu] = u2nu(e_draws,Sigma_draws,Omega_draws) + +nu=zeros(size(e_draws)); +for dd = 1 : size(e_draws,3) + errors = e_draws(:,:,dd); + A = chol( Sigma_draws(:,:,dd),'lower'); + nu(:,:,dd) = errors * inv( Omega_draws(:,:,dd)' * A'); %#ok + + % nu(:,:,dd) = errors / ( Omega_draws(:,:,dd)' * A'); % structural innovations +end + diff --git a/examples/BVAR tutorial/example_2_minn.m b/examples/BVAR tutorial/example_2_minn.m index 094d287..2115ae2 100644 --- a/examples/BVAR tutorial/example_2_minn.m +++ b/examples/BVAR tutorial/example_2_minn.m @@ -101,3 +101,25 @@ %% ESSENCE: OPTIMALLY CHOSEN AND CHERRY PICKED CHOSEN %% HYPER-PARAMETER VALUES MAKE LITTLE DIFFERENCE FOR SHAPE OF IRFS. + +%% 3.4 Use a presample as prior +clear options; +% run a VAR on presample data +presample = 50; % 8 years of presample +lags = 6; +bvar1 = bvar_(y(1:presample,:),lags); + + +% use the VAR estimates to set the priors for the LP +options.priors.name = 'Conjugate'; +% posterior mean of the VAR AR coeff and constant +options.priors.Phi.mean = mean(bvar1.Phi_draws,3); +% average variance of the AR coeff and constant +options.priors.Phi.cov = diag(mean(var(bvar1.Phi_draws,0,3),2)); +% posterior mean of the Covariance of the VAR residuals +options.priors.Sigma.scale = mean(bvar1.Sigma_draws,3); +options.priors.Sigma.df = size(bvar1.Phi_draws,1)-2; +options.K = 1000; + +bvar2 = bvar_(y(presample+1:end,:),lags,options); +