Please help me find the error in my calculation

The intention of the function below is to calcualte the beta of a security. 


I have used Investopedia to verify I'm calculating beta correctly.(trying to at least)


correlation*(STD_deviation_of_return_of_spy/STD_of_return_of_stock) = beta


The function below is my attemtp to calcualte beta: 

import numpy as np
from scipy import stats

def get_beta(self, security, period):
        if not self.Securities.ContainsKey(security):return 'no data'
        security_data = self.History([security], period, Resolution.Daily)
        spy_data = self.History(['SPY'],period,Resolution.Daily)
        if 'close' not in security_data:return 'no close'

        y = security_data['close'].pct_change().dropna().values

        x = spy_data['close'].pct_change().dropna().values
        slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
        return r_value * ((np.std(y))/(np.std(x)))

Can someone please tell me where my error is ? Thanks. 

Update Backtest


i always wanted to take a look at that stuff and play with that

so today i tried to give it chance......after 5 minutes of google investopedia says the opposite of your post???:

the other way around maybe.

For example, an investor wants to calculate the beta of Apple Inc. (AAPL) when compared to the SPDR S&P 500 ETF Trust (SPY). Based on data over the past five years, the correlation between AAPL and SPY is 0.83. AAPL has a standard deviation of returns of 23.42% and SPY has a standard deviation of returns of 32.21%.

Beta of AAPL = 0.83 x (0.2342 ÷ 0.3221) = 0.6035

i dont have any knowledge of beta calculation but i will look into it when i have time :)


from bottom in this article: alternatively scipy linear regression
from scipy import stats
slope, intercept, r_value, p_value, std_err = stats.linregress(X, y)


Yahoo Finance gives Facebook a Beta value of 0.58. Our regression model gives it a value of 0.5751 which when rounded off is 0.58.

As a bonus, I am also going to show how Scipy’s lingress method can be used to easily make a linear regression as well.


Thanks for the help albiet if you look at my code, my code does exactly what investopedia does. I admit I wrote the formula incorrectly in my explanation. But the code does exactly what investopedia says if you look at my original post. Despite this it's extremely off finviz's calculation. 


For example take: 



If you go on finviz the beta is 2:31


Look at what my beta is in my backtest around the same time. 0.7 (if you can please run the backtests go to logs and download)Am I using the wrong timeframe by using 90 days ? I'll probably end up emailing finviz support but I can't seem to figure out why my calculation is so far off. 


hm the link above

says something about monthly prices...

yahoo lists acer with beta of 2,8

and the link above says how yahoo calculate this


hmm i dont think its possible to get a value around 2.8 with 250 days and not even with 90.

to get a beta value around 2.8 with acer you would need around 20 values. maybe it is really monthly data for a period of 1 year (Acer)


Update Backtest


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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


This discussion is closed