Notebook
In [27]:
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, AverageDollarVolume
from quantopian.pipeline.classifiers.morningstar import Sector
from quantopian.pipeline.filters import Q500US
from alphalens import utils, performance as perf

# Lookahead periods for Alphalens.
periods = (1,5,10)
In [28]:
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' ,    
}

Define our pipelines.

In [29]:
universe = Q500US()

# Generate an empty pipeline with an index of the Q500.
q500_pipe = Pipeline(screen=universe)
In [30]:
# Our pipeline to optimize.
class LowVol(CustomFactor):
    inputs = [Returns(window_length=2)]
    window_length = 25
    
    def compute(self, today, assets, out, close):
        out[:] = -np.nanstd(close, axis=0)


def make_low_vol_pipeline(lookback):
    pipe_low_vol = Pipeline(
        columns={
            'LowVol' : LowVol(window_length=lookback, mask=universe),
            'Sector': Sector(mask=universe),
        },
        screen=universe
    )
    
    return pipe_low_vol

Get pricing for assets in the Q500.

In [31]:
# Run our empty pipeline to get the Q500 assets
q500 = run_pipeline(q500_pipe, '2015-06-30', '2016-06-30')

assets = q500.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')

Use Alphalens to evaluate our factor.

In [32]:
for lookback in [5, 10, 15, 20, 25, 30, 35]:
    results = run_pipeline(make_low_vol_pipeline(lookback), '2015-06-30', '2016-06-30')


    factor, forward_returns = utils.get_clean_factor_and_forward_returns(factor=results['LowVol'],
                                                                         prices=pricing,
                                                                         groupby=results['Sector'],
                                                                         periods=periods,
                                                                         filter_zscore=10,
                                                                         groupby_labels=MORNINGSTAR_SECTOR_CODES)

    ic = perf.factor_information_coefficient(factor,
                                             forward_returns,
                                             group_adjust=False,
                                             by_group=False)

    mean_monthly_ic = perf.mean_information_coefficient(factor,
                                                        forward_returns,
                                                        by_time="M")

    factor_returns = perf.factor_returns(factor, forward_returns, long_short=True)

    alpha_beta = perf.factor_alpha_beta(factor,
                                        forward_returns,
                                        factor_returns=factor_returns)

    print 'Metrics for window_length %d:' % lookback
    print alpha_beta
    print ''
Metrics for window_length 5:
                  1         5         10
Ann. alpha  0.021914  0.066547  0.072649
beta       -0.426887 -0.476566 -0.530505

Metrics for window_length 10:
                  1         5         10
Ann. alpha  0.020714  0.079548  0.090629
beta       -0.469346 -0.547448 -0.592381

Metrics for window_length 15:
                  1         5         10
Ann. alpha  0.023038  0.100235  0.109981
beta       -0.494985 -0.572206 -0.592575

Metrics for window_length 20:
                  1         5         10
Ann. alpha  0.051107  0.107579  0.101216
beta       -0.497849 -0.564373 -0.581353

Metrics for window_length 25:
                  1         5         10
Ann. alpha  0.038029  0.086211  0.090831
beta       -0.498427 -0.574324 -0.596356

Metrics for window_length 30:
                  1         5         10
Ann. alpha  0.026244  0.084876  0.087027
beta       -0.499211 -0.580429 -0.603627

Metrics for window_length 35:
                  1         5         10
Ann. alpha  0.017002  0.077360  0.078682
beta       -0.502177 -0.579785 -0.596721

In [ ]: