当输入参数小时,scipy.optimize返回初始猜测

时间:2019-03-22 11:08:27

标签: python optimization scipy

我需要使用scipy优化来找到最小方差投资组合。我有以下协方差矩阵:

[[0.00076593 0.00087803 0.00082423 0.00094616 0.00090782]
  [0.00087803 0.0015638  0.00086395 0.00097013 0.00107642]
  [0.00082423 0.00086395 0.00092068 0.00104474 0.00099357]
  [0.00094616 0.00097013 0.00104474 0.00133651 0.00119894]
  [0.00090782 0.00107642 0.00099357 0.00119894 0.00132278]]]

对投资组合权重的最初猜测是:

[0.2,0.2,0.2,0.2,0.2]

运行以下代码,其中V是上方的协方差矩阵:

import pandas as pd
import numpy as np
from scipy.optimize import minimize
from tqdm import tqdm

#Minimum Variance

cov_rol = df_smartbeta.rolling(window=36).cov()
cov_rol = cov_rol.values.reshape(665,5,5)
V = cov_rol

def objective(w, V): #portfolio variance, 1x1 array
    w1= np.matrix(w)
    wt = np.matrix(w1)
    wt = np.transpose(wt)
    return (w1*V)*wt

def constraint1(x): #constraint 1: sum w0 = 1
    return np.sum(x)-1

w0 = np.array([0.2,0.2,0.2,0.2,0.2]) #initial guess
cons = ({"type":"eq", "fun": constraint1}) #merge constraints
b = (0,None) #define bounds. Lower bound: 0. Upper bound: None
bnds = (b,b,b,b,b)

count = 0
w_min = []
for i in tqdm(range(len(cov_rol))):
    res =minimize(objective, w0, args=(V[count]), method="SLSQP",bounds=bnds, constraints=cons)
    w_min.append(res.x)
    count += 1

w_min_dataframe = pd.DataFrame(w_min)

该代码仅返回初始猜测,尽管它应该产生

[1.0,0,0,0,0]

如果我乘以100重新缩放协方差矩阵,则该代码有效。有谁知道这是为什么发生,以及如何解决问题而无需重新调整规模?我之前也看到过类似的问题,但还没有找到任何可行的解决方案...

0 个答案:

没有答案