问题描述
我正在使用辛辣最小化函数来获取投资组合证券的权重,但是当我使用点积计算波动率时,总是会出错。谁能帮我解决这个错误?
prices = data.history(context.longs,'price',15,'1d')
prices = prices.sort_index(ascending=False)
prices.columns = map(lambda x: x.symbol,prices.columns)
daily_returns = np.log(prices/prices.shift(-1)).dropna()
mean_returns = daily_returns.mean()
cov_matrix = daily_returns.cov()
def get_ret_vol_sr(weights):
weights = np.array(weights)
ret = np.sum(mean_returns * weights)*252
**vol = np.sqrt(np.dot(np.transpose(weights),np.dot(cov_matrix*252,weights)))**
sr = ret/vol
return np.array([ret,vol,sr])
def neg_sharpe(weights):
return get_ret_vol_sr(weights)[2]*(-1)
def constraint_sum(weights):
return np.sum(weights)-1
def bounds_():
lst = []
for security in context.longs:
lst.append((0,1))
return lst
bounds = tuple(bounds_())
def init_guess_():
lst = []
for security in context.longs:
lst.append(0.25)
return lst
init_guess = init_guess_()
cons = ({'type':'eq','fun':constraint_sum})
opt_results = minimize(neg_sharpe,init_guess,method='SLSQP',bounds=bounds,constraints=cons)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)