2

我正在尝试实施一个投资组合优化,它使用约束来定义例如对国家/部门/行业等的最大敞口。我已经实现了下面的代码,其中我传入一个“非洲”向量以将股票映射到非洲国家,在我的然后,我将这些约束限制为总体权重不超过 40%。我设法实现它的唯一方法是在 africa = 1 的索引上使用 sum_weights。我也尝试使用 Parameter 函数但没有成功。希望有一种更优雅的方式来应用这些约束。任何建议表示赞赏。此外,如果有人知道一个显示使用跟踪误差约束、周转约束或波动性约束的示例,那么这些也是我仍在努力解决的问题。

import numpy as np
from cvxpy import *

np.random.seed(1)
n = 10 # number of assets

mu = np.abs(np.random.randn(n,1)) #mean
Sigma = np.random.randn(n,n)
Sigma = Sigma.T.dot(Sigma)


# Long only PFO Opt
w = Variable(n)

#africa = Parameter(10, sign='positive') 
#africa.value = [1,1,1,0,0,0,0,0,0,0]

africa = [0,0,0,0,0,0,0,1,1,1]

gamma = Parameter(sign='positive')
ret = mu.T*w
risk = quad_form(w,Sigma)

filters = [i for i in range(len(africa)) if africa[i] == 1]

constraints = [sum_entries(w) == 1, w >=0, w[1] > 0.50, w[0] == 0,   sum_entries(w[filters]) == 0.4]

#prob = Problem(Maximize(ret - gamma*risk), [sum_entries(w) == 1, w >=0])  

prob = Problem(Minimize(risk), constraints)

SAMPLE = 1000
risk_data = np.zeros(SAMPLE)
ret_data = np.zeros(SAMPLE)
gamma_vals = np.logspace(-2,3,num=SAMPLE)

for i in range(SAMPLE):
    gamma.value = gamma_vals[i]
    prob.solve()
    risk_data[i] = sqrt(risk).value
    ret_data[i] = ret.value

print(prob.status)
print(prob.value)

print('OPT WEIGHTS : ')
for i in range(n):
    print(round(w[i].value,3))
4

1 回答 1

-2

我想你可能想看看这些例子。开发商已将投资组合风险约束纳入如下:

import cvxpy as cp

w = cp.Variable(n)
gamma = cp.Parameter(nonneg=True)
ret = mu.T*w 
risk = cp.quad_form(w, Sigma)
Lmax = cp.Parameter()

# Portfolio optimization with a leverage limit and a bound on risk.
prob = cp.Problem(cp.Maximize(ret), 
              [cp.sum(w) == 1, 
               cp.norm(w, 1) <= Lmax,
               risk <= 2])

这是jupyter nbviewer的链接

于 2021-01-21T10:39:26.043 回答