其他分享
首页 > 其他分享> > pandas_设计样本(资产)组合

pandas_设计样本(资产)组合

作者:互联网

目录

需求

设计-选择标的

设计-检验有效性

设计-计算风险与夏普比率


需求

某个客户需要9%的收益率,对该客户进行组合优化配置,设计适合该客户的金融产品雏形

设计-选择标的

选择10个标的,计算这10个标的的年化收益率、年化协方差

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

df_004 = pd.read_csv('600004.csv',encoding='utf-8')
df_015 = pd.read_csv('600015.csv',encoding='utf-8')
df_023 = pd.read_csv('600023.csv',encoding='utf-8')
df_033 = pd.read_csv('600033.csv',encoding='utf-8')
df_343 = pd.read_csv('600343.csv',encoding='utf-8')
df_346 = pd.read_csv('600346.csv',encoding='utf-8')
df_183 = pd.read_csv('600183.csv',encoding='utf-8')
df_1398 = pd.read_csv('601398.csv',encoding='utf-8')
df_050 = pd.read_csv('600050.csv',encoding='utf-8')
df_000 = pd.read_csv('600000.csv',encoding='utf-8')

df_004['ret_004'] = df_004['closePrice'].pct_change()
df_004 = df_004.loc[:,['tradeDate','ret_004']]
df_015['ret_015'] = df_015['closePrice'].pct_change()
df_015 = df_015.loc[:,['tradeDate','ret_015']]
df_023['ret_023'] = df_023['closePrice'].pct_change()
df_023 = df_023.loc[:,['tradeDate','ret_023']]
df_033['ret_033'] = df_033['closePrice'].pct_change()
df_033 = df_033.loc[:,['tradeDate','ret_033']]
df_343['ret_343'] = df_343['closePrice'].pct_change()
df_343 = df_343.loc[:,['tradeDate','ret_343']]
df_346['ret_346'] = df_346['closePrice'].pct_change()
df_346 = df_346.loc[:,['tradeDate','ret_346']]
df_183['ret_183'] = df_183['closePrice'].pct_change()
df_183 = df_183.loc[:,['tradeDate','ret_183']]
df_1398['ret_1398'] = df_1398['closePrice'].pct_change()
df_1398 = df_1398.loc[:,['tradeDate','ret_1398']]
df_050['ret_050'] = df_050['closePrice'].pct_change()
df_050 = df_050.loc[:,['tradeDate','ret_050']]
df_000['ret_000'] = df_000['closePrice'].pct_change()
df_000 = df_000.loc[:,['tradeDate','ret_000']]

ten_df = pd.merge(df_004,df_015,on='tradeDate')
ten_df = pd.merge(ten_df,df_023,on='tradeDate')
ten_df = pd.merge(ten_df,df_033,on='tradeDate')
ten_df = pd.merge(ten_df,df_343,on='tradeDate')
ten_df = pd.merge(ten_df,df_346,on='tradeDate')
ten_df = pd.merge(ten_df,df_183,on='tradeDate')
ten_df = pd.merge(ten_df,df_1398,on='tradeDate')
ten_df = pd.merge(ten_df,df_050,on='tradeDate')
ten_df = pd.merge(ten_df,df_000,on='tradeDate')
ten_df.dropna(inplace=True)
ten_df['tradeDate'] = pd.to_datetime(ten_df['tradeDate'])
ten_df.set_index('tradeDate',inplace=True)

def annualize_rets(returns,n_periods):
    '''
    给定一系列的收益率和期数,算出年化收益率
    '''
    # 每一期的平均收益
    r_periodic_mean = ((1+returns).prod())**(1/returns.shape[0])-1
    return (1+r_periodic_mean)**n_periods-1

def annualize_std(returns,n_periods):
    '''
    给定一系列的收益率,算出年化的标准差
    '''
    return returns.std()*np.sqrt(n_periods)

def portfolio_return(weights,returns):
    '''
    计算投资组合收益率,weights和returns需要矩阵形式
    weights是组合资产的权重
    returns是组合中的资产年化收益率
    '''
    return weights.T @ returns

def portfolio_vol(weights,covmat):
    '''
    计算投资组合风险(波动率),weights和covmat需要矩阵形式
    covmat代表的是协方差矩阵
    '''
    return np.sqrt(weights.T @ covmat @ weights)

def get_gmvp(covmat):
    '''
    寻找全局最小方差点
    covmat 代表资产之间的协方差矩阵
    '''
    from scipy.optimize import minimize
    n = covmat.shape[0]
    init_guess = np.repeat(1/n,n)
    bounds = ((0.0,1.0),)*n #每个资产的权重在0~1之间
    weights_sum_to_1 = {'type':'eq','fun': lambda weights:np.sum(weights)-1}
    weights = minimize(portfolio_vol,init_guess,args=(covmat,),method='SLSQP',bounds=bounds,constraints=(weights_sum_to_1))
    return weights.x

def minimize_vol(target_return,annual_rets,covmat):
    '''
    最小方差边界函数
    target_return 为客户所要求的收益率水平
    annual_rets 代表组合中的资产的年化收益率
    covmat 代表资产之间的协方差矩阵
    '''
    from scipy.optimize import minimize
    n = annual_rets.shape[0]
    init_guess = np.repeat(1/n,n)
    bounds = ((0.0,1.0),)*n #每个资产的权重在0~1之间
    weights_sum_to_1 = {'type':'eq','fun': lambda weights:np.sum(weights)-1}
    return_is_target = {'type':'eq','args':(annual_rets,),'fun': lambda weights,annual_rets: portfolio_return(weights,annual_rets)-target_return}
    weights = minimize(portfolio_vol,init_guess,args=(covmat,),method='SLSQP',bounds=bounds,constraints=(weights_sum_to_1,return_is_target))
    return weights.x

# 这10个标的的年化收益与年化协方差
er_009 = annualize_rets(ten_df,252)
cov_009 = np.cov(ten_df,rowvar=False)*252
er_009

 

cov_009

 

设计-检验有效性

# 客户要去0.09的收益率,检查该收益率的组合是否>=gmvp的收益率,如果成立,那么该产品有效
gmvp_10samples_weights = get_gmvp(cov_009)
gmvp_10samples_ret = portfolio_return(gmvp_10samples_weights,er_009)
gmvp_10samples_ret
# out: 0.07938558348434649

该产品GMVP的收益率为: 0.07938558348434649

客户要去的0.09收益率 > GMVP的收益率0.07938558348434649, 该产品有效

设计-计算风险与夏普比率

# 设计出该产品的权重,并计算该基金的期望收益率和波动率
fund_009_weights = minimize_vol(0.09,er_009,cov_009)
fund_009_ret = portfolio_return(fund_009_weights,er_009)
fund_009_vol = portfolio_vol(fund_009_weights,cov_009)
fund_009_ret,fund_009_vol
# out: (0.09000000000051449, 0.18991361945461066)

# 夏普比率
rf = 0.0135
sharpe_009 = (fund_009_ret-rf)/fund_009_vol
sharpe_009
# 0.4028147123950633

标签:ten,组合,tradeDate,df,样本,ret,weights,pd,pandas
来源: https://blog.csdn.net/m0_37967652/article/details/123308201