Alphalens and Pyfolio integration

Alphalens can simulate the performance of a portfolio where the factor values are use to weight stocks. Once the portfolio is built, it can be analyzed by Pyfolio. For details on how this portfolio is built see:

  • alphalens.performance.factor_returns
  • alphalens.performance.cumulative_returns
  • alphalens.performance.create_pyfolio_input
In [ ]:
%pylab inline --no-import-all
import alphalens
import pyfolio
import pandas as pd
import numpy as np
import datetime

First load some stocks data

In [2]:
tickers = [ 'ACN', 'ATVI', 'ADBE', 'AMD', 'AKAM', 'ADS', 'GOOGL', 'GOOG', 'APH', 'ADI', 'ANSS', 'AAPL',
'AVGO', 'CA', 'CDNS', 'CSCO', 'CTXS', 'CTSH', 'GLW', 'CSRA', 'DXC', 'EBAY', 'EA', 'FFIV', 'FB',
'FLIR', 'IT', 'GPN', 'HRS', 'HPE', 'HPQ', 'INTC', 'IBM', 'INTU', 'JNPR', 'KLAC', 'LRCX', 'MA', 'MCHP',
'MSFT', 'MSI', 'NTAP', 'NFLX', 'NVDA', 'ORCL', 'PAYX', 'PYPL', 'QRVO', 'QCOM', 'RHT', 'CRM', 'STX',
'AMG', 'AFL', 'ALL', 'AXP', 'AIG', 'AMP', 'AON', 'AJG', 'AIZ', 'BAC', 'BK', 'BBT', 'BRK.B', 'BLK', 'HRB',
'BHF', 'COF', 'CBOE', 'SCHW', 'CB', 'CINF', 'C', 'CFG', 'CME', 'CMA', 'DFS', 'ETFC', 'RE', 'FITB', 'BEN',
'GS', 'HIG', 'HBAN', 'ICE', 'IVZ', 'JPM', 'KEY', 'LUK', 'LNC', 'L', 'MTB', 'MMC', 'MET', 'MCO', 'MS',
'NDAQ', 'NAVI', 'NTRS', 'PBCT', 'PNC', 'PFG', 'PGR', 'PRU', 'RJF', 'RF', 'SPGI', 'STT', 'STI', 'SYF', 'TROW',
'ABT', 'ABBV', 'AET', 'A', 'ALXN', 'ALGN', 'AGN', 'ABC', 'AMGN', 'ANTM', 'BCR', 'BAX', 'BDX', 'BIIB', 'BSX',
'BMY', 'CAH', 'CELG', 'CNC', 'CERN', 'CI', 'COO', 'DHR', 'DVA', 'XRAY', 'EW', 'EVHC', 'ESRX', 'GILD', 'HCA',
'HSIC', 'HOLX', 'HUM', 'IDXX', 'ILMN', 'INCY', 'ISRG', 'IQV', 'JNJ', 'LH', 'LLY', 'MCK', 'MDT', 'MRK', 'MTD',
'MYL', 'PDCO', 'PKI', 'PRGO', 'PFE', 'DGX', 'REGN', 'RMD', 'SYK', 'TMO', 'UNH', 'UHS', 'VAR', 'VRTX', 'WAT',
'MMM', 'AYI', 'ALK', 'ALLE', 'AAL', 'AME', 'AOS', 'ARNC', 'BA', 'CHRW', 'CAT', 'CTAS', 'CSX', 'CMI', 'DE',
'DAL', 'DOV', 'ETN', 'EMR', 'EFX', 'EXPD', 'FAST', 'FDX', 'FLS', 'FLR', 'FTV', 'FBHS', 'GD', 'GE', 'GWW',
'HON', 'INFO', 'ITW', 'IR', 'JEC', 'JBHT', 'JCI', 'KSU', 'LLL', 'LMT', 'MAS', 'NLSN', 'NSC', 'NOC', 'PCAR',
'PH', 'PNR', 'PWR', 'RTN', 'RSG', 'RHI', 'ROK', 'COL', 'ROP', 'LUV', 'SRCL', 'TXT', 'TDG', 'UNP', 'UAL',
'AES', 'LNT', 'AEE', 'AEP', 'AWK', 'CNP', 'CMS', 'ED', 'D', 'DTE', 'DUK', 'EIX', 'ETR', 'ES', 'EXC']
In [ ]:
import pandas_datareader.data as web
pan = web.DataReader(tickers, "google", datetime.datetime(2015, 1, 1),  datetime.datetime(2017, 1, 1))
In [4]:
pan = pan.transpose(2,1,0)

We'll compute a simple mean reversion factor looking at recent stocks performance: stocks that performed well in the last 5 days will have high rank and vice versa.

In [5]:
factor = pan.loc[:,:,'Open']
factor = -factor.pct_change(5)

factor = factor.stack()
factor.index = factor.index.set_names(['date', 'asset'])

The pricing data passed to alphalens should contain the entry price for the assets so it must reflect the next available price after a factor value was observed at a given timestamp. Those prices must not be used in the calculation of the factor values for that time. Always double check to ensure you are not introducing lookahead bias to your study.

The pricing data must also contain the exit price for the assets, for period 1 the price at the next timestamp will be used, for period 2 the price after 2 timestats will be used and so on.

There are no restrinctions/assumptions on the time frequencies a factor should be computed at and neither on the specific time a factor should be traded (trading at the open vs trading at the close vs intraday trading), it is only required that factor and price DataFrames are properly aligned given the rules above.

In our example, before the trading starts every day, we observe yesterday factor values. The price we pass to alphalens is the next available price after that factor observation: the daily open price that will be used as assets entry price. Also, we are not adding additional prices so the assets exit price will be the following days open prices (how many days depends on 'periods' argument). The retuns computed by Alphalens will therefore based on assets open prices.

In [6]:
pricing = pan.loc[:,:,'Open'].iloc[1:]

Prepare data and run Alphalens

Pyfolio wants timezone set to UTC otherwise it refuses to work

In [7]:
pricing.index = pricing.index.tz_localize('UTC')
In [8]:
factor = factor.unstack()
factor.index = factor.index.tz_localize('UTC')
factor = factor.stack()
In [9]:
factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factor,
                                                                   pricing,
                                                                   periods=(1, 3, 5),
                                                                   quantiles=5,
                                                                   bins=None)
Dropped 1.1% entries from factor data: 1.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 [10]:
alphalens.tears.create_summary_tear_sheet(factor_data)
Quantiles Statistics
min max mean std count count %
factor_quantile
1 -63.109756 0.075731 -0.044736 0.410758 24032 20.210923
2 -0.113547 0.096591 -0.013790 0.020938 23681 19.915732
3 -0.080349 0.110527 -0.001656 0.019788 23644 19.884615
4 -0.047502 0.127588 0.010392 0.020289 23673 19.909004
5 -0.028090 0.846154 0.036673 0.034158 23876 20.079727
Returns Analysis
1D 3D 5D
Ann. alpha 0.501 0.151 0.073
beta 0.188 -0.022 0.078
Mean Period Wise Return Top Quantile (bps) 4.379 1.847 1.800
Mean Period Wise Return Bottom Quantile (bps) -2.226 -1.390 -1.259
Mean Period Wise Spread (bps) 6.651 3.249 3.059
Information Analysis
1D 3D 5D
IC Mean 0.013 0.009 0.015
IC Std. 0.174 0.169 0.170
Risk-Adjusted IC 0.077 0.051 0.086
t-stat(IC) 1.723 1.139 1.903
p-value(IC) 0.086 0.255 0.058
IC Skew 0.040 0.178 0.065
IC Kurtosis 0.297 0.311 0.388
Turnover Analysis
1D 3D 5D
Quantile 1 Mean Turnover 0.345 0.601 0.789
Quantile 2 Mean Turnover 0.605 0.744 0.801
Quantile 3 Mean Turnover 0.649 0.764 0.786
Quantile 4 Mean Turnover 0.604 0.746 0.798
Quantile 5 Mean Turnover 0.349 0.597 0.780
1D 3D 5D
Mean Factor Rank Autocorrelation 0.753 0.361 -0.014
<matplotlib.figure.Figure at 0x7fddeb2cacc0>

Prepare data for Pyfolio

We can see in Alphalens analysis that quantiles 1 and 5 are the most predictive so we'll build a portfolio data using only those quantiles.

In [11]:
pf_returns, pf_positions, pf_benchmark = \
    alphalens.performance.create_pyfolio_input(factor_data,
                                               period='1D',
                                               capital=100000,
                                               long_short=True,
                                               group_neutral=False,
                                               equal_weight=True,
                                               quantiles=[1,5],
                                               groups=None,
                                               benchmark_period='1D')

Now that we have prepared the data we can run Pyfolio functions

In [12]:
pyfolio.tears.create_full_tear_sheet(pf_returns,
                                     positions=pf_positions,
                                     benchmark_rets=pf_benchmark)
Start date2015-01-09
End date2016-12-23
Total months24
Backtest
Annual return 8.3%
Cumulative returns 17.7%
Annual volatility 6.4%
Sharpe ratio 1.28
Calmar ratio 1.16
Stability 0.69
Max drawdown -7.2%
Omega ratio 1.28
Sortino ratio 2.15
Skew 1.61
Kurtosis 11.79
Tail ratio 1.01
Daily value at risk -0.8%
Gross leverage 0.97
Alpha 0.07
Beta 0.11
Worst drawdown periods Net drawdown in % Peak date Valley date Recovery date Duration
0 7.17 2016-03-31 2016-11-15 NaT NaN
1 3.24 2015-02-05 2015-03-06 2015-04-08 45
2 3.15 2016-01-05 2016-01-20 2016-02-15 30
3 2.58 2015-09-04 2015-09-29 2015-10-22 35
4 1.99 2015-07-06 2015-08-24 2015-08-25 37
/home/lucasca/.local/lib/python3.5/site-packages/numpy/lib/function_base.py:4291: RuntimeWarning: Invalid value encountered in percentile
  interpolation=interpolation)
Stress Events mean min max
Fall2015 0.02% -1.07% 3.31%
New Normal 0.03% -1.15% 3.31%
Top 10 long positions of all time max
A 1.04%
AAL 1.04%
AAPL 1.04%
ABBV 1.04%
ABC 1.04%
ACN 1.04%
ADBE 1.04%
ADI 1.04%
ADS 1.04%
AEE 1.04%
Top 10 short positions of all time max
A -1.04%
AAL -1.04%
AAPL -1.04%
ABBV -1.04%
ABC -1.04%
ABT -1.04%
ACN -1.04%
ADBE -1.04%
ADI -1.04%
ADS -1.04%
Top 10 positions of all time max
A 1.04%
AAL 1.04%
AAPL 1.04%
ABBV 1.04%
ABC 1.04%
ABT 1.04%
ACN 1.04%
ADBE 1.04%
ADI 1.04%
ADS 1.04%
All positions ever held max
A 1.04%
GOOG 1.04%
JNPR 1.04%
JPM 1.04%
KEY 1.04%
KLAC 1.04%
KSU 1.04%
L 1.04%
LH 1.04%
LLL 1.04%
LLY 1.04%
LMT 1.04%
LNC 1.04%
LNT 1.04%
LRCX 1.04%
LUK 1.04%
LUV 1.04%
MA 1.04%
MAS 1.04%
MCHP 1.04%
MCK 1.04%
MCO 1.04%
MDT 1.04%
MET 1.04%
MMC 1.04%
MMM 1.04%
MRK 1.04%
MS 1.04%
MSFT 1.04%
JEC 1.04%
JCI 1.04%
JBHT 1.04%
HUM 1.04%
GPN 1.04%
GS 1.04%
GWW 1.04%
HBAN 1.04%
HCA 1.04%
HIG 1.04%
HOLX 1.04%
HON 1.04%
HPQ 1.04%
HRB 1.04%
HRS 1.04%
HSIC 1.04%
IBM 1.04%
IVZ 1.04%
ICE 1.04%
IDXX 1.04%
ILMN 1.04%
INCY 1.04%
INFO 1.04%
INTC 1.04%
INTU 1.04%
IQV 1.04%
IR 1.04%
ISRG 1.04%
IT 1.04%
ITW 1.04%
MSI 1.04%
MTB 1.04%
MTD 1.04%
STX 1.04%
RHT 1.04%
RJF 1.04%
RMD 1.04%
ROK 1.04%
ROP 1.04%
RSG 1.04%
RTN 1.04%
SCHW 1.04%
SPGI 1.04%
SRCL 1.04%
STI 1.04%
STT 1.04%
SYF 1.04%
RF 1.04%
SYK 1.04%
TDG 1.04%
TMO 1.04%
TROW 1.04%
TXT 1.04%
UAL 1.04%
UHS 1.04%
UNH 1.04%
UNP 1.04%
VAR 1.04%
VRTX 1.04%
WAT 1.04%
RHI 1.04%
REGN 1.04%
MYL 1.04%
PDCO 1.04%
NAVI 1.04%
NDAQ 1.04%
NFLX 1.04%
NLSN 1.04%
NOC 1.04%
NSC 1.04%
NTAP 1.04%
NTRS 1.04%
NVDA 1.04%
ORCL 1.04%
PAYX 1.04%
PCAR 1.04%
PFE 1.04%
RE 1.04%
PFG 1.04%
PGR 1.04%
PH 1.04%
PKI 1.04%
PNC 1.04%
PNR 1.04%
PRGO 1.04%
PRU 1.04%
PWR 1.04%
PYPL 1.04%
QCOM 1.04%
QRVO 1.04%
AAL 1.04%
GOOGL 1.04%
GLW 1.04%
BEN 1.04%
APH 1.04%
ARNC 1.04%
ATVI 1.04%
AVGO 1.04%
AWK 1.04%
AXP 1.04%
AYI 1.04%
BA 1.04%
BAC 1.04%
BAX 1.04%
BBT 1.04%
BDX 1.04%
BHF 1.04%
AON 1.04%
BIIB 1.04%
BK 1.04%
BLK 1.04%
BMY 1.04%
BSX 1.04%
C 1.04%
CA 1.04%
CAT 1.04%
CB 1.04%
CBOE 1.04%
CDNS 1.04%
CELG 1.04%
AOS 1.04%
ANTM 1.04%
CFG 1.04%
AFL 1.04%
AAPL 1.04%
ABBV 1.04%
ABC 1.04%
ABT 1.04%
ACN 1.04%
ADBE 1.04%
ADI 1.04%
ADS 1.04%
AEE 1.04%
AEP 1.04%
AES 1.04%
AET 1.04%
AGN 1.04%
ANSS 1.04%
AIG 1.04%
AIZ 1.04%
AKAM 1.04%
ALK 1.04%
ALL 1.04%
ALLE 1.04%
ALXN 1.04%
AMD 1.04%
AME 1.04%
AMG 1.04%
AMGN 1.04%
AMP 1.04%
GE 1.04%
CERN 1.04%
CHRW 1.04%
EVHC 1.04%
DXC 1.04%
EA 1.04%
EBAY 1.04%
ED 1.04%
EFX 1.04%
EIX 1.04%
EMR 1.04%
ES 1.04%
ESRX 1.04%
ETFC 1.04%
ETN 1.04%
ETR 1.04%
EW 1.04%
DUK 1.04%
EXC 1.04%
EXPD 1.04%
FAST 1.04%
FB 1.04%
FBHS 1.04%
FDX 1.04%
FFIV 1.04%
FITB 1.04%
FLIR 1.04%
FLR 1.04%
FLS 1.04%
GD 1.04%
CI 1.04%
DVA 1.04%
CSCO 1.04%
DTE 1.04%
CINF 1.04%
CMA 1.04%
CME 1.04%
CMI 1.04%
CMS 1.04%
CNC 1.04%
CNP 1.04%
COF 1.04%
COL 1.04%
COO 1.04%
CRM 1.04%
XRAY 1.04%
DGX 1.04%
DOV 1.04%
CSX 1.04%
CTAS 1.04%
CTSH 1.04%
CTXS 1.04%
D 1.04%
DAL 1.04%
DHR 1.04%
DE 1.04%
DFS 1.04%
AJG 1.04%
FTV 1.04%
HPE 1.04%
CAH 1.04%
PBCT 1.04%
CSRA 1.04%
JNJ 1.04%