Quantopian has this package alphalens to do series of analysis on factors.

I decided to dig in the code and make sense of the analysis.

The question I have is: There are a lot of demean in the factors and factors returns, the argument is when you demean, the analysis is for long short portfolio and when you do not demean, you have a long only portfolio.

Can anyone explain why demean of the return gives analysis on long short portfolio?


With the factor values in Alphalens, what is specifically done is demeaning followed by division by the sum of the absolute values of all the demeaned factors. With some offhand notation, this is more like:

$$ \bar{F}_i =\frac{F_i - E[F]}{\sum_{n=1}^N |F_n|} $$

Where $F_i$ is the original factor value, $F$ is the set of all original factor values, and $\bar{F}_i$ is our new demeaned and compressed factor value. This new set of $\{\bar{F}_i\}_{i=1}^N$ is then used to construct a factor-weighted portfolio.

Our new factor values will now all be centered around the mean, split with about half of them positive and half of them negative. By using the new factor values to determine the weights of their corresponding securities in our portfolios, we create a de facto long-short portfolio. New factor values that are positive will correspond to positive weights (and hence are longed) and new factor values that are negative will correspond to negative weights (and hence are shorted). If you want to read a little more about how the specific function that carries out this "standardization" is defined, check out the docs here.

This demeaning and factor-weighting is primarily what makes it long-short.

  • $\begingroup$ Thanks. Your explanation of demean makes sense. Can you also explain why in mean_return_by_quantile() function, you demean the forward returns? I understand it is trying to eliminate the market effect, but why demeaned return represent the return for long short portfolio and not demeaned return represent the long only portfolio? $\endgroup$ – JOHN May 18 '17 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.