Notebook
In [75]:
import numpy as np
import pandas as pd
from quantopian.research import run_pipeline
from quantopian.pipeline import Pipeline
from quantopian.pipeline.factors import CustomFactor
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.data.quandl import cboe_vix as vix

from time import time

start = '2004-01-01'
end = '2004-05-03'
In [78]:
import alphalens as al
universe = QTradableStocksUS()
In [79]:
class VIX(CustomFactor):  
    inputs = [vix.vix_close]  
    window_length = 1  
    def compute(self, today, assets, out, v_close):  
        out[:] = v_close


pipe = Pipeline(  
    columns={  
        'Vix' : VIX(),
        'Close': USEquityPricing.close.latest,
    },
    screen=universe)  

factor_data = run_pipeline(pipe, start, end) 
factor_data_clean = factor_data['Vix'].dropna()

asset_list = factor_data_clean.index.levels[1]
prices = get_pricing(asset_list, start_date=start, end_date=end, fields='close_price')
prices.head()
Out[79]:
Equity(2 [ARNC]) Equity(24 [AAPL]) Equity(31 [ABAX]) Equity(41 [ARCB]) Equity(60 [ABS]) Equity(62 [ABT]) Equity(64 [GOLD]) Equity(67 [ADSK]) Equity(76 [TAP]) Equity(85 [ACF]) ... Equity(25920 [MAR]) Equity(26169 [SHLD]) Equity(26204 [FHN]) Equity(26259 [HNI]) Equity(26307 [LAUR]) Equity(26350 [PRX]) Equity(26434 [MFE]) Equity(26437 [PDCO]) Equity(26439 [VVI]) Equity(26470 [IAC])
2004-01-02 00:00:00+00:00 37.335 21.18 16.66 31.619 22.356 43.184 23.00 24.45 55.538 15.90 ... 46.028 23.00 43.017 43.146 29.63 64.129 14.95 63.05 25.368 33.31
2004-01-05 00:00:00+00:00 38.530 22.17 15.59 31.908 22.838 43.231 23.35 24.72 56.086 16.05 ... 46.208 29.10 42.967 43.246 30.29 66.210 15.99 61.27 25.797 34.00
2004-01-06 00:00:00+00:00 38.281 22.10 17.13 31.430 22.808 42.935 22.94 24.58 55.408 16.16 ... 46.088 30.00 43.037 42.977 30.23 65.489 16.16 60.33 25.767 33.22
2004-01-07 00:00:00+00:00 38.102 22.59 18.10 31.728 23.093 43.258 22.71 24.74 54.421 16.20 ... 46.058 29.15 43.116 42.837 31.42 64.360 15.84 60.76 25.657 33.01
2004-01-08 00:00:00+00:00 38.540 23.41 18.25 31.818 23.192 42.020 22.63 25.05 53.753 16.72 ... 46.537 30.26 42.561 43.027 31.59 63.840 16.05 60.35 25.697 32.68

5 rows × 1757 columns

In [81]:
periods = (1, 2, 5)
factor_data = al.utils.get_clean_factor_and_forward_returns(factor=factor_data_clean,
                                                            prices=prices,
                                                            periods=periods, max_loss=0)

ValueErrorTraceback (most recent call last)
<ipython-input-81-4c3b7a9e2a3c> in <module>()
      2 factor_data = al.utils.get_clean_factor_and_forward_returns(factor=factor_data_clean,
      3                                                             prices=prices,
----> 4                                                             periods=periods, max_loss=0)

/usr/local/lib/python2.7/dist-packages/alphalens/utils.pyc in get_clean_factor_and_forward_returns(factor, prices, groupby, binning_by_group, quantiles, bins, periods, filter_zscore, groupby_labels, max_loss, zero_aware, cumulative_returns)
    797                                    quantiles=quantiles, bins=bins,
    798                                    binning_by_group=binning_by_group,
--> 799                                    max_loss=max_loss, zero_aware=zero_aware)
    800 
    801     return factor_data

/usr/local/lib/python2.7/dist-packages/alphalens/utils.pyc in get_clean_factor(factor, forward_returns, groupby, binning_by_group, quantiles, bins, groupby_labels, max_loss, zero_aware)
    604         binning_by_group,
    605         no_raise,
--> 606         zero_aware
    607     )
    608 

/usr/local/lib/python2.7/dist-packages/alphalens/utils.pyc in dec(*args, **kwargs)
     76         except ValueError as e:
     77             if 'Bin edges must be unique' in str(e):
---> 78                 rethrow(e, message)
     79             raise
     80     return dec

/usr/local/lib/python2.7/dist-packages/alphalens/utils.pyc in rethrow(exception, additional_message)
     43     else:
     44         e.args = (e.args[0] + m,) + e.args[1:]
---> 45     raise e
     46 
     47 

ValueError: Bin edges must be unique: array([ 18.22,  18.22,  18.22,  18.22,  18.22,  18.22])

    An error occurred while computing bins/quantiles on the input provided.
    This usually happens when the input contains too many identical
    values and they span more than one quantile. The quantiles are choosen
    to have the same number of records each, but the same value cannot span
    multiple quantiles. Possible workarounds are:
    1 - Decrease the number of quantiles
    2 - Specify a custom quantiles range, e.g. [0, .50, .75, 1.] to get unequal
        number of records per quantile
    3 - Use 'bins' option instead of 'quantiles', 'bins' chooses the
        buckets to be evenly spaced according to the values themselves, while
        'quantiles' forces the buckets to have the same number of records.
    4 - for factors with discrete values use the 'bins' option with custom
        ranges and create a range for each discrete value
    Please see utils.get_clean_factor_and_forward_returns documentation for
    full documentation of 'bins' and 'quantiles' options.

In [ ]:
start_timer = time()
al.tears.create_returns_tear_sheet(factor_data, by_group=False);
end_timer = time()
print "Time to run tearsheet %.2f secs" % (end_timer - start_timer)
In [ ]: