Notebook

Alphalens + Quantopian | How To

For more information on how to read and understand the plots check out:

In [4]:
import numpy as np
from quantopian.research import run_pipeline
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import CustomFactor, Returns
#from quantopian.pipeline.filters import Q1500US
from quantopian.pipeline import CustomFilter

Import Factor & Print

In [11]:
from quantopian.interactive.data.quandl import adp_empl_sec

Define our factor

In [5]:
#class LowVol(CustomFactor):
#    inputs = [Returns(window_length=2)]
#    window_length = 25
#    
#    def compute(self, today, assets, out, close):
#        out[:] = -np.nanstd(close, axis=0)
In [14]:
class adp_empl_sec(CustomFactor):
    inputs = [Returns(window_length=2)]
    window_length = 25
<class '__main__.adp_empl'>
In [15]:
adp_empl_sec.sort('asof_date')
Out[15]:
total_private goods_producing service_providing asof_date timestamp
0 111573.000000 24275.294168 87297.757625 2001-04-30 2001-05-01
1 111398.132821 24140.307995 87257.824826 2001-05-31 2001-06-01
2 111167.552706 23992.631803 87174.920903 2001-06-30 2001-07-01
3 110964.904589 23854.365510 87110.539079 2001-07-31 2001-08-01
4 110719.120440 23699.879744 87019.240696 2001-08-31 2001-09-01
5 110457.629117 23562.811692 86894.817424 2001-09-30 2001-10-01
6 110078.236801 23390.394579 86687.842222 2001-10-31 2001-11-01
7 109716.868489 23206.664911 86510.203579 2001-11-30 2001-12-01
8 109494.189038 23070.799900 86423.389138 2001-12-31 2002-01-01
9 109424.930934 22951.233078 86473.697856 2002-01-31 2002-02-01
10 109334.446257 22861.907749 86472.538508 2002-02-28 2002-03-01

Define our universe

In [5]:
#universe = Q1500US()
universe = Symbol('SPY')

Create the Pipeline

In [6]:
pipe_low_vol = Pipeline(
    columns={
        'adp_empl_sec' : adp_empl_sec(mask=universe)
    },
    screen=universe
)

Run the Pipeline

In [7]:
results = run_pipeline(pipe_low_vol, '2015-06-30', '2016-06-30')
In [8]:
results.head()
Out[8]:
LowVol
2015-06-30 00:00:00+00:00 Equity(2 [ARNC]) -0.008829
Equity(24 [AAPL]) -0.008808
Equity(41 [ARCB]) -0.019180
Equity(53 [ABMD]) -0.018621
Equity(62 [ABT]) -0.009697

Get pricing

In [9]:
assets = results.index.levels[1].unique()
# We need to get a little more pricing data than the 
# length of our factor so we can compare forward returns.
# We'll tack on another month in this example.
pricing = get_pricing(assets, start_date='2015-06-30', end_date='2016-07-31', fields='open_price')
In [10]:
import alphalens

factor_data = alphalens.utils.get_clean_factor_and_forward_returns(results['adp_empl_sec'],
                                                                   pricing,
                                                                   quantiles=5,
                                                                   periods=(1,5,10))

alphalens.tears.create_full_tear_sheet(factor_data)
Quantiles Statistics
min max mean std count count %
factor_quantile
1 -1.646902 -0.018440 -0.042382 0.034546 75689 20.027678
2 -0.040590 -0.013349 -0.023782 0.004740 75531 19.985870
3 -0.029796 -0.010987 -0.018049 0.003490 75532 19.986135
4 -0.023733 -0.008924 -0.014463 0.002929 75531 19.985870
5 -0.018973 -0.000457 -0.010543 0.003019 75639 20.014447
Returns Analysis
1 5 10
Ann. alpha 0.054 0.030 0.027
beta -0.442 -0.487 -0.512
Mean Period Wise Return Top Quantile (bps) 5.072 24.195 40.105
Mean Period Wise Return Bottom Quantile (bps) -5.399 -25.580 -42.974
Mean Period Wise Spread (bps) 10.442 10.119 8.489
Information Analysis
1 5 10
IC Mean 0.027 0.049 0.053
IC Std. 0.238 0.249 0.252
t-stat(IC) 1.819 3.125 3.335
p-value(IC) 0.070 0.002 0.001
IC Skew 0.150 -0.142 -0.211
IC Kurtosis -0.554 -0.811 -0.846
Ann. IR 1.812 3.113 3.322
Turnover Analysis
1 5 10
Quantile 1 Mean Turnover 0.044 0.138 0.222
Quantile 2 Mean Turnover 0.097 0.280 0.421
Quantile 3 Mean Turnover 0.128 0.350 0.498
Quantile 4 Mean Turnover 0.133 0.353 0.493
Quantile 5 Mean Turnover 0.066 0.186 0.283
1 5 10
Mean Factor Rank Autocorrelation 0.988 0.934 0.871
<matplotlib.figure.Figure at 0x7faf512785d0>