-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQ4_EXT~1.GMS
265 lines (207 loc) · 8.26 KB
/
Q4_EXT~1.GMS
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
$TITLE Value at Risk and Conditional Value at Risk models
* VaR_CVaR.gms: Value at Risk and Conditional Value at Risk models.
$eolcom //
option optcr=0.005, reslim=120;
* Define Sets and Parameters
SETS
Assets Set of assets tickers
MonthlyCount
Dates
Scenario;
ALIAS (Assets, i, j);
ALIAS (Scenario, s);
ALIAS (MonthlyCount, c);
ALIAS (Dates, t);
PARAMETER
AssetReturns(t, i)
MonthlyScenario(s, c, i) Monthly Scenarios;
* Read data
$GDXIN Scenario_generator.gdx
$LOAD Assets, MonthlyCount, Scenario,MonthlyScenario
$GDXIN
$GDXIN DataFinal.gdx
$LOAD Dates, AssetReturns
$GDXIN
* Get only first month's Scenarios
parameter MonthOneScens(s, i);
MonthOneScens(s, i) = MonthlyScenario(s, 'MTH_1', i);
display MonthOneScens;
SCALARS
Budget 'Nominal investment budget'
alpha 'Confidence level'
RISK_TARGET 'Bound on CVaR (risk)'
lambda 'lambda'
CurrentAsset 'Current Assets for existing portfolio'
;
Budget = 100.0;
alpha = 0.95;
PARAMETERS
pr(s) 'Scenario probability'
P(s, i) 'Final values'
EP(i) 'Expected final values'
;
pr(s) = 1.0 / CARD(s);
P(s, i) = 1 + MonthOneScens ( s, i );
EP(i) = SUM(s, pr(s) * P(s, i));
POSITIVE VARIABLES
x(i) 'Holdings of assets in monetary units (not proportions)'
VaRDev(s) 'Measures of the deviations from the VaR'
;
VARIABLES
VaR 'Value-at-Risk'
Ret 'Objective function value'
CVaR 'Objective function value'
Losses(s) 'Measures of the losses'
z 'Objective function value'
;
EQUATIONS
BudgetCon 'Equation defining the budget contraint'
LossDef(s) 'Equations defining the losses'
CVaRCon 'Equation defining the CVaR allowed'
VaRDevCon(s) 'Equations defining the VaR deviation constraints'
ObjDefReturn 'Objective function definition for return mazimization'
ObjDefCVaR 'Objective function definition for CVaR minimization'
ObjDef 'Objective function definition for Mean/CVaR Model'
;
BudgetCon .. SUM(i, x(i)) =E= Budget;
LossDef(s).. Losses(s) =E= (Budget)$(lambda = 0.001) - SUM(i, P(s,i) * x(i));
VaRDevCon(s) .. VaRDev(s) =G= Losses(s) - VaR;
ObjDefCVaR .. CVaR =E= VaR + SUM(s, pr(s) * VaRDev(s)) / (1 - alpha);
CVaRCon .. CVaR =L= RISK_TARGET;
ObjDefReturn .. Ret =E= SUM(i, EP(i) * x(i));
ObjDef .. z =E= (1 - lambda)*Ret - lambda*CVaR;
MODEL MaxReturn 'The MaxReturn model' /BudgetCon, CVaRCon, LossDef, VaRDevCon, ObjDefCVaR, ObjDefReturn, ObjDef/;
PARAMETERS
ReP(s, i) 'Final values'
ReEP(i) 'Expected final values'
HistMonthly(c, i) 'Historial Monthly'
Y(i) 'Existing Portfolio'
ActualRet_RA(c) 'Risk Averse Actual Returns'
RevPort_RA(c, i) 'Risk Averse Revised Portfolios'
Sell_RA(c, i) 'Risk Averse Assets Sold'
Buy_RA(c, i) 'Risk Averse Assets Bought'
ActualRet_RN(c) 'Risk Neutral Actual Return'
RevPort_RN(c, i) 'Risk Neutral Revised Portfolios'
Sell_RN(c, i) 'Risk Neutral Assets Sold'
Buy_RN(c, i) 'Risk Neutral Assets Bought'
ActualRet_BM(c) 'BenchMark Mean Return'
RetChange_BM(c) 'BenchMark Return Change'
RevPort_BM(c, i) 'BenchMark Revised Portfolios'
SummaryReport(*,*,*)
;
loop(c,
HistMonthly(c, i) = PROD(t$(ord(t) > (155 + (ord(c)-1)*4) and ord(t) < (ord(c)*4 + 156)), (1 + AssetReturns(t, i)));
);
POSITIVE VARIABLES
sell(i) 'Assets Sold'
buy(i) 'Assets Bought'
;
EQUATIONS
BudgetConRev 'Revision Budget Constraint'
LossDefRev(s) 'Revision Loss function'
ReturnRev 'Revision Returns'
ChangeCon(i) 'Revision Change in Assets'
;
ChangeCon(i).. x(i) =E= Y(i) - sell(i) + buy(i);
BudgetConRev .. SUM(i, x(i)) =E= CurrentAsset;
LossDefRev(s).. Losses(s) =E= (CurrentAsset)$(lambda = 0.001) - SUM(i, ReP(s,i) * x(i));
ReturnRev .. Ret =E= SUM(i, ReEP(i) * x(i));
MODEL Revision 'Portoflio Revision Model' /ChangeCon, BudgetConRev, CVaRCon, LossDefRev, VaRDevCon, ObjDefCVaR, ReturnRev, ObjDef/;
parameter SumScenRet(c, s), WC(c), BC(c), AC(c);
* BenchMark
x.l(i) = 1/CARD(i) * Budget;
*DISPLAY x.l;
LOOP(c,
CurrentAsset = SUM(i, x.l(i));
x.l(i) = x.l(i) * HistMonthly(c, i);
Ret.l = SUM(i, x.l(i));
ActualRet_BM(c) = Ret.l;
RevPort_BM(c, i) = x.l(i);
);
SummaryReport('BenchMark','Actual_BM', c) = ActualRet_BM(c);
SummaryReport('BenchMark',i, c) = RevPort_BM(c, i);
* Risk Averse
lambda = 0.999;
RISK_TARGET = 1000000;
SOLVE MaxReturn MAXIMIZING z USING LP;
RISK_TARGET = CVaR.l;
SOLVE MaxReturn MAXIMIZING z USING LP;
Y(i) = x.l(i) * HistMonthly('MTH_1', i);
* Save data for output
ActualRet_RA('MTH_1') = SUM(i, Y(i));
RevPort_RA('MTH_1', i) = x.l(i);
* Generate Beest Case Worst Case and Average Case scenario
SumScenRet('MTH_1', s) = sum(i, (x.l(i) * (1 + MonthlyScenario(s, 'MTH_1', i))));
WC('MTH_1')= smin(s, SumScenRet('MTH_1',s));
BC('MTH_1')= smax(s, SumScenRet('MTH_1',s));
AC('MTH_1')= sum(s,SumScenRet('MTH_1',s))/card(s);
LOOP(c$(ORD(c) > 1),
ReP(s, i) = 1 + MonthlyScenario(s, c, i);
ReEP(i) = SUM(s, pr(s) * ReP(s, i));
CurrentAsset = SUM(i, Y(i));
SOLVE Revision MAXIMIZING z USING LP;
break$(Revision.MODELSTAT = 4);
Y(i) = x.l(i) * HistMonthly(c, i);
* Save Results *
ActualRet_RA(c) = SUM(i, Y(i));
RevPort_RA(c, i) = x.l(i);
Sell_RA(c, i) = sell.l(i);
Buy_RA(c, i) = buy.l(i);
* Generate Best Case Worst Case and Average Case *
SumScenRet(c, s) = sum(i, (x.l(i) * (1 + MonthlyScenario(s, c, i))));
WC(c)= smin(s, SumScenRet(c,s));
BC(c)= smax(s, SumScenRet(c,s));
AC(c)= sum(s,SumScenRet(c,s))/card(s);
);
*Saving Data for output*
SummaryReport('RiskAverse','Actual_RA', c) = ActualRet_RA(c);
SummaryReport('RiskAverseSell',i, c) = Sell_RA(c, i);
SummaryReport('RiskAverseBuy',i, c) = Buy_RA(c, i);
SummaryReport('RiskAverse',i, c) = RevPort_RA(c, i);
SummaryReport('RiskAverseBestCase','Ret_BC', c) = BC(c);
SummaryReport('RiskAverseWorstCase','Ret_WC', c) = WC(c);
SummaryReport('RiskAverseAverageCase','Ret_AC', c) = AC(c);
* Risk Neutral
lambda = 0.001;
RISK_TARGET = 1000000;
SOLVE MaxReturn MAXIMIZING z USING LP;
RISK_TARGET = CVaR.l;
SOLVE MaxReturn MAXIMIZING z USING LP;
Y(i) = x.l(i) * HistMonthly('MTH_1', i);
* Save First Month Data
ActualRet_RN('MTH_1') = SUM(i, Y(i));
RevPort_RN('MTH_1', i) = x.l(i);
* Generate Best Case Worst Case Average Case for first month
SumScenRet('MTH_1', s) = sum(i, (x.l(i) * (1 + MonthlyScenario(s, 'MTH_1', i))));
WC('MTH_1')= smin(s, SumScenRet('MTH_1',s));
BC('MTH_1')= smax(s, SumScenRet('MTH_1',s));
AC('MTH_1')= sum(s,SumScenRet('MTH_1',s))/card(s);
LOOP(c$(ORD(c) > 1),
ReP(s, i) = 1 + MonthlyScenario(s, c, i);
ReEP(i) = SUM(s, pr(s) * ReP(s, i));
CurrentAsset = SUM(i, Y(i));
SOLVE Revision MAXIMIZING z USING LP;
break$(Revision.MODELSTAT = 4);
Y(i) = x.l(i) * HistMonthly(c, i);
* Save Data for output *
ActualRet_RN(c) = SUM(i, Y(i));
RevPort_RN(c, i) = x.l(i);
Sell_RN(c, i) = sell.l(i);
Buy_RN(c, i) = buy.l(i);
* Generate Best Case Worst Case and Average Case *
SumScenRet(c, s) = sum(i, x.l(i) * (1 + MonthlyScenario(s, c, i)));
WC(c)=smin(s, SumScenRet(c,s));
BC(c)=smax(s, SumScenRet(c,s));
AC(c)=sum(s,SumScenRet(c,s))/card(s);
*
);
SummaryReport('RiskNeutral','Actual_RN', c) = ActualRet_RN(c);
SummaryReport('RiskNeutral',i, c) = RevPort_RN(c, i);
SummaryReport('RiskNeutralBuy',i, c) = Buy_RN(c, i);
SummaryReport('RiskNeutralSell',i, c) = Sell_RN(c, i);
SummaryReport('RiskNeutralBestCase','Ret_BC', c) = BC(c);
SummaryReport('RiskNeutralWorstCase','Ret_WC', c) = WC(c);
SummaryReport('RiskNeutralAverageCase','Ret_AC', c) = AC(c);
DISPLAY SummaryReport, RISK_TARGET;
EXECUTE_UNLOAD 'Summary.gdx', SummaryReport;
EXECUTE 'gdxxrw.exe Summary.gdx O=Q4_ExtendedBacktesting.xls par=SummaryReport rng=sheet1!a1' ;;