title: Calculating Covariance with Python and Numpy
body: <p>I am trying to figure out how to calculate covariance with the Python Numpy function cov.  When I pass it two one-dimentional arrays, I get back a 2x2 matrix of results.  I don't know what to do with that.  I'm not great at statistics, but I believe covariance in such a situation should be a single number.  <a href="http://i.investopedia.com/inv/articles/site/Proper%20Covariance.gif">This</a> is what I am looking for.  I wrote my own:</p>
  
  <pre><code>def cov(a, b):
  
      if len(a) != len(b):
          return
  
      a_mean = np.mean(a)
      b_mean = np.mean(b)
  
      sum = 0
  
      for i in range(0, len(a)):
          sum += ((a[i] - a_mean) * (b[i] - b_mean))
  
      return sum/(len(a)-1)
  </code></pre>
  
  <p>That works, but I figure the Numpy version is much more efficient, if I could figure out how to use it.</p>
  
  <p>Does anybody know how to make the Numpy cov function perform like the one I wrote?</p>
  
  <p>Thanks,</p>
  
  <p>Dave</p>