I'm trying to calculate a partial correlation matrix for a high dimensional problem. I'm using this paper as a guide. I'm also referencing this function from Pingouin.

Starting from the inverse covariance matrix of the variables:

inv_cov = np.linalg.pinv(np.cov(var_mat))

Then using equation 2.5 from the paper, calculating partial correlation between variable 0 and 1:

-inv_cov[0,1] / np.sqrt(inv_cov[0,0] * inv_cov[1,1])
>>> 0.8683796638219119

That formula seems to be the same as numpy's corrcoef function, however, when I use that:

>>> 0.7114911879489708

Using the Pingouin function returns the same as the manual calculation:

inv_diag = np.diag(np.sqrt(1 / np.diag(inv_cov)))
partial_corr = -1 * (inv_diag @ inv_cov @ inv_diag)
>>> 0.8683796638219119

What's the difference in Numpy's corrcoef that causes it to have a different value? In the linked paper they use R's cov2cor function as an example of how to calculate the matrix, does corrcoef not do the same thing?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.