Back to Community
Quantopian Lecture Series: Factor Analysis

Building portfolios of alpha factors allows us to more carefully monitor and analyze the source and consistency of our returns. In this lecture we cover the basics of determining whether an alpha factor is suitable for a long-short equity algorithm by analyzing it using Alphalens, an open source package that we developed specifically for this purpose. We discuss the various graphs and statistics that compose an Alphalens tear sheet and provide background on how they indicate factor quality.

All of our lectures are available at:
www.quantopian.com/lectures

Loading notebook preview...
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

5 responses

Hi,nice tools. I found alphalens code on github and there is an example folder in it which shows two examples. There's also .jpg pictures showing the result , I'm wondering how to save the results as a .jpg file? Expecially the dataframe

You can right click on any image while the notebook is loaded in your environment. You should see an option to "Save Image As". It will download as an image file to your computer.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Greetings from Sweden! First post!

I am relatively new to python and quantopian, so rookie-warning question here ;)

I am trying to customize a notebook template for factor analysis and I've been trying to take out pieces from the Alphalens IC tear sheet and Returns tear sheet to only plot the things I desire and by that save time by leaving out the things I do not need in these. However, I have not been successful in extracting the bars plot.

This line here:
al.plotting.plot_quantile_returns_bar(mean_return_by_q.apply(al.utils.rate_of_return, axis=0));

First, without messing with the cloned notebook and just running it, I get "TypeError: ('rate_of_return() takes exactly 2 arguments (1 given)', u'occurred at index 1D')" .

Since this post is 3 years old, I can imagine things could have changed code-wise.

I've checked out https://quantopian.github.io/alphalens/alphalens.html#alphalens.utils.rate_of_return. Where the rate_of_return function takes on two arguments,

"Parameters:
period_ret: pd.DataFrame
DataFrame containing returns values with column headings representing the return period.
base_period: string
The base period length used in the conversion It must follow pandas.Timedelta constructor format (e.g. ‘1 days’, ‘1D’, ‘30m’, ‘3h’, ‘1D1h’, etc)"

so I passed in:
al.plotting.plot_quantile_returns_bar(mean_return_by_q.apply(al.utils.rate_of_return(mean_return_by_q_daily, base_period='1D'), axis = 0)

Doing that, I get "AttributeError: 'DataFrame' object has no attribute 'name'"

So, does anyone know how one would go about extracting the barschart as it is calculated here in this notebook, with the growth rates for each time period, rather than just the means?

Any help is appreciated! :)

Loading notebook preview...

Hi,
Try to change to

al.plotting.plot_quantile_returns_bar(mean_return_by_q.apply(al.utils.rate_of_return, axis=0, args=('1D',) ))  

Hi,

my first post and already a question on how to implement code - sorry for this.

I read Trading Evolved and the book made me curious to jump into quantopian and formulate trading strategies. From the lecture I see the point in using alphalens and want to test on the following method on how to calculate momentum:

from scipy import stats

def momentum_score(ts):
x = range(self.window_length)
log_ts = np.log(ts)
slope, intercept, r_value, p_value, std_err = stats.linregress(x,log_ts)
annualized_slope = (np.power(np.exp(slope),252)-1)*100
score = annualized_slope *(r_value **2)
return score

Is there anyone who could show me how to implement this in the 'Class MyFactor(CustomFactor)'-structure?
I've tried different variations, but keep getting the ValueError: all the input array dimensions except for the concatenation axis must match exactly.

Thanks so much for your help!