def calculate_portfolio_var(w,V):
w = np.matrix(w)
return (w * V * w.T)[0,0]
def calculate_risk_contribution(w,V):
w = np.matrix(w)
sigma = np.sqrt(calculate_portfolio_var(w,V))
MRC = V * w.T
RC = np.multiply(MRC,w.T)/sigma
RC = np.array(RC).reshape(-1,1)
return RC
def cal_pro_r(w,R):
R = R.reshape(-1,1)
print('w',w,'\n','\n','rp',(-w * R)[0,0],'\n')
return -(w * R)[0,0]
# 约束条件
def total_weight_constraint1(w):
return np.sum(w)-1.0
def long_only_constraint(w):
return np.array(w).reshape(-1,1)
TB=0.1
def con2(V,x_t):
x_t=np.array(x_t).reshape(-1,1)
def total_weight_constraint2(w):
RC = calculate_risk_contribution(w,V)
sig_p = np.sqrt(calculate_portfolio_var(w,V))
print('cons2',RC/sig_p-x_t*(1-TB),'\n')
return RC/sig_p-x_t*(1-TB)
return total_weight_constraint2
def con3(V,x_t):
x_t=np.array(x_t).reshape(-1,1)
def total_weight_constraint3(w):
RC = calculate_risk_contribution(w,V)
sig_p = np.sqrt(calculate_portfolio_var(w,V))
print('cons2', -(RC/sig_p)+x_t*(1+TB) ,'\n')
return -(RC/sig_p)+x_t*(1+TB)
return total_weight_constraint3
def calcu_wr(V,x_t,w0,R):
# 约束条件
cons = ({'type': 'eq', 'fun': total_weight_constraint1},
{'type': 'ineq', 'fun': con2(V,x_t)},
{'type': 'ineq', 'fun': con3(V,x_t)},
{'type': 'ineq', 'fun': long_only_constraint},)
res= minimize(cal_pro_r, w0, args=R, method='SLSQP',constraints=cons, options={'disp': True})
return res
运行calcu_wr(V,x_t,w0,R)函数时候报错
函数calcu_wr(V,x_t,w0,R)的输入变量分别是
检查了约束条件的输出,实在没检查出来是哪里出了问题,求各位大佬解答,而且约束条件可以输出