You are on page 1of 3

clc;

clear;
data= xlsread('oil_gas.xlsx');
P1=data(7:end,2); %Gas price
P2=data(7:end,1); %Oil price
G = log(P1); %log gas price
O= log(P2); %log oil price
dG=diff(G); %convert the log price series into a stationary time series
dO=diff(O);
N = length(dG); % no.of observations
Y = [dG,dO];
figure (1)
subplot(2,1,1)
parcorr(dG)
subplot(2,1,2)
parcorr(dO)
%Create a VAR model:
%determine optimal lags
pmax=30;
logl=zeros(pmax,1);
N_Act=zeros(pmax,1);
for p=1:pmax
Spec = vgxset('n',2,'ARlag',p);
[EstSpec,EstStdErrors,logl(p,1),w] = vgxvarx(Spec,Y);
[N_P,N_Act(p,1)] = vgxcount(EstSpec);
end
[aic,bic] = aicbic(logl,N_Act,N.*ones(pmax,1))
minAIC=min(aic)
best_aic=find(aic==minAIC)
minBIC=min(bic)
best_bic=find(bic==minBIC)
% both AIC and BIC indicate Lag 5
%compare and estimate two VAR(5) models
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ypre = Y(1:5,:);
T = ceil(.9*size(Y,1)); %decide estimation period, 90% of the data
Yest = Y(6:T,:);
YF = Y((T+1):end,:); %forecast period
TF = size(YF,1);
Spec1 = vgxset('n',2,'nAR',5,'Constant',false);%Model1: VAR(5) with all lags
Spec2 = vgxset('n',2,'ARlag',[5],'Constant',false );%Model1: VAR(5) with lag 5 o
nly
[EstSpec1,EstStdErrors1,LLF1,W1] = ...
vgxvarx(Spec1,Yest,[],Ypre);
[EstSpec2,EstStdErrors2,LLF2,W2] = ...
vgxvarx(Spec2,Yest,[],Ypre);

vgxdisp(EstSpec1, EstStdErrors1)
vgxdisp(EstSpec2, EstStdErrors2)
%Checking model stability
[isStable1,isInvertible1] = vgxqual(EstSpec1);
[isStable2,isInvertible2] = vgxqual(EstSpec2);
[isStable1,isStable2]
%Compare model fit, likelihood ration test and AIC BIC
[n1,n1p] = vgxcount(EstSpec1);
[n2,n2p] = vgxcount(EstSpec2);
reject1 = lratiotest(LLF1,LLF2,n1p - n2p) %h is 1 indicating the restrict model
is rejected.
[AIC,BIC] = aicbic([LLF1 LLF2],[n1p n2p],N-5)%AIC and BIC indicate Model 1 is be
tter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compare forecast error
%%
[FY1,FYCov1] = vgxpred(EstSpec1,TF,[],Yest);
[FY2,FYCov2] = vgxpred(EstSpec2,TF,[],Yest);
figure(2)
vgxplot(EstSpec2,Yest,FY2,FYCov2)
error1 = YF - FY1;
error2 = YF - FY2;
SSerror1 = error1(:)' * error1(:);
SSerror2 = error2(:)' * error2(:);
figure(3)
bar([SSerror1 SSerror2],.5)
ylabel('Sum of squared errors')
set(gca,'XTickLabel',...
{'AR5 Full' 'AR[5]' })
title('Sum of Squared Forecast Errors')
% % impulse response function (60 days)
FT=60;
impSpec1 = vgxvarx(Spec1,Y(6:end,:),[],Y(1:5,:));
impSpec1 = vgxset(impSpec1,'Series',...
{'Log difference of gas prices','Log difference of oil prices'});
W0 = zeros(60, 2); % Innovations without a shock
W1 = W0;
W1(1,1) = sqrt(impSpec1.Q(1,1)); % Innovations with a gas price (in terms of log
price difference) shock
Yimpulse = vgxproc(impSpec1,W1,[],Y); % Process with a gas price shock
Ynoimp = vgxproc(impSpec1,W0,[],Y); % Process with no shock
Yimp1 = exp(cumsum(Yimpulse)); % Undo scaling, change back to prices
Ynoimp1 = exp(cumsum(Ynoimp));
Diff1= Yimp1(:,1) - Ynoimp1(:,1); %difference between the gas price with a gas p
rice shock and no gas price shock
figure(4)

subplot(2,1,1)
plot(100.*Diff1);
title(...
'Impact of a Gas Price Shock on the Gas Prices')
ylabel('Net Impact')
subplot(2,1,2)
Diff2 = Yimp1(:,2) - Ynoimp1(:,2); %difference between the oil price with a gas
price shock and no gas price shock
plot(100.*Diff2);
title(...
'Impact of a Gas Price Shock on the Oil Prices')
ylabel('Net impact')
%gas price shock
figure(5)
W2=W0;
W2(1,2) = sqrt(impSpec1.Q(2,2)); % Innovations with an oil price shock (in terms
of log price difference)
Yimpulse
Ynoimp =
Yimp11 =
Ynoimp11

= vgxproc(impSpec1,W2,[],Y); % Process with an oil price shock


vgxproc(impSpec1,W0,[],Y); % Process with no shock
exp(cumsum(Yimpulse)); % Undo scaling
= exp(cumsum(Ynoimp));

subplot(2,1,1)
Diff3=(Yimp11(:,1) - Ynoimp11(:,1));
plot(100.*Diff3)
title(...
'Impact of an Oil Price Shock on the Gas Prices')
ylabel('Net Impact')
subplot(2,1,2)
Diff4 = Yimp11(:,2) - Ynoimp11(:,2); %impact of an Oil price shock on oil price
s
plot(100.*Diff4);
title(...
'Impact of An Oil Price Shock on Oil Prices')
ylabel('Net impact')
shock_to_logprices=[W1(1,1),W2(1,2)]
shock_to_prices=[exp(W1(1,1)),exp(W2(1,2))]

You might also like