title: Negative product containing vector and covariance matrix
body: <p>I'm implementing the <a href="https://en.wikipedia.org/wiki/Mahalanobis_distance" rel="nofollow noreferrer">Mahalanobis distance</a> to measure the distance between two vectors of the same pool and just recognized that it seems to be correct the most but sometimes not, maybe due to negative values..
  I realized that sometimes for </p>
  
  <blockquote>
    <p><a href="https://i.stack.imgur.com/DPHbX.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DPHbX.gif" alt="formula"></a></p>
  </blockquote>
  
  <p>negative values occur. That's why the distance gets negative respectively the code throws up an error because root of a negative value..
  I wonder about the problem. The data is (a row represents an object):</p>
  
  <pre><code>A: 376.498943729227 2.75082585760394    376.688899264061    2.75084113940164
  B: 373.287817831307 2.75074375125675    373.392663499518    2.75092754974534
  C: 377.091938091279 2.75082292557743    377.466035993347    2.75077191984784
  D: 374.799551607287 2.75094834987157    374.209110037364    2.75091796001419
  </code></pre>
  
  <p>The covariance matrix S is then</p>
  
  <pre><code>7.13457e-09  3.13933e-05  5.45925e-10  3.80508e-06
   3.13933e-05      2.96355 -0.000115865      3.28797
   5.45925e-10 -0.000115865  5.31665e-09 -0.000137211
   3.80508e-06      3.28797 -0.000137211      3.79042
  </code></pre>
  
  <p>and the inverse of it is</p>
  
  <pre><code> 3.24779e+22 -8.58499e+18  1.40166e+22  7.92177e+18
  -8.58499e+18   2.2693e+15 -3.70505e+18 -2.09399e+15
   1.40166e+22 -3.70505e+18  6.04917e+21  3.41882e+18
   7.92177e+18 -2.09399e+15  3.41882e+18  1.93222e+15
  </code></pre>
  
  <p>Now I wonder why I get negative results out of the highlighted product (in case of B and D)?
  I'm not sure if its a programming problem (that's why I didn't include code lines, yet) or rather a theoretical one but I appreciate any help a lot!</p>
  
  <p>I use the Eigen class.</p>
  
  <p><strong>edit:</strong>
  I calculated the eigenvalues of the covariance matrix S via <code>R</code> and get:</p>
  
  <blockquote>
    <p>7.593311e+02  1.243531e-01  1.156646e-02 -3.920936e-04</p>
  </blockquote>
  
  <p>Why do I have different ones?
  I used</p>
  
  <pre><code>M&lt;- matrix(c(376.498943729227, 2.75082585760394, 376.688899264061, 2.75084113940164, 
         373.287817831307,    2.75074375125675,   373.392663499518,   2.75092754974534,
         377.091938091279,    2.75082292557743,   377.466035993347,   2.75077191984784,
         374.799551607287,    2.75094834987157,   374.209110037364,   2.75091796001419
         ), 4, 4)
  &gt; M
             [,1]       [,2]       [,3]       [,4]
  [1,] 376.498944 373.287818 377.091938 374.799552
  [2,]   2.750826   2.750744   2.750823   2.750948
  [3,] 376.688899 373.392663 377.466036 374.209110
  [4,]   2.750841   2.750928   2.750772   2.750918
  ev&lt;- eigen(M)
  values&lt;- ev$values
  values
  [1]  7.593311e+02  1.243531e-01  1.156646e-02 -3.920936e-04
  </code></pre>