Kilian 2009 Code
Kilian 2009 Code
%%%%%%%%%%%%%%%%%%
figure(1)
subplot(3,3,1)
x1=-sort(cumsum(squeeze(IRF(1,1,:,:)),1),2);
temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:','linewidth',1), hold on,
plot(HO,-cumsum(squeeze(ir_aer(1,1,:))),'r:','linewidth',2)
axis([0 hmax-1 -2.5 1.5])
set(gca,'XTick',0:5:20)
set(gca,'YTick',-2.5:1:1.5)
ylabel('Oil production','fontsize',11)
title('Oil supply shock','fontsize',11)
%ylabel('percent','fontsize',12)
subplot(3,3,4)
x1=sort(cumsum(squeeze(IRF(1,2,:,:)),1),2);
temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:','linewidth',1), hold on,
plot(HO,cumsum(squeeze(ir_aer(1,2,:))),'r:','linewidth',2)
axis([0 hmax-1 -2.5 1.5])
set(gca,'XTick',0:5:20)
set(gca,'YTick',-2.5:1:1.5)
ylabel('Oil production','fontsize',11)
title('Aggregate demand shock','fontsize',11)
subplot(3,3,7)
x1=sort(cumsum(squeeze(IRF(1,3,:,:)),1),2);
temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:','linewidth',1), hold on,
plot(HO,cumsum(squeeze(ir_aer(1,3,:))),'r:','linewidth',2)
axis([0 hmax-1 -2.5 1.5])
set(gca,'XTick',0:5:20)
set(gca,'YTick',-2.5:1:1.5)
ylabel('Oil production','fontsize',11)
title('Oil-specific demand shock','fontsize',11)
xlabel('Months','fontsize',10)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,3,2)
x1=-sort(squeeze(IRF(2,1,:,:)),2);
temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:','linewidth',1), hold on,
plot(HO,-squeeze(ir_aer(2,1,:)),'r:','linewidth',2)
axis([0 hmax-1 -5 10])
set(gca,'XTick',0:5:20)
set(gca,'YTick',-5:5:10)
ylabel('Real activity','fontsize',11)
title('Oil supply shock','fontsize',11)
%ylabel('percent','fontsize',12)
subplot(3,3,5)
x1=sort(squeeze(IRF(2,2,:,:)),2);
temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:','linewidth',1), hold on,
plot(HO,squeeze(ir_aer(2,2,:)),'r:','linewidth',2)
axis([0 hmax-1 -5 10])
set(gca,'XTick',0:5:20)
set(gca,'YTick',-5:5:10)
ylabel('Real activity','fontsize',11)
title('Aggregate demand shock','fontsize',11)
subplot(3,3,8)
x1=sort(squeeze(IRF(2,3,:,:)),2);
temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:','linewidth',1), hold on,
plot(HO,squeeze(ir_aer(2,3,:)),'r:','linewidth',2)
axis([0 hmax-1 -5 10])
set(gca,'XTick',0:5:20)
set(gca,'YTick',-5:5:35)
ylabel('Real activity','fontsize',11)
title('Oil-specific demand shock','fontsize',11)
xlabel('Months','fontsize',10)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,3,3)
x1=-sort(squeeze(IRF(3,1,:,:)),2);
temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:','linewidth',1), hold on,
plot(HO,-squeeze(ir_aer(3,1,:)),'r:','linewidth',2)
axis([0 hmax-1 -5 10])
set(gca,'XTick',0:5:20)
set(gca,'YTick',-5:5:10)
ylabel('Real oil price','fontsize',11)
title('Oil supply shock','fontsize',11)
%ylabel('percent','fontsize',12)
subplot(3,3,6)
x1=sort(squeeze(IRF(3,2,:,:)),2);
temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:','linewidth',1), hold on,
plot(HO,squeeze(ir_aer(3,2,:)),'r:','linewidth',2)
axis([0 hmax-1 -5 10])
set(gca,'XTick',0:5:20)
set(gca,'YTick',-5:5:10)
ylabel('Real oil price','fontsize',11)
title('Aggregate demand shock','fontsize',11)
subplot(3,3,9)
x1=sort(squeeze(IRF(3,3,:,:)),2);
temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:','linewidth',1), hold on,
plot(HO,squeeze(ir_aer(3,3,:)),'r:','linewidth',2)
axis([0 hmax-1 -5 10])
set(gca,'XTick',0:5:20)
set(gca,'YTick',-5:5:10)
title('Oil-specific demand shock','fontsize',11)
ylabel('Real oil price','fontsize',11)
xlabel('Months','fontsize',10)
function M_i=getM(lambda0,lambda1,lambda2,lambda3,S,nlags,n)
k=nlags*n+1;
M_i=zeros(k,k,n);
i = 0;
while i < n
i = i + 1;
M = (lambda0*lambda2)*ones(k,1);
M(k,1) = (lambda0*lambda3);
if nlags > 0
el = 0;
while el < nlags
el = el + 1;
M((el-1)*n+1:el*n,1) = M((el-1)*n+1:el*n,1)./S;
M((el-1)*n+1:el*n,1) = M((el-1)*n+1:el*n,1)/(el^lambda1);
M((el-1)*n+i,1) = M((el-1)*n+i,1)/lambda2;
end
end
M = M .^ 2;
M_i(:,:,i) = diag(M);
end
function m_star = getmstar(M_star,Sxy,M,eta,A)
m_star=M_star*(Sxy'+inv(M)*eta')*A';
end
function taustar = gettau(kappa,omega,A,Syy,Sxy,eta,M,M_star)
tau=kappa/(1/omega);
m=A*eta;
taustar=tau+0.5*(A*Syy*A'+m*inv(M)*m')-0.5*(A*Sxy*M_star*Sxy'*A') ...
-0.5*(m*inv(M)*M_star*inv(M)*m')-(m*inv(M)*M_star*Sxy'*A');
end
function [X,y,ess] = getXy(data,ndet,nlags)
if ndet==1
data=[data const];
elseif ndet==2
data=[data const trend];
end
x=data(:,1:end-ndet);
X=zeros(ess,ncoe);
for k=1:nlags
X(:,nvars*(k-1)+1:nvars*k) = x(sb-k:sl-k,:); %first lag of all variables, second
lag of all variables etc.
end
if ndet==1
X=[X ones(ess,1)]; %without trend
elseif ndet==2
X=[X ones(ess,1) trend(1:ess,1)];
end
y=x(sb:sl,:);
function IR = impulse_response_1SD(A,D,b,n,nlags,hmax)
rdc=inv(A)*D^.5;
phi=inv(A)*b;
F=varcompanion(phi,1,n,nlags);
J=[eye(n) zeros(n,(nlags-1)*n)];
IR=[];
for h=0:hmax
IR=cat(3,IR,(J*(F^h)*J')*rdc);
end
[BETAnc,B1,XZ, SIGMA, U, V]=lsvarc(kiliandata,24);
xmax=17;
[IRFaer, K1]=VARirf(BETAnc,SIGMA,xmax);
ir_aer=reshape(IRFaer,3,3,18);
save KAER_original ir_aer
function out=lag0(x,p)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%For an input vector or matrix x=(a_{1}; a_{2}; ...; a_{n}) where a_{i}%
%are row vectors, it returns the vector or matrix: %
% %
%xl0= (0; ... ; 0 ; a_{1}; a_{2}; ....; a_{n-p}) %
% %
%In other words it lags the variable p periods and places zeros in the %
%rows corresponding to the first p periods %
% %
%Version is 1.01 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Revision History:
%v 1.01
%Replaced x1=x(1:(length(x)-p),:) with x1=x(1:(R-p),:);
%Now should work correctly for matrices;
function data=lagn(data,m)
data=data(m+1:end,:)-data(1:end-m,:);
for i=1:p-1
Y=[Y; y(:,p-i:t-i)];
end;
X=[ones(1,t-p); Y(:,1:t-p)];
Y=y(:,p+1:t);
%Run LS regression
B=(Y*X')/(X*X');
U=Y-B*X;
SIGMA=U*U'/(t-p); % with degrees-of-freedom adjustment: (t-p-p*K-1);
V=B(:,1);
A=B(:,2:K*p+1);
%%% This code replicates Figure 3 in Kilian (AER 2009), "Not All Oil Price
Shocks
%%% Are Alike: Disentangling Demand and Supply Shocks in the Crude Oil
Market"
%%% using the BH algorithm and includes posterior distributions of key
%%% parameters;
%%% See Figures 1-2 in: Baumeister and Hamilton, "Structural Interpretation
%%% of Vector Autoregressions with Incomplete Identification: Revisiting
%%% the Role of Oil Supply and Demand Shocks" (December 2017)
clear;
clc;
% NOTE:
% The parameter xsi is the tuning parameter for the MH step and has to be
% re-set for each application such that it implies a 30-35% acceptance ratio.
% If acceptance ratio is too high, make xsi bigger;
% if acceptance ratio is too low, make xsi smaller.
xsi=1.2^2; %tuning parameter to achieve a target acceptance rate of
around 35%
if select_sample==1
load data_k3.txt %sample: 1973M1 to 2014M1
% column 1: global crude oil production (in million barrels per day)
% column 2: global real economic activity index (Kilian, AER 2009)
% column 3: real refiners' acquisition cost of crude oil imports (deflated with
US CPI)
% transformations of variables
qo=lagn(100*log(data_k3(:,1)),1);
rea=data_k3(2:end,2);
rpo=100*log(data_k3(2:end,3));
yy=[qo rea rpo];
yy=yy(1:428,:);
elseif select_sample==2
load kiliandata.txt %sample: 1973M2 to 2008M9 (from JEEA)
yy=kiliandata;
time=(1973+1/12:1/12:2008+8/12)';
elseif select_sample==3
load data_AER %sample: 1973M2 to 2006M12
yy=y;
clear y
time=(1973+1/12:1/12:2006+11/12)';
end
s=size(time,1);
n=size(yy,2); %number of endogenous variables
seednumber=140778;
rand('seed',seednumber);
randn('seed',seednumber);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
%%% ALGORITHM FOR GETTING POSTERIORS OF A, B and D %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 1a: Set parameters of the prior distributions for impact coefficients (A)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bounds = 5;
%x1=-bounds:.01:0; %grid for negative parameters
%y1=0:.01:bounds; %grid for positive parameters
z1=-bounds:.01:bounds; %grid for parameters where no sign is imposed a
priori
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
% STEP 1b: Set informative priors on lagged coefficients (B) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
% Specify the prior mean of the coefficients of the 3 equations of the VAR
% and their prior covariance
% PRIOR COVARIANCE
SS=[s11;s22;s33];
M=getM(lambda0,lambda1,lambda2,lambda3,SS,nlags,n);
M1=M(:,:,1);
M2=M(:,:,2);
M3=M(:,:,3);
% Compute M_star(i)
M_star1=inv(Sxx+inv(M1));
M_star2=inv(Sxx+inv(M2));
M_star3=inv(Sxx+inv(M3));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%
% STEP 1c: Set priors for inverse of diagonal elements (D) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%
% The mean is calibrated on diagonal elements in omega
uhat=[uhat1 uhat2 uhat3];
S=uhat'*uhat/T;
kappa=0.5;
kappastar=kappa+(T/2); % posterior kappastar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%
% JIM's PROPOSAL to deal with scaling issue
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%
%fixed parameter values
param=[c_eta1;sigma_eta1;nu_eta1;c_gamma2;sigma_gamma2;nu_gamma2;
...
c_beta;sigma_beta;nu_beta;T;vec(omega_hatT);kappastar;kappa];
f_anon =
@(theta_hat)post_val(theta_hat,param,Syy,Sxy,eta,M1,M2,M3,M_star1,M_sta
r2,M_star3,S);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
% STEP 2: Set the variance of the candidate generating density (P) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
W=xsi*eye(c); %variance of RW-MH
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%
% STEP 3: Evaluate posterior at starting value for A: %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%
omega=A*S*A';
taustar1=gettau(kappa,omega(1,1),A(1,:),Syy,Sxy,eta,M1,M_star1);
taustar2=gettau(kappa,omega(2,2),A(2,:),Syy,Sxy,eta,M2,M_star2);
taustar3=gettau(kappa,omega(3,3),A(3,:),Syy,Sxy,eta,M3,M_star3);
% RW-MH algorithm
naccept=0;
count=0;
while count<ndraws
count=count+1;
if (count/10000) == floor(count/10000)
count
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%
% STEP 4a: Generate draw for A from the RW candidate density %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%
A_new=A_old+chol(W)'*PH*randn(c,1)/sqrt(0.5*(randn(1)^2 + randn(1)^2));
% fat tails
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 4b: Evaluate posterior at new draw %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Evaluate prior p(A) at new draw
prior_e = student_prior(A_new(1,1),c_eta1,sigma_eta1,nu_eta1);
prior_b = student_prior(A_new(2,1),c_beta,sigma_beta,nu_beta);
prior_c =
student_prior(A_new(3,1),c_gamma2,sigma_gamma2,nu_gamma2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 5: Compute acceptance probability %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
accept=min([exp(posteriorNEW-posteriorOLD);1]);
u=rand(1); %draw from a uniform distribution
if u<=accept
A_old=A_new; %we retain the new draw
posteriorOLD=posteriorNEW;
naccept=naccept+1; %count the number of acceptances
end
if count>nburn
d11=inv(gamrnd(kappastar,1/gettau(kappa,omega(1,1),AA(1,:),Syy,Sxy,eta,M1,
M_star1)));
d22=inv(gamrnd(kappastar,1/gettau(kappa,omega(2,2),AA(2,:),Syy,Sxy,eta,M2,
M_star2)));
d33=inv(gamrnd(kappastar,1/gettau(kappa,omega(3,3),AA(3,:),Syy,Sxy,eta,M3,
M_star3)));
DD=diag([d11;d22;d33]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%
% STEP 8: Generate a draw for b(i) from multivariate normal %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%
m1_star=getmstar(M_star1,Sxy,M1,eta,AA(1,:));
m2_star=getmstar(M_star2,Sxy,M2,eta,AA(2,:));
m3_star=getmstar(M_star3,Sxy,M3,eta,AA(3,:));
b1=m1_star+(randn(1,nlags*n+1)*chol(d11.*M_star1))';
b2=m2_star+(randn(1,nlags*n+1)*chol(d22.*M_star2))';
b3=m3_star+(randn(1,nlags*n+1)*chol(d33.*M_star3))';
BB=[b1';b2';b3'];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 9: Compute IRFs (not cumulated) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
IRF(:,:,:,count-nburn)=impulse_response_1SD(AA,DD,BB,n,nlags,hmax-1);
clear AA DD BB
end
end
% Prepare histograms
nbin=500;
for jc=1:size(A_post,1)
[ag,bg]=hist(A_post(jc,:),nbin);
delta=bg(1,2)-bg(1,1);
bg_i(jc,:)=bg;
post_i(jc,:)=ag./((ndraws-nburn)*delta);
clear ag bg delta
end
figure(2)
subplot(2,2,1)
bar(bg_i(1,:),post_i(1,:)), hold on, plot(z1,prior_eta1,'r','linewidth',2); box on
axis([-1 1 0 3.5])
title('${\alpha}_{yp}$','interpreter','latex','fontsize',18)
subplot(2,2,2)
bar(bg_i(2,:),post_i(2,:)), hold on, plot(z1,prior_beta,'r','linewidth',2); box on
axis([-1 1 0 2.5])
title('${\alpha}_{pq}$','interpreter','latex','fontsize',18)
subplot(2,2,3)
bar(bg_i(3,:),post_i(3,:)), hold on, plot(z1,prior_gamma2,'r','linewidth',2); box
on
axis([0 0.5 0 7])
title('${\alpha}_{py}$','interpreter','latex','fontsize',18)
subplot(2,2,4)
bar(bgi,post_ii)
axis([-30 30 0 0.25])
title('Short-run oil demand elasticity','interpreter','latex','fontsize',13)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
AM=mean(A_post,2);
AA=[1 0 0; -AM(1,1) 1 0; -AM(2:3,1)' 1];
HH=inv(AA);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot impulse responses for one-standard deviation shocks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alpha=0.025;
index=[alpha*(ndraws-nburn) (1-alpha)*(ndraws-nburn)]; %implies 68%
coverage of the entire distribution
HO=(0:1:hmax-1)'; %impulse response horizon
% figure(1)
% subplot(3,3,1)
% x1=-sort(cumsum(squeeze(IRF(1,1,:,:)),1),2);
% temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
% plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:')
% axis([0 hmax-1 -2.5 1.5])
% set(gca,'XTick',0:5:20)
% set(gca,'YTick',-2.5:1:1.5)
% ylabel('Oil production','fontsize',14)
% title('Oil supply shock','fontsize',14)
% %ylabel('percent','fontsize',12)
%
% subplot(3,3,4)
% x1=sort(cumsum(squeeze(IRF(1,2,:,:)),1),2);
% temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
% plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:')
% axis([0 hmax-1 -2.5 1.5])
% set(gca,'XTick',0:5:20)
% set(gca,'YTick',-2.5:1:1.5)
% ylabel('Oil production','fontsize',14)
% title('Aggregate demand shock','fontsize',14)
%
% subplot(3,3,7)
% x1=sort(cumsum(squeeze(IRF(1,3,:,:)),1),2);
% temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
% plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:')
% axis([0 hmax-1 -2.5 1.5])
% set(gca,'XTick',0:5:20)
% set(gca,'YTick',-2.5:1:1.5)
% ylabel('Oil production','fontsize',14)
% title('Oil-specific demand shock','fontsize',14)
% xlabel('Months','fontsize',12)
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
% subplot(3,3,2)
% x1=-sort(squeeze(IRF(2,1,:,:)),2);
% temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
% plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:')
% axis([0 hmax-1 -5 10])
% set(gca,'XTick',0:5:20)
% set(gca,'YTick',-5:5:10)
% ylabel('Real activity','fontsize',14)
% title('Oil supply shock','fontsize',14)
% %ylabel('percent','fontsize',12)
%
% subplot(3,3,5)
% x1=sort(squeeze(IRF(2,2,:,:)),2);
% temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
% plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:')
% axis([0 hmax-1 -5 10])
% set(gca,'XTick',0:5:20)
% set(gca,'YTick',-5:5:10)
% ylabel('Real activity','fontsize',14)
% title('Aggregate demand shock','fontsize',14)
%
% subplot(3,3,8)
% x1=sort(squeeze(IRF(2,3,:,:)),2);
% temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
% plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:')
% axis([0 hmax-1 -5 10])
% set(gca,'XTick',0:5:20)
% set(gca,'YTick',-5:5:35)
% ylabel('Real activity','fontsize',14)
% title('Oil-specific demand shock','fontsize',14)
% xlabel('Months','fontsize',12)
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% subplot(3,3,3)
% x1=-sort(squeeze(IRF(3,1,:,:)),2);
% temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
% plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:')
% axis([0 hmax-1 -5 10])
% set(gca,'XTick',0:5:20)
% set(gca,'YTick',-5:5:10)
% ylabel('Real oil price','fontsize',14)
% title('Oil supply shock','fontsize',14)
% %ylabel('percent','fontsize',12)
%
% subplot(3,3,6)
% x1=sort(squeeze(IRF(3,2,:,:)),2);
% temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
% plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:')
% axis([0 hmax-1 -5 10])
% set(gca,'XTick',0:5:20)
% set(gca,'YTick',-5:5:10)
% ylabel('Real oil price','fontsize',14)
% title('Aggregate demand shock','fontsize',14)
%
% subplot(3,3,9)
% x1=sort(squeeze(IRF(3,3,:,:)),2);
% temp1=[(median(x1,2)) x1(:,index(1)) x1(:,index(2))];
% plotx1(temp1,HO); box on; plot(HO,zeros(hmax,1),'k:')
% axis([0 hmax-1 -5 10])
% set(gca,'XTick',0:5:20)
% set(gca,'YTick',-5:5:10)
% title('Oil-specific demand shock','fontsize',14)
% ylabel('Real oil price','fontsize',14)
% xlabel('Months','fontsize',12)
%
% save results_KAER
% OLSVARC.M
% Lutz Kilian
% University of Michigan
% April 1997
%
% This program estimates a level VAR with intercept in companion format by
LS
function [A,SIGMA,U,V,X]=olsvarc(y,p);
global q
[t,q]=size(y);
y=y';
Y=y(:,p:t);
for i=1:p-1
Y=[Y; y(:,p-i:t-i)];
end;
X=[ones(1,t-p); Y(:,1:t-p)];
Y=Y(:,2:t-p+1);
A=(Y*X')/(X*X');
U=Y-A*X;
SIGMA=U*U'/(t-p-p*q-1);
V=A(:,1);
A=A(:,2:q*p+1);
function plotx1(y,tt)
set(gcf,'DefaultAxesColorOrder',[0 0 1;1 0 0;1 0 0;0 0 1]);
cu=y(:,2);
cl=y(:,3);
%t=size(y,1);
%h=0:t-1;
%h=h';
h=tt;
% hhx=fill([h(2); h(1:end); flipud([h(1:end); h(end)])],[cu1(1); cl1(1:end);
flipud([cu1(1:end); cl1(size(cl1,1))])],'b');
% set(hhx,'edgecolor',[1 1 1]);
% set(hhx,'facecolor',[0.90 0.90 1]);
%
% hold on;
hold on;
plot(tt,y(:,1),'LineWidth',2);
hold on;
% hold on;
%
% cu=y(:,5);
% cl=y(:,6);
%
% hh=fill([h(2); h(1:end); flipud([h(1:end); h(end)])],[cu(1); cl(1:end);
flipud([cu(1:end); cl(size(cl,1))])],'b');
% set(hh,'edgecolor',[0.95 0.75 1]);
% set(hh,'facecolor',[0.95 0.75 1]);
%
% hold on;
%
% plot(h,y(:,4),'LineWidth',2);
%
function
objective=post_val(theta_hat,param,Syy,Sxy,eta,M1,M2,M3,M_star1,M_star2,
M_star3,S)
c_eta1=param(1);
sigma_eta1=param(2);
nu_eta1=param(3);
c_gamma2=param(4);
sigma_gamma2=param(5);
nu_gamma2=param(6);
c_beta=param(7);
sigma_beta=param(8);
nu_beta=param(9);
T=param(10);
omega_hatT=reshape(param(11:19),3,3);
kappastar=param(20);
kappa=param(21);
prior_e = student_prior(theta_hat(1,1),c_eta1,sigma_eta1,nu_eta1);
prior_b = student_prior(theta_hat(2,1),c_beta,sigma_beta,nu_beta);
prior_c =
student_prior(theta_hat(3,1),c_gamma2,sigma_gamma2,nu_gamma2);
omega=A*S*A';
taustar1=gettau(kappa,omega(1,1),A(1,:),Syy,Sxy,eta,M1,M_star1);
taustar2=gettau(kappa,omega(2,2),A(2,:),Syy,Sxy,eta,M2,M_star2);
taustar3=gettau(kappa,omega(3,3),A(3,:),Syy,Sxy,eta,M3,M_star3);
log_priors=log(prior_e)+log(prior_c)+log(prior_b);
up=log_priors+T/2*log(det(A*omega_hatT*A'));
down=kappastar*log((2/T)*taustar1) ...
+kappastar*log((2/T)*taustar2) ...
+kappastar*log((2/T)*taustar3);
objective=-(up-down);
function out=rows(x)
out=size(x,1);
function [s,uhat] = sd_prior(y,p)
x=ones(size(y,1),1);
for jx=1:p
xx=lag0(y,jx);
x=[x xx];
end
k=size(x,2);
y=y(k:end,:);
x=x(k:end,:);
b=inv(x'*x)*(x'*y);
uhat=y-x*b;
s=sqrt(((y-x*b)'*(y-x*b))/(rows(y)));
function p=student_prior(x, c, sigma, nu)
p = (1/sigma)*tpdf((x-c)/sigma,nu);
function A=varcompanion(A,ndet,n,p)
%creates companion matrix of A
A=A(:,1:end-ndet);
A=[A; eye(n*(p-1)) zeros(n*(p-1),n)];
%estimate VAR impulse response function
%takes estimtes of slope coefficients and structural innovation variance matrix
%h is horizon
function [IRM, K]=VARirf(BETAnc,SIGMA,h)
[K, n]=size(BETAnc);
p=n/K; %determine number of lags used in original estimation
Theta=J*A^(0)*J'*chol(SIGMA)';
IRM =reshape(Theta,K^2,1);
for i=1:h;
Theta=J*A^(i)*J'*chol(SIGMA)';
eval(['IRM' num2str(i) '=reshape(Theta,K^2,1);']);
eval(['IRM=[IRM, IRM' num2str(i) '];' ]);
end;
function vecy=vec(y)
[row,column]=size(y);
vecy=reshape(y,row*column,1);