-
Notifications
You must be signed in to change notification settings - Fork 1
/
maximize_return.py
61 lines (45 loc) · 1.78 KB
/
maximize_return.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
import cvxpy as cvx
'''
expected_returns = [60, 50] ##
def maximize_return(expected_returns):
weights = cvx.Variable(len(expected_returns))
objective = cvx.Maximize(weights.T * expected_returns)
problem = cvx.Problem(objective, [cvx.sum(cvx.abs(weights))<=1])
# problem = cvx.Problem(objective, [cvx.sum_entries(weights)<=1])
problem.solve()
return weights.value.round(3)
solution = maximize_return(expected_returns)
'''
####################### ######################
import numpy as np
"""
expected_ret = np.array([0.001, 0.002, 0.003, 0.004, 0.005])
cov = np.array([[0.02, 0 , 0 ,0 ,0 ],
[0, 0.02 , 0. ,0 ,0],
[0, 0 , 0.02 ,0. ,0. ],
[0, 0 , 0. ,0.02 ,0.01 ],
[0, 0 , 0 ,0 ,0.02 ]])
x = np.random.randn(100)
y = np.random.randn(100)
cov = np.cov(x, y)
expected_ret = np.array([0.001, 0.002])
"""
def markwitz_portpolio(means, cov, risk_aversion=0.7):
weights = cvx.Variable(len(means))
expected_return = weights.T*means
expected_vol = cvx.quad_form(weights, cov)
utility = expected_return - risk_aversion*expected_vol
objective = cvx.Maximize(utility)
constraints = [
cvx.sum(weights, axis = 0) == 1,
weights >= 0,
]
problem = cvx.Problem(objective, constraints)
# problem = cvx.Problem(objective, [cvx.sum_entries(weights)<=1])
problem.solve()
return np.array(weights.value.flat).round(4)
#, expected_return.value, expected_vol.value
#weights, rets, var = markwitz_portpolio(expected_ret, cov, risk_aversion = 0.2)
def sharpe_ratio(w, mean, cov):
sr = (np.matmul(w.T,mean))/(np.sqrt((np.matmul(w.T,np.matmul(cov,w)))))
return sr