From 50ac6802fc247814dc4dd6232f6304b928a2d78b Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sun, 8 Nov 2009 16:33:52 +0100 Subject: [PATCH] New module src/math/correlation Created a new module to hold some common routines pertaining to correlation coefficients. --- src/language/stats/correlations.c | 42 +-------------------- src/math/automake.mk | 2 + src/math/correlation.c | 63 +++++++++++++++++++++++++++++++ src/math/correlation.h | 27 +++++++++++++ 4 files changed, 93 insertions(+), 41 deletions(-) create mode 100644 src/math/correlation.c create mode 100644 src/math/correlation.h diff --git a/src/language/stats/correlations.c b/src/language/stats/correlations.c index e397dae5..605609a7 100644 --- a/src/language/stats/correlations.c +++ b/src/language/stats/correlations.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -46,21 +47,6 @@ #define N_(msgid) msgid -static double -significance_of_correlation (double rho, double w) -{ - double t = w - 2; - t /= 1 - MIN (1, pow2 (rho)); - t = sqrt (t); - t *= rho; - - if (t > 0) - return gsl_cdf_tdist_Q (t, w - 2); - else - return gsl_cdf_tdist_P (t, w - 2); -} - - struct corr { size_t n_vars_total; @@ -291,32 +277,6 @@ output_correlation (const struct corr *corr, const struct corr_opts *opts, } -static gsl_matrix * -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; -} - - - - static void run_corr (struct casereader *r, const struct corr_opts *opts, const struct corr *corr) { diff --git a/src/math/automake.mk b/src/math/automake.mk index 9fc15aaf..a17919e4 100644 --- a/src/math/automake.mk +++ b/src/math/automake.mk @@ -17,6 +17,8 @@ src_math_libpspp_math_la_SOURCES = \ src/math/covariance.h \ src/math/covariance-matrix.c \ src/math/covariance-matrix.h \ + src/math/correlation.c \ + src/math/correlation.h \ src/math/design-matrix.c src/math/design-matrix.h \ src/math/extrema.c src/math/extrema.h \ src/math/group.c src/math/group.h \ diff --git a/src/math/correlation.c b/src/math/correlation.c new file mode 100644 index 00000000..303b48bc --- /dev/null +++ b/src/math/correlation.c @@ -0,0 +1,63 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2009 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include "correlation.h" + +#include +#include +#include +#include +#include "minmax.h" + + +double +significance_of_correlation (double rho, double w) +{ + double t = w - 2; + t /= 1 - MIN (1, pow2 (rho)); + t = sqrt (t); + t *= rho; + + if (t > 0) + return gsl_cdf_tdist_Q (t, w - 2); + else + return gsl_cdf_tdist_P (t, w - 2); +} + +gsl_matrix * +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; +} diff --git a/src/math/correlation.h b/src/math/correlation.h new file mode 100644 index 00000000..27621c47 --- /dev/null +++ b/src/math/correlation.h @@ -0,0 +1,27 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2009 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + + +#ifndef SRC_MATH_CORRELATION_H +#define SRC_MATH_CORRELATION_H + +#include + +gsl_matrix * correlation_from_covariance (const gsl_matrix *cv, const gsl_matrix *v); + +double significance_of_correlation (double rho, double w); + +#endif -- 2.30.2