我试图建立一个优化问题来确定我的投资组合中资产的权重。我有点困惑哦我应该如何提及不等式约束
我正在尝试最小化两个约束的锐化率: 1.权重总和必须等于一(平等约束) 2.重量值不能超过1
import scipy.optimize as spo
def sharpe(dr, riskf, wts):
portfolio=np.ones(dr.shape[0])
dr[0:]=wts*dr[0:]
portfolio[0:]=dr.sum(axis=1)
net=portfolio.copy()
net[0:]=portfolio[0:]-riskf[0:]
sharpe_ratio=-np.mean(net)/np.std(net)
return sharpe_ratio
def wts_con(wts):
return wts[0]+wts[1]+wts[2]+wts[3]-1
def range_con():
def sharpe_optimize(dr, riskf, sharpe):
wts_guess=np.array([0.25,0.25,0.25,0.25])
con=[{"type"="eq", "fun"=wts_con}
{"type"="ineq", "fun"=range_con}]
result=spo.minimize(sharpe, wts_guess, args={dr,riskf,}, method="SLSQP", constraints=con, options={"disp":True})
return result
我想知道如何编写range_con()函数,该函数指定我的个人权重不能低于0或高于1