From 8eb60896161a514e0df6208ff20cd90a75cc8938 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Thu, 4 May 2017 17:27:31 +0200 Subject: [PATCH] New function covariance_from_correlation --- src/math/correlation.c | 26 ++++++++++++++++++++++++++ src/math/correlation.h | 4 +++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/math/correlation.c b/src/math/correlation.c index f5bba52379..bac81e4349 100644 --- a/src/math/correlation.c +++ b/src/math/correlation.c @@ -16,6 +16,7 @@ #include +#include #include "math/correlation.h" #include @@ -68,3 +69,28 @@ correlation_from_covariance (const gsl_matrix *cv, const gsl_matrix *v) return corr; } + +gsl_matrix * +covariance_from_correlation (const gsl_matrix *corr, const gsl_matrix *v) +{ + size_t i, j; + assert (corr->size1 == corr->size2); + + gsl_matrix *output = gsl_matrix_calloc (corr->size1, corr->size2); + + for (i = 0 ; i < corr->size1; ++i) + { + for (j = 0 ; j < corr->size2; ++j) + { + double r = gsl_matrix_get (corr, i, j); + + r *= sqrt (gsl_matrix_get (v, i, j)) + * + sqrt (gsl_matrix_get (v, j, i)); + + gsl_matrix_set (output, i, j, r); + } + } + + return output; +} diff --git a/src/math/correlation.h b/src/math/correlation.h index 27621c47c9..5771f43e71 100644 --- a/src/math/correlation.h +++ b/src/math/correlation.h @@ -20,7 +20,9 @@ #include -gsl_matrix * correlation_from_covariance (const gsl_matrix *cv, const gsl_matrix *v); +gsl_matrix *correlation_from_covariance (const gsl_matrix *cv, const gsl_matrix *v); + +gsl_matrix *covariance_from_correlation (const gsl_matrix *corr, const gsl_matrix *v); double significance_of_correlation (double rho, double w); -- 2.30.2