-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_efficient_cdar.py
More file actions
447 lines (359 loc) · 12.9 KB
/
test_efficient_cdar.py
File metadata and controls
447 lines (359 loc) · 12.9 KB
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
import numpy as np
import pytest
from skbase.utils.dependencies import _check_soft_dependencies
from pypfopt import EfficientCDaR, expected_returns, objective_functions
from pypfopt.exceptions import OptimizationError
from tests.utilities_for_tests import get_data, setup_efficient_cdar
def test_cdar_example():
beta = 0.95
cd = setup_efficient_cdar(beta=beta)
w = cd.min_cdar()
cdar = cd.portfolio_performance()[1]
assert isinstance(w, dict)
assert set(w.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 1)
assert all([i >= -1e-5 for i in w.values()])
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.14798, 0.056433),
rtol=1e-4,
atol=1e-4,
)
df = get_data()
historical_rets = expected_returns.returns_from_prices(df).dropna()
portfolio_rets = historical_rets @ cd.weights
cum_rets = portfolio_rets.cumsum(0)
drawdown = cum_rets.cummax() - cum_rets
dar_hist = drawdown.quantile(beta)
cdar_hist = drawdown[drawdown > dar_hist].mean()
np.testing.assert_almost_equal(cdar_hist, cdar, decimal=3)
def test_es_return_sample():
cd = setup_efficient_cdar()
w = cd.efficient_return(0.2)
assert isinstance(w, dict)
assert set(w.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 1)
assert all([i >= -1e-5 for i in w.values()])
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.2, 0.063709),
rtol=1e-4,
atol=1e-4,
)
# Cover verbose param case
np.testing.assert_equal(
cd.portfolio_performance(verbose=True), cd.portfolio_performance()
)
def test_cdar_example_weekly():
beta = 0.90
df = get_data()
df = df.resample("W").first()
mu = expected_returns.mean_historical_return(df, frequency=52)
historical_rets = expected_returns.returns_from_prices(df).dropna()
cd = EfficientCDaR(mu, historical_rets, beta=beta)
cd.efficient_return(0.21)
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.21, 0.045085),
rtol=1e-4,
atol=1e-4,
)
cdar = cd.portfolio_performance()[1]
portfolio_rets = historical_rets @ cd.weights
cum_rets = portfolio_rets.cumsum(0)
drawdown = cum_rets.cummax() - cum_rets
dar_hist = drawdown.quantile(beta)
cdar_hist = drawdown[drawdown > dar_hist].mean()
np.testing.assert_almost_equal(cdar_hist, cdar, decimal=3)
def test_cdar_example_monthly():
beta = 0.90
df = get_data()
df = df.resample("M").first()
mu = expected_returns.mean_historical_return(df, frequency=12)
historical_rets = expected_returns.returns_from_prices(df).dropna()
cd = EfficientCDaR(mu, historical_rets, beta=beta)
cd.efficient_return(0.23)
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.23, 0.035683),
rtol=1e-4,
atol=1e-4,
)
cdar = cd.portfolio_performance()[1]
portfolio_rets = historical_rets @ cd.weights
cum_rets = portfolio_rets.cumsum(0)
drawdown = cum_rets.cummax() - cum_rets
dar_hist = drawdown.quantile(beta)
cdar_hist = drawdown[drawdown > dar_hist].mean()
np.testing.assert_almost_equal(cdar_hist, cdar, decimal=3)
def test_cdar_beta():
# cdar should decrease (i.e higher loss) as beta increases
cd = setup_efficient_cdar()
cd._beta = 0.5
cd.min_cdar()
cdar = cd.portfolio_performance()[1]
for beta in np.arange(0.55, 1, 0.05):
cd = setup_efficient_cdar()
cd._beta = beta
cd.min_cdar()
cdar_test = cd.portfolio_performance()[1]
assert cdar_test >= cdar
cdar = cdar_test
def test_cdar_example_short():
cd = setup_efficient_cdar(weight_bounds=(-1, 1))
w = cd.efficient_return(0.2, market_neutral=True)
assert isinstance(w, dict)
assert set(w.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 0)
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.2, 0.022799),
rtol=1e-4,
atol=1e-4,
)
def test_min_cdar_extra_constraints():
cd = setup_efficient_cdar()
w = cd.min_cdar()
assert w["GOOG"] < 0.02 and w["MA"] > 0.02
cd = setup_efficient_cdar()
cd.add_constraint(lambda x: x[0] >= 0.03)
cd.add_constraint(lambda x: x[16] <= 0.03)
w = cd.min_cdar()
assert w["GOOG"] >= 0.025 and w["MA"] <= 0.035
@pytest.mark.skipif(
not _check_soft_dependencies(["ecos"], severity="none"),
reason="skip test if ecos is not installed in environment",
)
def test_min_cdar_different_solver():
cd = setup_efficient_cdar(solver="ECOS")
w = cd.min_cdar()
assert isinstance(w, dict)
assert set(w.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 1)
assert all([i >= -1e-5 for i in w.values()])
test_performance = (0.14798, 0.056433)
np.testing.assert_allclose(
cd.portfolio_performance(), test_performance, rtol=1e-2, atol=1e-2
)
def test_min_cdar_tx_costs():
# Baseline
cd = setup_efficient_cdar()
cd.min_cdar()
w1 = cd.weights
# Pretend we were initally equal weight
cd = setup_efficient_cdar()
prev_w = np.array([1 / cd.n_assets] * cd.n_assets)
cd.add_objective(objective_functions.transaction_cost, w_prev=prev_w)
cd.min_cdar()
w2 = cd.weights
# TX cost should pull closer to prev portfolio
assert np.abs(prev_w - w2).sum() < np.abs(prev_w - w1).sum()
@pytest.mark.skipif(
not _check_soft_dependencies(["ecos"], severity="none"),
reason="skip test if ecos is not installed in environment",
)
def test_min_cdar_L2_reg():
cd = setup_efficient_cdar(solver="ECOS")
cd.add_objective(objective_functions.L2_reg, gamma=0.1)
weights = cd.min_cdar()
assert isinstance(weights, dict)
assert set(weights.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 1)
assert all([i >= -1e-5 for i in weights.values()])
cd2 = setup_efficient_cdar()
cd2.min_cdar()
# L2_reg should pull close to equal weight
equal_weight = np.full((cd.n_assets,), 1 / cd.n_assets)
assert (
np.abs(equal_weight - cd.weights).sum()
< np.abs(equal_weight - cd2.weights).sum()
)
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.135105, 0.060849),
rtol=1e-4,
atol=1e-4,
)
def test_min_cdar_sector_constraints():
sector_mapper = {
"GOOG": "tech",
"AAPL": "tech",
"FB": "tech",
"AMZN": "tech",
"BABA": "tech",
"GE": "utility",
"AMD": "tech",
"WMT": "retail",
"BAC": "fig",
"GM": "auto",
"T": "auto",
"UAA": "airline",
"SHLD": "retail",
"XOM": "energy",
"RRC": "energy",
"BBY": "retail",
"MA": "fig",
"PFE": "pharma",
"JPM": "fig",
"SBUX": "retail",
}
sector_upper = {
"tech": 0.2,
"utility": 0.1,
"retail": 0.2,
"fig": 0.4,
"airline": 0.05,
"energy": 0.2,
}
sector_lower = {"utility": 0.01, "fig": 0.02, "airline": 0.01}
cd = setup_efficient_cdar()
cd.add_sector_constraints(sector_mapper, sector_lower, sector_upper)
weights = cd.min_cdar()
for sector in list(set().union(sector_upper, sector_lower)):
sector_sum = 0
for t, v in weights.items():
if sector_mapper[t] == sector:
sector_sum += v
assert sector_sum <= sector_upper.get(sector, 1) + 1e-5
assert sector_sum >= sector_lower.get(sector, 0) - 1e-5
def test_efficient_risk():
cd = setup_efficient_cdar()
w = cd.efficient_risk(0.08)
assert isinstance(w, dict)
assert set(w.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 1)
assert all([i >= -1e-5 for i in w.values()])
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.261922, 0.08),
rtol=1e-4,
atol=1e-4,
)
def test_efficient_risk_low_risk():
cd = setup_efficient_cdar()
cd.min_cdar()
min_value = cd.portfolio_performance()[1]
# Should fail below
with pytest.raises(OptimizationError):
cd = setup_efficient_cdar()
cd.efficient_risk(min_value - 0.01)
cd = setup_efficient_cdar()
cd.efficient_risk(min_value + 0.01)
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.212772, min_value + 0.01),
rtol=1e-4,
atol=1e-4,
)
def test_efficient_risk_market_neutral():
cd = setup_efficient_cdar(weight_bounds=(-1, 1))
w = cd.efficient_risk(0.025, market_neutral=True)
assert isinstance(w, dict)
assert set(w.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 0)
assert (cd.weights < 1).all() and (cd.weights > -1).all()
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.219306, 0.025),
rtol=1e-4,
atol=1e-4,
)
def test_efficient_risk_L2_reg():
cd = setup_efficient_cdar()
cd.add_objective(objective_functions.L2_reg, gamma=1)
weights = cd.efficient_risk(0.18)
assert isinstance(weights, dict)
assert set(weights.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 1)
np.testing.assert_array_less(np.zeros(len(weights)), cd.weights + 1e-4)
# L2 regularization changes the optimization landscape
# Use relaxed tolerance to account for solver variations
perf = cd.portfolio_performance()
np.testing.assert_allclose(perf[0], 0.289, rtol=1e-2, atol=1e-2)
np.testing.assert_allclose(perf[1], 0.18, rtol=1e-2, atol=1e-2)
cd2 = setup_efficient_cdar()
cd2.efficient_risk(0.18)
# L2_reg should pull close to equal weight
equal_weight = np.full((cd.n_assets,), 1 / cd.n_assets)
assert (
np.abs(equal_weight - cd.weights).sum()
< np.abs(equal_weight - cd2.weights).sum()
)
def test_efficient_return():
cd = setup_efficient_cdar()
w = cd.efficient_return(0.25)
assert isinstance(w, dict)
assert set(w.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 1)
assert all([i >= -1e-5 for i in w.values()])
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.25, 0.076193),
rtol=1e-4,
atol=1e-4,
)
def test_efficient_return_short():
cd = setup_efficient_cdar(weight_bounds=(-3.0, 3.0))
w = cd.efficient_return(0.28)
assert isinstance(w, dict)
assert set(w.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 1)
np.testing.assert_allclose(
cd.portfolio_performance(),
(0.28, 0.045999),
rtol=1e-4,
atol=1e-4,
)
cdar = cd.portfolio_performance()[1]
ef_long_only = cd = setup_efficient_cdar(weight_bounds=(0.0, 1.0))
ef_long_only.efficient_return(0.26)
long_only_cdar = ef_long_only.portfolio_performance()[1]
assert long_only_cdar > cdar
def test_efficient_return_L2_reg():
cd = setup_efficient_cdar()
cd.add_objective(objective_functions.L2_reg, gamma=1)
w = cd.efficient_return(0.25)
assert isinstance(w, dict)
assert set(w.keys()) == set(cd.tickers)
np.testing.assert_almost_equal(cd.weights.sum(), 1)
assert all([i >= -1e-5 for i in w.values()])
# L2 regularization changes the optimization landscape
# Use relaxed tolerance to account for solver variations
perf = cd.portfolio_performance()
np.testing.assert_allclose(perf[0], 0.25, rtol=1e-2, atol=1e-2)
np.testing.assert_allclose(perf[1], 0.101, rtol=1e-2, atol=1e-2)
def test_cdar_errors():
df = get_data()
mu = expected_returns.mean_historical_return(df)
historical_rets = expected_returns.returns_from_prices(df)
with pytest.warns(UserWarning):
EfficientCDaR(mu, historical_rets)
historical_rets = historical_rets.dropna(axis=0, how="any")
assert EfficientCDaR(mu, historical_rets)
cd = setup_efficient_cdar()
with pytest.raises(NotImplementedError):
cd.min_volatility()
with pytest.raises(NotImplementedError):
cd.max_sharpe()
with pytest.raises(NotImplementedError):
cd.max_quadratic_utility()
with pytest.raises(ValueError):
# Beta must be between 0 and 1
cd = EfficientCDaR(mu, historical_rets, 1)
with pytest.raises(OptimizationError):
# Must be <= max expected return
cd = EfficientCDaR(mu, historical_rets)
cd.efficient_return(target_return=np.abs(mu).max() + 0.01)
with pytest.raises(TypeError):
# list not supported.
EfficientCDaR(mu, historical_rets.to_numpy().tolist())
historical_rets = historical_rets.iloc[:, :-1]
with pytest.raises(ValueError):
EfficientCDaR(mu, historical_rets)
def test_parametrization():
cd = setup_efficient_cdar()
cd.efficient_risk(0.08)
cd.efficient_risk(0.07)
cd = setup_efficient_cdar()
cd.efficient_return(0.08)
cd.efficient_return(0.07)