-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalternative_identification.m
487 lines (429 loc) · 17.3 KB
/
alternative_identification.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
%////////////////////////////////////////////////////////////////%
%//////- Structural-VAR (Assessment Idetificatiojn) - ///////%
%////////////////////////////////////////////////////////////////%
clc;
clear;
%---
z1=xlsread('Data_oil_1.xlsx');
time=(1973+1/12:1/12:2019)';
RAC = [z1(:,1)]; % US Refiner Acquisition Cost of Crude Oil
WTI = [z1(:,2)]; % West Texas Intermediate Oil Price
Oil_p = [z1(:,3)]; % Global Oil production
Oil_i = [z1(:,4)]; % OECD oil invetories
Kil_i = [z1(:,5)]; % A proxy of Global economic activity
Ham_i = [z1(:,6)]; % 2 years growth in global industry production
%Montly Percentage change in global crude oil production
[T,~]=size(Oil_p);
for i =1: size(Oil_p,2)
for ii=2 : size(Oil_p,1)
goil_p(ii-1,i)=((Oil_p(ii,i)-Oil_p(ii-1,i)))*100;
end
end
%Montly Percentage change in OECD oil inventories
[T,~]=size(Oil_i);
for i =1: size(Oil_i,2)
for ii=2 : size(Oil_i,1)
goil_i(ii-1,i)=((Oil_i(ii,i)-Oil_i(ii-1,i)))*100;
end
end
% Anual Percentage change in global crude oil production
gaoil_p=100*((Oil_p(13:T,1)./Oil_p(1:T-12,1))-1);
%% VAR Estimation
%The model is estimating using seasonal dummies and 24 autoregressive lags
% Information assemble. The sample goes from 1973m2 to 2006m12
%z2= [goil_p Kil_i(2:T,:) RAC(2:T,:) goil_i ];
z3= [goil_p Kil_i(2:T,:) RAC(2:T,:) ];
mmm=size(z3,2);
pp=24;
Horiz=18;
no_ofIRF=535;
NO_MAX_TRIALS=100000;
which_Shock_plot=3;
which_Shock_plot_1=2;
which_Shock_plot_2=1;
[AR_3d,Chol_Var] = VAR_OLS(z3,pp,1,[]);
Ai_mat = dyn_multipliers(mmm,pp,AR_3d,Horiz);
%-----------------------------------------------%
%//// Impulse Responses by sign restriction ////%
%-----------------------------------------------%
[Q] = sing_restriction(Ai_mat,Chol_Var,NO_MAX_TRIALS);
%Oil Production
for ii=1:1:no_ofIRF
[Q] = sing_restriction(Ai_mat,Chol_Var,NO_MAX_TRIALS);
Store_Q(:,ii)=Q(:);
Shock_2=zeros(3,1); Shock_2(which_Shock_plot_2,1)=1;
IRF3d_2(:,:,ii)=Sirf(mmm,Horiz,Ai_mat,Chol_Var*Q,Shock_2)';
for shsh = 1:3
Shock=zeros(3,1); Shock(shsh,1)=1;
IRF4d_2(:,:,shsh,ii)=Sirf(mmm,Horiz,Ai_mat,Chol_Var*Q,Shock)';
end
clear Q
end
%Econoic Activity
for ii=1:1:no_ofIRF
[Q] = sing_restriction(Ai_mat,Chol_Var,NO_MAX_TRIALS);
Store_Q(:,ii)=Q(:);
Shock_1=zeros(3,1); Shock_1(which_Shock_plot_1,1)=1;
IRF3d_1(:,:,ii)=Sirf(mmm,Horiz,Ai_mat,Chol_Var*Q,Shock_1)';
for shsh = 1:3
Shock=zeros(3,1); Shock(shsh,1)=1;
IRF4d_1(:,:,shsh,ii)=Sirf(mmm,Horiz,Ai_mat,Chol_Var*Q,Shock)';
end
clear Q
end
%Real Price of Oil
for ii=1:1:no_ofIRF
[Q] = sing_restriction(Ai_mat,Chol_Var,NO_MAX_TRIALS);
Store_Q(:,ii)=Q(:);
Shock=zeros(3,1); Shock(which_Shock_plot,1)=1;
IRF3d(:,:,ii)=Sirf(mmm,Horiz,Ai_mat,Chol_Var*Q,Shock)';
for shsh = 1:3
Shock=zeros(3,1); Shock(shsh,1)=1;
IRF4d(:,:,shsh,ii)=Sirf(mmm,Horiz,Ai_mat,Chol_Var*Q,Shock)';
end
clear Q
end
%%
%---------------------------------------------%
%----------------- Ploting--------------------%
%---------------------------------------------%
TITLES=['Oil Supply Shock(-) ';...
'Oil Supply Shock(-) ';...
'Oil Supply Shock (+) ';...
'Aggregate Demand Shock(+) ';...
'Aggregate Demand Shock(+) ';...
'Aggregate Demand Shock(+) ';...
'Oil-Specific Demand Shock(+)';...
'Oil-Specific Demand Shock(-)';...
'Oil-Specific Demand Shock(+)';];
labels=['Oil Production ';...
'Real Activity ';...
'Real Price of Oil';...
'Oil Production ';...
'Real Activity ';...
'Real Price of Oil';...
'Oil Production ';...
'Real Activity ';...
'Real Price of Oil';];
for variab=1:1:mmm
figure(1)
subplot(1,3,variab);
PLOT_IRF=[];
for ii=1:1:no_ofIRF
PLOT_IRF=[PLOT_IRF IRF3d_2(:,variab,ii)];
end
plot([0:1:Horiz]',[PLOT_IRF],'linewidth',.1,'color','b');
xlim([0 Horiz])
title(TITLES(3*(which_Shock_plot_2-1)+variab,:),'FontSize',11);
ylabel(labels(3*(which_Shock_plot_2-1)+variab,:),'FontSize',11);
sgtitle('Graph: Impulse Responses (Identification by sign-restrictions)' )
end
for variab=1:1:mmm
figure(2)
subplot(1,3,variab);
PLOT_IRF=[];
for ii=1:1:no_ofIRF
PLOT_IRF=[PLOT_IRF IRF3d_1(:,variab,ii)];
end
plot([0:1:Horiz]',[PLOT_IRF],'linewidth',.1,'color','b');
xlim([0 Horiz])
title(TITLES(3*(which_Shock_plot_1-1)+variab,:),'FontSize',11);
ylabel(labels(3*(which_Shock_plot_1-1)+variab,:),'FontSize',11);
sgtitle('Graph: Impulse Responses (Identification by sign-restrictions)' )
end
for variab=1:1:mmm
figure(3)
subplot(1,3,variab);
PLOT_IRF=[];
for ii=1:1:no_ofIRF
PLOT_IRF=[PLOT_IRF IRF3d(:,variab,ii)];
end
plot([0:1:Horiz]',[PLOT_IRF],'linewidth',.1,'color','b');
xlim([0 Horiz])
title(TITLES(3*(which_Shock_plot-1)+variab,:),'FontSize',11);
ylabel(labels(3*(which_Shock_plot-1)+variab,:),'FontSize',11);
sgtitle('Graph: Impulse Responses (Identification by sign-restrictions)' )
end
%%
%-------------------------------------------%
%--------- Median Impulse Responses---------%
%-------------------------------------------%
low_quant = .16;
up_quant = .84;
for d1 = 1:size(IRF4d,1)
for d2 = 1:size(IRF4d,2)
for shsh = 1:size(IRF4d,3)
IRF3d_MED(d1,d2,shsh)=median(squeeze(IRF4d(d1,d2,shsh,:)));
IRF3d_UP(d1,d2,shsh)=quantile(squeeze(IRF4d(d1,d2,shsh,:)),up_quant);
IRF3d_DOWN(d1,d2,shsh)=quantile(squeeze(IRF4d(d1,d2,shsh,:)),low_quant);
end
end
end
zirf_med_1=IRF3d_MED(:,1,1);% Oil Supply to Oil production
zirf_med_2=IRF3d_MED(:,2,1);%Aggregate demand to Oil produciton
zirf_med_3=IRF3d_MED(:,3,1);%Specific-demand to Oil production
zirf_med_4=IRF3d_MED(:,1,2);%Oil supply to real activity
zirf_med_5=IRF3d_MED(:,2,2);%Aggregate demand to real activity
zirf_med_6=IRF3d_MED(:,3,2);%Specific-demand to Oil production
zirf_med_7=IRF3d_MED(:,1,3);%Oil supply to real price of oil
zirf_med_8=IRF3d_MED(:,2,3);% Aggregate demand to real price
zirf_med_9=IRF3d_MED(:,3,3);%Oil-specific demand to real price of oil
%--------------------------------------
TITLES=['Oil Supply Shock(-) ';...
'Oil Supply Shock(-) ';...
'Oil Supply Shock(+) ';...
'Aggregate Demand Shock(+) ';...
'Aggregate Demand Shock(+) ';...
'Aggregate Demand Shock(+) ';...
'Oil-Specific Demand Shock(+)';...
'Oil-Specific Demand Shock(-)';...
'Oil-Specific Demand Shock(+)';];
labels=['Oil Production ';...
'Real Activity ';...
'Real Price of Oil';...
'Oil Production ';...
'Real Activity ';...
'Real Price of Oil';...
'Oil Production ';...
'Real Activity ';...
'Real Price of Oil';];
% Ploting
for ii=1:1:3;
for variab=1:1:mmm
figure(4)
subplot(3,3,3*(ii-1)+variab);
plot([0:1:Horiz]',[IRF3d_MED(:,variab,ii)'],'linewidth',2);
hold
plot([0:1:Horiz]',[IRF3d_DOWN(:,variab,ii) IRF3d_UP(:,variab,ii)],'linewidth',1,'LineStyle','--','color','k');
title(TITLES(3*(ii-1)+variab,:),'FontSize',11);
ylabel(labels(3*(ii-1)+variab,:),'FontSize',11);
xlim([0 Horiz])
sgtitle('Graph: Impulse Responses (Identification by sign-restrictions)' )
end
end
%% Choleski vs Sign Restriction Identification
%First, SVAR estimation by Choleski decomposition
[T, N] = size(z3);
Horiz_1=24;
[A_2,SIGMA_2, Uhat_2, V_2]=lsvarcsa(z3,Horiz_1);
mm_2=size(z3,2);
B0inv_2=chol(SIGMA_2(1:N,1:N))';
J_1=[eye(N,N) zeros(N,N*(Horiz_1-1))];
IRF_storage_2=reshape(J_1*A_2^0*J_1'*B0inv_2,N^2,1);
for i=1:T-Horiz_1-1
IRF_storage_2=([IRF_storage_2 reshape(J_1*A_2^i*J_1'*B0inv_2,N^2,1)]);
end;
IRF_storage_2_w=IRF_storage_2
IRF_storage_2=IRF_storage_2(:,1:19)'
%--Ploting the impulses responses with and with-out inventories
hh = 18;
figure(5);
subplot(3,3,1);
plot([0:hh]',[zirf_med_1],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,1) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sign-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Oil production','fontsize',11)
title('Oil supply shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,4);
plot([0:hh]',[zirf_med_2],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,2) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sign-Restriction' , 'Rescursive Restriction', 'Location', 'best')
ylabel('Oil production','fontsize',11)
title('Aggregate demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,7);
plot([0:hh]',[zirf_med_3],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,3) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Oil production','fontsize',11)
title('Oil-specific demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,3,2);
plot([0:hh]',[zirf_med_4],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,4) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real Activity','fontsize',11)
title('Oil supply shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,5);
plot([0:hh]',[zirf_med_5],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,5) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restrcition' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real Activity','fontsize',11)
title('Aggregate demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,8);
plot([0:hh]',[zirf_med_6],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,6) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real Activity','fontsize',11)
title('Oil-specific demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,3,3);
plot([0:hh]',[zirf_med_7],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,7) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real oil price','fontsize',11)
title('Oil supply shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,6);
plot([0:hh]',[zirf_med_8],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,8) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restrcition' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real oil price','fontsize',11)
title('Aggregate demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,9);
plot([0:hh]',[zirf_med_9],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,9) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real oil price','fontsize',11)
title('Oil-specific demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
sgtitle('Graph: Structural Impulse Responses' )
%////////////////////////////////////////////////////////////////////////////////////////
%////////////////////////////////////////////////////////////////////////////////////////
figure(6);
subplot(3,3,1);
plot([0:hh]',[zirf_med_1],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) -cumsum(squeeze(IRF_storage_2(:,1))) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sign-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Oil production','fontsize',11)
title('Oil supply shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,4);
plot([0:hh]',[zirf_med_2],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,2) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sign-Restriction' , 'Rescursive Restriction', 'Location', 'best')
ylabel('Oil production','fontsize',11)
title('Aggregate demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,7);
plot([0:hh]',[zirf_med_3],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,3) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Oil production','fontsize',11)
title('Oil-specific demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,3,2);
plot([0:hh]',[zirf_med_4],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) cumsum(IRF_storage_2(:,4)) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real Activity','fontsize',11)
title('Oil supply shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,5);
plot([0:hh]',[zirf_med_5],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,5) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restrcition' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real Activity','fontsize',11)
title('Aggregate demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,8);
plot([0:hh]',[zirf_med_6],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,6) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real Activity','fontsize',11)
title('Oil-specific demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,3,3);
plot([0:hh]',[zirf_med_7],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) cumsum(IRF_storage_2(:,7)) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real oil price','fontsize',11)
title('Oil supply shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,6);
plot([0:hh]',[zirf_med_8],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,8) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restrcition' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real oil price','fontsize',11)
title('Aggregate demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
subplot(3,3,9);
plot([0:hh]',[zirf_med_9],'linewidth',2)
hold on;
plot([0:hh]',[zeros(hh+1,1) IRF_storage_2(:,9) ],'linewidth',1,'LineStyle','-','color','k');
xlim([0 hh])
legend([], 'Sing-Restriction' , 'Recursive Restriction', 'Location', 'best')
ylabel('Real oil price','fontsize',11)
title('Oil-specific demand shock','fontsize',11)
set(gcf, 'color', 'w')
legend('boxoff');
hold off;
sgtitle('Graph: Structural Impulse Responses' )
%////////////////////////////////////////////////////////////////////////////////
%///////////////////////////////////////////////////////////////////////////////