Notebook
In [21]:
import numpy as np
import pandas as pd
import alphalens
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 Q1500US

class Sector(Sector):
    window_safe = True
    
class LowVol(CustomFactor):
    inputs = [Returns(window_length=2)]
    window_length = 25
    
    def compute(self, today, assets, out, close):
        out[:] = -np.nanstd(close, axis=0)

universe = Q1500US()

pipe_low_vol = Pipeline(
    columns={
        'LowVol' : LowVol(mask=universe),
        'Sector': Sector(mask=universe),
    },
    screen=universe
)

results = run_pipeline(pipe_low_vol, '2017-05-31', '2017-05-31')

assets = results.index.levels[1].unique()

pricing = get_pricing(assets, start_date='2017-05-31', end_date='2017-06-30', fields='open_price')

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

alphalens.tears.create_full_tear_sheet(factor_data)
Quantiles Statistics
min max mean std count count %
factor_quantile
1 -0.112409 -0.023403 -0.034698 0.012328 298 20.04035
2 -0.023381 -0.016728 -0.019560 0.001906 297 19.97310
3 -0.016727 -0.012897 -0.014745 0.001072 297 19.97310
4 -0.012895 -0.009531 -0.011226 0.001032 297 19.97310
5 -0.009524 -0.000643 -0.007305 0.001755 298 20.04035

ValueErrorTraceback (most recent call last)
<ipython-input-21-fbe1299137eb> in <module>()
     41                                                                    periods=(1,5,10))
     42 
---> 43 alphalens.tears.create_full_tear_sheet(factor_data)

/usr/local/lib/python2.7/dist-packages/alphalens/plotting.pyc in call_w_context(*args, **kwargs)
     39             with plotting_context(), axes_style():
     40                 sns.despine(left=True)
---> 41                 return func(*args, **kwargs)
     42         else:
     43             return func(*args, **kwargs)

/usr/local/lib/python2.7/dist-packages/alphalens/tears.pyc in create_full_tear_sheet(factor_data, long_short, group_adjust, by_group)
    348 
    349     plotting.plot_quantile_statistics_table(factor_data)
--> 350     create_returns_tear_sheet(factor_data, long_short, by_group)
    351     create_information_tear_sheet(factor_data, group_adjust, by_group)
    352     create_turnover_tear_sheet(factor_data)

/usr/local/lib/python2.7/dist-packages/alphalens/plotting.pyc in call_w_context(*args, **kwargs)
     39             with plotting_context(), axes_style():
     40                 sns.despine(left=True)
---> 41                 return func(*args, **kwargs)
     42         else:
     43             return func(*args, **kwargs)

/usr/local/lib/python2.7/dist-packages/alphalens/tears.pyc in create_returns_tear_sheet(factor_data, long_short, by_group)
    164     compstd_quant_daily = std_quant_daily.apply(utils.rate_of_return, axis=0)
    165 
--> 166     alpha_beta = perf.factor_alpha_beta(factor_data)
    167 
    168     mean_ret_spread_quant, std_spread_quant = perf.compute_mean_returns_spread(

/usr/local/lib/python2.7/dist-packages/alphalens/performance.pyc in factor_alpha_beta(factor_data)
    209 
    210         reg_fit = OLS(y, x).fit()
--> 211         alpha, beta = reg_fit.params
    212 
    213         alpha_beta.loc['Ann. alpha', period] = (1 + alpha) ** (252.0/period) - 1

ValueError: need more than 1 value to unpack
<matplotlib.figure.Figure at 0x7f20dbe32ad0>
In [ ]: