Notebook

# 101 Alphas Project: Pipeline Factor Information Coefficent¶

This analysis computes the Spearman Rank correlation between an alpha factor and 1, 5, and 10 day forward price movements. We disagregate by sector to isolate sector specific trends.

In [188]:
from __future__ import division
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.research import run_pipeline
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import Latest
import numpy as np
import pandas as pd
import scipy as sp
import pyfolio as pf
import matplotlib.pyplot as plt

In [162]:
start_date = pd.to_datetime('2014-10-1')
end_date = pd.to_datetime('2015-2-1')


Here's the alpha we are looking to analyze:

In [163]:
alpha_name = "alpha101"
class Alpha101(CustomFactor):
inputs = [USEquityPricing.close, USEquityPricing.high, USEquityPricing.low]
window_length = 2

def compute(self, today, assets, out, close, high, low):
# Using close of t-1 as the t open. Workaround for known open bug.
out[:] = (close[-1] - close[-2]) / ((high[-1] - low[-1]) + .001)


We'll also use a five day liquidity factor to ensure we are only dealing with liquid names:

In [164]:
class Liquidity(CustomFactor):
inputs = [USEquityPricing.volume, USEquityPricing.close]
window_length = 5

def compute(self, today, assets, out, volume, close):
out[:] = (volume * close).mean(axis=0)


Pull in sector codes for sector analysis:

In [165]:
class Sector(CustomFactor):
inputs = [morningstar.asset_classification.morningstar_sector_code]
window_length = 1
def compute(self, today, assets, out, msc):
out[:] = msc[-1]

In [166]:
alpha101 = Alpha101()
sector = Sector()
liquidity = Liquidity()

liquidity_rank = liquidity.rank(ascending=False)
# the alpha101.eq(alpha101) bit is a handy trick to filter out rows containing NaNs.
ok_universe = (1000 > liquidity_rank)  & alpha101.eq(alpha101) & sector.eq(sector)


In [167]:
pipe = Pipeline()
pipe.set_screen(ok_universe)

In [168]:
pipe.show_graph(format='png')

Out[168]:
In [169]:
daily_alpha = run_pipeline(pipe, start_date=start_date, end_date=end_date)
daily_alpha = daily_alpha.reset_index().rename(columns={'level_0': 'date', 'level_1':'sid'})

Out[169]:
date sid alpha101 sector_code
0 2014-10-01 00:00:00+00:00 Equity(2 [AA]) 0.788382 101
1 2014-10-01 00:00:00+00:00 Equity(24 [AAPL]) 0.809102 311
2 2014-10-01 00:00:00+00:00 Equity(62 [ABT]) -0.705596 206
3 2014-10-01 00:00:00+00:00 Equity(64 [ABX]) -0.996979 101
4 2014-10-01 00:00:00+00:00 Equity(67 [ADSK]) 0.177936 311
In [170]:
len(equities)

Out[170]:
1685

Now pull in pricing data and compute 1, 5, and 10 day forward price movements.

In [210]:
equities = daily_alpha.sid.unique()
equities = map(lambda x: x.sid, equities)
td = pd.Timedelta(days=25)
prices = get_pricing(equities, start_date=start_date, end_date=end_date+td, fields='open_price')

In [211]:
col_n = '%s_day_fwd_price_pct_change'

daily_alpha = daily_alpha.set_index(['date', 'sid'])
for i in [1, 5, 10]:
delta = prices.pct_change(i).shift(-i)
daily_alpha[col_n%i] = delta.stack()
daily_alpha = daily_alpha.reset_index()

In [212]:
daily_alpha.head()

Out[212]:
date sid alpha101 sector_code 1_day_fwd_price_pct_change 5_day_fwd_price_pct_change 10_day_fwd_price_pct_change y_m
0 2014-10-01 00:00:00+00:00 Equity(2 [AA]) 0.788382 101 -0.025418 -0.009919 -0.132052 2014-10-01 00:00:00+00:00
1 2014-10-01 00:00:00+00:00 Equity(24 [AAPL]) 0.809102 311 -0.012327 -0.018193 -0.026046 2014-10-01 00:00:00+00:00
2 2014-10-01 00:00:00+00:00 Equity(62 [ABT]) -0.705596 206 -0.019738 -0.014507 -0.039715 2014-10-01 00:00:00+00:00
3 2014-10-01 00:00:00+00:00 Equity(64 [ABX]) -0.996979 101 0.005420 -0.058266 -0.077236 2014-10-01 00:00:00+00:00
4 2014-10-01 00:00:00+00:00 Equity(67 [ADSK]) 0.177936 311 0.062699 0.056148 -0.082725 2014-10-01 00:00:00+00:00

Now compute the Spearman Rank correlation within each day and sector.

In [213]:
cn = "%s_day_IC"
src = pd.DataFrame()
src_fn = lambda x: sp.stats.spearmanr(daily_alpha.loc[x.index, alpha_name],
daily_alpha.loc[x.index, x.name])[0]

src = daily_alpha.groupby(['date', 'sector_code']).agg({col_n%1: src_fn,
col_n%5: src_fn,
col_n%10: src_fn})

src = src.rename(columns={col_n%1: cn%1, col_n%5: cn%5,col_n%10: cn%10})
src = src[[cn%1, cn%5, cn%10]]
src = src.reset_index()

In [214]:
sector_names = {
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 [215]:
for sc, cor in src.groupby('sector_code'):
t = alpha_name + ": " + sector_names[sc] + " Information Coeficient"
cor.drop('sector_code', axis=1).set_index('date').plot(title=t)