tushare金融数据api分享

news/2024/11/17 12:43:37/

本文选择股票博汇纸业(600966.SH)、万泽股份(000534.SZ)、武汉控股(600168.SH)、华新水泥(600801.SH)、海澜之家(600398.SZ)、华银电力(600744.SH)、民生银行(600016.SH)、申通地铁(600834.SZ)作为样本股,括号内的数字为上市公司股票代码。本文研究区间为2000年至2019年,共计240个月的月度收盘价数据和无风险利率数据,并将该8支股票的日收益数据作为对CAPM模型做单资产时间序列检验的研究样本,该8支股票的月收益数据作为对CAPM模型做多资产时间序列检验的研究样本。考虑到样本股包括上海和深圳交易所交易的股票,本文选择沪深300指数作为市场指数。股票收盘价数据来源是Tushare,市场指数和无风险收益率数据来自锐思数据库。需要加以说明的是,本文选取的股票代码不都是以“34”结尾,这是因为以“34”结尾的股票较少,所以本文增加了股票武汉控股、海澜之家、华银电力、民生银行等4支股票代码不以“34”结尾的股票。

在对中国A股股票的惯性效应或反转效应的实证研究中,本文选取2000年1月1日以前(包括2000年1月1日)上市的深圳证券交易所和上海证券交易所的A股股票的月度收盘价数据作为研究样本,研究区间为2000年至2019年,数据来自锐思数据库,简称RESET。

tushre的官网链接:Tushare数据

代码如下:

#CAPM单资产估计及检验\# 单资产的时间序列检验import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport statsmodels.api as smimport tushare as tsplt.rcParams.update({"font.family": "STIXGeneral",​           "font.size": 20,​           "mathtext.fontset": "cm"})ts.set_token('5fedae5fd4d575f421e613edc98ce135fb851f26b35b4d838ba3f61e')pro = ts.pro_api('5fedae5fd4d575f421e613edc98ce135fb851f26b35b4d838ba3f61e')index_data = pd.read_csv('index_data2000_2019.csv', encoding='gb2312')#沪深300市场指数收益率index_data.dropna(inplace=True)index_data['return'] = np.log(index_data['close'].shift(periods=-1)) - np.log(index_data['close'])index_data.dropna(inplace=True)Date = index_data['Date']date = []for i in range(0,len(index_data)):date.append(str(Date[i]))date = pd.DataFrame(date,columns=['date'])index_data = pd.concat([index_data,date],axis=1)stock_data81 = ts.pro_bar(ts_code='600966.SH', adj=None, asset='E', freq='D', start_date='20000101', end_date='20191231')#600966.SH 博汇纸业stock_data81.columns = ['code', 'date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data81 = stock_data81.reindex(index=stock_data81.index[::-1])stock_data81.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data81.dropna(inplace=True)stock_data81['return'] = np.log(stock_data81['close'].shift(periods=-1)) - np.log(stock_data81['close'])stock_data81.dropna(inplace=True)ind1 = (stock_data81['return'] >= -0.1) & (stock_data81['return'] <= 0.1)stock_data81 = stock_data81[ind1]stock_data81.reset_index(drop=True, inplace=True) # DataFrame的index重新排列plt.figure(1)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data82 = ts.pro_bar(ts_code='000534.SZ', adj=None, asset='E', freq='D', start_date='20000101', end_date='20191231')#000534.SZ 万泽股份stock_data82.columns = ['code', 'date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data82 = stock_data82.reindex(index=stock_data82.index[::-1])stock_data82.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data82.dropna(inplace=True)stock_data82['return'] = np.log(stock_data82['close'].shift(periods=-1)) - np.log(stock_data82['close'])stock_data82.dropna(inplace=True)ind2 = (stock_data82['return'] >= -0.1) & (stock_data82['return'] <= 0.1)stock_data82 = stock_data82[ind2]stock_data82.reset_index(drop=True, inplace=True) # DataFrame的index重新排列plt.figure(2)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data83 = ts.pro_bar(ts_code='600168.SH', adj=None, asset='E', freq='D', start_date='20000101', end_date='20191231')#600168.SH 武汉控股stock_data83.columns = ['code', 'date', 'open', 'high', '6low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data83 = stock_data83.reindex(index=stock_data83.index[::-1])stock_data83.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data83.dropna(inplace=True)stock_data83['return'] = np.log(stock_data83['close'].shift(periods=-1)) - np.log(stock_data83['close'])stock_data83.dropna(inplace=True)ind3 = (stock_data83['return'] >= -0.1) & (stock_data83['return'] <= 0.1)stock_data83 = stock_data83[ind3]stock_data83.reset_index(drop=True, inplace=True) # DataFrame的index重新排列plt.figure(3)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data84 = ts.pro_bar(ts_code='600801.SH', adj=None, asset='E', freq='D', start_date='20000101', end_date='20191231')#600801.SH 华新水泥stock_data84.columns = ['code', 'date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data84 = stock_data84.reindex(index=stock_data84.index[::-1])stock_data84.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data84.dropna(inplace=True)stock_data84['return'] = np.log(stock_data84['close'].shift(periods=-1)) - np.log(stock_data84['close'])stock_data84.dropna(inplace=True)ind4 = (stock_data84['return'] >= -0.1) & (stock_data84['return'] <= 0.1)stock_data84 = stock_data84[ind4]stock_data84.reset_index(drop=True, inplace=True) # DataFrame的index重新排列plt.figure(4)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data85 = ts.pro_bar(ts_code='600398.SH', adj=None, asset='E', freq='D', start_date='20000101', end_date='20191231')#600398.SH 海澜之家stock_data85.columns = ['code', 'date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data85 = stock_data85.reindex(index=stock_data85.index[::-1])stock_data85.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data85.dropna(inplace=True)stock_data85['return'] = np.log(stock_data85['close'].shift(periods=-1)) - np.log(stock_data85['close'])stock_data85.dropna(inplace=True)ind5 = (stock_data85['return'] >= -0.1) & (stock_data85['return'] <= 0.1)stock_data85 = stock_data85[ind5]stock_data85.reset_index(drop=True, inplace=True) # DataFrame的index重新排列plt.figure(5)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data86 = ts.pro_bar(ts_code='600744.SH', adj=None, asset='E', freq='D', start_date='20000101', end_date='20191231')#600744.SH 华银电力stock_data86.columns = ['code', 'date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data86 = stock_data86.reindex(index=stock_data86.index[::-1])stock_data86.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data86.dropna(inplace=True)stock_data86['return'] = np.log(stock_data86['close'].shift(periods=-1)) - np.log(stock_data86['close'])stock_data86.dropna(inplace=True)ind6 = (stock_data86['return'] >= -0.1) & (stock_data86['return'] <= 0.1)stock_data86 = stock_data86[ind6]stock_data86.reset_index(drop=True, inplace=True) # DataFrame的index重新排列plt.figure(6)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data87 = ts.pro_bar(ts_code='600016.SH', adj=None, asset='E', freq='D', start_date='20000101', end_date='20191231')#600016.SH 民生银行stock_data87.columns = ['code', 'date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data87 = stock_data87.reindex(index=stock_data87.index[::-1])stock_data87.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data87.dropna(inplace=True)stock_data87['return'] = np.log(stock_data87['close'].shift(periods=-1)) - np.log(stock_data87['close'])stock_data87.dropna(inplace=True)ind7 = (stock_data87['return'] >= -0.1) & (stock_data87['return'] <= 0.1)stock_data87 = stock_data87[ind7]stock_data87.reset_index(drop=True, inplace=True) # DataFrame的index重新排列plt.figure(7)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data88 = ts.pro_bar(ts_code='600834.SH', adj=None, asset='E', freq='D', start_date='20000101', end_date='20191231')#600834.SZ 申通地铁stock_data88.columns = ['code', 'date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data88 = stock_data88.reindex(index=stock_data88.index[::-1])stock_data88.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data88.dropna(inplace=True)stock_data88['return'] = np.log(stock_data88['close'].shift(periods=-1)) - np.log(stock_data88['close'])stock_data88.dropna(inplace=True)ind8 = (stock_data88['return'] >= -0.1) & (stock_data88['return'] <= 0.1)stock_data88 = stock_data88[ind8]stock_data88.reset_index(drop=True, inplace=True) # DataFrame的index重新排列plt.figure(8)plt.plot(stock_data81['return'], 'k-')plt.show()DRfRet2000 = pd.read_csv('DRfRet2000.csv', encoding='gb2312')#无风险收益率DRfRet2001_2010 = pd.read_csv('DRfRet2001_2010.csv', encoding='gb2312')DRfRet2011_2015 = pd.read_csv('DRfRet2011_2015.csv', encoding='gb2312')DRfRet2016_2019 = pd.read_csv('DRfRet2016_2019.csv', encoding='gb2312')rfreturn2000_2010 = DRfRet2000.append(DRfRet2001_2010)rfreturn2000_2015 = rfreturn2000_2010.append(DRfRet2011_2015)rfreturn2000_2019 = rfreturn2000_2015.append(DRfRet2016_2019)rfreturn2000_2019.columns = ['Date', 'rfreturn']rfreturn2000_2019.reset_index(drop=True, inplace=True) # DataFrame的index重新排列rfreturn2000_2019.dropna(inplace=True)rfreturn2000_2019.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date_rf = rfreturn2000_2019['Date']date_rf = []for i in range(0,len(rfreturn2000_2019)):date_rf.append(str(Date_rf[i]))date_rf = pd.DataFrame(date_rf,columns=['date'])rfreturn_data = pd.concat([rfreturn2000_2019, date_rf],axis=1)\# stock81:将三组数据以日期为交集合并成一组数据merge_data1 = pd.merge(left=stock_data81[['date', 'return']], right=index_data[['date', 'return']],​           on='date', how='inner', suffixes=('_stk', '_ind'))merge_data81 = pd.merge(left=merge_data1[['date', 'return_stk', 'return_ind']], right=rfreturn_data[['date', 'rfreturn']],​            on = 'date', how = 'inner')print(len(merge_data81))print(merge_data81)stk_ret81 = merge_data81['return_stk'].valuesrf_ret81 = merge_data81['rfreturn'].valuesind_ret81 = merge_data81['return_ind'].valuesplt.figure(1)plt.plot(ind_ret81 - rf_ret81, stk_ret81 - rf_ret81, 'o', ms=5, mfc='w', lw=2)plt.xlabel(r'$r_m - r_f$', fontsize=20)plt.ylabel(r'$r_i - r_f$', fontsize=20)plt.show()\# 利用sm回归包,回归数据x81 = sm.add_constant(ind_ret81 - rf_ret81)print(x81)y81 = stk_ret81 - rf_ret81model81 = sm.OLS(y81, x81)results81 = model81.fit()print(results81.summary())\# stock82:将三组数据以日期为交集合并成一组数据merge_data2 = pd.merge(left=stock_data82[['date', 'return']], right=index_data[['date', 'return']],​           on='date', how='inner', suffixes=('_stk', '_ind'))merge_data82 = pd.merge(left=merge_data2[['date', 'return_stk', 'return_ind']], right=rfreturn_data[['date', 'rfreturn']],​            on = 'date', how = 'inner')print(len(merge_data82))print(merge_data82)stk_ret82 = merge_data82['return_stk'].valuesrf_ret82 = merge_data82['rfreturn'].valuesind_ret82 = merge_data82['return_ind'].valuesplt.figure(2)plt.plot(ind_ret82 - rf_ret82, stk_ret82 - rf_ret82, 'o', ms=5, mfc='w', lw=2, label=r'$Return$')plt.plot(ind_ret82 - rf_ret82, (ind_ret82 - rf_ret82)*1.0669+0.0002, 'r', lw=2, label=r'$OLS$')plt.xlabel(r'$r_{m} - r_{f}$', fontsize=20)plt.ylabel(r'$r_{i} - r_{f}$', fontsize=20)plt.legend(loc='upper left', fontsize=10)plt.savefig('Fig_Essay_22.jpg', dpi=600, bbox_inches='tight')plt.show()\# 利用sm回归包,回归数据x82 = sm.add_constant(ind_ret82 - rf_ret82)print(x82)y82 = stk_ret82 - rf_ret82model82 = sm.OLS(y82, x82)results82 = model82.fit()print(results82.summary())\# stock83:将三组数据以日期为交集合并成一组数据merge_data3 = pd.merge(left=stock_data83[['date', 'return']], right=index_data[['date', 'return']],​           on='date', how='inner', suffixes=('_stk', '_ind'))merge_data83 = pd.merge(left=merge_data3[['date', 'return_stk', 'return_ind']], right=rfreturn_data[['date', 'rfreturn']],​            on = 'date', how = 'inner')print(len(merge_data83))print(merge_data83)stk_ret83 = merge_data83['return_stk'].valuesrf_ret83 = merge_data83['rfreturn'].valuesind_ret83 = merge_data83['return_ind'].valuesplt.figure(3)plt.plot(ind_ret83 - rf_ret83, stk_ret83 - rf_ret83, 'o', ms=5, mfc='w', lw=2,label=r'$Return$')plt.plot(ind_ret83 - rf_ret83, (ind_ret83 - rf_ret83)*1.0291-4.391*(10**(-5)), 'r', lw=2,label=r'$OLS$')plt.xlabel(r'$r_{m} - r_{f}$', fontsize=20)plt.ylabel(r'$r_{i} - r_{f}$', fontsize=20)plt.legend(loc='upper left', fontsize=10)plt.savefig('Fig_Essay_23.jpg', dpi=600, bbox_inches='tight')plt.show()\# 利用sm回归包,回归数据x83 = sm.add_constant(ind_ret83 - rf_ret83)print(x83)y83 = stk_ret83 - rf_ret83model83 = sm.OLS(y83, x83)results83 = model83.fit()print(results83.summary())\# stock84:将三组数据以日期为交集合并成一组数据merge_data4 = pd.merge(left=stock_data84[['date', 'return']], right=index_data[['date', 'return']],​           on='date', how='inner', suffixes=('_stk', '_ind'))merge_data84 = pd.merge(left=merge_data4[['date', 'return_stk', 'return_ind']], right=rfreturn_data[['date', 'rfreturn']],​            on = 'date', how = 'inner')print(len(merge_data84))print(merge_data84)stk_ret84 = merge_data84['return_stk'].valuesrf_ret84 = merge_data84['rfreturn'].valuesind_ret84 = merge_data84['return_ind'].valuesplt.figure(4)plt.plot(ind_ret84 - rf_ret84, stk_ret84 - rf_ret84, 'o', ms=5, mfc='w', lw=2)plt.xlabel(r'$r_m - r_f$', fontsize=20)plt.ylabel(r'$r_i - r_f$', fontsize=20)plt.show()\# 利用sm回归包,回归数据x84 = sm.add_constant(ind_ret84 - rf_ret84)print(x84)y84 = stk_ret84 - rf_ret84model84 = sm.OLS(y84, x84)results84 = model84.fit()print(results84.summary())\# stock85:将三组数据以日期为交集合并成一组数据merge_data5 = pd.merge(left=stock_data85[['date', 'return']], right=index_data[['date', 'return']],​           on='date', how='inner', suffixes=('_stk', '_ind'))merge_data85 = pd.merge(left=merge_data5[['date', 'return_stk', 'return_ind']], right=rfreturn_data[['date', 'rfreturn']],​            on = 'date', how = 'inner')print(len(merge_data85))print(merge_data85)stk_ret85 = merge_data85['return_stk'].valuesrf_ret85 = merge_data85['rfreturn'].valuesind_ret85 = merge_data85['return_ind'].valuesplt.figure(5)plt.plot(ind_ret85 - rf_ret85, stk_ret85 - rf_ret85, 'o', ms=5, mfc='w', lw=2)plt.xlabel(r'$r_m - r_f$', fontsize=20)plt.ylabel(r'$r_i - r_f$', fontsize=20)plt.show()\# 利用sm回归包,回归数据x85 = sm.add_constant(ind_ret85 - rf_ret85)print(x85)y85 = stk_ret85 - rf_ret85model85 = sm.OLS(y85, x85)results85 = model85.fit()print(results85.summary())\# stock86:将三组数据以日期为交集合并成一组数据merge_data6 = pd.merge(left=stock_data86[['date', 'return']], right=index_data[['date', 'return']],​           on='date', how='inner', suffixes=('_stk', '_ind'))merge_data86 = pd.merge(left=merge_data6[['date', 'return_stk', 'return_ind']], right=rfreturn_data[['date', 'rfreturn']],​            on = 'date', how = 'inner')print(len(merge_data86))print(merge_data86)stk_ret86 = merge_data86['return_stk'].valuesrf_ret86 = merge_data86['rfreturn'].valuesind_ret86 = merge_data86['return_ind'].valuesplt.figure(6)plt.plot(ind_ret86 - rf_ret86, stk_ret86 - rf_ret86, 'o', ms=5, mfc='w', lw=2)plt.xlabel(r'$r_m - r_f$', fontsize=20)plt.ylabel(r'$r_i - r_f$', fontsize=20)plt.show()\# 利用sm回归包,回归数据x86 = sm.add_constant(ind_ret86 - rf_ret86)print(x86)y86 = stk_ret86 - rf_ret86model86 = sm.OLS(y86, x86)results86 = model86.fit()print(results86.summary())\# stock87:将三组数据以日期为交集合并成一组数据merge_data7 = pd.merge(left=stock_data87[['date', 'return']], right=index_data[['date', 'return']],​           on='date', how='inner', suffixes=('_stk', '_ind'))merge_data87 = pd.merge(left=merge_data7[['date', 'return_stk', 'return_ind']], right=rfreturn_data[['date', 'rfreturn']],​            on = 'date', how = 'inner')print(len(merge_data87))print(merge_data87)stk_ret87 = merge_data87['return_stk'].valuesrf_ret87 = merge_data87['rfreturn'].valuesind_ret87 = merge_data87['return_ind'].valuesplt.figure(7)plt.plot(ind_ret87 - rf_ret87, stk_ret87 - rf_ret87, 'o', ms=5, mfc='w', lw=2)plt.xlabel(r'$r_m - r_f$', fontsize=20)plt.ylabel(r'$r_i - r_f$', fontsize=20)plt.show()\# 利用sm回归包,回归数据x87 = sm.add_constant(ind_ret87 - rf_ret87)print(x87)y87 = stk_ret87 - rf_ret87model87 = sm.OLS(y87, x87)results87 = model87.fit()print(results87.summary())\# stock88:将三组数据以日期为交集合并成一组数据merge_data8 = pd.merge(left=stock_data88[['date', 'return']], right=index_data[['date', 'return']],​           on='date', how='inner', suffixes=('_stk', '_ind'))merge_data88 = pd.merge(left=merge_data8[['date', 'return_stk', 'return_ind']], right=rfreturn_data[['date', 'rfreturn']],​            on = 'date', how = 'inner')print(len(merge_data88))print(merge_data88)stk_ret88 = merge_data88['return_stk'].valuesrf_ret88 = merge_data88['rfreturn'].valuesind_ret88 = merge_data88['return_ind'].values\# 利用sm回归包,回归数据x88 = sm.add_constant(ind_ret88 - rf_ret88)print(x88)y88 = stk_ret88 - rf_ret88model88 = sm.OLS(y88, x88)results88 = model88.fit()print(results88.summary())plt.figure(8)plt.plot(ind_ret88 - rf_ret88, stk_ret88 - rf_ret88, 'o', ms=5, mfc='w', lw=2, label=r'$Return$')plt.plot(ind_ret88 - rf_ret88, (ind_ret88 - rf_ret88)*0.9481+1.202*(10**(-5)), 'r', lw=2, label=r'$OLS$')plt.xlabel(r'$r_m - r_f$', fontsize=20)plt.ylabel(r'$r_i - r_f$', fontsize=20)plt.legend(loc='upper left', fontsize=10)plt.savefig('Fig_Essay_28.jpg', dpi=600, bbox_inches='tight')plt.show()新建Essay2_multip.py文件,在文件中输入以下代码:\#CAPM多资产估计及检验\# 多资产的时间序列检验import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport statsmodels.api as smimport tushare as tsts.set_token('5fedae5fd4d575f421e613edc98ce135fb851f26b35b4d838ba3f61e')pro = ts.pro_api('5fedae5fd4d575f421e613edc98ce135fb851f26b35b4d838ba3f61e')index_data = pd.read_csv('index_data2000_2019_M.csv', encoding='gb2312')#沪深300市场指数收益率index_data.dropna(inplace=True)index_data.columns = ['Date', 'close']index_data['return'] = np.log(index_data['close'].shift(periods=-1)) - np.log(index_data['close'])index_data.dropna(inplace=True)Date = index_data['Date']date = []for i in range(0,len(index_data)):d0 = str(Date[i])date.append(d0[0:6])date = pd.DataFrame(date,columns=['date'])index_data = pd.concat([index_data,date],axis=1)stock_data81 = ts.pro_bar(ts_code='600966.SH', adj=None, asset='E', freq='M', start_date='20000101', end_date='20191231')#600966.SH 博汇纸业stock_data81.columns = ['code', 'Date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data81 = stock_data81.reindex(index=stock_data81.index[::-1])stock_data81.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data81.dropna(inplace=True)stock_data81['return'] = np.log(stock_data81['close'].shift(periods=-1)) - np.log(stock_data81['close'])stock_data81.dropna(inplace=True)ind1 = (stock_data81['return'] >= -0.1) & (stock_data81['return'] <= 0.1)stock_data81 = stock_data81[ind1]stock_data81.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date1 = stock_data81['Date']date1 = []for i in range(0,len(stock_data81)):d1 = str(Date1[i])date1.append(d1[0:6])date1 = pd.DataFrame(date1,columns=['date'])stock_data81 = pd.concat([stock_data81,date1],axis=1)plt.figure(1)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data82 = ts.pro_bar(ts_code='000534.SZ', adj=None, asset='E', freq='M', start_date='20000101', end_date='20191231')#000534.SZ 万泽股份stock_data82.columns = ['code', 'Date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data82 = stock_data82.reindex(index=stock_data82.index[::-1])stock_data82.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data82.dropna(inplace=True)stock_data82['return'] = np.log(stock_data82['close'].shift(periods=-1)) - np.log(stock_data82['close'])stock_data82.dropna(inplace=True)ind2 = (stock_data82['return'] >= -0.1) & (stock_data82['return'] <= 0.1)stock_data82 = stock_data82[ind2]stock_data82.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date2 = stock_data82['Date']date2 = []for i in range(0,len(stock_data82)):d2 = str(Date2[i])date2.append(d2[0:6])date2 = pd.DataFrame(date2,columns=['date'])stock_data82 = pd.concat([stock_data82,date2],axis=1)plt.figure(2)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data83 = ts.pro_bar(ts_code='600168.SH', adj=None, asset='E', freq='M', start_date='20000101', end_date='20191231')#600168.SH 武汉控股stock_data83.columns = ['code', 'Date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data83 = stock_data83.reindex(index=stock_data83.index[::-1])stock_data83.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data83.dropna(inplace=True)stock_data83['return'] = np.log(stock_data83['close'].shift(periods=-1)) - np.log(stock_data83['close'])stock_data83.dropna(inplace=True)ind3 = (stock_data83['return'] >= -0.1) & (stock_data83['return'] <= 0.1)stock_data83 = stock_data83[ind3]stock_data83.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date3 = stock_data83['Date']date3 = []for i in range(0,len(stock_data83)):d3 = str(Date3[i])date3.append(d3[0:6])date3 = pd.DataFrame(date3,columns=['date'])stock_data83 = pd.concat([stock_data83,date3],axis=1)plt.figure(3)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data84 = ts.pro_bar(ts_code='600801.SH', adj=None, asset='E', freq='M', start_date='20000101', end_date='20191231')#600801.SH 华新水泥stock_data84.columns = ['code', 'Date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data84 = stock_data84.reindex(index=stock_data84.index[::-1])stock_data84.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data84.dropna(inplace=True)stock_data84['return'] = np.log(stock_data84['close'].shift(periods=-1)) - np.log(stock_data84['close'])stock_data84.dropna(inplace=True)ind4 = (stock_data84['return'] >= -0.1) & (stock_data84['return'] <= 0.1)stock_data84 = stock_data84[ind4]stock_data84.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date4 = stock_data84['Date']date4 = []for i in range(0,len(stock_data84)):d4 = str(Date4[i])date4.append(d4[0:6])date4 = pd.DataFrame(date4,columns=['date'])stock_data84 = pd.concat([stock_data84,date4],axis=1)plt.figure(4)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data85 = ts.pro_bar(ts_code='600398.SH', adj=None, asset='E', freq='M', start_date='20000101', end_date='20191231')#'600398.SH' 海澜之家stock_data85.columns = ['code', 'Date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data85 = stock_data85.reindex(index=stock_data85.index[::-1])stock_data85.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data85.dropna(inplace=True)stock_data85['return'] = np.log(stock_data85['close'].shift(periods=-1)) - np.log(stock_data85['close'])stock_data85.dropna(inplace=True)ind5 = (stock_data85['return'] >= -0.1) & (stock_data85['return'] <= 0.1)stock_data85 = stock_data85[ind5]stock_data85.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date5 = stock_data85['Date']date5 = []for i in range(0,len(stock_data85)):d5 = str(Date5[i])date5.append(d5[0:6])date5 = pd.DataFrame(date5,columns=['date'])stock_data85 = pd.concat([stock_data85,date5],axis=1)plt.figure(5)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data86 = ts.pro_bar(ts_code='600744.SH', adj=None, asset='E', freq='M', start_date='20000101', end_date='20191231')#600744.SH 华银电力stock_data86.columns = ['code', 'Date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data86 = stock_data86.reindex(index=stock_data86.index[::-1])stock_data86.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data86.dropna(inplace=True)stock_data86['return'] = np.log(stock_data86['close'].shift(periods=-1)) - np.log(stock_data86['close'])stock_data86.dropna(inplace=True)ind6 = (stock_data86['return'] >= -0.1) & (stock_data86['return'] <= 0.1)stock_data86 = stock_data86[ind6]stock_data86.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date6 = stock_data86['Date']date6 = []for i in range(0,len(stock_data86)):d6 = str(Date6[i])date6.append(d6[0:6])date6 = pd.DataFrame(date6,columns=['date'])stock_data86 = pd.concat([stock_data86,date6],axis=1)plt.figure(6)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data87 = ts.pro_bar(ts_code='600016.SH', adj=None, asset='E', freq='M', start_date='20000101', end_date='20191231')#600016.SH 民生银行stock_data87.columns = ['code', 'Date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data87 = stock_data87.reindex(index=stock_data87.index[::-1])stock_data87.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data87.dropna(inplace=True)stock_data87['return'] = np.log(stock_data87['close'].shift(periods=-1)) - np.log(stock_data87['close'])stock_data87.dropna(inplace=True)ind7 = (stock_data87['return'] >= -0.1) & (stock_data87['return'] <= 0.1)stock_data87 = stock_data87[ind7]stock_data87.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date7 = stock_data87['Date']date7 = []for i in range(0,len(stock_data87)):d7 = str(Date7[i])date7.append(d7[0:6])date7 = pd.DataFrame(date7,columns=['date'])stock_data87 = pd.concat([stock_data87,date7],axis=1)plt.figure(7)plt.plot(stock_data81['return'], 'k-')plt.show()stock_data88 = ts.pro_bar(ts_code='600834.SH', adj=None, asset='E', freq='M', start_date='20000101', end_date='20191231')#600834.SH 申通地铁stock_data88.columns = ['code', 'Date', 'open', 'high', 'low', 'close', 'pre_close', 'change', 'pct_chg', 'volume', 'trade_amount']stock_data88 = stock_data88.reindex(index=stock_data88.index[::-1])stock_data88.reset_index(drop=True, inplace=True) # DataFrame的index重新排列stock_data88.dropna(inplace=True)stock_data88['return'] = np.log(stock_data88['close'].shift(periods=-1)) - np.log(stock_data88['close'])stock_data88.dropna(inplace=True)ind8 = (stock_data88['return'] >= -0.1) & (stock_data88['return'] <= 0.1)stock_data88 = stock_data88[ind8]stock_data88.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date8 = stock_data88['Date']date8 = []for i in range(0,len(stock_data88)):d8 = str(Date8[i])date8.append(d8[0:6])date8 = pd.DataFrame(date8,columns=['date'])stock_data88 = pd.concat([stock_data88,date8],axis=1)plt.figure(8)plt.plot(stock_data81['return'], 'k-')plt.show()rfreturn2000_2019 = pd.read_csv('DRfRet2000_2019_M.csv', encoding='gb2312')#无风险收益率rfreturn2000_2019.columns = ['Date', 'rfreturn']rfreturn2000_2019.dropna(inplace=True)rfreturn2000_2019.reset_index(drop=True, inplace=True) # DataFrame的index重新排列Date_rf = rfreturn2000_2019['Date']date_rf = []for i in range(0,len(rfreturn2000_2019)):drf = str(Date_rf[i])date_rf.append(drf[0:6])date_rf = pd.DataFrame(date_rf,columns=['date'])rfreturn_data = pd.concat([rfreturn2000_2019, date_rf],axis=1)print(stock_data81)print(stock_data82)print(stock_data83)print(stock_data84)print(stock_data85)print(stock_data86)print(stock_data87)print(stock_data88)data_matrix = pd.merge(left=index_data[['date', 'return']], right=stock_data81[['date', 'return']],​           on='date', how='inner', sort='True')data_matrix = pd.merge(left=data_matrix, right=stock_data82[['date', 'return']],​           on='date', how='inner', sort='True')data_matrix = pd.merge(left=data_matrix, right=stock_data83[['date', 'return']],​           on='date', how='inner', sort='True')data_matrix = pd.merge(left=data_matrix, right=stock_data84[['date', 'return']],​           on='date', how='inner', sort='True')data_matrix = pd.merge(left=data_matrix, right=stock_data85[['date', 'return']],​           on='date', how='inner', sort='True')data_matrix = pd.merge(left=data_matrix, right=stock_data86[['date', 'return']],​           on='date', how='inner', sort='True')data_matrix = pd.merge(left=data_matrix, right=stock_data87[['date', 'return']],​           on='date', how='inner', sort='True')data_matrix = pd.merge(left=data_matrix, right=stock_data88[['date', 'return']],​           on='date', how='inner', sort='True')data_matrix = pd.merge(left=data_matrix, right=rfreturn_data[['date', 'rfreturn']], on='date', how='inner', sort='True')data_matrix.columns = ['date', 'ind_ret', 'stk1_ret', 'stk2_ret', 'stk3_ret', 'stk4_ret', 'stk5_ret', 'stk6_ret', 'stk7_ret', 'stk8_ret', 'rf_ret']print(data_matrix)data_matrix['ind_exc_ret'] = data_matrix['ind_ret'] - data_matrix['rf_ret']data_matrix['stk1_exc_ret'] = data_matrix['stk1_ret'] - data_matrix['rf_ret']data_matrix['stk2_exc_ret'] = data_matrix['stk2_ret'] - data_matrix['rf_ret']data_matrix['stk3_exc_ret'] = data_matrix['stk3_ret'] - data_matrix['rf_ret']data_matrix['stk4_exc_ret'] = data_matrix['stk4_ret'] - data_matrix['rf_ret']data_matrix['stk5_exc_ret'] = data_matrix['stk5_ret'] - data_matrix['rf_ret']data_matrix['stk6_exc_ret'] = data_matrix['stk6_ret'] - data_matrix['rf_ret']data_matrix['stk7_exc_ret'] = data_matrix['stk7_ret'] - data_matrix['rf_ret']data_matrix['stk8_exc_ret'] = data_matrix['stk8_ret'] - data_matrix['rf_ret']print(data_matrix)\# 无限制模型x = sm.add_constant(data_matrix['ind_exc_ret'].values)print(x)y = data_matrix[['stk1_exc_ret', 'stk2_exc_ret', 'stk3_exc_ret', 'stk4_exc_ret', 'stk5_exc_ret', 'stk6_exc_ret', 'stk7_exc_ret', 'stk8_exc_ret']].valuesprint(y)\# 无限制模型:beta estimate:beta_hat = (X'X)^(-1)(X'y)xTx = np.dot(np.transpose(x), x)xTy = np.dot(np.transpose(x), y)AB_hat = np.dot(np.linalg.inv(xTx), xTy)print(AB_hat)\# 限制模型xr = np.zeros((len(data_matrix['ind_exc_ret']), 1))# 转置,一列xr[:, 0] = data_matrix['ind_exc_ret'].valuesprint(xr)\# 限制模型:beta estimate:beta_hat = (X'X)^(-1)(X'y)xrTxr = np.dot(np.transpose(xr), xr)xrTy = np.dot(np.transpose(xr), y)ABr_hat = np.dot(np.linalg.inv(xrTxr), xrTy)print(ABr_hat)\# Wald Test:不需要考虑限制模型,是不限制模型(加1)T = len(data_matrix['ind_exc_ret'])N = 8mu_market = np.mean(data_matrix['ind_exc_ret'].values)sigma_market = np.sum((data_matrix['ind_exc_ret'].values - mu_market)**2)/TRESD = y - np.dot(x, AB_hat)COV = np.dot(np.transpose(RESD), RESD)/TinvCOV = np.linalg.inv(COV)ALPHA = AB_hat[0]# beta0,每支股票对应的系数为一列,取的各支股票beta0即alpha为一行trans_ALPHA = np.zeros((len(ALPHA), 1)) # 转置trans_ALPHA[:, 0] = ALPHAWchi2 = T*1/(1+mu_market**2/sigma_market)*np.dot(np.dot(ALPHA, invCOV), trans_ALPHA) # Wald检验统计量,Wchi2WF = (T-N-1)/N/(1+mu_market**2/sigma_market)*np.dot(np.dot(ALPHA, invCOV), trans_ALPHA)# Wald检验统计量,WFprint(Wchi2)print(WF)\# Wald Test:根据统计量计算p-valuefrom scipy.stats import chi2, fpvalue_Wchi2 = 1 - chi2.cdf(Wchi2[0], N)pvalue_Wf = 1 - f.cdf(WF[0], N, T-N-1)print(pvalue_Wchi2)print(pvalue_Wf)\# LR Test:需要用到限制模型和无限制模型RESDr = y - np.dot(xr, ABr_hat) # 估计限制模型(不加1)的参数,限制模型的RESDr 无限制模型的参数在Wald检验中已经估计好了COVr = np.dot(np.transpose(RESDr), RESDr)/T# 限制模型的COVrinvCOVr = np.linalg.inv(COVr)SLR = (np.log(np.linalg.det(COVr)) - np.log(np.linalg.det(COV)))*T #构造统计量SLR(限制模型-无限制模型)print(SLR)\# LR Test:根据统计量计算p-valuepvalue_SLR = 1 - chi2.cdf(SLR, N)print(pvalue_SLR)\# LM Test:需要用到限制模型和无限制模型\#print(RESDr)a = np.zeros((N, 1))a[:, 0] = np.sum(RESDr, axis=0)salpha = np.dot(invCOVr, a)# 对数似然函数对alpha求偏导print(salpha)b = np.dot(data_matrix['ind_exc_ret'].values, RESDr)sbeta = np.zeros((N, 1))sbeta[:, 0] = np.dot(invCOVr, b)# 对数似然函数对beta求偏导print(sbeta)score = np.concatenate((salpha, sbeta), axis=0)# 两个偏导连接成得分向量print(score)a = np.concatenate((invCOVr*T, invCOVr*np.sum(data_matrix['ind_exc_ret'].values)), axis=1)# 对上面两个偏导再对alpha求偏导print(a)b = np.concatenate((invCOVr*np.sum(data_matrix['ind_exc_ret'].values), invCOVr*np.sum(data_matrix['ind_exc_ret'].values**2)), axis=1)# 对上面两个偏导再对beta求偏导print(b)Minfo = np.concatenate((a, b), axis=0)# 偏导a,b连接成信息矩阵print(Minfo)SLM_chi2 = np.dot(np.dot(np.transpose(score), np.linalg.inv(Minfo)), score)# 构造统计量SLM_chi2print(SLM_chi2)\# LM Test:根据统计量计算p-valuepvalue_SLMchi2 = 1- chi2.cdf(SLM_chi2[0][0], N)print(pvalue_SLMchi2)print('{:>10s}, {:>10s}, {:>10s}, {:>10s}'.format('Wald Test1', 'Wald Test2', 'LR Test', 'LM Test'))print('{:10.5f}, {:10.5f}, {:10.5f}, {:10.5f}'.format(Wchi2[0], WF[0], SLR, SLM_chi2[0][0]))print('{:10.5f}, {:10.5f}, {:10.5f}, {:10.5f}'.format(pvalue_Wchi2, pvalue_Wf, pvalue_SLR, pvalue_SLMchi2))新建Essay2_StockA.py文件,在文件中输入以下代码:\# 中国A股市场惯性效应或反转效应的实证检验import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport statsmodels.api as smstock_dataA1 = pd.read_csv('Astock_1.csv',encoding='gb2312')stock_dataA2 = pd.read_csv('Astock_2.csv',encoding='gb2312')stock_dataA3 = pd.read_csv('Astock_3.csv',encoding='gb2312')stock_dataA4 = pd.read_csv('Astock_4.csv',encoding='gb2312')stockA = stock_dataA1.append(stock_dataA2)stockA = stockA.append(stock_dataA3, ignore_index=True)stockA = stockA.append(stock_dataA4, ignore_index=True)stockA.columns = ['code', 'date', 'close']stockA['date'] = pd.to_datetime(stockA['date'])stockA.dropna(inplace=True)\#print(len(stockA))\#print(stockA)stk_codes = np.unique(stockA['code'].values)\#print(len(stk_codes))N = len(stk_codes) # N = 745n = N/5stockA_ret = pd.DataFrame()'''for i in range(0,N):stock_data = stockA[stockA['code'] == stk_codes[i]]stock_data.sort_values(by='date', inplace=True)stock_data['1m_ret'] = np.log(stock_data['close'].shift(periods=-1)) - np.log(stock_data['close'])stock_data['3m_ret'] = np.log(stock_data['close'].shift(periods=-3)) - np.log(stock_data['close'])stock_data['6m_ret'] = np.log(stock_data['close'].shift(periods=-6)) - np.log(stock_data['close'])stock_data['12m_ret'] = np.log(stock_data['close'].shift(periods=-12)) - np.log(stock_data['close'])\#stock_data.dropna(inplace=True)\#ind = (stock_data['return'] >= -0.1) & (stock_data['return'] <= 0.1)\#stock_data = stock_data[ind]\#print(stock_data)stock_data.to_csv('Ret'+str(stk_codes[i])+'.csv')'''merge1 = pd.read_csv('Ret1.csv', encoding='gb2312')Dateall1 = merge1['date']ym1 = []for i in range(0, len(merge1)):d1 = str(Dateall1[i])ym1.append(d1[0:7])merge1['year_month'] = pd.Series(ym1)merge1[str(stk_codes[0])] = merge1['1m_ret']merge1 = merge1.drop(columns=['code', 'date', 'close','1m_ret', '3m_ret', '6m_ret', '12m_ret'])list_240code = []for i in range(1,N):data_skt = pd.DataFrame(pd.read_csv('Ret'+str(stk_codes[i])+'.csv', encoding='gb2312'))\#print(len(data_skt))if len(data_skt) == 240:​    list_240code.append(str(stk_codes[i]))​    data_skt.dropna(axis=0, how='all', thresh=None, subset=None, inplace=False)​    Dateall = data_skt['date']​    ym = []​    for i in range(0, len(data_skt)):​      d = str(Dateall[i])​      ym.append(d[0:7])​    data_skt['year_month'] = pd.Series(ym)​    \#print(merge1)​    \#print(data_skt)​    data_1mmerge = pd.merge(left=merge1, right=data_skt[['year_month', '1m_ret']], on='year_month', how='inner', sort=True)​    merge1 = data_1mmergeelse:​    continue\#print(merge1)\#print(list_240code) #414list_col = ['num','year_month', str(stk_codes[0])]for i in list_240code:list_col.append(i)merge1.columns = list_colmerge1 = merge1.drop(columns=['num'])\#print(merge1)del list_col[0]del list_col[0]\#print(list_col)Year_Month = merge1['year_month']Year_Month = Year_Month.drop(index=len(Year_Month)-1)Year_Month = list(Year_Month.values)\#print(Year_Month)compare = merge1.drop(columns=['year_month'])compare1 = compare.drop(index=len(compare)-1)compare4 = compare1.drop(index=0)compare5 = compare4.drop(index=1)# 3mcompare7 = compare5.drop(index=2)compare8 = compare7.drop(index=3)compare9 = compare8.drop(index=4)# 6mcompare10 = compare9.drop(index=5)compare11 = compare10.drop(index=6)compare13 = compare11.drop(index=7)compare14 = compare13.drop(index=8)compare15 = compare14.drop(index=9)compare16 = compare15.drop(index=10)# 12m\#---------------------------------------------------------------------------------------merge3 = pd.read_csv('Ret1.csv', encoding='gb2312')Dateall3 = merge3['date']ym3 = []for i in range(0, len(merge3)):d3 = str(Dateall3[i])ym3.append(d3[0:7])merge3['year_month'] = pd.Series(ym3)merge3[str(stk_codes[0])] = merge3['3m_ret']merge3 = merge3.drop(columns=['code', 'date', 'close','1m_ret', '3m_ret', '6m_ret', '12m_ret'])list_240code3 = []for i in range(1,N):data_skt3 = pd.DataFrame(pd.read_csv('Ret'+str(stk_codes[i])+'.csv', encoding='gb2312'))\#print(len(data_skt))if len(data_skt3) == 240:​    list_240code3.append(str(stk_codes[i]))​    data_skt3.dropna(axis=0, how='all', thresh=None, subset=None, inplace=False)​    Dateall3 = data_skt3['date']​    ym3 = []​    for i in range(0, len(data_skt3)):​      d3 = str(Dateall3[i])​      ym3.append(d3[0:7])​    data_skt3['year_month'] = pd.Series(ym3)​    \#print(merge1)​    \#print(data_skt)​    data_3mmerge = pd.merge(left=merge3, right=data_skt3[['year_month', '3m_ret']], on='year_month', how='inner', sort=True)​    merge3 = data_3mmergeelse:​    continue\#print(merge1)\#print(list_240code) #414list_col3 = ['num','year_month', str(stk_codes[0])]for i in list_240code3:list_col3.append(i)merge3.columns = list_col3merge3 = merge3.drop(columns=['num'])\#print(merge1)del list_col3[0]del list_col3[0]\#print(list_col)Year_Month3 = merge3['year_month']Year_Month3 = Year_Month3.drop(index=len(Year_Month3)-1)Year_Month3 = Year_Month3.drop(index=len(Year_Month3)-1)Year_Month3 = Year_Month3.drop(index=len(Year_Month3)-1)Year_Month3 = list(Year_Month3.values)\#print(Year_Month)compare3 = merge3.drop(columns=['year_month'])compare3 = compare3.drop(index=len(compare3)-1)compare3 = compare3.drop(index=len(compare3)-1)compare3 = compare3.drop(index=len(compare3)-1)\#---------------------------------------------------------------------------------------merge6 = pd.read_csv('Ret1.csv', encoding='gb2312')Dateall6 = merge6['date']ym6 = []for i in range(0, len(merge6)):d6 = str(Dateall6[i])ym6.append(d6[0:7])merge6['year_month'] = pd.Series(ym6)merge6[str(stk_codes[0])] = merge6['6m_ret']merge6 = merge6.drop(columns=['code', 'date', 'close','1m_ret', '3m_ret', '6m_ret', '12m_ret'])list_240code6 = []for i in range(1,N):data_skt6 = pd.DataFrame(pd.read_csv('Ret'+str(stk_codes[i])+'.csv', encoding='gb2312'))\#print(len(data_skt))if len(data_skt6) == 240:​    list_240code6.append(str(stk_codes[i]))​    data_skt6.dropna(axis=0, how='all', thresh=None, subset=None, inplace=False)​    Dateall6 = data_skt6['date']​    ym6 = []​    for i in range(0, len(data_skt6)):​      d6 = str(Dateall6[i])​      ym6.append(d6[0:7])​    data_skt6['year_month'] = pd.Series(ym6)​    \#print(merge1)​    \#print(data_skt)​    data_6mmerge = pd.merge(left=merge6, right=data_skt6[['year_month', '6m_ret']], on='year_month', how='inner', sort=True)​    merge6 = data_6mmergeelse:​    continue\#print(merge1)\#print(list_240code) #414list_col6 = ['num','year_month', str(stk_codes[0])]for i in list_240code6:list_col6.append(i)merge6.columns = list_col6merge6 = merge6.drop(columns=['num'])\#print(merge1)del list_col6[0]del list_col6[0]\#print(list_col)Year_Month6 = merge6['year_month']Year_Month6 = Year_Month6.drop(index=len(Year_Month6)-1)Year_Month6 = Year_Month6.drop(index=len(Year_Month6)-1)Year_Month6 = Year_Month6.drop(index=len(Year_Month6)-1)Year_Month6 = Year_Month6.drop(index=len(Year_Month6)-1)Year_Month6 = Year_Month6.drop(index=len(Year_Month6)-1)Year_Month6 = Year_Month6.drop(index=len(Year_Month6)-1)Year_Month6 = list(Year_Month6.values)\#print(Year_Month)compare6 = merge6.drop(columns=['year_month'])compare6 = compare6.drop(index=len(compare6)-1)compare6 = compare6.drop(index=len(compare6)-1)compare6 = compare6.drop(index=len(compare6)-1)compare6 = compare6.drop(index=len(compare6)-1)compare6 = compare6.drop(index=len(compare6)-1)compare6 = compare6.drop(index=len(compare6)-1)\#---------------------------------------------------------------------------------------merge12 = pd.read_csv('Ret1.csv', encoding='gb2312')Dateall12 = merge12['date']ym12 = []for i in range(0, len(merge12)):d12 = str(Dateall12[i])ym12.append(d12[0:7])merge12['year_month'] = pd.Series(ym12)merge12[str(stk_codes[0])] = merge12['12m_ret']merge12 = merge12.drop(columns=['code', 'date', 'close','1m_ret', '3m_ret', '6m_ret', '12m_ret'])list_240code12 = []for i in range(1,N):data_skt12 = pd.DataFrame(pd.read_csv('Ret'+str(stk_codes[i])+'.csv', encoding='gb2312'))\#print(len(data_skt))if len(data_skt12) == 240:​    list_240code12.append(str(stk_codes[i]))​    data_skt12.dropna(axis=0, how='all', thresh=None, subset=None, inplace=False)​    Dateall12 = data_skt12['date']​    ym12 = []​    for i in range(0, len(data_skt12)):​      d12 = str(Dateall12[i])​      ym12.append(d12[0:7])​    data_skt12['year_month'] = pd.Series(ym12)​    \#print(merge1)​    \#print(data_skt)​    data_12mmerge = pd.merge(left=merge12, right=data_skt12[['year_month', '12m_ret']], on='year_month', how='inner', sort=True)​    merge12 = data_12mmergeelse:​    continue\#print(merge1)\#print(list_240code) #414list_col12 = ['num','year_month', str(stk_codes[0])]for i in list_240code12:list_col12.append(i)merge12.columns = list_col12merge12 = merge12.drop(columns=['num'])\#print(merge1)del list_col12[0]del list_col12[0]\#print(list_col)Year_Month12 = merge12['year_month']Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = Year_Month12.drop(index=len(Year_Month12)-1)Year_Month12 = list(Year_Month12.values)\#print(Year_Month)compare12 = merge12.drop(columns=['year_month'])compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)compare12 = compare12.drop(index=len(compare12)-1)\#---------------------------------------------------------------------------------------def struct_panel(compare, p1, p2, p3, p4, p5,k):hang = compare.iloc[k]hang = hang.sort_values()#这里比较的是1m_ret,即历史收益率1个月的一行\#print(hang)hanglist = list(hang.index)\#print(hanglist)L = len(hanglist)m = int(np.around(L/5))\#print(int(m))p1 = hanglist[0:m]# 每个月都会有5个组合, 比如2000-01,每个月的五分位,最差p2 = hanglist[m:m*2]p3 = hanglist[m*2:m*3]p4 = hanglist[m*3:m*4]p5 = hanglist[m*4:L]# 最好\#print(panel1)\#2000-01构建的组合,追踪1个月,3个月,6个月,12个月return p1, p2, p3, p4, p5def compute_M1_Ret(compare, M_Ret, panel):everyMret = []if len(compare) - i - 1 >= 12:​    for j in range(1, 13):​      SUM = sum(list(compare.iloc[i + j].loc[panel].values/len(panel)))​      everyMret.append(SUM)​    panelafter1M = everyMret[0]​    panelafter3M = sum(everyMret[0:3])​    panelafter6M = sum(everyMret[0:6])​    panelafter12M = sum(everyMret)​    M_Ret[str(i)] = pd.Series([panelafter1M, panelafter3M, panelafter6M, panelafter12M])#2000-01构建的组合,追踪1个月,3个月,6个月,12个月的收益return M_RetQ1 = []Q2 = []Q3 = []Q4 = []Q5 = []M1_Ret1 = pd.DataFrame()M1_Ret2 = pd.DataFrame()M1_Ret3 = pd.DataFrame()M1_Ret4 = pd.DataFrame()M1_Ret5 = pd.DataFrame()for i in range(0,len(compare1)):#每一个月都循环一次, 比如2000-01(变)x = struct_panel(compare1, Q1, Q2, Q3, Q4, Q5, i)M1_RetP1 = compute_M1_Ret(compare1, M1_Ret1, x[0])M1_RetP2 = compute_M1_Ret(compare1, M1_Ret2, x[1])M1_RetP3 = compute_M1_Ret(compare1, M1_Ret3, x[2])M1_RetP4 = compute_M1_Ret(compare1, M1_Ret4, x[3])M1_RetP5 = compute_M1_Ret(compare1, M1_Ret5, x[4])Q13 = []Q23 = []Q33 = []Q43 = []Q53 = []M3_Ret1 = pd.DataFrame()M3_Ret2 = pd.DataFrame()M3_Ret3 = pd.DataFrame()M3_Ret4 = pd.DataFrame()M3_Ret5 = pd.DataFrame()for i in range(0,len(compare3)):#每一个月都循环一次, 比如2000-01(变)y = struct_panel(compare3, Q13, Q23, Q33, Q43, Q53, i)M3_RetP1 = compute_M1_Ret(compare5, M3_Ret1, y[0])M3_RetP2 = compute_M1_Ret(compare5, M3_Ret2, y[1])M3_RetP3 = compute_M1_Ret(compare5, M3_Ret3, y[2])M3_RetP4 = compute_M1_Ret(compare5, M3_Ret4, y[3])M3_RetP5 = compute_M1_Ret(compare5, M3_Ret5, y[4])Q16 = []Q26 = []Q36 = []Q46 = []Q56 = []M6_Ret1 = pd.DataFrame()M6_Ret2 = pd.DataFrame()M6_Ret3 = pd.DataFrame()M6_Ret4 = pd.DataFrame()M6_Ret5 = pd.DataFrame()for i in range(0,len(compare6)):#每一个月都循环一次, 比如2000-01(变)z = struct_panel(compare6, Q16, Q26, Q36, Q46, Q56, i)M6_RetP1 = compute_M1_Ret(compare9, M6_Ret1, z[0])M6_RetP2 = compute_M1_Ret(compare9, M6_Ret2, z[1])M6_RetP3 = compute_M1_Ret(compare9, M6_Ret3, z[2])M6_RetP4 = compute_M1_Ret(compare9, M6_Ret4, z[3])M6_RetP5 = compute_M1_Ret(compare9, M6_Ret5, z[4])Q112 = []Q212 = []Q312 = []Q412 = []Q512 = []M12_Ret1 = pd.DataFrame()M12_Ret2 = pd.DataFrame()M12_Ret3 = pd.DataFrame()M12_Ret4 = pd.DataFrame()M12_Ret5 = pd.DataFrame()for i in range(0,len(compare12)):#每一个月都循环一次, 比如2000-01(变)w = struct_panel(compare12, Q112, Q212, Q312, Q412, Q512, i)M12_RetP1 = compute_M1_Ret(compare16, M12_Ret1, w[0])M12_RetP2 = compute_M1_Ret(compare16, M12_Ret2, w[1])M12_RetP3 = compute_M1_Ret(compare16, M12_Ret3, w[2])M12_RetP4 = compute_M1_Ret(compare16, M12_Ret4, w[3])M12_RetP5 = compute_M1_Ret(compare16, M12_Ret5, w[4])del Year_Month[-12:]del Year_Month3[-12:]del Year_Month6[-12:]del Year_Month12[-12:]def obtain_average(Year, M1_RetPi):M1_RetPi.columns = Yearaver = []for i in range(0, 4):​    sumlist = list(M1_RetPi.iloc[i])​    \# print(sumlist)​    Aver = sum(sumlist) / len(Year)​    \# print(sum(sumlist))​    \# print(Aver)​    aver.append(Aver)\# print(aver)aver = pd.DataFrame(aver, columns=['Average'])M1_RetPi = pd.concat([M1_RetPi, aver], axis=1)M1_RetPi.index = ['after1M', 'after3M', 'after6M', 'after12M']return M1_RetPiM1_RetP1 = obtain_average(Year_Month, M1_RetP1)M1_RetP2 = obtain_average(Year_Month, M1_RetP2)M1_RetP3 = obtain_average(Year_Month, M1_RetP3)M1_RetP4 = obtain_average(Year_Month, M1_RetP4)M1_RetP5 = obtain_average(Year_Month, M1_RetP5)M3_RetP1 = obtain_average(Year_Month3, M3_RetP1)M3_RetP2 = obtain_average(Year_Month3, M3_RetP2)M3_RetP3 = obtain_average(Year_Month3, M3_RetP3)M3_RetP4 = obtain_average(Year_Month3, M3_RetP4)M3_RetP5 = obtain_average(Year_Month3, M3_RetP5)M6_RetP1 = obtain_average(Year_Month6, M6_RetP1)M6_RetP2 = obtain_average(Year_Month6, M6_RetP2)M6_RetP3 = obtain_average(Year_Month6, M6_RetP3)M6_RetP4 = obtain_average(Year_Month6, M6_RetP4)M6_RetP5 = obtain_average(Year_Month6, M6_RetP5)M12_RetP1 = obtain_average(Year_Month12, M12_RetP1)M12_RetP2 = obtain_average(Year_Month12, M12_RetP2)M12_RetP3 = obtain_average(Year_Month12, M12_RetP3)M12_RetP4 = obtain_average(Year_Month12, M12_RetP4)M12_RetP5 = obtain_average(Year_Month12, M12_RetP5)print(M1_RetP1)print(M1_RetP2)print(M1_RetP3)print(M1_RetP4)print(M1_RetP5)print("------------------------------------------")print(M3_RetP1)print(M3_RetP2)print(M3_RetP3)print(M3_RetP4)print(M3_RetP5)print("------------------------------------------")print(M6_RetP1)print(M6_RetP2)print(M6_RetP3)print(M6_RetP4)print(M6_RetP5)print("------------------------------------------")print(M12_RetP1)print(M12_RetP2)print(M12_RetP3)print(M12_RetP4)print(M12_RetP5)

http://www.ppmy.cn/news/471301.html

相关文章

推荐几个好的域名交易站点

一、过期域名查询 1、 http://www.eachnic.net 金米网 不用介绍了 2、 http://www.haovip.com 好VIP 刚刚优化过了 3、 http://www.admin5.com 站长资讯网 4、 http://www.DNSxxx.com 极限互联 域名交易、抢注、过期域名查询 5、 http://exp.y81.com 过期域名检测网…

MySQL ⽀持哪些存储引擎?默认使⽤哪个?MyISAM 和 InnoDB 引擎有什么区别,如何选择?

&#x1f4a7; M y S Q L ⽀持哪些存储引擎&#xff1f;默认使⽤哪个&#xff1f; M y I S A M 和 I n n o D B 引擎有什么区别&#xff0c;如何选择&#xff1f; \color{#FF1493}{MySQL ⽀持哪些存储引擎&#xff1f;默认使⽤哪个&#xff1f;MyISAM 和 InnoDB 引擎有什么区别…

分子谱线数据绘图——matab

用了分子谱线数据&#xff0c;matlab画图与python的matplotlib库函数是一样的。 %import data [x80,y80]textread(Adata_cut/A8_158228-158680.txt); [x81,y81]textread(/home/luoluo/Documents/python_scripts/job__XCLASS/job__C2H5OH_A/A8/xclass_spectrum_output.dat); [x…

【组合数学】生成函数 ( 正整数拆分 | 无序不重复拆分示例 )

文章目录 一、正整数拆分总结二、正整数拆分示例 参考博客 : 【组合数学】生成函数 简要介绍 ( 生成函数定义 | 牛顿二项式系数 | 常用的生成函数 | 与常数相关 | 与二项式系数相关 | 与多项式系数相关 )【组合数学】生成函数 ( 线性性质 | 乘积性质 )【组合数学】生成函数 ( …

u-boot for tiny210 ver3.1 (by liukun321咕唧咕唧)

这次更新&#xff0c;实现了自动识别Nand或MMC/SD启动&#xff08;环境变量统一存放于Nandflash中&#xff09;&#xff0c;统一SD及Nand启动模式的nandflash驱动均为8bit HW ECC校验&#xff0c;并调整部分源码文件的结构。 您可以从下面的链接获得源码&#xff0c;也可以下载…

ug侧铣头编程_数控机械加工侧铣头(角度头)编程入门知识

①什么是角度头。如下图&#xff1a; ②角度头的结构(来自网络)。 ③角度头怎样与机床主轴(spindle)连接。 ④角度头的应用。 ⑤角度头的方向(这种角度头有称之为侧铣头)。 从角度头与主轴的连接可以看出&#xff0c;当主轴定位后(如M19)&#xff0c;通常角度头的朝向只有一个&…

error: ‘uint8_t’,‘uint16_t’ ,‘uint32_t’ does not name a type

阅读前请看一下&#xff1a;我是一个热衷于记录的人&#xff0c;每次写博客会反复研读&#xff0c;尽量不断提升博客质量。文章设置为仅粉丝可见&#xff0c;是因为写博客确实花了不少精力。希望互相进步谢谢&#xff01;&#xff01; 文章目录 阅读前请看一下&#xff1a;我是…

y81.第四章 Prometheus大厂监控体系及实战 -- 监控扩展(十二)

10.2 监控redis https://github.com/oliver006/redis_exporter 10.2.1 部署redis root@k8s-master1:~/app-monitor-case/redis/yaml# cat redis-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata:name: redisnamespace: raymond-1 spec:replicas: 1selector…