title: Python: convert matrix to positive semi-definite
body: <p>I'm currently working on kernel methods, and at some point I needed to make a non positive semi-definite matrix (i.e. similarity matrix) into one PSD matrix. 
  I tried this approach:</p>
  
  <pre><code>def makePSD(mat):
      #make symmetric
      k = (mat+mat.T)/2
      #make PSD
      min_eig = np.min(np.real(linalg.eigvals(mat)))
      e = np.max([0, -min_eig + 1e-4])
      mat = k + e*np.eye(mat.shape[0]);
      return mat
  </code></pre>
  
  <p>but it fails if I test the resulting matrix with the following function:</p>
  
  <pre><code>def isPSD(A, tol=1e-8):
      E,V = linalg.eigh(A)
      return np.all(E &gt;= -tol)
  </code></pre>
  
  <p>I also tried the approach suggested in other related question (<a href="https://stackoverflow.com/questions/10939213/how-can-i-calculate-the-nearest-positive-semi-definite-matrix">How can I calculate the nearest positive semi-definite matrix?</a>), but the resulting matrix also failed to pass the isPSD test. </p>
  
  <p>Do you have any suggestions on how to correctly make such transformation correctly?</p>