From 84700307bb7101c8400018058d1dc7ce857988f8 Mon Sep 17 00:00:00 2001 From: Matthew Pitkin Date: Fri, 13 Jan 2017 16:48:12 +0000 Subject: [PATCH] pulsarpputils.py: add function to convert a covariance matrix to a correlation matrix Original: 44991dccb4e4db87785425fa9bdfaa6c4cf72b93 --- .../pulsar/HeterodyneSearch/pulsarpputils.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lalapps/src/pulsar/HeterodyneSearch/pulsarpputils.py b/lalapps/src/pulsar/HeterodyneSearch/pulsarpputils.py index ad9fbd87e6..1c195fd244 100644 --- a/lalapps/src/pulsar/HeterodyneSearch/pulsarpputils.py +++ b/lalapps/src/pulsar/HeterodyneSearch/pulsarpputils.py @@ -3029,3 +3029,25 @@ def get_atnf_info(psr): assoc = vals[1] return (dist, p1_I, assoc, atnfurl) + + +def cov_to_cor(cov): + """ + Convert a covariance matrix to a correlation coefficient matrix. + Return the correlation coefficient matrix and the standard deviations + from the covariance matrix. + """ + + # get the standard deviations from the covariance matrix + sigmas = np.sqrt(np.diag(cov)) + + # convert these into a diagonal matrix + D = sigmas*np.identity(cov.shape[0]) + + # invert D + Dinv = np.linalg.inv(D) + + # get correlation coefficient matrix + Ccor = np.dot(np.dot(Dinv, cov), Dinv) + + return Ccor, sigmas -- GitLab