Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
RollingLinearRegressionOfReturns Help

Can someone clarify what the inputs are for RollingLinearRegressionOfReturns?

Most interested in returns_length and regression_length. I'm looking to calculate 1-year betas and want to know how these 2 parameters affect the calculation.

I'm assuming returns_length would be the 1 year, but I'm not sure how regression_length affects it. Would it also be 1 year if I want a single beta value?

7 responses

Hi Mark,

The regression_length is the number of days over which the regression is computed. If you want the beta value over the last year, you should set this value to something like 252.

The returns_length is the number of days over which your returns are computed. If you want day-to-day returns, you should use a returns_length of 2. If you want rolling 5-day returns, you should use a returns_length of 5.

You should get a single beta value regardless of what you set the parameters to be.

Does this help?

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.

Thank you!

That helps a lot

Hi Jamie,

Noob question, but why would you set the regression_length to 252 for the past year. Wouldn't it be 365?

Thanks for the help,
Thomas

Also, I'm trying to calculate the 100 day Beta like this, but it's not returning to me a floating point:

regression = RollingLinearRegressionOfReturns(  
    target=symbol('SPY'),  
    returns_length=2,  
    regression_length=100  
)

beta = regression.beta  
print(beta)

Instead it returns this in the log:

RecarrayField((RollingLinearRegressionOfReturns((Returns((USEquityPricing.close::float64,), window_length=2), Slice(Returns, column=Equity(8554 [SPY]))), window_length=100),), window_length=0)

Am I doing this correctly?

Hi Thomas,

You would set the length to 252 for the past year because there are ~252 business days in a calendar year. We do not have data outside of market hours.

For the second question, Pipeline factors aren't computed until you run your pipeline. If you haven't seen it already, I would recommend going through the Pipeline Tutorial. Essentially, you will need to add your regression factor to a pipeline, run the pipeline, and then look at the output.

Let me know if this helps.

Hey Jamie,

Thanks so much for your reply. I got it working calculating the beta using factors, but my question is, when I'm looping through stocks and trying to access the beta value, I'm not able to do something like this:

for stock in context.security_list:  
    stock.beta

How should I loop through stocks while also being able to access the respective beta values? Is this the only way to do it? I feel like it's kinda messy :/

for stock in context.security_list:  
    context.output.loc[stock].beta

Thanks for the help!

Hi everyone,

Thomas, I think the answer to your question is below. In this case, I was using 1/beta to weight the order target; however, the idea of the loop is the same:

    for security in context.security_list:  
        if data.can_trade(security):  
            order_target_percent(security,  (1/context.output.get_value(security,'beta')))  

You just have to be sure to attach the beta to the Pipeline when you build it. Does this make sense?

Does anyone know if its possible to offset this function? That is, I would like to do a rolling linear regression starting at year -2 and going to year -1. Thank you in advance for the help!

Eric