Scipy最小化:ValueError:传递的值的形状为(10,10),索引表示(1,10)

时间:2019-02-28 03:35:35

标签: python python-3.x scipy minimization

我正在做均方差投资组合优化练习,但我坚持标题中提到的错误。我的优化目标是在具有附加跟踪误差约束的情况下,最大化10个资产组合的锐化率。

对于跟踪误差投资组合,基准是10只基金(每张中)的均等加权投资组合。跟踪误差定义为投资组合的收益与基准之间的差的标准偏差。在此项目中,我们要求年度跟踪误差不超过5%。参见约束公式图片:

enter image description here

这是我的代码:

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

# importing the data 
data_ff = pd.read_excel(r'path\Project_2.xlsx', 'Fama-French Factor', parse_dates=True, index_col="Date")
data_mf = pd.read_excel(r'path\Project_2.xlsx', 'Mutual Fund', parse_dates=True, index_col="Date")
data_sb = pd.read_excel(r'path\Project_2.xlsx', 'SmartBeta', parse_dates=True, index_col="Date")
data_hf = pd.read_excel(r'path\Project_2.xlsx', 'Hedge Fund Index', parse_dates=True, index_col="Date")
data_ff.rename(columns={'dateff':'Date'}, inplace = True)

# in-sample period
data_ff = data_ff[data_ff.index>=200101].dropna()
data_ff = data_ff[data_ff.index<=201212].dropna()
data_mf = data_mf[data_mf.index>=200101].dropna()
data_mf = data_mf[data_mf.index<=201212].dropna()
data_sb = data_sb[data_sb.index>=200101].dropna()
data_sb = data_sb[data_sb.index<=201212].dropna()
data_hf = data_hf[data_hf.index>=200101].dropna()
data_hf = data_hf[data_hf.index<=201212].dropna()

mf_cov_mat = data_mf.cov()*(12)
mf_mu = data_mf.mean()*12
sb_cov_mat = data_sb.cov()*(12)
sb_mu = data_sb.mean()*12
hf_cov_mat = data_hf.cov()*(12)
hf_mu = data_hf.mean()*12

weight_MVP_mf = pd.DataFrame(index = mf_mu.index)
weight_MVP_sb = pd.DataFrame(index = sb_mu.index)
weight_MVP_hf = pd.DataFrame(index = hf_mu.index)
results_MVP_mf = pd.DataFrame(np.zeros((3,4)), index = ('sharpe','return','volatility'),
                              columns = ('long-only','long-short','error-tracking','factor-neutral'))
results_MVP_sb = pd.DataFrame(np.zeros((3,4)), index = ('sharpe','return','volatility'),
                              columns = ('long-only','long-short','error-tracking','factor-neutral'))
results_MVP_hf = pd.DataFrame(np.zeros((3,4)), index = ('sharpe','return','volatility'),
                              columns = ('long-only','long-short','error-tracking','factor-neutral'))


for df in (data_mf, data_sb, data_hf):
    # return vector and var-cov matrix
    df_mu = pd.Series.to_frame(df.mean()*12)
    df_cov = df.cov()

    eq_weight = pd.DataFrame(np.array([0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]))

    # sharpe ratio
    def objective_df(x):
        risk_free = data_ff['rf'].iloc[-1]
        df_port_mu = np.dot(df_mu.T, x)
        df_port_stdev = np.sqrt(np.dot(np.dot(x.T,df_cov),x))*np.sqrt(12)
        return -df_port_mu/df_port_stdev


    # total weight = 100%
    def constraint1(x):
        return x.sum()-1

    # tracking error 
    def constraint2(x):
        y = x-eq_weight
        z = float(np.sqrt(np.dot(np.dot(y.T,df_cov),y))*np.sqrt(12)-0.05)
        return z

    x0 = eq_weight1
    b = (0,1)
    bnds = (b,b,b,b,b,b,b,b,b,b)
    cons1 = {'type': 'eq', 'fun': constraint1}
    cons2 = {'type': 'ineq', 'fun': constraint2}
    cons = [cons1,cons2]

    sol = minimize(objective_df, x0, method = 'SLSQP', bounds = bnds, constraints = cons)
    print (sol.fun)

我收到的错误消息似乎来自约束2。有人可以帮助我吗?

0 个答案:

没有答案