-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPPM_BH.m
178 lines (161 loc) · 6.88 KB
/
MPPM_BH.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
% Script by Arman Hassanniakalager
% Last Modified 04 Jan 2019 14:31
clear;
clc;
labels={'MXWO' 'NDDUUS' 'NDDUUK' 'NDDUJN' 'MXEF' 'NDEUSRU' 'NDEUCHF' 'NDUEBRAF'...
'MXFEM' 'MSEIESUN' 'M1MA' 'MIMUJORN'};
regions={'Advanced' 'US' 'UK' 'Japan' 'Emerging' 'Russia' 'China' 'Brazil' ...
'Frontier' 'Estonia' 'Morocco' 'Jordan'};
rftable=readtable('FEDL01.csv');
rftable{:,2}=rftable{:,2}/100/260;
transser=[25*ones(1,4),50*ones(1,8)];
%% MPPM definition
rho=2;
d_t=260;
MPPM=@(r_t,r_f) (1/((1-rho)*(1/d_t))*log(mean(((1+r_t)./(1+r_f)).^(1-rho))));
%% Year range
yearser=2006:2015;
startyr=2002;
finishyr=2016;
IS=2; % In year
oosperiod=1;
tic
iter=0;
%% Main loop
for i=1:numel(labels)
lbl=labels{i};
transbasis=transser(i);
fllbl=[lbl,'_',num2str(startyr),'_',num2str(finishyr),'-insample-',num2str(transbasis),'.mat'];
load(fllbl,'master','ret','TF1');
S1=load([lbl,'_',num2str(yearser(1)),'_',num2str(yearser(end)),'.mat'],'resvar');
resvarIS=S1.resvar;
disp(['Dataset loaded for ',lbl]);
iterplus=0;
tic;
for yearOOS=yearser(1):yearser(end)%2006:2016
iter=iter+1;
PortRetIS=[];
PortRetOOS1M=[];
PortRetOOS3M=[];
PortRetOOS6M=[];
ISportFDRsize=[];
ISportFDRser=[];
OOS1MportFDRser=[];
OOS3MportFDRser=[];
OOS6MportFDRser=[];
rf_IS=[];
rf_1M=[];
rf_3M=[];
rf_6M=[];
for month=1:12
iterplus=iterplus+1;
yr=yearOOS-IS;
begindtstrIS=[num2str(month),'/',num2str(yr)];
TF1SMP = contains(master.textdata(:,1),begindtstrIS);
TF1SMP=find(TF1SMP==1,1,'first');
startdtIS=master.textdata(TF1SMP,1);
%% IS
finishdtstrIS=[num2str(month),'/',num2str(yr+IS)];
TF2SMP = contains(master.textdata(:,1),finishdtstrIS);
TF2SMP=find(TF2SMP==1,1,'first')-1;
finishdtIS=master.textdata(TF2SMP,1);
retIS=ret(TF1SMP-TF1+1:TF2SMP-TF1+1,:);
%% IS+1M OOS
finishdtstrIOS1M=[num2str(rem(month+oosperiod,12+1e-10)),'/',num2str(yr+IS+(month+oosperiod>12))];
TF3SMP1M = contains(master.textdata(:,1),finishdtstrIOS1M);
TF3SMP1M=find(TF3SMP1M==1,1,'first')-1;
finishdtIOS1M=master.textdata(TF3SMP1M,1);
%% IS+3M OOS
finishdtstrIOS3M=[num2str(rem(month+3*oosperiod,12+1e-10)),'/',num2str(yr+IS+(month+3*oosperiod>12))];
TF3SMP3M = contains(master.textdata(:,1),finishdtstrIOS3M);
TF3SMP3M=find(TF3SMP3M==1,1,'first')-1;
finishdtIOS3M=master.textdata(TF3SMP3M,1);
%% IS+6M OOS
finishdtstrIOS6M=[num2str(rem(month+6*oosperiod,12+1e-10)),'/',num2str(yr+IS+(month+6*oosperiod>12))];
TF3SMP6M = contains(master.textdata(:,1),finishdtstrIOS6M);
TF3SMP6M=find(TF3SMP6M==1,1,'first')-1;
finishdtIOS6M=master.textdata(TF3SMP6M,1);
%% OOS return
% 1M
retOOS1M=ret(TF2SMP+1-TF1+1:TF3SMP1M-TF1+1,:);
% 3M
retOOS3M=ret(TF2SMP+1-TF1+1:TF3SMP3M-TF1+1,:);
% 6M
retOOS6M=ret(TF2SMP+1-TF1+1:TF3SMP6M-TF1+1,:);
%% Excess return
rfind1=find(rftable{:,1}==char(startdtIS),1,'first');
rfind2=find(rftable{:,1}==char(finishdtIS),1,'first');
excessretIS=retIS-rftable{rfind1:rfind2,2};
rfind31M=find(rftable{:,1}==char(finishdtIOS1M),1,'first');
rfind33M=find(rftable{:,1}==char(finishdtIOS3M),1,'first');
rfind36M=find(rftable{:,1}==char(finishdtIOS6M),1,'first');
rf_IS(end+1:end+size(retIS,1),1)=rftable{rfind1:rfind2,2};
rf_1M(end+1:end+size(retOOS1M,1),1)=rftable{rfind2+1:rfind31M,2};
rf_3M(end+1:end+size(retOOS3M,1),1)=rftable{rfind2+1:rfind33M,2};
rf_6M(end+1:end+size(retOOS6M,1),1)=rftable{rfind2+1:rfind36M,2};
%% Loading the FDR portfolios
ISportFDR=resvarIS{(iterplus-1)*2+IS,1};
%% Portfolio generation
% IS portfolio return
finalretIS=sum(excessretIS);
PortRetIS(end+1)=finalretIS*ISportFDR/max(sum(ISportFDR),1);
ISportFDRser(end+1:end+size(excessretIS,1))=mean(excessretIS(:,ISportFDR==1),2);
% OOS portfolio return
% 1M
finalretOOS1M=sum(retOOS1M);
PortRetOOS1M(end+1)=finalretOOS1M*ISportFDR/max(sum(ISportFDR),1);
OOS1MportFDRser(end+1:end+size(retOOS1M,1))=mean(retOOS1M(:,ISportFDR==1),2);
% 3M
finalretOOS3M=sum(retOOS3M);
PortRetOOS3M(end+1)=finalretOOS3M*ISportFDR/max(sum(ISportFDR),1);
OOS3MportFDRser(end+1:end+size(retOOS3M,1))=mean(retOOS3M(:,ISportFDR==1),2);
% 6M
finalretOOS6M=sum(retOOS6M);
PortRetOOS6M(end+1)=finalretOOS6M*ISportFDR/max(sum(ISportFDR),1);
OOS6MportFDRser(end+1:end+size(retOOS6M,1))=mean(retOOS6M(:,ISportFDR==1),2);
end
%% B&H Ret & Sharpe
T_S = find(contains(master.textdata(:,1),num2str(yearOOS)),1,'first')-1;
T_L = find(contains(master.textdata(:,1),num2str(yearOOS)),1,'last')-1;
t_ser=price2ret(master.data(T_S-1:T_L,end));
t_excess=t_ser-rf_1M;
B_H_ret=sum(t_excess);
B_H_Sharpe=sharpe(t_excess,0)*260^.5;
%% Manipulation ratio
IS_MPPM=MPPM(ISportFDRser',rf_IS);
OOS1M_SMPPM=MPPM(OOS1MportFDRser',rf_1M);
OOS3M_SMPPM=MPPM(OOS3MportFDRser',rf_3M);
OOS6M_SMPPM=MPPM(OOS6MportFDRser',rf_6M);
%% Recording the results
% Period
dim=1;
resvar{iter,dim}=yearOOS;
% B&H RET
dim=dim+1;
resvar{iter,dim}=B_H_ret;
% B&H Sharpe
dim=dim+1;
resvar{iter,dim}=B_H_Sharpe;
% MPPM
dim=dim+1;
resvar{iter,dim}=IS_MPPM;
dim=dim+1;
resvar{iter,dim}=OOS1M_SMPPM;
dim=dim+1;
resvar{iter,dim}=OOS3M_SMPPM;
dim=dim+1;
resvar{iter,dim}=OOS6M_SMPPM;
dim=dim+1;
resvar{iter,dim}=regions{i};
disp(['Processing done for ',regions{i},' period ',num2str(yearOOS), ' completed ...']);
toc;
end
end
result_table=cell2table(resvar);
result_table.Properties.VariableNames={'Year' 'BH_RET' ...
'BH_SHARPE' 'IS_MPPM' 'OOS1M_MPPM' 'OOS3M_MPPM' 'OOS6M_MPPM' ...
'Country'};
%drpadd='/Empirics3/';
drpadd='C:\Users\ah2493\Dropbox\Shared_Folder\Chapter3\';
writetable(result_table,[drpadd,'AnnualTable_',num2str(yearser(1)),'_',num2str(yearser(end)),'_IS_',num2str(IS),'_MPPM.xlsx'])
toc