Back

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








hi,

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

https://www.investopedia.com/ask/answers/070615/what-formula-calculating-beta.asp

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

0

https://corporatefinanceinstitute.com/resources/knowledge/finance/beta-coefficient/https://www.quantopian.com/posts/beta-quantopian-calculation

from bottom in this article:

https://medium.com/python-data/capm-analysis-calculating-stock-beta-as-a-regression-in-python-c82d189db536# alternatively scipy linear regression
from scipy import stats
slope, intercept, r_value, p_value, std_err = stats.linregress(X, y)

print(slope)

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.

0

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: 

'ACER'

 

If you go on finviz the beta is 2:31 https://finviz.com/quote.ashx?t=acer

 

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. 

0


hm the link above

https://medium.com/python-data/capm-analysis-calculating-stock-beta-as-a-regression-in-python-c82d189db536

says something about monthly prices...

yahoo lists acer with beta of 2,8

and the link above says how yahoo calculate this

https://finance.yahoo.com/quote/ACER?p=ACER

0

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)

0

Update Backtest





0

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.


Loading...

This discussion is closed