-
Notifications
You must be signed in to change notification settings - Fork 225
/
single_period_opt.py
107 lines (87 loc) · 3.63 KB
/
single_period_opt.py
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
# Copyright 2016 Enzo Busseti, Stephen Boyd, Steven Diamond, BlackRock Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""*Work in progress.*"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import cvxportfolio as cvx
from .common import (paper_hcost_model, paper_optimization_tcost_model,
paper_returns_forecast, paper_simulated_tcost_model)
from .data_risk_model import paper_market_data, paper_risk_model
# Start and end times of the back-test.
start_t = "2012-01-01"
end_t = "2016-12-31"
# Get market data.
market_data = paper_market_data()
# Define initial weights.
w_b = pd.Series(index=market_data.returns.columns, data=1)
w_b.USDOLLAR = 0.
w_b /= sum(w_b)
# Cost models.
simulated_tcost = paper_simulated_tcost_model()
simulated_hcost = paper_hcost_model()
# Market simulator.
simulator = cvx.MarketSimulator(market_data = paper_market_data(),
costs=[simulated_tcost, simulated_hcost])
# Optimization cost models.
optimization_tcost = paper_optimization_tcost_model()
optimization_hcost = paper_hcost_model()
return_estimate = paper_returns_forecast()
factor_exposures, factor_sigma, idyosincratic = paper_risk_model()
risk_model = cvx.FactorModelCovariance(
F=cvx.estimator.DataEstimator(
factor_exposures, use_last_available_time=True,
compile_parameter=True),
d=cvx.estimator.DataEstimator(
idyosincratic, use_last_available_time=True),
Sigma_F=cvx.estimator.DataEstimator(
factor_sigma, use_last_available_time=True,
ignore_shape_check=True))
results = {}
## SPO coarse search
policies = {}
gamma_risks_coarse = [.1, .3, 1, 3, 10, 30, 100, 300, 1000]
gamma_tcosts_coarse = [1, 2, 5, 10, 20]
for gamma_risk in gamma_risks_coarse:
for gamma_tcost in gamma_tcosts_coarse :
policies[(gamma_risk, gamma_tcost)] = \
cvx.SinglePeriodOpt(
cvx.ReturnsForecast(return_estimate) - gamma_risk*risk_model
- gamma_tcost*optimization_tcost
- optimization_hcost, [cvx.LeverageLimit(3)])
results.update(dict(zip(policies.keys(),
simulator.run_multiple_backtest(h=[1E8*w_b] * len(policies),
start_time=start_t, end_time=end_t,
policies=policies.values(), parallel=True))))
result_df_coarse = pd.DataFrame()
for k in results:
if k[0] in gamma_risks_coarse and k[1] in gamma_tcosts_coarse:
result_df_coarse.loc[k[0], k[1]] = results[k]
result_df = result_df_coarse.loc[
sorted(result_df_coarse.index), sorted(result_df_coarse.columns)]
# plt.figure(figsize=(8,5))
for gamma_tcost in result_df.columns:
x = [el.excess_returns.std()*100*np.sqrt(250) for el in result_df[gamma_tcost]]
y = [el.excess_returns.mean()*100*250 for el in result_df[gamma_tcost]]
plt.plot(np.array(x), np.array(y), '.-', label='$\gamma^\mathrm{trade} = %g$'%gamma_tcost)
plt.legend(loc='lower right')
plt.xlabel('Risk')
plt.ylabel('Return')
plt.xlim([0, 20])
plt.ylim([0, 30])
import matplotlib.ticker as mtick
ax = plt.gca()
ax.xaxis.set_major_formatter(mtick.FormatStrFormatter('%.0f%%'))
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.0f%%'))
# plt.savefig(plotdir+'spo_riskrewardfrontier.png')