| 1 | /* Correlation.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2020 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "Correlation.h"cxx . . $ `<file>/dwtools/Correlation.h`/ |
| 20 | #include "Eigen.h"cxx . . $ `<file>/dwsys/Eigen.h`/ |
| 21 | #include "NUMlapack.h"cxx . . $ `<file>/dwsys/NUMlapack.h`/ |
| 22 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 23 | #include "SVD.h"cxx . . $ `<file>/dwsys/SVD.h`/ |
| 24 | |
| 25 | |
| 26 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Correlation, SSCP, 0); |
| 27 | |
| 28 | |
| 29 | autoCorrelationcxx . . $ autoCorrelation# TableOfReal_to_Correlationcxx . . $ TableOfReal_to_Correlation(c61ad4251955e1e). (TableOfRealcxx . . $ TableOfReal# me) { |
| 30 | try { |
| 31 | autoSSCPcxx . . $ autoSSCP# sscp = TableOfReal_to_SSCPcxx . . $ TableOfReal_to_SSCP(e6b6a917f4e16020). (me, 0, 0, 0, 0); |
| 32 | autoCorrelationcxx . . $ autoCorrelation# thee = SSCP_to_Correlationcxx . . $ SSCP_to_Correlation(8f3ffd89372af6fe). (sscp.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 33 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 34 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 35 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": correlations not created."); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | autoCorrelationcxx . . $ autoCorrelation# TableOfReal_to_Correlation_rankcxx . . $ TableOfReal_to_Correlation_rank(c61ad4251955e1e). (TableOfRealcxx . . $ TableOfReal# me) { |
| 40 | try { |
| 41 | autoTableOfRealcxx . . $ autoTableOfReal# t = TableOfReal_rankColumnscxx . . $ TableOfReal_rankColumns(9b66c0af0e652f50). (me, 1, mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 42 | autoCorrelationcxx . . $ autoCorrelation# thee = TableOfReal_to_Correlationcxx . . $ TableOfReal_to_Correlation(c61ad4251955e1e). (t.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 43 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 44 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 45 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": rank correlations not created."); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | autoCorrelationcxx . . $ autoCorrelation# Correlation_createSimplecxx . . $ Correlation_createSimple(8b12cce79a0611ca). (constVECVUcxx . . $ constVECVU# const& correlations, constVECVUcxx . . $ constVECVU# const& centroid, integercxx . . $ integer# numberOfObservations) { |
| 50 | try { |
| 51 | integercxx . . $ integer# numberOfCorrelations_wanted = centroid.sizecxx . . $ constvectorview#size. * (centroid.sizecxx . . $ constvectorview#size. + 1) / 2; |
| 52 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (correlations.size == numberOfCorrelations_wanted, |
| 53 | U"The number of correlation matrix elements and the number of centroid elements should agree. " |
| 54 | "If there are d centroid values, there should be d(d+1)/2 correlation values, " |
| 55 | "so if there are ", centroid.size, U" centroid values, there should be ", numberOfCorrelations_wanted, U" correlation values." |
| 56 | ); |
| 57 | |
| 58 | autoCorrelationcxx . . $ autoCorrelation# me = Correlation_createcxx . . $ Correlation_create(daa5c53f023e950f). (centroid.sizecxx . . $ constvectorview#size.); |
| 59 | /* |
| 60 | Construct the full correlation matrix from the upper-diagonal elements |
| 61 | */ |
| 62 | integercxx . . $ integer# rowNumber = 1; |
| 63 | for (integercxx . . $ integer# inum = 1; inum <= correlations.sizecxx . . $ constvectorview#size.; inum ++) { |
| 64 | const integercxx . . $ integer# nmissing = (rowNumber - 1) * rowNumber / 2; |
| 65 | const integercxx . . $ integer# inumc = inum + nmissing; |
| 66 | rowNumber = (inumc - 1) / centroid.sizecxx . . $ constvectorview#size. + 1; |
| 67 | const integercxx . . $ integer# icol = ( (inumc - 1) % centroid.sizecxx . . $ constvectorview#size.) + 1; |
| 68 | mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).rowNumber] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).icol] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).rowNumber] = correlations [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).inum]; |
| 69 | if (icol == centroid.sizecxx . . $ constvectorview#size.) |
| 70 | rowNumber ++; |
| 71 | } |
| 72 | /* |
| 73 | Check if a valid correlations, first check diagonal then off-diagonals |
| 74 | */ |
| 75 | for (integercxx . . $ integer# irow = 1; irow <= centroid.sizecxx . . $ constvectorview#size.; irow ++) |
| 76 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! data [irow] [irow] == 1.0, |
| 77 | U"The diagonal matrix elements should all equal 1.0."); |
| 78 | for (integercxx . . $ integer# irow = 1; irow <= centroid.sizecxx . . $ constvectorview#size.; irow ++) |
| 79 | for (integercxx . . $ integer# icol = irow + 1; icol <= centroid.sizecxx . . $ constvectorview#size.; icol ++) |
| 80 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (fabs (mycxx . . $ `melder/melder.h:44:9`! data [irow] [icol]) <= 1.0, |
| 81 | U"The correlation in cell [", irow, U",", icol, U"], i.e. input item ", |
| 82 | (irow - 1) * centroid.size + icol - (irow - 1) * irow / 2, U" should not exceed 1.0."); |
| 83 | mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). centroid; |
| 84 | mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. = numberOfObservations; |
| 85 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 86 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 87 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Simple Correlation not created."); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | autoCorrelationcxx . . $ autoCorrelation# Correlation_createcxx . . $ Correlation_create(daa5c53f023e950f). (integercxx . . $ integer# dimension) { |
| 92 | try { |
| 93 | autoCorrelationcxx . . $ autoCorrelation# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Correlation); |
| 94 | SSCP_initcxx . . $ SSCP_init(82a7ba5d0b6123a). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), dimension, kSSCPstoragecxx . . $ kSSCPstorage#::COMPLETEcxx . . $ kSSCPstorage#COMPLETE.); |
| 95 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 96 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 97 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Correlation not created."); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | autoCorrelationcxx . . $ autoCorrelation# SSCP_to_Correlationcxx . . $ SSCP_to_Correlation(8f3ffd89372af6fe). (SSCPcxx . . $ SSCP# me) { |
| 102 | try { |
| 103 | autoCorrelationcxx . . $ autoCorrelation# thee = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Correlation); |
| 104 | mycxx . . $ `melder/melder.h:44:9`! structSSCPcxx . . $ structSSCP# :: v1_copycxx . . $ structSSCP#v1_copy(42659c2335f2eda7). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 105 | for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) |
| 106 | for (integercxx . . $ integer# icol = irow; icol <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; icol ++) |
| 107 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).icol] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow] = thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] /= sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow] * mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).icol] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol]); |
| 108 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 109 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 110 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": Correlation not created."); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | autoTableOfRealcxx . . $ autoTableOfReal# Correlation_confidenceIntervalscxx . . $ Correlation_confidenceIntervals(615540b72065c4bd). (Correlationcxx . . $ Correlation# me, double confidenceLevel, integercxx . . $ integer# numberOfTests, int method) { |
| 115 | try { |
| 116 | const integercxx . . $ integer# m_bonferroni = mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. * (mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. - 1) / 2; |
| 117 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (confidenceLevel > 0.0, |
| 118 | U"The confidence level that you asked for should be greater than 0.0."); |
| 119 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (confidenceLevel <= 1.0, |
| 120 | U"The confidence level that you asked for should not be greater than 1.0."); |
| 121 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfObservations > 4, |
| 122 | U"The number of observations in the Correlation object should be greater than 4."); |
| 123 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfTests >= 0, |
| 124 | U"The number of tests that you asked for should not be less than zero."); |
| 125 | |
| 126 | if (numberOfTests == 0) |
| 127 | numberOfTests = m_bonferroni; |
| 128 | if (numberOfTests > m_bonferroni) |
| 129 | Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"The number of tests that you asked for (", numberOfTests, |
| 130 | U") should not exceed the number of elements in the Correlation object (", m_bonferroni, U")."); |
| 131 | |
| 132 | autoTableOfRealcxx . . $ autoTableOfReal# thee = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.); |
| 133 | |
| 134 | TableOfReal_copyLabelscxx . . $ TableOfReal_copyLabels(4a8870dfd6c30d44). (me, thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1, 1); |
| 135 | /* |
| 136 | Obtain large-sample conservative multiple tests and intervals by the |
| 137 | Bonferroni inequality and the Fisher z transformation. |
| 138 | Put upper value of confidence intervals in upper part and lower |
| 139 | values of confidence intervals in lower part of resulting table. |
| 140 | */ |
| 141 | const double z = NUMinvGaussQcxx . . $ NUMinvGaussQ(447c67c6676f75c3). ( (1 - confidenceLevel) / (2.0 * numberOfTests)); |
| 142 | const double zf = z / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 3.0); |
| 143 | const double two_n = 2.0 * mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 144 | |
| 145 | for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 146 | for (integercxx . . $ integer# icol = irow + 1; icol <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; icol ++) { |
| 147 | const double rij = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol]; |
| 148 | double rmin, rmax; |
| 149 | if (method == 2) { |
| 150 | /* |
| 151 | Fisher's approximation |
| 152 | */ |
| 153 | const double zij = 0.5 * logcxx . . $ log(fee4f1d22bc8e617). ( (1 + rij) / (1 - rij)); |
| 154 | rmax = tanhcxx . . $ tanh(fee4f1d22bc8e617). (zij + zf); |
| 155 | rmin = tanhcxx . . $ tanh(fee4f1d22bc8e617). (zij - zf); |
| 156 | } else if (method == 1) { |
| 157 | /* |
| 158 | Ruben's approximation |
| 159 | */ |
| 160 | const double rs = rij / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (1.0 - rij * rij); |
| 161 | const double a = two_n - 3.0 - z * z; |
| 162 | const double b = rs * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). ( (two_n - 3.0) * (two_n - 5.0)); |
| 163 | const double c = (a - 2.0) * rs * rs - 2.0 * z * z; |
| 164 | /* |
| 165 | Solve: a y^2 - 2b y + c = 0 |
| 166 | q = -0.5((-2b) + sgn(-2b) sqrt((-2b)^2 - 4ac)) |
| 167 | y1 = q/a; y2 = c/q; |
| 168 | */ |
| 169 | double d = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (b * b - a * c); |
| 170 | if (b > 0) |
| 171 | d = - d; |
| 172 | const double q = b - d; |
| 173 | rmin = q / a; |
| 174 | rmin /= sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (1.0 + rmin * rmin); |
| 175 | rmax = c / q; |
| 176 | rmax /= sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (1.0 + rmax * rmax); |
| 177 | if (rmin > rmax) |
| 178 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (rmin, rmax); |
| 179 | } else { |
| 180 | rmax = rmin = 0.0; |
| 181 | } |
| 182 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = rmax; |
| 183 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).icol] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow] = rmin; |
| 184 | } |
| 185 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow] = 1; |
| 186 | } |
| 187 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 188 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 189 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": confidence intervals not created."); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* Morrison, page 118 */ |
| 194 | void Correlation_testDiagonality_bartlettcxx . . $ Correlation_testDiagonality_bartlett(b42aab4b6eae93eb). (Correlationcxx . . $ Correlation# me, integercxx . . $ integer# numberOfContraints, double *out_chisq, double *out_prob, double *out_df) { |
| 195 | const integercxx . . $ integer# p = mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 196 | const double df = p * (p -1) / 2.0; |
| 197 | |
| 198 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfContraints > 0 && numberOfContraints <= mycxx . . $ `melder/melder.h:44:9`! numberOfObservations, |
| 199 | U"Correlation_testDiagonality_bartlett: number of constraints should be in the interval [1, ", mycxx . . $ `melder/melder.h:44:9`! numberOfObservations, U"]."); |
| 200 | |
| 201 | const double ln_determinant = NUMdeterminant_fromSymmetricMatrixcxx . . $ NUMdeterminant_fromSymmetricMatrix(3ee797411d4a69d9). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 202 | const double chisq = - ln_determinant * (mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. - numberOfContraints - (2.0 * p + 5.0) / 6.0); |
| 203 | if (out_prob) |
| 204 | *out_prob = NUMchiSquareQcxx . . $ NUMchiSquareQ(d8e7e0733fa41c87). (chisq, df); |
| 205 | if (out_chisq) |
| 206 | *out_chisq = chisq; |
| 207 | if (out_df) |
| 208 | *out_df = df; |
| 209 | } |
| 210 | |
| 211 | /* End of file Correlation.cpp */ |