@Channelchan
2017-12-02T18:14:58.000000Z
字数 7429
阅读 12179
PB/LCAP/HBETA/ROE/InvestCashGrowRate/Momentum
from fxdayu_data import DataAPI
from datetime import datetime
start = datetime(2017,1,1)
end = datetime(2017,11,11)
codes = DataAPI.info.codes('hs300')
get_factor = DataAPI.factor(codes,('PB','LCAP','HBETA', 'ROE', 'InvestCashGrowRate', 'ROC20'),start,end)
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
prices = DataAPI.candle(codes,'D','close',start=start, end=end).minor_xs('close')
import alphalens
import pandas as pd
factors = {'PB': get_factor.minor_xs('PB'),
'LCAP' : get_factor.minor_xs('LCAP'),
'HBETA' : get_factor.minor_xs('HBETA'),
'ROE' : get_factor.minor_xs('ROE'),
'InvestCashGrowRate' : get_factor.minor_xs('InvestCashGrowRate'),
'ROC20' :get_factor.minor_xs('ROC20')}
def cal_monthly_ic(factor_df):
factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factor_df.stack(), prices, quantiles=5)
return alphalens.performance.mean_information_coefficient(factor_data, by_time='M')
monthly_ic = {key: cal_monthly_ic(value) for key, value in factors.items()}
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}
monthly_ic_mean = pd.DataFrame(
list(map(lambda frame: frame.mean(), monthly_ic.values())),
monthly_ic.keys()
)
print(monthly_ic_mean)
monthly_ic_std = pd.DataFrame(
list(map(lambda frame: frame.std(), monthly_ic.values())),
monthly_ic.keys()
)
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
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
N=6
ind = np.arange(N) # the x locations for the groups
width = 0.3 # the width of the bars
# Example data
ind_name = tuple(monthly_ic_mean.index)
y_pos = np.arange(len(ind))
one_mean = monthly_ic_mean.iloc[:,0]
one_std = monthly_ic_std.iloc[:,0]
five_mean = monthly_ic_mean.iloc[:,1]
five_std = monthly_ic_std.iloc[:,1]
ten_mean = monthly_ic_mean.iloc[:,2]
ten_std = monthly_ic_std.iloc[:,2]
ax.barh(ind - width, one_mean, align='edge',height=0.2, xerr=one_std, label='one_day', color='r')
ax.barh(ind - 0.05, five_mean, align='edge',height=0.2,xerr=five_std, label='five_day', color='y')
ax.barh(ind + width, ten_mean, align='center',height=0.2,xerr=ten_std, label='ten_day', color='b')
ax.set_yticks(y_pos)
ax.set_yticklabels(ind_name)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('mean_IC')
ax.set_title('HS300_IC in Different Period')
plt.legend()
plt.show()
PN_IC = pd.Panel(monthly_ic)
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
fig = plt.figure(figsize=(15,7))
plt.plot(PN_IC.minor_xs(1))
plt.hlines(0.02,PN_IC.minor_xs(1).index[0],PN_IC.minor_xs(1).index[-1] , linestyles='dashed', alpha=0.5)
plt.legend(PN_IC.minor_xs(1).columns)
plt.title('OneDay_IC')
plt.show()
fig = plt.figure(figsize=(15,7))
plt.plot(PN_IC.minor_xs(5))
plt.hlines(0.02,PN_IC.minor_xs(5).index[0],PN_IC.minor_xs(5).index[-1] , linestyles='dashed', alpha=0.5)
plt.legend(PN_IC.minor_xs(5).columns)
plt.title('FiveDay_IC')
plt.show()
fig = plt.figure(figsize=(15,7))
plt.plot(PN_IC.minor_xs(10))
plt.hlines(0.02,PN_IC.minor_xs(10).index[0],PN_IC.minor_xs(10).index[-1] , linestyles='dashed', alpha=0.5)
plt.legend(PN_IC.minor_xs(10).columns)
plt.title('TenDay_IC')
plt.show()
from statsmodels.stats.stattools import jarque_bera
print (PN_IC.minor_xs(1))
_, pvalue1, _, _ = jarque_bera(PN_IC.minor_xs(1).dropna().values)
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]
# IR = mean/std
def IR(df):
return df.mean()/df.std()
print ('OneDay_IR:\n', IR(PN_IC.minor_xs(1)))
print ('FiveDay_IR:\n', IR(PN_IC.minor_xs(5)))
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