ValueError:形状5,5和1,不对齐:5dim 1!= 1dim 0

问题描述

我正在使用辛辣最小化函数获取投资组合证券的权重,但是当我使用点积计算波动率时,总是会出错。谁能帮我解决这个错误

    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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...