[关闭]
@Channelchan 2017-12-02T18:14:58.000000Z 字数 7429 阅读 12179

多因子IC对比

Step1 用DataAPI获取数据(Bundle/Mongodb)

PB/LCAP/HBETA/ROE/InvestCashGrowRate/Momentum

  1. from fxdayu_data import DataAPI
  2. from datetime import datetime
  3. start = datetime(2017,1,1)
  4. end = datetime(2017,11,11)
  5. codes = DataAPI.info.codes('hs300')
  6. get_factor = DataAPI.factor(codes,('PB','LCAP','HBETA', 'ROE', 'InvestCashGrowRate', 'ROC20'),start,end)
  1. get_factor
<class 'pandas.core.panel.Panel'>
Dimensions: 300 (items) x 207 (major_axis) x 6 (minor_axis)
Items axis: 000001.XSHE to 603993.XSHG
Major_axis axis: 2017-01-03 15:00:00 to 2017-11-10 15:00:00
Minor_axis axis: HBETA to ROE
  1. prices = DataAPI.candle(codes,'D','close',start=start, end=end).minor_xs('close')

Step2 计算每月IC的均值与标准差

  1. import alphalens
  2. import pandas as pd
  3. factors = {'PB': get_factor.minor_xs('PB'),
  4. 'LCAP' : get_factor.minor_xs('LCAP'),
  5. 'HBETA' : get_factor.minor_xs('HBETA'),
  6. 'ROE' : get_factor.minor_xs('ROE'),
  7. 'InvestCashGrowRate' : get_factor.minor_xs('InvestCashGrowRate'),
  8. 'ROC20' :get_factor.minor_xs('ROC20')}
  9. def cal_monthly_ic(factor_df):
  10. factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factor_df.stack(), prices, quantiles=5)
  11. return alphalens.performance.mean_information_coefficient(factor_data, by_time='M')
  12. monthly_ic = {key: cal_monthly_ic(value) for key, value in factors.items()}
  13. print(monthly_ic)
{'PB':                   1         5         10
date                                    
2017-01-31 -0.051354 -0.101471 -0.148771
2017-02-28 -0.011522  0.030340  0.130004
2017-03-31 -0.009924 -0.054229 -0.088316
2017-04-30 -0.014042 -0.018074 -0.034717
2017-05-31 -0.047567 -0.056865 -0.042870
2017-06-30  0.030800  0.072486  0.006870
2017-07-31 -0.090369 -0.174932 -0.111368
2017-08-31  0.030458  0.110808  0.142843
2017-09-30  0.044697  0.107524  0.149778
2017-10-31  0.036301  0.075570  0.126472, 'LCAP':                   1         5         10
date                                    
2017-01-31  0.023576  0.061641  0.098639
2017-02-28 -0.059331 -0.113417 -0.165790
2017-03-31  0.036386  0.098071  0.139402
2017-04-30  0.058504  0.121172  0.235443
2017-05-31  0.116725  0.208003  0.254681
2017-06-30 -0.079743 -0.112652 -0.100068
2017-07-31  0.034546  0.047470 -0.014171
2017-08-31 -0.043348 -0.077705 -0.092500
2017-09-30 -0.001407  0.012640  0.102361
2017-10-31  0.091907  0.259473  0.295311, 'HBETA':                   1         5         10
date                                    
2017-01-31 -0.055204 -0.122555 -0.113722
2017-02-28  0.033864  0.063798  0.005742
2017-03-31 -0.063956 -0.136380 -0.180089
2017-04-30 -0.063926 -0.169757 -0.288208
2017-05-31 -0.093147 -0.113701 -0.127945
2017-06-30  0.036101  0.042147  0.100855
2017-07-31  0.011152  0.049852  0.034442
2017-08-31  0.054402  0.056981  0.081977
2017-09-30 -0.015988 -0.080974 -0.173622
2017-10-31 -0.054526 -0.144930 -0.147669, 'ROE':                   1         5         10
date                                    
2017-01-31 -0.006668  0.015202  0.030877
2017-02-28  0.005288  0.074668  0.191549
2017-03-31  0.051860  0.089752  0.067186
2017-04-30  0.047768  0.119150  0.247734
2017-05-31  0.082157  0.157802  0.211361
2017-06-30 -0.010230  0.005716 -0.050100
2017-07-31 -0.052204 -0.163624 -0.205317
2017-08-31 -0.049709 -0.011418  0.002672
2017-09-30  0.060064  0.168057  0.275069
2017-10-31  0.109693  0.265186  0.304174, 'InvestCashGrowRate':                   1         5         10
date                                    
2017-01-31  0.020613  0.108701  0.145501
2017-02-28 -0.010852 -0.067592 -0.081507
2017-03-31  0.019315  0.018223  0.018885
2017-04-30 -0.032513 -0.191393 -0.205431
2017-05-31  0.030667 -0.021695  0.002054
2017-06-30  0.012109 -0.013274  0.013362
2017-07-31 -0.052979 -0.119140 -0.136129
2017-08-31 -0.005626 -0.033312 -0.078426
2017-09-30 -0.009825  0.027542 -0.003980
2017-10-31 -0.022611 -0.171967 -0.268641, 'ROC20':                   1         5         10
date                                    
2017-01-31 -0.034584 -0.042003 -0.023535
2017-02-28 -0.029989  0.018661  0.000333
2017-03-31  0.002398  0.020556  0.035565
2017-04-30  0.027474  0.044562  0.090148
2017-05-31  0.078576  0.097411  0.164281
2017-06-30 -0.088641 -0.067039 -0.094156
2017-07-31 -0.000194  0.009889 -0.010843
2017-08-31 -0.069707 -0.193947 -0.224456
2017-09-30 -0.023964 -0.019605  0.004773
2017-10-31  0.060376  0.205338  0.280861}
  1. monthly_ic_mean = pd.DataFrame(
  2. list(map(lambda frame: frame.mean(), monthly_ic.values())),
  3. monthly_ic.keys()
  4. )
  5. print(monthly_ic_mean)
  6. monthly_ic_std = pd.DataFrame(
  7. list(map(lambda frame: frame.std(), monthly_ic.values())),
  8. monthly_ic.keys()
  9. )
  10. print (monthly_ic_std)
                          1         5         10
PB                 -0.008252 -0.000884  0.012992
LCAP                0.017782  0.050469  0.075331
HBETA              -0.021123 -0.055552 -0.080824
ROE                 0.023802  0.072049  0.107520
InvestCashGrowRate -0.005170 -0.046391 -0.059431
ROC20              -0.007825  0.007382  0.022297
                          1         5         10
PB                  0.044728  0.096099  0.115215
LCAP                0.064261  0.127900  0.162157
HBETA               0.051856  0.096405  0.128899
ROE                 0.054842  0.118580  0.165144
InvestCashGrowRate  0.026329  0.093160  0.121152
ROC20               0.053076  0.104467  0.137530

Step3 用柱图显示IC的均值与标准差

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. fig, ax = plt.subplots()
  4. N=6
  5. ind = np.arange(N) # the x locations for the groups
  6. width = 0.3 # the width of the bars
  7. # Example data
  8. ind_name = tuple(monthly_ic_mean.index)
  9. y_pos = np.arange(len(ind))
  10. one_mean = monthly_ic_mean.iloc[:,0]
  11. one_std = monthly_ic_std.iloc[:,0]
  12. five_mean = monthly_ic_mean.iloc[:,1]
  13. five_std = monthly_ic_std.iloc[:,1]
  14. ten_mean = monthly_ic_mean.iloc[:,2]
  15. ten_std = monthly_ic_std.iloc[:,2]
  16. ax.barh(ind - width, one_mean, align='edge',height=0.2, xerr=one_std, label='one_day', color='r')
  17. ax.barh(ind - 0.05, five_mean, align='edge',height=0.2,xerr=five_std, label='five_day', color='y')
  18. ax.barh(ind + width, ten_mean, align='center',height=0.2,xerr=ten_std, label='ten_day', color='b')
  19. ax.set_yticks(y_pos)
  20. ax.set_yticklabels(ind_name)
  21. ax.invert_yaxis() # labels read top-to-bottom
  22. ax.set_xlabel('mean_IC')
  23. ax.set_title('HS300_IC in Different Period')
  24. plt.legend()
  25. plt.show()

output_9_0.png-14.9kB

  1. PN_IC = pd.Panel(monthly_ic)
  1. PN_IC
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 10 (major_axis) x 3 (minor_axis)
Items axis: HBETA to ROE
Major_axis axis: 2017-01-31 00:00:00 to 2017-10-31 00:00:00
Minor_axis axis: 1 to 10

Step4 计算Rolling IC与设置IC=0.02的阈值

  1. fig = plt.figure(figsize=(15,7))
  2. plt.plot(PN_IC.minor_xs(1))
  3. plt.hlines(0.02,PN_IC.minor_xs(1).index[0],PN_IC.minor_xs(1).index[-1] , linestyles='dashed', alpha=0.5)
  4. plt.legend(PN_IC.minor_xs(1).columns)
  5. plt.title('OneDay_IC')
  6. plt.show()

![png](output_13_0.png)

  1. fig = plt.figure(figsize=(15,7))
  2. plt.plot(PN_IC.minor_xs(5))
  3. plt.hlines(0.02,PN_IC.minor_xs(5).index[0],PN_IC.minor_xs(5).index[-1] , linestyles='dashed', alpha=0.5)
  4. plt.legend(PN_IC.minor_xs(5).columns)
  5. plt.title('FiveDay_IC')
  6. plt.show()

![png](output_14_0.png)

  1. fig = plt.figure(figsize=(15,7))
  2. plt.plot(PN_IC.minor_xs(10))
  3. plt.hlines(0.02,PN_IC.minor_xs(10).index[0],PN_IC.minor_xs(10).index[-1] , linestyles='dashed', alpha=0.5)
  4. plt.legend(PN_IC.minor_xs(10).columns)
  5. plt.title('TenDay_IC')
  6. plt.show()

![png](output_15_0.png)

Step5 检验IC是否满足正态分布

  1. from statsmodels.stats.stattools import jarque_bera
  2. print (PN_IC.minor_xs(1))
  3. _, pvalue1, _, _ = jarque_bera(PN_IC.minor_xs(1).dropna().values)
  4. print ('pvalue One_Day is normally distributed', pvalue1)
               HBETA  InvestCashGrowRate      LCAP        PB     ROC20  \
date                                                                     
2017-01-31 -0.055204            0.020613  0.023576 -0.051354 -0.034584   
2017-02-28  0.033864           -0.010852 -0.059331 -0.011522 -0.029989   
2017-03-31 -0.063956            0.019315  0.036386 -0.009924  0.002398   
2017-04-30 -0.063926           -0.032513  0.058504 -0.014042  0.027474   
2017-05-31 -0.093147            0.030667  0.116725 -0.047567  0.078576   
2017-06-30  0.036101            0.012109 -0.079743  0.030800 -0.088641   
2017-07-31  0.011152           -0.052979  0.034546 -0.090369 -0.000194   
2017-08-31  0.054402           -0.005626 -0.043348  0.030458 -0.069707   
2017-09-30 -0.015988           -0.009825 -0.001407  0.044697 -0.023964   
2017-10-31 -0.054526           -0.022611  0.091907  0.036301  0.060376   

                 ROE  
date                  
2017-01-31 -0.006668  
2017-02-28  0.005288  
2017-03-31  0.051860  
2017-04-30  0.047768  
2017-05-31  0.082157  
2017-06-30 -0.010230  
2017-07-31 -0.052204  
2017-08-31 -0.049709  
2017-09-30  0.060064  
2017-10-31  0.109693  
pvalue One_Day is normally distributed [ 0.62171525  0.78662971  0.79006356  0.71264684  0.83603692  0.74936821]

Step 6 计算IC的IR

  1. # IR = mean/std
  2. def IR(df):
  3. return df.mean()/df.std()
  4. print ('OneDay_IR:\n', IR(PN_IC.minor_xs(1)))
  5. print ('FiveDay_IR:\n', IR(PN_IC.minor_xs(5)))
  6. print ('TenDay_IR:\n', IR(PN_IC.minor_xs(10)))
OneDay_IR:
 HBETA                -0.407332
InvestCashGrowRate   -0.196366
LCAP                  0.276707
PB                   -0.184496
ROC20                -0.147439
ROE                   0.434004
dtype: float64
FiveDay_IR:
 HBETA                -0.576238
InvestCashGrowRate   -0.497970
LCAP                  0.394602
PB                   -0.009201
ROC20                 0.070667
ROE                   0.607596
dtype: float64
TenDay_IR:
 HBETA                -0.627030
InvestCashGrowRate   -0.490551
LCAP                  0.464554
PB                    0.112766
ROC20                 0.162126
ROE                   0.651069
dtype: float64
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注