对于这个最大化问题,我怎么了?

时间:2019-02-15 08:46:06

标签: constraints linear-algebra finance portfolio maximization

我收到ValueError:形状(6,)和(1,6)未对齐:6(dim 0)!= 1(dim 0)。当我在第23行执行最大化问题时,就会发生该错误。您知道我可以在此代码中进行哪些更改以使工作正常吗?

我试图多次更改这些矢量和矩阵的形状,但我不断收到此错误或错误“无法调用矩阵对象”。

ETFs_mu是6乘1的向量

cov_matrix是6 x 6矩阵

# Case 2: No borrowing

# The objective function maximizes the expected return of the portfolio 
def objective_func(w,ETFs_mu,risk_free):
    w = np.empty((6,1))
    return -(np.dot(np.transpose(w),ETFs_mu) + (1-np.sum(w))*risk_free)

# Define constraint: sigma = .15
def constraint1(w):
    pf_sigma = np.sqrt(np.dot(np.transpose(w), np.matmul(cov_matrix,w)))
    return .15 - pf_sigma

# Define constraint: no access to risk-free
def constraint2(w):
    return 1.0-np.sum(w)    # Define constraint: all the weights + risk free sum up to one

# Load constraints into dictionary form
cons1 = {'type':'ineq','fun':constraint1}
cons2 = {'type':'eq','fun':constraint2}

cons = (cons1,cons2)

res3 = opt.minimize(objective_func, (.1,.1,.1,.1,.1,.1), args=(ETFs_mu,risk_free), constraints=cons)

res3

这个问题的意思是找到由6个资产组成的投资组合的权重。我们不能使用无风险利率,因此其权重应为0。投资组合的标准偏差小于或等于15%。在给定约束的情况下,输出应为所需权重的向量,以最大化目标函数(预期收益组合)。

0 个答案:

没有答案