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

In order to calculate BETA of a stock for a filter in my algorithm, I'm taking the covariance of the average of the last year of daily close-prices for my asset and SPY, then dividing by the variance of SPY. This is similar to the method found on StackOverflow:

covariance = numpy.cov(asset , SPY)[0][1]  
variance = numpy.var(asset)

beta = covariance / variance  

Does using the last year of daily close-prices as historical data work for finding covariance and variance? Or does this affect these values, and what alternative methods for calculating BETA exist?

2 responses

You can use anything you want! I think it is important to take a step back and realize what we mean when we talk about "beta." When we think of "beta" to the market we are really asking how sensitive is this security to market movement, or how much of this security's returns can be explained by the market. There is a fundamentally more basic understanding of "beta" is though. In short when we regress (linearly) some data against something else we model that regression as y = alpha + beta*x_1 + beta*x_2 + .... So "x" and "y" can be any sort of data, e.g. regress daily AAPL returns to the daily temperature in Cupertino, and find the "beta" of that relationship. So yes, using daily closing prices is totally acceptable. For a waaaaaaay better explanation see Delaney's lecture on Beta hedging from the lectures page.

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.

After using the described method, I received this error:

RuntimeWarning: Degrees of freedom <= 0 for slice  

Where does my issue occur? The related excerpt of the source code can be found below.

priceFrom1yearAgo = history(bar_count=365, frequency='1d', field='price')

if (np.cov(np.mean(priceFrom1yearAgo[stock]), np.mean(priceFrom1yearAgo[sid(8554)]))[0][1])/(np.mean(priceFrom1yearAgo[sid(8554)])) < 3:  

The purpose of the code is to average the prices of the asset and the index, SPY, from the last year and find the beta, which must be less than 3 for the stock to be selected. Where is the error occurring, and how do I solve it?