minimize优化函数报输入变量维数错误如何解决


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)的输入变量分别是

检查了约束条件的输出,实在没检查出来是哪里出了问题,求各位大佬解答,而且约束条件可以输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值