| 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" |
| 20 |
#include "Eigen.h" |
| 21 |
#include "NUMlapack.h" |
| 22 |
#include "NUM2.h" |
| 23 |
#include "SVD.h" |
| 24 |
|
| 25 |
|
| 26 |
Thing_implement (Correlation, SSCP, 0); |
| 27 |
|
| 28 |
|
| 29 |
autoCorrelation TableOfReal_to_Correlation (TableOfReal me) { |
| 30 |
try { |
| 31 |
autoSSCP sscp = TableOfReal_to_SSCP (me, 0, 0, 0, 0); |
| 32 |
autoCorrelation thee = SSCP_to_Correlation (sscp.get()); |
| 33 |
return thee; |
| 34 |
} catch (MelderError) { |
| 35 |
Melder_throw (me, U": correlations not created."); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
autoCorrelation TableOfReal_to_Correlation_rank (TableOfReal me) { |
| 40 |
try { |
| 41 |
autoTableOfReal t = TableOfReal_rankColumns (me, 1, my numberOfColumns); |
| 42 |
autoCorrelation thee = TableOfReal_to_Correlation (t.get()); |
| 43 |
return thee; |
| 44 |
} catch (MelderError) { |
| 45 |
Melder_throw (me, U": rank correlations not created."); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
autoCorrelation Correlation_createSimple (constVECVU const& correlations, constVECVU const& centroid, integer numberOfObservations) { |
| 50 |
try { |
| 51 |
integer numberOfCorrelations_wanted = centroid.size * (centroid.size + 1) / 2; |
| 52 |
Melder_require (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 |
autoCorrelation me = Correlation_create (centroid.size); |
| 59 |
/* |
| 60 |
Construct the full correlation matrix from the upper-diagonal elements |
| 61 |
*/ |
| 62 |
integer rowNumber = 1; |
| 63 |
for (integer inum = 1; inum <= correlations.size; inum ++) { |
| 64 |
const integer nmissing = (rowNumber - 1) * rowNumber / 2; |
| 65 |
const integer inumc = inum + nmissing; |
| 66 |
rowNumber = (inumc - 1) / centroid.size + 1; |
| 67 |
const integer icol = ( (inumc - 1) % centroid.size) + 1; |
| 68 |
my data [rowNumber] [icol] = my data [icol] [rowNumber] = correlations [inum]; |
| 69 |
if (icol == centroid.size) |
| 70 |
rowNumber ++; |
| 71 |
} |
| 72 |
/* |
| 73 |
Check if a valid correlations, first check diagonal then off-diagonals |
| 74 |
*/ |
| 75 |
for (integer irow = 1; irow <= centroid.size; irow ++) |
| 76 |
Melder_require (my data [irow] [irow] == 1.0, |
| 77 |
U"The diagonal matrix elements should all equal 1.0."); |
| 78 |
for (integer irow = 1; irow <= centroid.size; irow ++) |
| 79 |
for (integer icol = irow + 1; icol <= centroid.size; icol ++) |
| 80 |
Melder_require (fabs (my 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 |
my centroid.all() <<= centroid; |
| 84 |
my numberOfObservations = numberOfObservations; |
| 85 |
return me; |
| 86 |
} catch (MelderError) { |
| 87 |
Melder_throw (U"Simple Correlation not created."); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
autoCorrelation Correlation_create (integer dimension) { |
| 92 |
try { |
| 93 |
autoCorrelation me = Thing_new (Correlation); |
| 94 |
SSCP_init (me.get(), dimension, kSSCPstorage::COMPLETE); |
| 95 |
return me; |
| 96 |
} catch (MelderError) { |
| 97 |
Melder_throw (U"Correlation not created."); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
autoCorrelation SSCP_to_Correlation (SSCP me) { |
| 102 |
try { |
| 103 |
autoCorrelation thee = Thing_new (Correlation); |
| 104 |
my structSSCP :: v1_copy (thee.get()); |
| 105 |
for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 106 |
for (integer icol = irow; icol <= my numberOfColumns; icol ++) |
| 107 |
thy data [icol] [irow] = thy data [irow] [icol] /= sqrt (my data [irow] [irow] * my data [icol] [icol]); |
| 108 |
return thee; |
| 109 |
} catch (MelderError) { |
| 110 |
Melder_throw (me, U": Correlation not created."); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
autoTableOfReal Correlation_confidenceIntervals (Correlation me, double confidenceLevel, integer numberOfTests, int method) { |
| 115 |
try { |
| 116 |
const integer m_bonferroni = my numberOfRows * (my numberOfRows - 1) / 2; |
| 117 |
Melder_require (confidenceLevel > 0.0, |
| 118 |
U"The confidence level that you asked for should be greater than 0.0."); |
| 119 |
Melder_require (confidenceLevel <= 1.0, |
| 120 |
U"The confidence level that you asked for should not be greater than 1.0."); |
| 121 |
Melder_require (my numberOfObservations > 4, |
| 122 |
U"The number of observations in the Correlation object should be greater than 4."); |
| 123 |
Melder_require (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_warning (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 |
autoTableOfReal thee = TableOfReal_create (my numberOfRows, my numberOfRows); |
| 133 |
|
| 134 |
TableOfReal_copyLabels (me, thee.get(), 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 = NUMinvGaussQ ( (1 - confidenceLevel) / (2.0 * numberOfTests)); |
| 142 |
const double zf = z / sqrt (my numberOfObservations - 3.0); |
| 143 |
const double two_n = 2.0 * my numberOfObservations; |
| 144 |
|
| 145 |
for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 146 |
for (integer icol = irow + 1; icol <= my numberOfRows; icol ++) { |
| 147 |
const double rij = my data [irow] [icol]; |
| 148 |
double rmin, rmax; |
| 149 |
if (method == 2) { |
| 150 |
/* |
| 151 |
Fisher's approximation |
| 152 |
*/ |
| 153 |
const double zij = 0.5 * log ( (1 + rij) / (1 - rij)); |
| 154 |
rmax = tanh (zij + zf); |
| 155 |
rmin = tanh (zij - zf); |
| 156 |
} else if (method == 1) { |
| 157 |
/* |
| 158 |
Ruben's approximation |
| 159 |
*/ |
| 160 |
const double rs = rij / sqrt (1.0 - rij * rij); |
| 161 |
const double a = two_n - 3.0 - z * z; |
| 162 |
const double b = rs * sqrt ( (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 = sqrt (b * b - a * c); |
| 170 |
if (b > 0) |
| 171 |
d = - d; |
| 172 |
const double q = b - d; |
| 173 |
rmin = q / a; |
| 174 |
rmin /= sqrt (1.0 + rmin * rmin); |
| 175 |
rmax = c / q; |
| 176 |
rmax /= sqrt (1.0 + rmax * rmax); |
| 177 |
if (rmin > rmax) |
| 178 |
std::swap (rmin, rmax); |
| 179 |
} else { |
| 180 |
rmax = rmin = 0.0; |
| 181 |
} |
| 182 |
thy data [irow] [icol] = rmax; |
| 183 |
thy data [icol] [irow] = rmin; |
| 184 |
} |
| 185 |
thy data [irow] [irow] = 1; |
| 186 |
} |
| 187 |
return thee; |
| 188 |
} catch (MelderError) { |
| 189 |
Melder_throw (me, U": confidence intervals not created."); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/* Morrison, page 118 */ |
| 194 |
void Correlation_testDiagonality_bartlett (Correlation me, integer numberOfContraints, double *out_chisq, double *out_prob, double *out_df) { |
| 195 |
const integer p = my numberOfRows; |
| 196 |
const double df = p * (p -1) / 2.0; |
| 197 |
|
| 198 |
Melder_require (numberOfContraints > 0 && numberOfContraints <= my numberOfObservations, |
| 199 |
U"Correlation_testDiagonality_bartlett: number of constraints should be in the interval [1, ", my numberOfObservations, U"]."); |
| 200 |
|
| 201 |
const double ln_determinant = NUMdeterminant_fromSymmetricMatrix (my data.get()); |
| 202 |
const double chisq = - ln_determinant * (my numberOfObservations - numberOfContraints - (2.0 * p + 5.0) / 6.0); |
| 203 |
if (out_prob) |
| 204 |
*out_prob = NUMchiSquareQ (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 */ |