In the service Quantopian, I program in Python to find the BETA of a given stock, then filter out stocks with BETAs greater than 3. I find the BETA by taking the covariance of the historical average of the asset to the index, then dividing by the historical average of the index. However, when I run this code:
priceFrom1yearAgo = history(bar_count=365, frequency='1d', field='price')
if (np.cov(np.mean(priceFrom1yearAgo[stock]), np.mean(priceFrom1yearAgo[index]))[0][1])/np.var(np.mean(priceFrom1yearAgo[index])) < 3:
However, when I run this code, I receive this error:
RuntimeWarning: Degrees of freedom <= 0 for slice
I don't fully understand the variance and covariance functions of numpy, and I struggle to see where my issue lies.
Where is the error occurring, and how do I solve it? And is there a better way to calculate BETA of a stock?