quantopian / alphalens

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

Using lower frequency data(monthly, quarterly,annually) for generating tear sheet #335

Closed Dgomzi closed 5 years ago

Dgomzi commented 5 years ago

use alphalens.tears.create_returns_tear_sheet with quarterly/annually dataseries

Since I have access to return data, I didn't use utils.get_clean_factor_and_forward_returns but created dataframe as suggested. I also set df.index.levels[0].freq = pd.DateOffset(months = 1)

df.head()

                          1M        3M       12M    factor  group  \
date       asset                                                    
2010-04-30 0028262 -0.087310  0.085591  0.599461  0.459896    3.0   
           0045614 -0.149729  0.027014  0.516753 -0.088869    8.0   
           0056650 -0.107866 -0.014220  0.395736 -0.970670    8.0   
           0096162 -0.136532 -0.080226  0.344415 -1.018054    6.0   
           0118271 -0.080590 -0.064945 -0.025323 -0.055454    7.0   

                    factor_quantile  
date       asset                     
2010-04-30 0028262              4.0  
           0045614              3.0  
           0056650              1.0  
           0096162              1.0  
           0118271              3.0  

But I am not getting graphs. Is there some way to generate quantile performance charts using alphalens using longer frequency dataseries

luca-s commented 5 years ago

Please have a look at get_clean_factor_and_forward_returns implementation, You will see that it computes forward returns, which you already have and then it calls get_clean_factor, that is what you need to call. In doing so, please make sure your forward returns are properly formatted (the format is documented in get_clean_factor function).

Once you are sure that your input data is properly formatted everything should work fine. If that's not the case please report the error you get together with a minimal example that is able to reproduce the error.

thanks.

Dgomzi commented 5 years ago

My forward returns column names are ('1M','3M','12M') respectively for monthly/quarterly/annual returns. Will this work? I can see in "get_clean_factor" documentation column names should follow pd.Timedelta format. When I try pd.Timedelta('1M') it returns Timedelta('0 days 00:01:00') i.e. 1 minute object

luca-s commented 5 years ago

When I try pd.Timedelta('1M') it returns Timedelta('0 days 00:01:00') i.e. 1 minute object

Exactly, that's the problem. 'M' refers to minute. Also, it wouldn't be a good a idea to specify the forward return period as number of months or years, since those don't represent a specific number of days. I would suggest you to use the exact number of trading days your returns represent (e.g. "252D" for a year or "21D" for a month). Then make sure to call infer_trading_calendar like here, so that Alphalens will be able to handle holidays, weekends and all the non-trading days correctly

luca-s commented 5 years ago

Feel free to re-open the issue if you still have problems with this

tongxinw commented 3 years ago

Hi, I run into similar problems here as I would like to analyze 1 month forward returns at month ends. I used '21D' as the forwarding return name, and it works well. However, may I ask how you solve the freq issue if you have any? I have a month-end price dataset, and the df.index.levels[0].freq = M as I have month-end dates. I don't think using Bday=21 would solve this problem as adding 21 business days does not always return month ends.