-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmarkowitz.py
40 lines (34 loc) · 1.35 KB
/
markowitz.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
from cvxpy import *
import numpy as np
from posdef import nearestPD
def MarkowitzOpt(mean, variance, covariance, interest_rate, min_return):
n = mean.size + 1 # Number of assets (number of stocks + interest rate)
mu = mean.values # Mean returns of n assets
temp = np.full(n, interest_rate)
temp[:-1] = mu
mu = temp
counter = 0
Sigma = np.zeros((n,n)) # Covariance of n assets
for i in np.arange(n-1):
for j in np.arange(i, n-1):
if i==j:
Sigma[i,j] = variance[i]
else:
Sigma[i,j] = covariance[counter]
Sigma[j,i] = Sigma[i,j]
counter+=1
Sigma = nearestPD(Sigma) # Converting covariance to the nearest positive-definite matrix
# Ensuring feasability of inequality contraint
if mu.max() < min_return:
min_return = interest_rate
w = Variable(n) # Portfolio allocation vector
ret = mu.T* w
risk = quad_form(w, Sigma)
min_ret = Parameter(sign='positive')
min_ret.value = min_return
prob = Problem(Minimize(risk), # Restricting to long-only portfolio
[ret >= min_ret,
sum_entries(w) == 1,
w >= 0])
prob.solve()
return w.value