python-3.xscipycorrelationcovariance

Error converting covariance to correlation using scipy


I am trying to convert a covaraince matrix (from scipy.optimize.curve_fit) to a correlation matrix using the method here: https://math.stackexchange.com/questions/186959/correlation-matrix-from-covariance-matrix

My test data is from here https://blogs.sas.com/content/iml/2010/12/10/converting-between-correlation-and-covariance-matrices.html

My code is here

    import numpy as np

    S = [[1.0,  1.0,  8.1],
         [1.0, 16.0, 18.0],
         [8.1, 18.0, 81.0] ]

    S = np.array(S)
    diag = np.sqrt(np.diag(np.diag(S)))
    gaid = np.linalg.inv(diag)
    corl = gaid * S * gaid
    print(corl)

I was expecting to see [[1. 0.25 0.9 ], [0.25 1. 0.5 ], [0.9 0.5 1. ]] but instead get [[1. 0. 0.], [0. 1. 0.], [0. 0. 1.]]. I am obviously doing something silly but just not sure what so all suggestions gratefully received - thanks!


Solution

  • you've probably figured it out by now but you have to use the @ operator for matrix multiplication in numpy. The operator * is for an element-wise multiplication. So

    corl = gaid @ S @ gaid 
    

    gives the answer you are looking for.