X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Fcorrelation.c;h=bac81e4349360ef902a7e99b63768c0cafed42af;hb=96ccc18094e24d977bb8285143dc4c69f9caa8b4;hp=47762747340974f815207389fb48b71a59bf3605;hpb=3bbb4370239deb29ebbf813d258aef6249e2a431;p=pspp diff --git a/src/math/correlation.c b/src/math/correlation.c index 4776274734..bac81e4349 100644 --- a/src/math/correlation.c +++ b/src/math/correlation.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009 Free Software Foundation, Inc. + Copyright (C) 2009, 2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -16,13 +16,16 @@ #include -#include "correlation.h" +#include +#include "math/correlation.h" #include #include #include -#include -#include "minmax.h" + +#include "libpspp/misc.h" + +#include "gl/minmax.h" double @@ -37,7 +40,7 @@ significance_of_correlation (double rho, double w) t = sqrt (t); t *= rho; - + if (t > 0) return gsl_cdf_tdist_Q (t, w - 2); else @@ -49,20 +52,45 @@ correlation_from_covariance (const gsl_matrix *cv, const gsl_matrix *v) { size_t i, j; gsl_matrix *corr = gsl_matrix_calloc (cv->size1, cv->size2); - + for (i = 0 ; i < cv->size1; ++i) { for (j = 0 ; j < cv->size2; ++j) { double rho = gsl_matrix_get (cv, i, j); - + rho /= sqrt (gsl_matrix_get (v, i, j)) - * + * sqrt (gsl_matrix_get (v, j, i)); - + gsl_matrix_set (corr, i, j, rho); } } - + 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; +}