Skip to content

Commit

Permalink
Added plotting for irf exo in panel var
Browse files Browse the repository at this point in the history
  • Loading branch information
ebenetce committed Mar 22, 2023
1 parent 956e943 commit d201593
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 169 deletions.
17 changes: 15 additions & 2 deletions tbx.prj
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,21 @@
</build-deliverables>
<workflow />
<matlab>
<root>C:\Program Files\MATLAB\R2022b</root>
<toolboxes />
<root>C:\Program Files\MATLAB\R2023a</root>
<toolboxes>
<toolbox name="matlabcoder" />
<toolbox name="embeddedcoder" />
</toolboxes>
<toolbox>
<matlabcoder>
<enabled>true</enabled>
</matlabcoder>
</toolbox>
<toolbox>
<embeddedcoder>
<enabled>true</enabled>
</embeddedcoder>
</toolbox>
</matlab>
<platform>
<unix>false</unix>
Expand Down
186 changes: 99 additions & 87 deletions tbx/bear/+bear/irfexo.m
Original file line number Diff line number Diff line change
@@ -1,92 +1,104 @@
function [exo_irf_record,exo_irf_estimates]=irfexo(beta_gibbs,It,Bu,IRFperiods,IRFband,n,m,p,k)



% create the cell aray that will store the values from the simulations
exo_irf_record=cell(n,m);


% deal with shocks in turn
for ii=1:m


% step 1: repeat the simulation process a number of times equal to the number of Gibbs iterations
for kk=1:It-Bu


% step 3: draw beta from its posterior distribution
beta=beta_gibbs(:,kk);
% reshape to obtain B
B=reshape(beta,k,n);
% recover C, the matrix of coefficients on exogenous
C=B(end-m+1:end,:);
% create a matrix of zeros of dimension p*n
Y=zeros(p,n);
% step 2: set the value of the last row, column i, equal to 1
Y(p,:)=C(ii,:);


% step 4: for each iteration kk, repeat the algorithm for periods T+1 to T+h
for jj=1:IRFperiods-1
% % % if prior==61
% % % %use the function lagx to obtain the matrix X; retain only the last row
% % % X=bear.lagx(Y,p-1);
% % % X=X(end,:);
% % %
% % %
% % % else


% use the function lagx to obtain a matrix temp, containing the endogenous regressors
temp=bear.lagx(Y,p-1);

% define the vector X
X=[temp(end,:) zeros(1,m)];
% % % end
% obtain the predicted value for T+jj
yp=X*B;

% concatenate yp at the top of Y
Y=[Y;yp];

% repeat until values are obtained for T+h
end


% step 5: record the results from current iteration in cell irf_record
% loop over variables
for jj=1:n
% consider column jj of matrix 'Y' and trim the (p-1) initial periods: what remains is the series of IRFs for period T to period T+h-1, for variable jj
temp=Y(p:end,jj);
% record these values in the corresponding matrix of irf_record
exo_irf_record{jj,ii}(kk,:)=temp';
end


% then go for next iteration
end

% conduct the same process with shocks in other variables
function [exo_irf_record,exo_irf_estimates]=irfexo(beta_gibbs,It,Bu,IRFperiods,IRFband,n,m,p,k,N)

arguments
beta_gibbs
It
Bu
IRFperiods
IRFband
n
m
p
k
N (1,1) double = 1;
end

%% Preallocate
% create the cell aray that will store the values from the simulations
exo_irf_record=cell(n,m,N);

% create first the cell that will contain the IRF estimates
exo_irf_estimates=cell(n,m);

% for the response of each variable to each shock, and each IRF period, compute the median, lower and upper bound from the Gibbs sampler records
% consider variables in turn
for ii=1:n
% consider shocks in turn
for jj=1:m
% consider IRF periods in turn
for kk=1:IRFperiods
% compute first the lower bound
exo_irf_estimates{ii,jj}(1,kk)=quantile(exo_irf_record{ii,jj}(:,kk),(1-IRFband)/2);
% then compute the median
exo_irf_estimates{ii,jj}(2,kk)=quantile(exo_irf_record{ii,jj}(:,kk),0.5);
% finally compute the upper bound
exo_irf_estimates{ii,jj}(3,kk)=quantile(exo_irf_record{ii,jj}(:,kk),1-(1-IRFband)/2);
end
end
end
exo_irf_estimates=cell(n,m,N);

for nn = 1 : N
% deal with shocks in turn
for ii=1:m


% step 1: repeat the simulation process a number of times equal to the number of Gibbs iterations
for kk=1:It-Bu


% step 3: draw beta from its posterior distribution
beta=beta_gibbs(:,kk,nn);
% reshape to obtain B
B=reshape(beta,k,n);
% recover C, the matrix of coefficients on exogenous
C=B(end-m+1:end,:);
% create a matrix of zeros of dimension p*n
Y=zeros(p,n);
% step 2: set the value of the last row, column i, equal to 1
Y(p,:)=C(ii,:);


% step 4: for each iteration kk, repeat the algorithm for periods T+1 to T+h
for jj=1:IRFperiods-1
% % % if prior==61
% % % %use the function lagx to obtain the matrix X; retain only the last row
% % % X=bear.lagx(Y,p-1);
% % % X=X(end,:);
% % %
% % %
% % % else


% use the function lagx to obtain a matrix temp, containing the endogenous regressors
temp=bear.lagx(Y,p-1);

% define the vector X
X=[temp(end,:) zeros(1,m)];
% % % end
% obtain the predicted value for T+jj
yp=X*B;

% concatenate yp at the top of Y
Y=[Y;yp];

% repeat until values are obtained for T+h
end


% step 5: record the results from current iteration in cell irf_record
% loop over variables
for jj=1:n
% consider column jj of matrix 'Y' and trim the (p-1) initial periods: what remains is the series of IRFs for period T to period T+h-1, for variable jj
temp=Y(p:end,jj);
% record these values in the corresponding matrix of irf_record
exo_irf_record{jj,ii,nn}(kk,:)=temp';
end


% then go for next iteration
end

% conduct the same process with shocks in other variables
end

% for the response of each variable to each shock, and each IRF period, compute the median, lower and upper bound from the Gibbs sampler records
% consider variables in turn
for ii=1:n
% consider shocks in turn
for jj=1:m
% consider IRF periods in turn
for kk=1:IRFperiods
% compute first the lower bound
exo_irf_estimates{ii,jj,nn}(1,kk)=quantile(exo_irf_record{ii,jj,nn}(:,kk),(1-IRFband)/2);
% then compute the median
exo_irf_estimates{ii,jj,nn}(2,kk)=quantile(exo_irf_record{ii,jj,nn}(:,kk),0.5);
% finally compute the upper bound
exo_irf_estimates{ii,jj,nn}(3,kk)=quantile(exo_irf_record{ii,jj,nn}(:,kk),1-(1-IRFband)/2);
end
end
end
end % for nn = 1 : N

118 changes: 50 additions & 68 deletions tbx/bear/+bear/irfexodisp.m
Original file line number Diff line number Diff line change
@@ -1,83 +1,65 @@
function irfexodisp(n,m,endo,exo,IRFperiods,exo_irf_estimates,pref)
function irfexodisp(n,m,endo,exo,IRFperiods,exo_irf_estimates,pref,N,Units)
% IRFEXODISP plot impulse response on exogenous variables and write results
% in excel file

if pref.plot
arguments
n
m
endo
exo
IRFperiods
exo_irf_estimates
pref
N (1,1) double = 1 % for panel var, pass the number of units
Units (:,1) string = "" % for panel var, the unit names
end

if pref.plot == 1

if ~isequal(Units,"")
Units = Units + "_";
end

% plot the figure
irf=figure('Tag','BEARresults');
set(irf,'Color',[0.9 0.9 0.9]);
set(irf,'name','impulse response functions (exogenous)');

% initiate the count
count=0;
% loop over variables
for ii=1:n
% loop over exogenous (for shocks)
for kk=2:m
% increment count
count=count+1;
% then plot
subplot(n,m,count)
temp=exo_irf_estimates{ii,kk};
hold on
Xpatch=[(1:IRFperiods) (IRFperiods:-1:1)];
Ypatch=[temp(1,:) fliplr(temp(3,:))];
IRFpatch=patch(Xpatch,Ypatch,[0.7 0.78 1]);
set(IRFpatch,'facealpha',0.5);
set(IRFpatch,'edgecolor','none');
plot(temp(2,:),'Color',[0.4 0.4 1],'LineWidth',2);
plot([1,IRFperiods],[0 0],'k--');
hold off
minband=min(temp(1,:));
maxband=max(temp(3,:));
space=maxband-minband;
Ymin=minband-0.2*space;
Ymax=maxband+0.2*space;
set(gca,'XLim',[1 IRFperiods],'YLim',[Ymin Ymax],'FontName','Times New Roman');
% top labels
title(exo{kk-1,1},'FontWeight','normal','interpreter','none');
% side labels
ylabel([endo{ii,1}],'FontWeight','normal','interpreter','none');
end
end
tl = tiledlayout('flow', 'Parent', irf);
% top supertitle
ax=axes('Units','Normal','Position',[.11 .075 .85 .88],'Visible','off');
set(get(ax,'Title'),'Visible','on')
title('Shock:','FontSize',11,'FontName','Times New Roman','FontWeight','normal','interpreter','none');
title(tl, 'Shock:','FontSize',11,'FontName','Times New Roman','FontWeight','normal','interpreter','none')
% side supertitle
ylabel('Response of:','FontSize',12,'FontName','Times New Roman','FontWeight','normal','interpreter','none');
set(get(ax,'Ylabel'),'Visible','on');

end % pref.plot
ylabel(tl, 'Response of:','FontSize',12,'FontName','Times New Roman','FontWeight','normal','interpreter','none');

for nn = 1 : N
% loop over variables
for ii=1:n
% loop over exogenous (for shocks)
for kk=2:m
% then plot
ax = nexttile(tl);

% create the cell that will be saved on excel
exo_irfcell={};
% build preliminary elements: space between the tables
horzspace=repmat({''},2,5*m);
vertspace=repmat({''},IRFperiods+3,1);
% loop over variables (vertical dimension)
for ii=1:n
tempcell={};
% loop over shocks (horizontal dimension)
for jj=1:m
% create cell of IRF record for variable ii in response to shock jj
if jj==1
temp=['response of ' endo{ii,1} ' to constant shocks'];
else
temp=['response of ' endo{ii,1} ' to ' exo{jj-1,1} ' shocks'];
temp=exo_irf_estimates{ii,kk,nn};
hold(ax, 'on');
Xpatch=[(1:IRFperiods) (IRFperiods:-1:1)];
Ypatch=[temp(1,:) fliplr(temp(3,:))];
patch(ax, Xpatch, Ypatch, [0.7 0.78 1], ...
'facealpha', 0.5, ...
'edgecolor', 'none');
plot(ax, temp(2,:),'Color',[0.4 0.4 1],'LineWidth',2);
plot(ax, [1,IRFperiods],[0 0],'k--');
hold(ax, 'off')
minband=min(temp(1,:));
maxband=max(temp(3,:));
space=maxband-minband;
Ymin=minband-0.2*space;
Ymax=maxband+0.2*space;
set(ax,'XLim',[1 IRFperiods],'YLim',[Ymin Ymax],'FontName','Times New Roman');
% top labels
title(ax, Units(nn) + exo{kk-1,1},'FontWeight','normal','interpreter','none');
% side labels
ylabel(ax, Units(nn) + endo{ii,1},'FontWeight','normal','interpreter','none');
end
end
irf_ij=[temp {''} {''} {''};{''} {''} {''} {''};{''} {'lw. bound'} {'median'} {'up. bound'};num2cell((1:IRFperiods)') num2cell((exo_irf_estimates{ii,jj})')];
tempcell=[tempcell irf_ij vertspace];
end
exo_irfcell=[exo_irfcell;horzspace;tempcell];
end

% trim
exo_irfcell=exo_irfcell(3:end,1:end-1);

% write in excel
if pref.results==1
bear.xlswritegeneral(fullfile(pref.results_path, [pref.results_sub '.xlsx']),exo_irfcell,'exogenous IRFs','B2');
end

24 changes: 12 additions & 12 deletions tbx/bear/BEARmain.m
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ function BEARmain(opts)

% estimate IRFs for exogenous variables
if isempty(data_exo)~=1 %%%%%&& m>0
[exo_irf_record,exo_irf_estimates]=bear.irfexo(beta_gibbs,opts.It,opts.Bu,IRFperiods,IRFband,n,m,p,k);
[~,exo_irf_estimates]=bear.irfexo(beta_gibbs,opts.It,opts.Bu,IRFperiods,IRFband,n,m,p,k);
% estimate IRFs for exogenous variables
bear.irfexodisp(n,m,endo,exo,IRFperiods,exo_irf_estimates,pref);
end
Expand Down Expand Up @@ -1276,12 +1276,11 @@ function BEARmain(opts)
end

% estimate IRFs for exogenous variables
if opts.panel == 3 || opts.panel == 4
if isempty(data_exo)~=1 %%%%%&& m>0
[~,exo_irf_estimates]=bear.models.panel.irfexo(N,beta_gibbs,opts.It,opts.Bu,IRFperiods,IRFband,n,m,p,k);

% estimate IRFs for exogenous variables
bear.irfexodisp(n,m,endo,exo,IRFperiods,exo_irf_estimates,pref);
if isempty(data_exo)~=1 %%%%%&& m>0
if opts.panel == 3 || opts.panel == 4
[~,exo_irf_estimates]=bear.irfexo(beta_gibbs,opts.It,opts.Bu,IRFperiods,IRFband,n,m,p,k,N);

bear.irfexodisp(n,m,endo,exo,IRFperiods,exo_irf_estimates,pref, N, Units);
end
end

Expand Down Expand Up @@ -2193,11 +2192,12 @@ function BEARmain(opts)

end


% estimate IRFs for exogenous variables
[exo_irf_record, exo_irf_estimates]=bear.irfexo(beta_gibbs,opts.It,opts.Bu,IRFperiods,IRFband,n,m,p,k);
% estimate IRFs for exogenous variables
bear.irfexodisp(n,m,endo,exo,IRFperiods,exo_irf_estimates,pref);
if isempty(data_exo)~=1 %%%%%&& m>0
% estimate IRFs for exogenous variables
[~, exo_irf_estimates]=bear.irfexo(beta_gibbs,opts.It,opts.Bu,IRFperiods,IRFband,n,m,p,k);
% estimate IRFs for exogenous variables
bear.irfexodisp(n,m,endo,exo,IRFperiods,exo_irf_estimates,pref);
end


%% BLOCK 6: FORECASTS
Expand Down

0 comments on commit d201593

Please sign in to comment.