Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why the Mean Period Wise Return from Alphalens brings means return differents as if you calculate by yourself??

Hello guys, I was trying to understand better the mean period wise return by quantiles calcutated with alphalens, because when I try to calculate it by my self using mean () function, the result give something totally different:

In the jupyter notebook you will find a boolean factor (True, False) which will only provide two quantiles, so in this order the factor quantile 2 is going to be True, or in others words the launch of my signal to go long and the factor quantile 1 is going to be the no launch of my signal to go long , when alphalens create_full_tears runs it give differents result in the period wise return for each factor quantile in compare with the manual calculation.

Manual calculation statement:

merged_data[merged_data["factor_quantile"]==2].mean()

On the jupyter notebook attached you will see the difference in the results.

2 responses

@Rene Ordosgoitia Very good question.

The first issue is the "mean period wise return by quantiles" is the mean of the daily mean returns. It isn't simply the mean of all the returns. Why is this? Alphalens make several assumptions about how the factor values will be used and traded. The calculations are designed around these assumptions.

  • There may be a different number of stocks in each quantile each day
  • One will trade all the stocks in each quantile and equally weight each stock. Therefore if 20 stocks are in quantile x on day 1, each would get a weight of 1/20. If 100 stocks are in quantile x on day 2, each would get a weight of 1/100.
  • The net return on a given day for a given quantile is the average return of the stocks in that quantile for that day.
  • The net return over several days for a given quantile is the compounded return of the average of the daily returns.

Therefore, to calculate the "mean period wise return by quantiles" one first needs to find the mean daily return, and then second, find the mean of those daily returns. Something like this for starters (which happens to be almost exactly how Alphalens actually does the calculations)

# First create a 'grouper' to group by date  
day_grouper = ['factor_quantile', merged_data.index.get_level_values('date')]

# Now use that grouper and get the mean of each returns column  
daily_means = merged_data.groupby(day_grouper)['1D','2D','3D','4D','5D'].mean()

# Next, create a 'grouper' to group by our factor quantiles  
quantile_grouper = [daily_means.index.get_level_values('factor_quantile')]

# Now use that grouper and get the mean of the daily means by factor quantile  
quantile_means = daily_means.groupby(quantile_grouper).mean()


That will calculate the "n day total mean return" for the given period. However (one more step), in order to make comparisons between different periods easier, Alphalens 'normalizes' all the values to the compounded daily return. In other words, the daily return, which if compounded over n days, will be the n day total mean return. This is then an apples to apples' comparison of the different n day returns. Something like this using pseudocode (which again is similar to how Alphalens actually does the calculations)

quantile_daily_returns['nD'] = quantile_total_returns['nD'].add(1).pow(1/n).sub(1).mul(10000).round(3)

That's it. Take the mean of the daily mean returns and then calculate the compounded daily rate of return.

However (there's always something else), this won't match the results from the default create_full_tear_sheet method. Why is that? The create_full_tear_sheet method, by default, assumes a long short portfolio. It assumes one will short stocks in the lower quantiles and long the stocks in the upper ones. The mean returns we calculated above just took the raw returns and didn't account for long and short. The short returns would need to be inverted. Won't go into that calculation here. Rather, there is a long_short parameter to force the create_full_tear_sheet method to assume an all long portfolio. Setting this parameter to False will result in an all long portfolio with returns that match our returns calculated above.

create_full_tear_sheet(merged_data, long_short=False)

It's generally a good idea to start a factor analysis with an all long portfolio (ie specifically set 'long_short=False'). I'd recommend it. The long short portfolio which Alphalens models is split along the mean factor values each day. It assumes one will long the upper half and short the lower half. However, this may not always make sense. Often only the highest or lowest quantiles of a factor show negative returns. It may make sense to then only short those quantiles and not arbitrarily short the entire bottom half. If long_short is set to True or left to the default, it hides what is actually going on.

Attached is a notebook showing the manual calculations for getting the "mean period wise return by quantiles" and how they match those returned by the tear sheet.

Good luck.

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.

Dan your explanation was insane, really ilustrative of each step, now I have many doubts about why alphalens "normalize" the results and not takes the true value but I will research it.