title: Python: finding the covariance matrix of an Nx2 matrix
body: <p>I'm trying to compute a covariance matrix for an Nx2 matrix called X by hand (I know that there is a cov() function, but I want to calculate it by hand).
  The code I'm trying to implement is:</p>
  
  <pre><code>N=len(X)
  S=numpy.zeros(2*2).reshape((2,2))
  sumx=numpy.zeros(N*2).reshape((N,2))
  b1=numpy.zeros(N*1).reshape((N,1))
  b2=numpy.zeros(N*1).reshape((N,1))
  
  for n in range(1,N):
      for i in range(0,N):
          b1[i]=(X[n,i]-mean(X[i,:]))
  
      for j in range(0,2):
          b2[j]=(X[n,j]-mean(X[:,j]))
  
      btot=b1v*b2v
      bsum=sum(btot)
      S=bsum/(N-1)
  print S
  </code></pre>
  
  <p>but this doesn't work! Sorry I am fairly new to python, so it may be a simple thing I am getting wrong.</p>
  
  <p>The error throws up from saying</p>
  
  <pre><code>b1[i]=(X[n,i]-mean(X[i,:]))
  
  IndexError: index 2 is out of bounds for axis 1 with size 2
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Where X is along the lines of:</p>
  
  <pre><code>[[  1.89235582e-01   1.91920908e+01]
   [  6.53377180e-02   1.78733112e+01]
   [  7.71620860e-02   1.79439764e+01]
   [  8.69048860e-02   1.80507024e+01]
   [  1.94832532e-01   1.85183166e+01]
   [  1.84917732e-01   1.86287646e+01]
    ...]
  </code></pre>