Alphalens is a Python package for performance analysis of alpha factors which can be used to create cross-sectional equity algos. Alpha factors express a predictive relationship between some given set of information and future returns. By applying this relationship to multiple stocks we can hope to generate an alpha signal and trade off of it. Developing a good alpha signal is challenging; so where can we (Quantopian) make things easier, and where do you (the quant) add the most value? We figured that a common set of tools for analyzing alpha factors would have a big impact.
By being able to analyze your factors in Research first you can spend less time writing and running backtests. Consequently, this allows for faster iteration of ideas, and a final algorithm that you can be confident in. Building a rigorous workflow with Alphalens will make your strategies more robust and less prone to overfitting - things we look for when evaluating algorithms.
We think that workflow looks something like this...
- Universe Selection: define the universe of tradeable components; the universe should be broad but have some degree of self similarity to enable extraction of relative value. It should also eliminate hard to trade or prohibited instruments.
- Single Alpha Factor Modeling: define and evaluate individual expressions which rank the cross section of equities in your universe.
- Alpha Combination: combine many single alphas into a final alpha which has stronger prediction power than the best single alpha. This is often due to the noise in each alpha being canceled out by noise in other alphas, allowing signal to come through.
- Risk Model: define and calculate the set of risk factors you want to use to constrain your portfolio.
- Portfolio Construction: implement a process which takes your final combined alpha and your risk model and produces a target portfolio that minimizes risk under your model.
- Execution: implement a trading process to transition the current portfolio (if any) to the target portfolio.
Alphalens is only one part of this process. It lives in step #2 - Single Alpha Factor Modeling. The main function of Alphalens is to surface the most relevant statistics and plots about a single alpha factor. This information can tell you if the alpha factor you found is predictive; whether you have found an "edge." These statistics cover:
- Returns Analysis
- Information Coefficient Analysis
- Turnover Analysis
- Sector Analysis
Using Alphalens in Quantopian Research is pretty simple:
1. Define your pipeline alpha factors
class Momentum(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 252
def compute(self, today, assets, out, close):
out[:] = close[-20] / close[0]
2. Run your pipeline
alphas = run_pipeline(alpha_pipe, start_date=start, end_date=end)
3. Get pricing data
assets = alphas.index.levels[1].unique()
pricing = get_pricing(assets, start, end + pd.Timedelta(days=30), fields="open_price")
4. Run the Alphalens factor tear sheet.
# Ingest and format data
factor_data = alphalens.utils.get_clean_factor_and_forward_returns(my_factor,
pricing,
quantiles=5,
groupby=ticker_sector,
groupby_labels=sector_names)
# Run analysis
alphalens.tears.create_full_tear_sheet(factor_data)
Open source
We think Alphalens fits in nicely with the rest of our open source ecosystem. Anyone in the world is now able to explore their idea with Alphalens, backtest their strategy with Zipline, and analyze the results with Pyfolio. We wanted to create a product that was platform agnostic, accessible to everyone, and scalable. This is incredibly important because we want anyone who contributes to Alphalens to have ownership over the project. Over the next few months we expect to uncover bugs and merge contributions. Alphalens is in its infancy and we can't wait to see the kind of changes it will go through.
Enjoy,
James and Andrew
Resources
Here are some places to check out too:
- Alphalens Docs for an analysis of a professional alpha factor.
- Alphalens Github repo
- Example notebook from the repo to use anywhere
Get Started
Not sure where to start? The Alphalens tutorial is the best way to try out alphalens for the first time.