quantopian / alphalens

Performance analysis of predictive (alpha) stock factors
http://quantopian.github.io/alphalens
Apache License 2.0
3.26k stars 1.13k forks source link

Double sort #291

Closed wj-c closed 6 years ago

wj-c commented 6 years ago

Hi, I'm new to this great project.

Right now the get_clean_factor_and_forward_returns only accepts the values for a single alpha factor, is it possible to apply sorting on multiple factors?

For example, first sort stocks by firm size into 5 groups. Then within each size group, sort firms based on book-to-market ratio also into 5 groups. In total, we get 5*5 portfolios.

Is there anyway that I can do this?

Thanks in advance!

luca-s commented 6 years ago

You can do your multi-factor computation before calling Alphalens and then pass the result of the combination to Alphalens. I believe this approach is more general as there is no unique and obvious way to combine multiple factors together, so the task is better left to the user.

You might find this issue interesting https://github.com/quantopian/alphalens/issues/219

wj-c commented 6 years ago

Thanks a lot!

But there is one more problem relating to this.

When I do multi-factor computation myself, for example, I may get two portfolios: size1_BM5 (which is the B/M ratio 5th quantile in the 1th quantile of firm size) and size5_BM1.

I want to treat these two portfolio differently, so I can not name them simply by their factor quantile number (add their quantile number together results in 6, which is the same for the two). I need to name them by characters like size1_BM5. But this method has problems when Alphalens computes the performance of the strategy, because Alphalens decide the number of quantiles according to the largest number in the column "factor_quantile".

Is there any advice on this?

luca-s commented 6 years ago

One solution would be to use contiguous numbers instead of strings (size1_BM1, size1_BM2 .... size5_BM4, size5_BM5), for example 1, 2, ..., 24, 25. Then you can use the 'bins' option (in get_clean_factor_and_forward_returns) with custom ranges to group the numbers as you like. For example, if you like to group them 3 by 3:

bins = [0, 3.5, 6.5, 9.5, 12.5, 15.5, 18.5, 21.5, 24.5, 26]

wj-c commented 6 years ago

Thanks for the help!