Alphalens初体验

alphalens
标签: #<Tag:0x00007fcf659b2940>

(iQuant) #1

Alphalens是一个常用的因子分析工具,本文是一个简单的尝试,希望能进行更多深入的分析。

克隆策略

Alphalens: 有预测意义的因子 vs.没有预测意义的因子

In [ ]:
本notebook给大家一个示例让大家dui Alphalens有一个直观的了解
In [1]:
import alphalens
import pandas as pd
import numpy as np
In [2]:
import warnings
warnings.filterwarnings('ignore')

以下是获得股票所在的行业

In [77]:
# 行业
industry_data = D.history_data(D.instruments(),'2016-01-16','2017-11-16',['industry_sw_level1'])
industry_data = industry_data.drop('date',axis=1).drop_duplicates()
ticker_sector = dict(zip(industry_data['instrument'],industry_data['industry_sw_level1']))
In [80]:
data = D.history_data(D.instruments()[:300],'2016-01-01','2017-11-18',['close','open','high','low','volume'])
In [58]:
df = {i:data[data['instrument']==i].set_index('date') for i in list(set(data.instrument))}
pan = pd.Panel(df) 

三日收益率作为非预测意义的因子

In [61]:
non_predictive_factor = pan.loc[:,:,'open']
non_predictive_factor = non_predictive_factor.pct_change(3)

non_predictive_factor = non_predictive_factor.stack()
non_predictive_factor.index = non_predictive_factor.index.set_names(['date', 'asset'])

未来五天的收益率数据将作为有预测意义的因子

In [62]:
lookahead_bias_days = 5

predictive_factor = pan.loc[:,:,'open']
predictive_factor = predictive_factor.pct_change(lookahead_bias_days)
# introduce look-ahead bias and make the factor predictive
predictive_factor = predictive_factor.shift(-lookahead_bias_days)

predictive_factor = predictive_factor.stack()
predictive_factor.index = predictive_factor.index.set_names(['date', 'asset'])
In [63]:
pricing = pan.loc[:,:,'open'].iloc[1:]

以及申万行业所对应的申万一级行业

In [83]:
sector_names = { 
210000:'采掘',
220000:'化工',
230000:'钢铁',
240000:'有色金属',
610000:'建筑材料/建筑建材',
620000:'建筑装饰',
630000:'电气设备',
640000:'机械设备',
650000:'国防军工',
280000:'汽车/交运设备',
330000:'家用电器',
360000:'轻工制造',
110000:'农林牧渔',
340000:'食品饮料',
350000:'纺织服装',
370000:'医药生物',
450000:'商业贸易',
460000:'休闲服务',
270000:'电子',
710000:'计算机',
720000:'传媒/信息服务',
730000:'通信',
410000:'公用事业',
420000:'交通运输',
430000:'房地产',
480000:'银行',
490000:'非银金融',
510000:'综合',
0:'其他'}

在非预测意义的因子上运行non_predictive_factor_data

In [84]:
non_predictive_factor_data = alphalens.utils.get_clean_factor_and_forward_returns(non_predictive_factor, 
                                                                                  pricing, 
                                                                                  quantiles=5,
                                                                                  bins=None,
                                                                                  groupby=ticker_sector,
                                                                                  groupby_labels=sector_names)
Dropped 2.2% entries from factor data (2.2% after in forward returns computation and 0.0% in binning phase). Set max_loss=0 to see potentially suppressed Exceptions.
In [86]:
alphalens.tears.create_full_tear_sheet(non_predictive_factor_data)
Quantiles Statistics
min max mean std count count %
factor_quantile
1 -0.503867 0.062112 -0.038623 0.040040 26245 20.862978
2 -0.178571 0.086792 -0.014620 0.028102 25534 20.297781
3 -0.151412 0.105952 -0.003393 0.025686 24799 19.713507
4 -0.120640 0.142616 0.009777 0.023965 24580 19.539417
5 -0.069767 0.455128 0.049362 0.045574 24639 19.586318
Returns Analysis
1 5 10
Ann. alpha 0.023 -0.059 -0.052
beta -0.099 -0.022 -0.021
Mean Period Wise Return Top Quantile (bps) -4.287 -5.524 -3.695
Mean Period Wise Return Bottom Quantile (bps) -1.277 1.891 -0.286
Mean Period Wise Spread (bps) -2.630 -7.277 -3.492
Information Analysis
1 5 10
IC Mean -0.055 -0.052 -0.026
IC Std. 0.165 0.152 0.142
Risk-Adjusted IC -0.332 -0.343 -0.181
t-stat(IC) -7.013 -7.225 -3.819
p-value(IC) 0.000 0.000 0.000
IC Skew 0.424 0.093 -0.282
IC Kurtosis 0.930 0.591 0.341
Turnover Analysis
1 5 10
Quantile 1 Mean Turnover 0.466 0.779 0.773
Quantile 2 Mean Turnover 0.663 0.784 0.793
Quantile 3 Mean Turnover 0.693 0.773 0.778
Quantile 4 Mean Turnover 0.666 0.804 0.799
Quantile 5 Mean Turnover 0.426 0.777 0.769
1 5 10
Mean Factor Rank Autocorrelation 0.584 -0.049 -0.009
<matplotlib.figure.Figure at 0x7f6d97b7b7b8>