Notebook
In [22]:
import numpy as np
import pandas as pd
from quantopian.research import run_pipeline
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data import Fundamentals
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import CustomFactor
from quantopian.pipeline.classifiers.fundamentals import Sector  
from quantopian.pipeline.filters import QTradableStocksUS
from time import time
In [23]:
import alphalens as al
In [24]:
MORNINGSTAR_SECTOR_CODES = {
     -1: 'Misc',
    101: 'Basic Materials',
    102: 'Consumer Cyclical',
    103: 'Financial Services',
    104: 'Real Estate',
    205: 'Consumer Defensive',
    206: 'Healthcare',
    207: 'Utilities',
    308: 'Communication Services',
    309: 'Energy',
    310: 'Industrials',
    311: 'Technology' ,    
}
In [25]:
base_universe = QTradableStocksUS()
In [26]:
test_factor = 1 - Fundamentals.peg_ratio.latest.winsorize(min_percentile=0.05, max_percentile=0.95)
In [27]:
pipe = Pipeline(
    columns = {
            'MyFactor' : test_factor.zscore(mask=base_universe),
            'Sector' : Sector()
    },
    screen=base_universe
)

start_timer = time()
results = run_pipeline(pipe, '2015-06-15', '2017-07-15')
end_timer = time()
results.fillna(value=0);

print "Time to run pipeline %.2f secs" % (end_timer - start_timer)
Time to run pipeline 8.93 secs
In [28]:
my_factor = results['MyFactor']
sectors = results['Sector']
asset_list = results.index.levels[1].unique()
prices = get_pricing(asset_list, start_date='2015-06-15', end_date='2017-09-30', fields='open_price')
periods = (1,5,10,21)

factor_data = al.utils.get_clean_factor_and_forward_returns(factor=my_factor,
                                                            prices=prices,
                                                            groupby=sectors,
                                                            groupby_labels=MORNINGSTAR_SECTOR_CODES,
                                                            periods=periods,
                                                            quantiles = 5)
Dropped 5.5% entries from factor data: 5.5% in forward returns computation and 0.0% in binning phase (set max_loss=0 to see potentially suppressed Exceptions).
max_loss is 35.0%, not exceeded: OK!
In [29]:
mean_return_by_q_daily, std_err_by_q_daily = al.performance.mean_return_by_quantile(factor_data,
                                                                                    by_date=True)
mean_return_by_q, std_err_by_q = al.performance.mean_return_by_quantile(factor_data,
                                                                        by_group=False)
ic = al.performance.factor_information_coefficient(factor_data)
In [ ]:
al.tears.create_summary_tear_sheet(factor_data)
Quantiles Statistics
min max mean std count count %
factor_quantile
1 -4.002039 -0.040199 -1.542329 1.377055 208263 20.020187
2 -0.147025 0.296672 0.140452 0.103607 207950 19.990099
3 0.264100 0.434692 0.357619 0.038837 207942 19.989330
4 0.404765 0.535920 0.462209 0.027803 207951 19.990195
5 0.492592 0.727781 0.584895 0.054386 208159 20.010190
Returns Analysis
1D 5D 10D 21D
Ann. alpha 0.010 0.008 0.006 0.002
beta -0.043 -0.038 -0.035 -0.020
Mean Period Wise Return Top Quantile (bps) -1.089 -1.143 -1.239 -1.163
Mean Period Wise Return Bottom Quantile (bps) -0.409 -0.255 -0.151 -0.097
Mean Period Wise Spread (bps) -0.635 -0.880 -1.081 -1.059
Information Analysis
1D 5D 10D 21D
IC Mean -0.002 -0.006 -0.007 -0.009
IC Std. 0.083 0.093 0.094 0.092
Risk-Adjusted IC -0.025 -0.061 -0.074 -0.097
t-stat(IC) -0.573 -1.406 -1.698 -2.224
p-value(IC) 0.567 0.160 0.090 0.027
IC Skew 0.324 0.378 0.204 0.349
IC Kurtosis 0.137 -0.038 -0.296 -0.289
Turnover Analysis
10D 1D 21D 5D
Quantile 1 Mean Turnover 0.049 0.010 0.087 0.029
Quantile 2 Mean Turnover 0.095 0.023 0.154 0.061
Quantile 3 Mean Turnover 0.130 0.034 0.204 0.086
Quantile 4 Mean Turnover 0.127 0.033 0.198 0.085
Quantile 5 Mean Turnover 0.065 0.015 0.106 0.042
1D 5D 10D 21D
Mean Factor Rank Autocorrelation 0.998 0.992 0.983 0.965
<matplotlib.figure.Figure at 0x7f2076b56810>
In [ ]:
al.tears.create_full_tear_sheet(factor_data, by_group=True);
Quantiles Statistics
min max mean std count count %
factor_quantile
1 -4.002039 -0.040199 -1.542329 1.377055 208263 20.020187
2 -0.147025 0.296672 0.140452 0.103607 207950 19.990099
3 0.264100 0.434692 0.357619 0.038837 207942 19.989330
4 0.404765 0.535920 0.462209 0.027803 207951 19.990195
5 0.492592 0.727781 0.584895 0.054386 208159 20.010190
Returns Analysis
1D 5D 10D 21D
Ann. alpha 0.010 0.008 0.006 0.002
beta -0.043 -0.038 -0.035 -0.020
Mean Period Wise Return Top Quantile (bps) -1.089 -1.143 -1.239 -1.163
Mean Period Wise Return Bottom Quantile (bps) -0.409 -0.255 -0.151 -0.097
Mean Period Wise Spread (bps) -0.635 -0.880 -1.081 -1.059
<matplotlib.figure.Figure at 0x7f2076b60550>