0

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?

tdh
  • 884
  • 1
  • 11
  • 22
  • 1
    Well a good first step would be do the operations in that `if` statement separately so that you can get a better idea of which one raises the error. – Tadhg McDonald-Jensen Mar 18 '16 at 06:46
  • This is a numpy/scipy error, not quantopian. Suggest some basic debugging by setting each input to a variable and setting breakpoints in your IDE. – Jason Strimpel Mar 18 '16 at 08:03

0 Answers0