Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Beginner - Trying to access historical returns of particular securities to perform OLS Regression

I need some help. I am trying to understand exactly how to access the historical returns of one of the individual securities and perform a regression of that against the historical returns of a set of the other securities. For example, PFF against AGG and CSJ.

5 responses

Hi Ash,

It sounds like a good place to start would be the Linear Regression lecture in the Quantopian Lecture Series. The lecture demonstrates how to do this in research. In an algorithm, you'll want to use data.history() instead of get_pricing() (in research only) similar to how it's being used in the example you shared. The rest of the calculation should follow the lecture pretty closely!

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.

Hi Jamie, I tried this, but am still running into 2 problems.

  1. When I put the call to "linreg" in my code on line 55 of the code I get a runtime error.
  2. How do I get into to the Research Environment to be able to see the plots and use the "put" function?

Hi Ash,

linreg() was a function defined in the lecture notebook, it's not built-in to Python. My pointer to the linear regression lecture was to point you to how that function is defined (see the second cell of the notebook). To get to research, go to "My Notebooks" under "My Code" in the nav bar. Check out this tutorial on how to use IPython notebooks (what's used in research).

Hi Jamie, I understand that linreg() is not a built in Python function. I added the call to it as well as the definition of the function to my code. But when I called it I kept getting a runtime error.

Hi Ash,

Sorry about that! It looks like summary() is not whitelisted in the IDE. Check out this example by Delaney that uses linear regression (see lines 39-43 specifically). This should help.