Notebook
In [1]:
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.psychsignal import stocktwits
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import CustomFactor, Returns
from quantopian.pipeline.classifiers.fundamentals import Sector  
from quantopian.pipeline.filters import QTradableStocksUS
from time import time
In [2]:
import alphalens as al
In [3]:
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 [4]:
base_universe = QTradableStocksUS()
In [5]:
class stupidFact(CustomFactor):
        inputs = [USEquityPricing.close]
        def compute(self, today, assets, out, values):
            out[:] = (values[0] - values[self.window_length-1])/values[self.window_length-1]
In [6]:
pipe = Pipeline(
    columns = {
            'factor1' : stupidFact(mask=base_universe,window_length=20),
            'factor2' : stupidFact(mask=base_universe,window_length=50),
            'Sector' : Sector()
    },
    screen=base_universe
)

start_timer = time()
results = run_pipeline(pipe, '2016-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 7.47 secs
In [7]:
my_factor = results['factor1']
sectors = results['Sector']
asset_list = results.index.levels[1].unique()
prices = get_pricing(asset_list, start_date='2016-06-15', end_date='2017-08-30', fields='open_price')
periods = (1,3,5,10,21)

factor1_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)
my_factor = results['factor2']
factor2_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 0.1% entries from factor data: 0.1% 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!
Dropped 0.1% entries from factor data: 0.1% 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 [71]:
factor1_data
Out[71]:
1D 3D 5D 10D 21D factor group factor_quantile
date asset
2016-06-15 00:00:00+00:00 Equity(2 [ARNC]) 0.002157 0.051833 0.071243 0.026977 0.157656 0.022527 Basic Materials 4
Equity(24 [AAPL]) -0.014005 -0.018607 -0.016055 -0.039359 0.011244 -0.040837 Technology 2
Equity(31 [ABAX]) -0.012906 -0.008981 0.008515 -0.018605 0.171682 -0.042694 Healthcare 2
Equity(39 [DDC]) -0.003069 -0.016298 -0.008149 -0.106043 -0.093767 0.111338 Basic Materials 5
Equity(41 [ARCB]) -0.010265 0.018133 -0.013953 -0.064601 0.083349 -0.018215 Industrials 3
Equity(52 [ABM]) -0.013010 -0.005702 -0.009495 -0.036872 0.011903 -0.105735 Industrials 1
Equity(53 [ABMD]) 0.002150 -0.022865 -0.008306 0.035568 0.152238 -0.077511 Healthcare 1
Equity(62 [ABT]) -0.010324 0.003441 -0.000792 0.005298 0.130083 0.000000 Healthcare 4
Equity(64 [ABX]) 0.077715 -0.014744 -0.023396 0.058414 0.074182 -0.022844 Basic Materials 3
Equity(67 [ADSK]) 0.003215 0.026617 0.031083 -0.074312 0.043230 0.040747 Technology 5
Equity(76 [TAP]) -0.010700 0.008028 0.014770 -0.032900 -0.019224 -0.027291 Consumer Defensive 3
Equity(84 [ACET]) -0.001753 -0.010159 -0.014609 -0.063739 0.125275 -0.111046 Healthcare 1
Equity(110 [ACXM]) -0.008329 0.012031 0.017584 -0.027765 0.061083 0.039981 Technology 5
Equity(114 [ADBE]) -0.005257 0.000928 -0.015256 -0.042264 0.014225 -0.010724 Technology 3
Equity(122 [ADI]) -0.013104 -0.008384 -0.002092 -0.044888 0.052218 -0.036608 Technology 2
Equity(128 [ADM]) 0.001190 0.023903 0.004786 -0.019366 0.041112 -0.102920 Consumer Defensive 1
Equity(154 [AEM]) 0.046017 -0.015002 -0.008795 0.060433 0.109038 -0.034187 Basic Materials 3
Equity(161 [AEP]) -0.008073 0.002983 -0.000156 0.029733 0.037806 -0.034720 Utilities 3
Equity(166 [AES]) -0.010874 0.053045 0.081789 0.070064 0.128593 -0.009892 Utilities 3
Equity(168 [AET]) -0.002585 0.016673 0.009333 -0.001166 -0.007196 -0.078364 Healthcare 1
Equity(185 [AFL]) -0.012712 0.018091 0.031809 0.010353 0.079110 0.002049 Financial Services 4
Equity(197 [AGCO]) -0.016641 -0.007001 -0.019109 -0.117234 -0.067311 -0.026226 Industrials 3
Equity(216 [HES]) -0.021855 0.009184 0.023925 -0.030879 0.007450 -0.007104 Energy 4
Equity(239 [AIG]) -0.020448 -0.002032 -0.005907 -0.070770 0.006641 0.019975 Financial Services 4
Equity(247 [AIN]) -0.021104 -0.010186 -0.009430 -0.035022 0.060109 -0.055925 Consumer Cyclical 2
Equity(253 [AIR]) -0.014657 0.013785 0.002137 -0.037951 0.000436 0.008865 Industrials 4
Equity(266 [AJG]) -0.015552 0.002761 -0.000221 -0.026641 0.043497 -0.000236 Financial Services 4
Equity(270 [AKRX]) -0.009934 -0.031457 -0.054967 -0.056954 0.027483 -0.009599 Healthcare 4
Equity(289 [MATX]) -0.003628 0.003008 -0.006635 -0.016279 0.066634 -0.055790 Industrials 2
Equity(300 [ALK]) -0.014282 -0.017687 -0.018828 -0.072699 0.049507 0.059073 Industrials 5
... ... ... ... ... ... ... ... ... ...
2017-07-17 00:00:00+00:00 Equity(50077 [TWLO]) 0.043523 0.082248 0.065113 0.028101 0.071624 -0.079421 Technology 1
Equity(50119 [TRTN]) 0.000578 0.033062 0.070965 0.108564 0.068256 -0.026831 Industrials 2
Equity(50138 [PI]) 0.028025 -0.043742 -0.007764 -0.032948 -0.365272 0.059293 Technology 5
Equity(50181 [FHB]) -0.001342 -0.022315 -0.017987 -0.026309 -0.036946 0.018636 Financial Services 4
Equity(50194 [MEDP]) 0.008330 0.015936 0.019196 0.000362 0.131474 0.024584 Healthcare 4
Equity(50242 [DVMT]) 0.022675 0.043393 0.037520 0.044046 0.105383 0.014294 Technology 4
Equity(50260 [ASIX]) 0.022103 0.033751 0.011947 0.022401 -0.035544 -0.119617 Basic Materials 1
Equity(50264 [EVBG]) 0.000000 0.027145 0.010508 0.036340 0.002627 0.064672 Technology 5
Equity(50279 [NTB]) -0.007797 -0.010973 -0.011262 -0.013861 -0.027144 0.006047 Financial Services 4
Equity(50287 [VSM]) 0.005932 0.043841 0.054816 0.047400 0.050337 -0.070803 Basic Materials 1
Equity(50288 [TTD]) -0.023735 0.070039 0.012257 0.057004 0.039883 -0.021564 Technology 3
Equity(50310 [DFIN]) 0.001757 0.039104 0.026362 0.017575 -0.079525 0.019383 Financial Services 4
Equity(50312 [LKSD]) 0.000000 0.043156 0.025037 0.065207 -0.161274 0.075184 Industrials 5
Equity(50318 [APTI]) -0.001779 -0.013049 -0.005338 0.059905 0.008304 0.007738 Technology 4
Equity(50320 [ELF]) -0.003967 -0.008330 -0.004760 0.027370 0.013883 0.049741 Consumer Defensive 5
Equity(50325 [VVV]) -0.014413 0.009638 0.005695 -0.004337 -0.027073 0.034980 Energy 5
Equity(50338 [NTNX]) -0.013956 0.067597 0.027039 -0.049717 -0.036633 -0.201627 Technology 1
Equity(50348 [SD]) 0.012228 0.035088 0.025518 0.040936 -0.061138 -0.063391 Energy 1
Equity(50350 [COUP]) -0.004241 0.005546 -0.038499 0.008483 -0.011419 0.018930 Technology 4
Equity(50357 [ARCH]) -0.004171 0.009033 0.011457 -0.011592 0.011322 -0.146763 Basic Materials 1
Equity(50361 [ADSW]) 0.002597 0.010823 0.013420 0.038528 -0.008225 0.024148 Industrials 4
Equity(50366 [CWH]) 0.011414 0.034211 0.008244 0.010146 0.118897 -0.094714 Consumer Cyclical 1
Equity(50368 [XOG]) -0.012289 0.000000 -0.056068 -0.029186 -0.075269 0.003060 Energy 4
Equity(50376 [CDEV]) 0.024328 0.033771 -0.011882 0.061914 0.024328 -0.101737 Energy 1
Equity(50382 [FTS]) 0.004594 0.012633 0.025553 0.042205 0.035601 -0.008021 Utilities 3
Equity(50390 [ADNT]) 0.013030 0.022428 -0.004982 -0.047979 -0.034034 -0.101641 Consumer Cyclical 1
Equity(50392 [YUMC]) -0.001079 0.005665 0.001888 -0.034260 0.003507 0.074723 Consumer Cyclical 5
Equity(50398 [FRTA]) -0.013018 0.029586 0.067456 0.047337 -0.474556 -0.244392 Basic Materials 1
Equity(50399 [IRTC]) -0.024864 -0.014682 0.010656 -0.018707 0.058726 -0.078972 Healthcare 1
Equity(50403 [QCP]) 0.001094 0.019147 0.016411 -0.090263 -0.145514 -0.035558 Real Estate 2

566610 rows × 8 columns

In [80]:
def computeCorr(f1,f2,col='factor'):
    df = pd.DataFrame({col + ' correlation' : []})
    df.index.name = 'dates'
    idx = f1.index
    for i in range(len(idx.levels[0])):
        corr = (f1.loc[([idx.levels[0][i]], slice(None)),[col]]).corrwith(
                    f2.loc[([idx.levels[0][i]], slice(None)), [col]])
        df.loc[idx.levels[0][i]] = corr[col]
    return df
In [81]:
df_corr = computeCorr(factor1_data,factor2_data,'factor')
df_corr.plot()
Out[81]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f078cddb350>
In [82]:
df_corr = computeCorr(factor1_data,factor2_data,'factor_quantile')
df_corr.plot()
Out[82]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f078d484c10>