| 1 | /* Covariance.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2023 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 "Covariance.h"cxx . . $ `<file>/dwtools/Covariance.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 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Covariance, SSCP, 0); |
| 26 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (CovarianceList, SSCPList, 0); |
| 27 | |
| 28 | autoTableOfRealcxx . . $ autoTableOfReal# Covariance_TableOfReal_mahalanobiscxx . . $ Covariance_TableOfReal_mahalanobis(bf87fb867ef00418). (Covariancecxx . . $ Covariance# me, TableOfRealcxx . . $ TableOfReal# thee, bool useTableCentroid) { |
| 29 | try { |
| 30 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfColumns == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 31 | U"The number of dimensions of the Covariance should be equal to that of the TableOfReal."); |
| 32 | autoTableOfRealcxx . . $ autoTableOfReal# him = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., 1); |
| 33 | autoVECcxx . . $ autoVEC# centroid = copy_VECcxx . . $ copy_VEC(d4415e68b25283a8). (mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 34 | autoMATcxx . . $ autoMAT# covari = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 35 | /* |
| 36 | Mahalanobis distance calculation. S = L.L' -> S**-1 = L**-1' . L**-1 |
| 37 | (x-m)'S**-1 (x-m) = (x-m)'L**-1' . L**-1. (x-m) = |
| 38 | (L**-1.(x-m))' . (L**-1.(x-m)) |
| 39 | |
| 40 | Get inverse of covari in lower triangular part. |
| 41 | */ |
| 42 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (covari.getcxx . . $ automatrix#get(b2b81450b5bdda66).(), nullptr); |
| 43 | |
| 44 | if (useTableCentroid) |
| 45 | columnMeans_VEC_outcxx . . $ columnMeans_VEC_out(4ac231a09fafb822). (centroidcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 46 | for (integercxx . . $ integer# k = 1; k <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; k ++) { |
| 47 | hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).k] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (covaricxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (k), centroidcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).())); |
| 48 | if (thycxx . . $ `melder/melder.h:45:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).k]) |
| 49 | TableOfReal_setRowLabelcxx . . $ TableOfReal_setRowLabel(7bba53c4f5b398fa). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), k, thycxx . . $ `melder/melder.h:45:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).k].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 50 | } |
| 51 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1, U"d"); |
| 52 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 53 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 54 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U"no Mahalanobis distances created."); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /* For nxn matrix only ! */ |
| 59 | void Covariance_PCA_generateOneVector_inlinecxx . . $ Covariance_PCA_generateOneVector_inline(8fd3da726b298de8). (Covariancecxx . . $ Covariance# me, PCAcxx . . $ PCA# thee, VECVUcxx . . $ VECVU# vec, VECcxx . . $ VEC# buf) { |
| 60 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (thycxx . . $ `melder/melder.h:45:9`! dimension == mycxx . . $ `melder/melder.h:44:9`! numberOfRows, |
| 61 | U"The PCA should have the same number of dimensions as the Covariance."); |
| 62 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (vec.size == buf.size && mycxx . . $ `melder/melder.h:44:9`! numberOfColumns == buf.size, |
| 63 | U"The vectors and the PCA should have the same number of dimensions."); |
| 64 | /* |
| 65 | Generate the multi-normal vector elements N(0,sigma) |
| 66 | */ |
| 67 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; j ++) |
| 68 | buf [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = NUMrandomGausscxx . . $ NUMrandomGauss(d8e7e0733fa41c87). (0.0, sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (thycxx . . $ `melder/melder.h:45:9`! eigenvaluescxx . . $ structEigen#eigenvalues. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j])); |
| 69 | /* |
| 70 | Rotate back |
| 71 | */ |
| 72 | mul_VEC_outcxx . . $ mul_VEC_out(2bf483c07f98b4df). (vec, bufcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 73 | vec +=cxx . . $ `operator+=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).(); |
| 74 | } |
| 75 | |
| 76 | autoTableOfRealcxx . . $ autoTableOfReal# Covariance_to_TableOfReal_randomSamplingcxx . . $ Covariance_to_TableOfReal_randomSampling(aaff9786ae7eb815). (Covariancecxx . . $ Covariance# me, integercxx . . $ integer# numberOfData) { |
| 77 | try { |
| 78 | if (numberOfData <= 0) |
| 79 | numberOfData = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.); |
| 80 | autoPCAcxx . . $ autoPCA# pca = SSCP_to_PCAcxx . . $ SSCP_to_PCA(67c2aab67f958214). (me); |
| 81 | autoTableOfRealcxx . . $ autoTableOfReal# thee = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (numberOfData, mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 82 | autoVECcxx . . $ autoVEC# buf = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 83 | for (integercxx . . $ integer# i = 1; i <= numberOfData; i ++) |
| 84 | Covariance_PCA_generateOneVector_inlinecxx . . $ Covariance_PCA_generateOneVector_inline(8fd3da726b298de8). (me, pca.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (i), buf.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 85 | thycxx . . $ `melder/melder.h:45:9`! columnLabelscxx . . $ structTableOfReal#columnLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).() <<=cxx . . $ `operator<<=`(5bcbc9f49fd394e1). mycxx . . $ `melder/melder.h:44:9`! columnLabelscxx . . $ structTableOfReal#columnLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).(); |
| 86 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 87 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 88 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not random sampled."); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | autoTableOfRealcxx . . $ autoTableOfReal# Covariance_TableOfReal_extractDistanceQuantileRangecxx . . $ Covariance_TableOfReal_extractDistanceQuantileRange(cf73752ab84b1832). (Covariancecxx . . $ Covariance# me, TableOfRealcxx . . $ TableOfReal# thee, double qlow, double qhigh) { |
| 93 | try { |
| 94 | autoTableOfRealcxx . . $ autoTableOfReal# him = Covariance_TableOfReal_mahalanobiscxx . . $ Covariance_TableOfReal_mahalanobis(bf87fb867ef00418). (me, thee, false); |
| 95 | |
| 96 | const double low = TableOfReal_getColumnQuantilecxx . . $ TableOfReal_getColumnQuantile(24395f3cb231684c). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1, qlow); |
| 97 | const double high = TableOfReal_getColumnQuantilecxx . . $ TableOfReal_getColumnQuantile(24395f3cb231684c). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1, qhigh); |
| 98 | /* |
| 99 | Count the number filtered. |
| 100 | nsel = (qhigh - qlow) * nrows is sometimes one off |
| 101 | */ |
| 102 | integercxx . . $ integer# nsel = 0; |
| 103 | for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; i ++) |
| 104 | if (low <= hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] && hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] < high) |
| 105 | nsel ++; |
| 106 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (nsel > 0, |
| 107 | U"Not enough data in quantile interval."); |
| 108 | |
| 109 | autoTableOfRealcxx . . $ autoTableOfReal# r = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (nsel, thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 110 | r ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). columnLabelscxx . . $ structTableOfReal#columnLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).() <<=cxx . . $ `operator<<=`(5bcbc9f49fd394e1). thycxx . . $ `melder/melder.h:45:9`! columnLabelscxx . . $ structTableOfReal#columnLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).(); |
| 111 | |
| 112 | integercxx . . $ integer# k = 0; |
| 113 | for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; i ++) |
| 114 | if (low <= hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] && hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] < high) |
| 115 | TableOfReal_copyOneRowWithLabelcxx . . $ TableOfReal_copyOneRowWithLabel(f4eb8ed484c5a163). (thee, r.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), i, ++ k); |
| 116 | return rcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 117 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 118 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"TableOfReal with distance quantile range not created."); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | autoCovariancecxx . . $ autoCovariance# TableOfReal_to_Covariancecxx . . $ TableOfReal_to_Covariance(3e7df3a959f2f2b3). (TableOfRealcxx . . $ TableOfReal# me) { |
| 123 | try { |
| 124 | autoSSCPcxx . . $ autoSSCP# sscp = TableOfReal_to_SSCPcxx . . $ TableOfReal_to_SSCP(e6b6a917f4e16020). (me, 0, 0, 0, 0); |
| 125 | autoCovariancecxx . . $ autoCovariance# thee = SSCP_to_Covariancecxx . . $ SSCP_to_Covariance(872697cc608a64ad). (sscp.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1); |
| 126 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 127 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 128 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": covariances not created."); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | autoCovariancecxx . . $ autoCovariance# CovarianceList_to_Covariance_withincxx . . $ CovarianceList_to_Covariance_within(56bfcf47216a02f2). (CovarianceListcxx . . $ CovarianceList# me) { |
| 133 | try { |
| 134 | autoCovariancecxx . . $ autoCovariance# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).1]); |
| 135 | SSCP_resetcxx . . $ SSCP_reset(b2bffa5b59e040d8). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 136 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i ++) { |
| 137 | const Covariancecxx . . $ Covariance# covi = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]; |
| 138 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (thycxx . . $ `melder/melder.h:45:9`! numberOfColumns == covi -> numberOfColumns && thycxx . . $ `melder/melder.h:45:9`! numberOfRows == covi -> numberOfRows, |
| 139 | U"The dimensions of item ", i, U" do not conform."); |
| 140 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() +=cxx . . $ `operator+=`(b78e8a4998bd3572). covicxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58). -> datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() *cxx . . $ `operator*`(3dd25102fdc24343). (covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1.0); |
| 141 | thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. += covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 142 | } |
| 143 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() *=cxx . . $ `operator*=`(50b469ae42a42f64). 1.0 / (thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1.0); |
| 144 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 145 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 146 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Covariance (within) created."); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | autoCovariancecxx . . $ autoCovariance# CovarianceList_to_Covariance_betweencxx . . $ CovarianceList_to_Covariance_between(56bfcf47216a02f2). (CovarianceListcxx . . $ CovarianceList# me) { |
| 151 | try { |
| 152 | autoCovariancecxx . . $ autoCovariance# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).1]); |
| 153 | SSCP_resetcxx . . $ SSCP_reset(b2bffa5b59e040d8). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 154 | |
| 155 | // First the new centroid, |
| 156 | |
| 157 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i ++) { |
| 158 | const Covariancecxx . . $ Covariance# covi = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]; |
| 159 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (thycxx . . $ `melder/melder.h:45:9`! numberOfColumns == covi -> numberOfColumns && thycxx . . $ `melder/melder.h:45:9`! numberOfRows == covi -> numberOfRows, |
| 160 | U"The dimensions of item ", i, U" do not conform."); |
| 161 | thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() +=cxx . . $ `operator+=`(99e6442b877026f3). covicxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). -> centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() *cxx . . $ `operator*`(b7c2885862e827f). covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 162 | thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. += covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 163 | } |
| 164 | thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() *=cxx . . $ `operator*=`(c555d170d72ff256). 1.0 / thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 165 | |
| 166 | autoVECcxx . . $ autoVEC# mean = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 167 | autoMATcxx . . $ autoMAT# outer = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns., thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 168 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i ++) { |
| 169 | const Covariancecxx . . $ Covariance# covi = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]; |
| 170 | mean.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(ce74bae6c180f663). covicxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). -> centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() -cxx . . $ operator-(fcdbb1279f7b512e). thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).(); |
| 171 | outer_MAT_outcxx . . $ outer_MAT_out(2869a0b58d3aa0f4). (outer.allcxx . . $ automatrix#all(2114117ba8ed4224).(), meancxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).(), meancxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).()); |
| 172 | if (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) |
| 173 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) +=cxx . . $ `operator+=`(99e6442b877026f3). outercxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..diagonalcxx . . $ automatrix#diagonal(a26726dad87555bb).() *cxx . . $ `operator*`(b7c2885862e827f). covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 174 | else |
| 175 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() +=cxx . . $ `operator+=`(b78e8a4998bd3572). outercxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..allcxx . . $ automatrix#all(2114117ba8ed4224).() *cxx . . $ `operator*`(3dd25102fdc24343). covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations.; // Y += aX |
| 176 | } |
| 177 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() *=cxx . . $ `operator*=`(50b469ae42a42f64). 1.0 / (thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1.0); |
| 178 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 179 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 180 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Covariance (between) created."); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | autoCovariancecxx . . $ autoCovariance# CovarianceList_to_Covariance_poolcxx . . $ CovarianceList_to_Covariance_pool(56bfcf47216a02f2). (CovarianceListcxx . . $ CovarianceList# me) { // Morrison sec 3.5, page 100 |
| 185 | try { |
| 186 | autoCovariancecxx . . $ autoCovariance# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).1]); |
| 187 | SSCP_resetcxx . . $ SSCP_reset(b2bffa5b59e040d8). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 188 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i ++) { |
| 189 | const Covariancecxx . . $ Covariance# covi = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]; |
| 190 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (covi -> numberOfRows == thycxx . . $ `melder/melder.h:45:9`! numberOfRows, |
| 191 | U"The dimension of item ", i, U" should agree."); |
| 192 | thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. += covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 193 | /* |
| 194 | Sum the sscp's and weigh the centroid. |
| 195 | */ |
| 196 | for (integercxx . . $ integer# k = 1; k <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; k ++) // catch 1xn |
| 197 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (k) +=cxx . . $ `operator+=`(99e6442b877026f3). covicxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). -> datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (k) *cxx . . $ `operator*`(b7c2885862e827f). (covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1.0); |
| 198 | |
| 199 | thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() +=cxx . . $ `operator+=`(99e6442b877026f3). covicxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). -> centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() *cxx . . $ `operator*`(b7c2885862e827f). covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 200 | } |
| 201 | thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() *=cxx . . $ `operator*=`(c555d170d72ff256). 1.0 / thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 202 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() *=cxx . . $ `operator*=`(50b469ae42a42f64). 1.0 / (thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. - mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.); |
| 203 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 204 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 205 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not pooled."); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | autoCovariancecxx . . $ autoCovariance# Covariance_createcxx . . $ Covariance_create(49f8ab4cf76ab9e9). (integercxx . . $ integer# dimension) { |
| 210 | try { |
| 211 | autoCovariancecxx . . $ autoCovariance# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Covariance); |
| 212 | SSCP_initcxx . . $ SSCP_init(82a7ba5d0b6123a). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), dimension, kSSCPstoragecxx . . $ kSSCPstorage#::COMPLETEcxx . . $ kSSCPstorage#COMPLETE.); |
| 213 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 214 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 215 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Covariance not created."); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | autoCovariancecxx . . $ autoCovariance# Covariance_create_reduceStoragecxx . . $ Covariance_create_reduceStorage(205004f52690bd11). (integercxx . . $ integer# dimension, kSSCPstoragecxx . . $ kSSCPstorage# storage) { |
| 220 | try { |
| 221 | autoCovariancecxx . . $ autoCovariance# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Covariance); |
| 222 | SSCP_initcxx . . $ SSCP_init(82a7ba5d0b6123a). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), dimension, storage); |
| 223 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 224 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 225 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Reduced storage covariance not created."); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | autoCovariancecxx . . $ autoCovariance# Covariance_createSimplecxx . . $ Covariance_createSimple(fb415e43f99f04d0). (constVECVUcxx . . $ constVECVU# const& covariances, constVECVUcxx . . $ constVECVU# const& centroid, integercxx . . $ integer# numberOfObservations) { |
| 232 | try { |
| 233 | const integercxx . . $ integer# numberOfCovariances_wanted = centroid.sizecxx . . $ constvectorview#size. * (centroid.sizecxx . . $ constvectorview#size. + 1) / 2; |
| 234 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (covariances.size == numberOfCovariances_wanted, |
| 235 | U"The number of covariance matrix elements and the number of centroid elements should agree. " |
| 236 | "If there are d centroid values, there should be d(d+1)/2 covariance values, " |
| 237 | "so if there are ", centroid.size, U" centroid values, there should be ", numberOfCovariances_wanted, U" covariance values."); |
| 238 | |
| 239 | autoCovariancecxx . . $ autoCovariance# me = Covariance_createcxx . . $ Covariance_create(49f8ab4cf76ab9e9). (centroid.sizecxx . . $ constvectorview#size.); |
| 240 | /* |
| 241 | Construct the full covariance matrix from the upper-diagonal elements |
| 242 | */ |
| 243 | integercxx . . $ integer# rowNumber = 1; |
| 244 | for (integercxx . . $ integer# inum = 1; inum <= covariances.sizecxx . . $ constvectorview#size.; inum ++) { |
| 245 | const integercxx . . $ integer# nmissing = (rowNumber - 1) * rowNumber / 2; |
| 246 | const integercxx . . $ integer# inumc = inum + nmissing; |
| 247 | rowNumber = (inumc - 1) / centroid.sizecxx . . $ constvectorview#size. + 1; |
| 248 | integercxx . . $ integer# icol = ((inumc - 1) % centroid.sizecxx . . $ constvectorview#size.) + 1; |
| 249 | 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] = covariances [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).inum]; |
| 250 | if (icol == centroid.sizecxx . . $ constvectorview#size.) |
| 251 | rowNumber ++; |
| 252 | } |
| 253 | |
| 254 | // Check if a valid covariance, first check variances then covariances |
| 255 | |
| 256 | for (integercxx . . $ integer# irow = 1; irow <= centroid.sizecxx . . $ constvectorview#size.; irow ++) |
| 257 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! data [irow] [irow] > 0.0, |
| 258 | U"The diagonal matrix elements should all be positive numbers."); |
| 259 | |
| 260 | for (integercxx . . $ integer# irow = 1; irow <= centroid.sizecxx . . $ constvectorview#size.; irow ++) |
| 261 | for (integercxx . . $ integer# icol = irow + 1; icol <= centroid.sizecxx . . $ constvectorview#size.; icol ++) |
| 262 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (fabs (mycxx . . $ `melder/melder.h:44:9`! data [irow] [icol] / sqrt (mycxx . . $ `melder/melder.h:44:9`! data [irow] [irow] * mycxx . . $ `melder/melder.h:44:9`! data [icol] [icol])) <= 1.0, |
| 263 | U"The covariance in cell [", irow, U",", icol, U"], i.e. input item ", |
| 264 | (irow - 1) * centroid.size + icol - (irow - 1) * irow / 2, U" is too large."); |
| 265 | mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). centroid; |
| 266 | mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. = numberOfObservations; |
| 267 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 268 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 269 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Simple Covariance not created."); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | autoCovariancecxx . . $ autoCovariance# SSCP_to_Covariancecxx . . $ SSCP_to_Covariance(872697cc608a64ad). (SSCPcxx . . $ SSCP# me, integercxx . . $ integer# numberOfConstraints) { |
| 274 | try { |
| 275 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (numberOfConstraints >= 0); |
| 276 | autoCovariancecxx . . $ autoCovariance# thee = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Covariance); |
| 277 | mycxx . . $ `melder/melder.h:44:9`! structSSCPcxx . . $ structSSCP# :: v1_copycxx . . $ structSSCP#v1_copy(42659c2335f2eda7). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 278 | |
| 279 | for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) |
| 280 | for (integercxx . . $ integer# icol = irow; icol <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; icol ++) // a covariance matrix is symmetric |
| 281 | 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] /= mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. - numberOfConstraints; |
| 282 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 283 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 284 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U"; Covariance not created."); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static autoCovariancecxx . . $ autoCovariance# Covariances_poolcxx . . $ Covariances_pool(5cd36e68b575e95f). (Covariancecxx . . $ Covariance# me, Covariancecxx . . $ Covariance# thee) { |
| 289 | try { |
| 290 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfRows == thycxx . . $ `melder/melder.h:45:9`! numberOfRows && mycxx . . $ `melder/melder.h:44:9`! numberOfColumns == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 291 | U"Matrices should have equal dimensions."); |
| 292 | autoSSCPListcxx . . $ autoSSCPList# sscps = SSCPList_createcxx . . $ SSCPList_create(a7dc2e0077c591a7). (); |
| 293 | autoSSCPcxx . . $ autoSSCP# sscp1 = Covariance_to_SSCPcxx . . $ Covariance_to_SSCP(76e37184877b64dd). (me); |
| 294 | sscps ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). addItem_movecxx . . $ CollectionOf#addItem_move(bcc0cff787be24fe). (sscp1cxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).()); |
| 295 | autoSSCPcxx . . $ autoSSCP# sscp2 = Covariance_to_SSCPcxx . . $ Covariance_to_SSCP(76e37184877b64dd). (thee); |
| 296 | sscps ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). addItem_movecxx . . $ CollectionOf#addItem_move(bcc0cff787be24fe). (sscp2cxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).()); |
| 297 | autoSSCPcxx . . $ autoSSCP# pool = SSCPList_to_SSCP_poolcxx . . $ SSCPList_to_SSCP_pool(b57a97f114ea42f6). (sscps.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 298 | autoCovariancecxx . . $ autoCovariance# him = SSCP_to_Covariancecxx . . $ SSCP_to_Covariance(872697cc608a64ad). (pool.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 2); |
| 299 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 300 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 301 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U"not pooled."); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static double traceOfSquaredMatrixProductcxx . . $ traceOfSquaredMatrixProduct(3d4663cac27f39ae). (constMATcxx . . $ constMAT# const& s1, constMATcxx . . $ constMAT# const& s2) { |
| 306 | // tr ((s1*s2)^2), s1, s2 are symmetric |
| 307 | autoMATcxx . . $ autoMAT# m = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (s1cxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b)., s2cxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b).); |
| 308 | const double trace = NUMtrace2cxx . . $ NUMtrace2(8213cc2c710f3dcb). (mcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), mcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 309 | return trace; |
| 310 | } |
| 311 | |
| 312 | double Covariance_getProbabilityAtPosition_stringcxx . . $ Covariance_getProbabilityAtPosition_string(6f6eaca3dda299c9). (Covariancecxx . . $ Covariance# me, conststring32cxx . . $ conststring32# vector_string) { |
| 313 | autoSTRVECcxx . . $ autoSTRVEC# vector = splitByWhitespace_STRVECcxx . . $ splitByWhitespace_STRVEC(d3c03a92d990d008). (vector_string); |
| 314 | autoVECcxx . . $ autoVEC# v = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 315 | for (integercxx . . $ integer# i = 1; i <= vector.sizecxx . . $ _autostringautovector#size.; i ++) { |
| 316 | v [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = Melder_atofcxx . . $ Melder_atof(13d725aae188c0cd). (vector [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 317 | if (i == mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.) |
| 318 | break; |
| 319 | } |
| 320 | const double prob = Covariance_getProbabilityAtPositioncxx . . $ Covariance_getProbabilityAtPosition(9672a574a0a205d1). (me, vcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 321 | return prob; |
| 322 | } |
| 323 | |
| 324 | double Covariance_getProbabilityAtPositioncxx . . $ Covariance_getProbabilityAtPosition(9672a574a0a205d1). (Covariancecxx . . $ Covariance# me, constVECcxx . . $ constVEC# x) { |
| 325 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (x.size == mycxx . . $ `melder/melder.h:44:9`! numberOfColumns, |
| 326 | U"The dimensions of the Covariance and the vector should agree."); |
| 327 | if (NUMisEmptycxx . . $ NUMisEmpty(43b61114f729c675). (mycxx . . $ `melder/melder.h:44:9`! lowerCholeskyInversecxx . . $ structSSCP#lowerCholeskyInverse..getcxx . . $ automatrix#get(b2b81450b5bdda66).())) |
| 328 | SSCP_expandWithLowerCholeskyInversecxx . . $ SSCP_expandWithLowerCholeskyInverse(b2bffa5b59e040d8). (me); |
| 329 | const double dsq = NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (mycxx . . $ `melder/melder.h:44:9`! lowerCholeskyInversecxx . . $ structSSCP#lowerCholeskyInverse..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), x, mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 330 | const double lnN = - 0.5 * (mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns. * logcxx . . $ log(fee4f1d22bc8e617). (NUM2picxx . . $ NUM2pi.) + mycxx . . $ `melder/melder.h:44:9`! lndcxx . . $ structSSCP#lnd. + dsq); |
| 331 | const double prob = expcxx . . $ exp(fee4f1d22bc8e617). (lnN); |
| 332 | return prob; |
| 333 | } |
| 334 | |
| 335 | double Covariance_getMarginalProbabilityAtPositioncxx . . $ Covariance_getMarginalProbabilityAtPosition(add1dc726be0e071). (Covariancecxx . . $ Covariance# me, constVECVUcxx . . $ constVECVU# const& vector, double x) { |
| 336 | double mu, stdev; |
| 337 | Covariance_getMarginalDensityParameterscxx . . $ Covariance_getMarginalDensityParameters(212e9081be60dafa). (me, vector, & mu, & stdev); |
| 338 | const double dx = (x - mu) / stdev; |
| 339 | const double prob = (NUM1_sqrt2picxx . . $ NUM1_sqrt2pi. / stdev) * expcxx . . $ exp(fee4f1d22bc8e617). (- 0.5 * dx * dx); |
| 340 | return prob; |
| 341 | } |
| 342 | |
| 343 | /* Precondition ||v|| = 1 */ |
| 344 | void Covariance_getMarginalDensityParameterscxx . . $ Covariance_getMarginalDensityParameters(212e9081be60dafa). (Covariancecxx . . $ Covariance# me, constVECVUcxx . . $ constVECVU# const& v, double *out_mean, double *out_stdev) { |
| 345 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (v.size == mycxx . . $ `melder/melder.h:44:9`! numberOfColumns); |
| 346 | if (out_mean) |
| 347 | *out_mean = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (v, mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 348 | if (out_stdev) { |
| 349 | longdoublecxx . . $ longdouble# stdev = 0.0; |
| 350 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) // 1xn diagonal matrix |
| 351 | for (integercxx . . $ integer# m = 1; m <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; m ++) |
| 352 | stdev += v [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).m] * mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).m] * v [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).m]; |
| 353 | else |
| 354 | for (integercxx . . $ integer# k = 1; k <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; k ++) |
| 355 | for (integercxx . . $ integer# m = 1; m <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; m ++) |
| 356 | stdev += v [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).k] * mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).k] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).m] * v [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).m]; |
| 357 | *out_stdev = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (double (stdev)); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | double Covariances_getMultivariateCentroidDifferencecxx . . $ Covariances_getMultivariateCentroidDifference(5c136fcd726efc4c). (Covariancecxx . . $ Covariance# me, Covariancecxx . . $ Covariance# thee, int equalCovariances, double *out_prob, double *out_fisher, double *out_ndof1, double *out_ndof2) { |
| 362 | const integercxx . . $ integer# nrow = mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., N = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. + thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.); |
| 363 | const integercxx . . $ integer# N1 = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.), n1 = N1 - 1; |
| 364 | const integercxx . . $ integer# N2 = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.), n2 = N2 - 1; |
| 365 | const double ndof1 = nrow; |
| 366 | double ndof2 = N - nrow - 1; |
| 367 | |
| 368 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (ndof2 >= 1.0, |
| 369 | U"Not enough observations (", N, U") for this test."); |
| 370 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (nrow <= N1 && nrow <= N2, |
| 371 | U"The number of observations should be larger than the number of variables."); |
| 372 | double dif = 0.0; |
| 373 | for (integercxx . . $ integer# i = 1; i <= nrow; i ++) { |
| 374 | const double dist = mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] - thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 375 | dif += dist * dist; |
| 376 | } |
| 377 | dif = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (dif); |
| 378 | double fisher = undefinedcxx . . $ undefined.; |
| 379 | if (equalCovariances) { |
| 380 | /* |
| 381 | Morrison, page 141 |
| 382 | */ |
| 383 | autoCovariancecxx . . $ autoCovariance# pool = Covariances_poolcxx . . $ Covariances_pool(5cd36e68b575e95f). (me, thee); |
| 384 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! data.ncol == nrow); // ppgb 20180913 |
| 385 | autoMATcxx . . $ autoMAT# s = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 386 | double lndet; |
| 387 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (s.getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & lndet); |
| 388 | |
| 389 | const double mahalanobis = NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (scxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 390 | const double hotelling_tsq = mahalanobis * N1 * N2 / N; |
| 391 | fisher = hotelling_tsq * ndof2 / ( (N - 2) * ndof1); |
| 392 | } else { |
| 393 | /* |
| 394 | Krishnamoorthy-Yu (2004): Modified Nel and Van der Merwe test |
| 395 | |
| 396 | Hotelling t^2 = (x1-x2)'*S^-1*(x1 -x2) follows nu*nrow*Fisher(nrow,nu-nrow+1)/(nu-nrow+1) |
| 397 | |
| 398 | Approximate number of degrees of freedom (their formula 7, page 164) |
| 399 | nu = (nrow+nrow^2)/((1/n1)(tr (S1*S^-1)^2 + (tr(S1*S^-1))^2)) +(1/n2)(tr (S2*S^-1)^2 + (tr(S2*S^-1))^2))) |
| 400 | the matrices S1 and S2 are the covariance matrices 'my data' and 'thy data' divided by N1 and N2 respectively. |
| 401 | S is the pooled covar divided by N. |
| 402 | */ |
| 403 | autoMATcxx . . $ autoMAT# s1 = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (nrow, nrow), s2 = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (nrow, nrow), s = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (nrow, nrow); |
| 404 | for (integercxx . . $ integer# i = 1; i <= nrow; i ++) { |
| 405 | for (integercxx . . $ integer# j = 1; j <= nrow; j ++) { |
| 406 | s1 [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] / mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 407 | s2 [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] / thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 408 | s [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = s1 [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] + s2 [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j]; |
| 409 | } |
| 410 | } |
| 411 | double lndet; |
| 412 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (s.getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & lndet); |
| 413 | /* |
| 414 | Krishan... formula 2, page 162 |
| 415 | */ |
| 416 | const double hotelling_tsq = NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (scxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 417 | |
| 418 | autoMATcxx . . $ autoMAT# si = newMATinverse_fromLowerCholeskyInversecxx . . $ newMATinverse_fromLowerCholeskyInverse(88e1bde181068261). (scxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 419 | const double tr_s1sisqr = traceOfSquaredMatrixProductcxx . . $ traceOfSquaredMatrixProduct(3d4663cac27f39ae). (s1cxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), sicxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 420 | const double tr_s1si = NUMtrace2cxx . . $ NUMtrace2(8213cc2c710f3dcb). (s1cxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), sicxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 421 | const double tr_s2sisqr = traceOfSquaredMatrixProductcxx . . $ traceOfSquaredMatrixProduct(3d4663cac27f39ae). (s2cxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), sicxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 422 | const double tr_s2si = NUMtrace2cxx . . $ NUMtrace2(8213cc2c710f3dcb). (s2cxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), sicxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 423 | |
| 424 | const double nu = (nrow + nrow * nrow) / ( (tr_s1sisqr + tr_s1si * tr_s1si) / n1 + (tr_s2sisqr + tr_s2si * tr_s2si) / n2); |
| 425 | ndof2 = nu - nrow + 1; |
| 426 | fisher = hotelling_tsq * (nu - nrow + 1) / (nu * nrow); |
| 427 | } |
| 428 | |
| 429 | if (out_prob) |
| 430 | *out_prob = NUMfisherQcxx . . $ NUMfisherQ(7678bba071383ab). (fisher, ndof1, ndof2); |
| 431 | if (out_fisher) |
| 432 | *out_fisher = fisher; |
| 433 | if (out_ndof1) |
| 434 | *out_ndof1 = ndof1; |
| 435 | if (out_ndof2) |
| 436 | *out_ndof2 = ndof2; |
| 437 | return dif; |
| 438 | } |
| 439 | |
| 440 | /* Schott 2001 */ |
| 441 | void Covariances_equalitycxx . . $ Covariances_equality(d66b820064d82689). (CovarianceListcxx . . $ CovarianceList# me, int method, double *out_prob, double *out_chisq, double *out_ndof) { |
| 442 | try { |
| 443 | |
| 444 | const integercxx . . $ integer# numberOfMatrices = mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; |
| 445 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfMatrices > 1, |
| 446 | U"We need at least two matrices"); |
| 447 | |
| 448 | autoCovariancecxx . . $ autoCovariance# pool = CovarianceList_to_Covariance_poolcxx . . $ CovarianceList_to_Covariance_pool(56bfcf47216a02f2). (me); |
| 449 | const double ns = pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfObservationscxx . . $ structSSCP#numberOfObservations. - mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; |
| 450 | const integercxx . . $ integer# nrow = pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 451 | |
| 452 | double chisq = undefinedcxx . . $ undefined., ndof = undefinedcxx . . $ undefined.; |
| 453 | if (method == 1) { |
| 454 | /* |
| 455 | Bartlett (see Morrison page 297) |
| 456 | The hypothesis H0 : Sigma [1] = .... = Sigma [k] of the equality of the covariance matrices of k nrow-dimensional |
| 457 | multinormal populations can be tested against the alternative by a modified generalized likelihood-ratio statistic. |
| 458 | Let S [i] be the unbiased estimate of Sigma [i] based on n [i] degrees of freedom, where n [i] = N [i]-1 for |
| 459 | the usual case of a random sample of N [i] observation vectors from the i-th population. When H0 is true |
| 460 | S = 1/(sum(i=1..k, n [i])) sum(i=1..k, n [i]*S [i]) |
| 461 | is the pooled estimate of the common covariance matrix. The test statistic is |
| 462 | M = sum(i=1..k,n [i])*ln|S| - sum(i=1..k, n [i]*ln|S [i]|). |
| 463 | Box (1949), "A general distribution theory for a class of likelihood criteria", |
| 464 | Biomerika, vol 36, pp. 317-346. has shown that if the scale factor |
| 465 | C^(-1) = 1 - (2p^2+3p-1)/(6(nrow+1)(k-1)) * (sum(i=1..k, 1/n [i]) - 1 / sum(i=1..k, n [i])) is introduced, |
| 466 | the quatity M/C is approximately distributed as a chi-squared variate with (k-1)nrow(nrow+1)/2 degrees of freedom |
| 467 | as the n [i] become large. |
| 468 | It is well known that this likelihood ratio test is very sensitive to violations of the normality assumption, |
| 469 | and so other more robust procedures have been proposed. |
| 470 | */ |
| 471 | double lnd; |
| 472 | try { |
| 473 | lnd = NUMdeterminant_fromSymmetricMatrixcxx . . $ NUMdeterminant_fromSymmetricMatrix(3ee797411d4a69d9). (poolcxx . . $ constmatrix#constmatrix(eed9513d24570b3d). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 474 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 475 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Pooled covariance matrix is singular."); |
| 476 | } |
| 477 | |
| 478 | double nsi = 0.0, m = ns * lnd; // First part of eq (3) page 297 |
| 479 | |
| 480 | for (integercxx . . $ integer# i = 1; i <= numberOfMatrices; i ++) { |
| 481 | const Covariancecxx . . $ Covariance# ci = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]; |
| 482 | try { |
| 483 | lnd = NUMdeterminant_fromSymmetricMatrixcxx . . $ NUMdeterminant_fromSymmetricMatrix(3ee797411d4a69d9). (cicxx . . $ constmatrix#constmatrix(eed9513d24570b3d). -> datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 484 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 485 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Covariance matrix ", i, U" is singular."); |
| 486 | } |
| 487 | nsi += 1.0 / (ci -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1); |
| 488 | m -= (ci -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1) * lnd; // Last part of eq (3) page 297 |
| 489 | } |
| 490 | /* |
| 491 | Eq (4) page 297 |
| 492 | */ |
| 493 | const double c1 = 1.0 - (2.0 * nrow * nrow + 3.0 * nrow - 1.0) / (6.0 * (nrow + 1) * (numberOfMatrices - 1)) * (nsi - 1.0 / ns); |
| 494 | |
| 495 | ndof = (numberOfMatrices - 1.0) * nrow * (nrow + 1) / 2.0; |
| 496 | chisq = m * c1; |
| 497 | } else if (method == 2) { // Schott (2001) Wald 1 |
| 498 | /* |
| 499 | T1 = sum(i=1..k, n [i]/n *tr((S [i]*S^-1)^2)- sum(i=1..k, sum(j=1..k, (n [i]/n)*(n [j]/n) *tr(S [i]*S^-1*S [j]*sS^-1))) = |
| 500 | sum(i=1..k, (ni/n -(ni/n)^2) tr((si*s^-1)^2) |
| 501 | - 2 * sum (i=1..k, sum(j=1..i-1, (ni/n)*(nj/n) *tr(si*s^-1*sj*s^-1))) |
| 502 | */ |
| 503 | double trace = 0.0; |
| 504 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), nullptr); |
| 505 | autoMATcxx . . $ autoMAT# si = newMATinverse_fromLowerCholeskyInversecxx . . $ newMATinverse_fromLowerCholeskyInverse(88e1bde181068261). (poolcxx . . $ constmatrix#constmatrix(eed9513d24570b3d). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 506 | for (integercxx . . $ integer# i = 1; i <= numberOfMatrices; i ++) { |
| 507 | const Covariancecxx . . $ Covariance# ci = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]; |
| 508 | const double ni = ci -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1; |
| 509 | autoMATcxx . . $ autoMAT# s1 = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (cicxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d). -> datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), sicxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 510 | const double trace_ii = NUMtrace2cxx . . $ NUMtrace2(8213cc2c710f3dcb). (s1cxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), s1cxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 511 | trace += (ni / ns) * (1 - (ni / ns)) * trace_ii; |
| 512 | for (integercxx . . $ integer# j = i + 1; j <= numberOfMatrices; j ++) { |
| 513 | const Covariancecxx . . $ Covariance# cj = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).j]; |
| 514 | const double nj = cj -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1; |
| 515 | autoMATcxx . . $ autoMAT# s2 = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (cjcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d). -> datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), sicxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 516 | const double trace_ij = NUMtrace2cxx . . $ NUMtrace2(8213cc2c710f3dcb). (s1cxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), s2cxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 517 | trace -= 2.0 * (ni / ns) * (nj / ns) * trace_ij; |
| 518 | } |
| 519 | } |
| 520 | ndof = (numberOfMatrices - 1) * nrow * (nrow + 1) / 2.0; |
| 521 | chisq = (ns / 2.0) * trace; |
| 522 | } else { |
| 523 | return; |
| 524 | } |
| 525 | if (out_prob) |
| 526 | *out_prob = NUMchiSquareQcxx . . $ NUMchiSquareQ(d8e7e0733fa41c87). (chisq, ndof); |
| 527 | if (out_ndof) |
| 528 | *out_ndof = ndof; |
| 529 | if (out_chisq) |
| 530 | *out_chisq = chisq; |
| 531 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 532 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Equality could not be tested."); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | double Covariance_TableOfReal_normalityTest_BHEPcxx . . $ Covariance_TableOfReal_normalityTest_BHEP(19ee1367b729c2b3). (Covariancecxx . . $ Covariance# me, TableOfRealcxx . . $ TableOfReal# thee, constVECcxx . . $ constVEC# const& responsibilities, double *inout_beta, double *out_tnb, double *out_lnmu, double *out_lnvar, bool *out_covarianceIsSingular) { |
| 537 | try { |
| 538 | const MATcxx . . $ MAT# data = thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(); |
| 539 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (data.nrow > data.ncol, |
| 540 | U"The number of data should be larger than the dimension of the data."); |
| 541 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfColumns == data.ncol, |
| 542 | U"The number of columns of the covariance and the data should be equal."); |
| 543 | |
| 544 | const bool weighting = responsibilities.sizecxx . . $ constvector#size. > 0; |
| 545 | bool covarianceIsSingular = false; |
| 546 | const double d = data.ncolcxx . . $ matrix#ncol.; |
| 547 | double n = data.nrowcxx . . $ matrix#nrow., testStatistic; |
| 548 | if (weighting) { |
| 549 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (responsibilities.size == data.nrow, |
| 550 | U"The size of the responsibilities vector should equal the number of data."); |
| 551 | n = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (responsibilitiescxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).); |
| 552 | } |
| 553 | |
| 554 | *inout_beta = ( *inout_beta > 0.0 ? NUMsqrt1_2cxx . . $ NUMsqrt1_2. / *inout_beta : NUMsqrt1_2cxx . . $ NUMsqrt1_2. * powcxx . . $ pow(b71928d39fde4295). ( (1.0 + 2.0 * d) / 4.0, 1.0 / (d + 4.0)) * powcxx . . $ pow(b71928d39fde4295). (n, 1.0 / (d + 4.0)) ); |
| 555 | const double d2 = d / 2.0, beta2 = *inout_beta * *inout_beta, gamma = 1.0 + 2.0 * beta2; |
| 556 | |
| 557 | autoVECcxx . . $ autoVEC# buf = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (data.nrowcxx . . $ matrix#nrow.); |
| 558 | |
| 559 | try { // in case the covariance matrix is singular and its inverse cannot be determined |
| 560 | SSCP_expandWithLowerCholeskyInversecxx . . $ SSCP_expandWithLowerCholeskyInverse(b2bffa5b59e040d8). (me); |
| 561 | const MATcxx . . $ MAT# lowerInverse = mycxx . . $ `melder/melder.h:44:9`! lowerCholeskyInversecxx . . $ structSSCP#lowerCholeskyInverse..getcxx . . $ automatrix#get(b2b81450b5bdda66).(); |
| 562 | /* |
| 563 | Heinze & Wagner (1997), on page 3 in the formula for W(n,beta) scaled residuals |
| 564 | Y(i)=S^(-1/2)*(X(i)-mean) are used. |
| 565 | However, we can work with the original data and use squared Mahalanobis distances in this calculation because |
| 566 | ||Y(j)-Y(k)||^2 = (X(j)-X(k))'S^-1((X(j)-X(k)), where X(i) and Y(i) are d-dimensional vectors. |
| 567 | This expression is symmetric in j and k. |
| 568 | */ |
| 569 | double doubleSum = 0.0; |
| 570 | for (integercxx . . $ integer# j = 1; j <= data.nrowcxx . . $ matrix#nrow. - 1; j ++) { |
| 571 | for (integercxx . . $ integer# k = j + 1; k <= data.nrowcxx . . $ matrix#nrow.; k ++) { |
| 572 | const double djk_sq = NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (lowerInversecxx . . $ constmatrix#constmatrix(eed9513d24570b3d)., datacxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..rowcxx . . $ matrix#row(2d545b3cb3f8e666). (j), datacxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..rowcxx . . $ matrix#row(2d545b3cb3f8e666). (k)); |
| 573 | buf [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] = expcxx . . $ exp(fee4f1d22bc8e617). (- 0.5 * beta2 * djk_sq); |
| 574 | } |
| 575 | if (weighting) |
| 576 | doubleSum += 2.0 * responsibilities [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j] * NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (bufcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (j + 1, data.nrowcxx . . $ matrix#nrow.), responsibilitiescxx . . $ constvectorview#constvectorview(fc39adb4395e9baf)..partcxx . . $ constvector#part(e8ca93054b46a634). (j + 1, data.nrowcxx . . $ matrix#nrow.)); |
| 577 | else |
| 578 | doubleSum += 2.0 * NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (bufcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (j + 1, data.nrowcxx . . $ matrix#nrow.)); |
| 579 | } |
| 580 | doubleSum += n; // contribution of all the terms in the double sum where j == k |
| 581 | |
| 582 | double singleSum = 0.0; |
| 583 | for (integercxx . . $ integer# j = 1; j <= data.nrowcxx . . $ matrix#nrow.; j ++) { |
| 584 | const double djj_sq = NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (lowerInversecxx . . $ constmatrix#constmatrix(eed9513d24570b3d)., datacxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..rowcxx . . $ matrix#row(2d545b3cb3f8e666). (j), mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 585 | buf [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = expcxx . . $ exp(fee4f1d22bc8e617). (- 0.5 * beta2 * djj_sq / (1.0 + beta2)); |
| 586 | } |
| 587 | if (weighting) |
| 588 | singleSum += NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (bufcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), responsibilitiescxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).); |
| 589 | else |
| 590 | singleSum += NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (bufcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 591 | /* |
| 592 | The test statistic is n times the W(n,beta) of page 3. |
| 593 | */ |
| 594 | testStatistic = doubleSum / n - 2.0 * powcxx . . $ pow(b71928d39fde4295). (1.0 + beta2, - d2) * singleSum + n * powcxx . . $ pow(b71928d39fde4295). (gamma, - d2); |
| 595 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 596 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 597 | testStatistic = 4.0 * n; |
| 598 | covarianceIsSingular = true; |
| 599 | } |
| 600 | const double beta4 = beta2 * beta2, beta8 = beta4 * beta4; |
| 601 | const double gamma2 = gamma * gamma, gamma4 = gamma2 * gamma2; // page 15 |
| 602 | const double delta = 1.0 + beta2 * (4.0 + 3.0 * beta2), delta2 = delta * delta; // page 15 |
| 603 | |
| 604 | const double mu = 1.0 - powcxx . . $ pow(b71928d39fde4295). (gamma, -d2) * (1.0 + d * beta2 / gamma + d * (d + 2.0) * beta4 / (2.0 * gamma2)); |
| 605 | const double var = 2.0 * powcxx . . $ pow(b71928d39fde4295). (1.0 + 4.0 * beta2, -d2) |
| 606 | + 2.0 * powcxx . . $ pow(b71928d39fde4295). (gamma, -d) * (1.0 + 2.0 * d * beta4 / gamma2 + 3.0 * d * (d + 2) * beta8 / (4.0 * gamma4)) |
| 607 | - 4.0 * powcxx . . $ pow(b71928d39fde4295). (delta, -d2) * (1.0 + 3.0 * d * beta4 / (2.0 * delta) + d * (d + 2) * beta8 / (2.0 * delta2)); |
| 608 | const double mu2 = mu * mu; |
| 609 | const double lnmu = logcxx . . $ log(fee4f1d22bc8e617). (sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mu2 * mu2 / (mu2 + var))); |
| 610 | const double lnvar = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (logcxx . . $ log(fee4f1d22bc8e617). ((mu2 + var) / mu2)); |
| 611 | if (out_lnmu) |
| 612 | *out_lnmu = lnmu; |
| 613 | if (out_lnvar) |
| 614 | *out_lnvar = lnvar; |
| 615 | if (out_tnb) |
| 616 | *out_tnb = testStatistic; |
| 617 | if (out_covarianceIsSingular) |
| 618 | *out_covarianceIsSingular = covarianceIsSingular; |
| 619 | double prob = NUMlogNormalQcxx . . $ NUMlogNormalQ(7678bba071383ab). (testStatistic, lnmu, lnvar); |
| 620 | return prob; |
| 621 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 622 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": Could not determine BHEP statistic."); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | void Covariance_differencecxx . . $ Covariance_difference(5a55ec33ac584ef2). (Covariancecxx . . $ Covariance# me, Covariancecxx . . $ Covariance# thee, double *out_prob, double *out_chisq, double *out_ndof) { |
| 627 | const integercxx . . $ integer# nrow = mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 628 | integercxx . . $ integer# numberOfObservations = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.); |
| 629 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfRows == thycxx . . $ `melder/melder.h:45:9`! numberOfRows, |
| 630 | U"Matrices should have equal dimensions."); |
| 631 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. != thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.) { |
| 632 | numberOfObservations = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations., thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.)); |
| 633 | Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"Covariance_difference: numbers of observations of the two matrices do not agree.\n" |
| 634 | U"The lesser of the two (", numberOfObservations, U") is used."); |
| 635 | } |
| 636 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfObservations > 1, |
| 637 | U"Number of observations too small."); |
| 638 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (thycxx . . $ `melder/melder.h:45:9`! data.ncol == nrow); |
| 639 | autoMATcxx . . $ autoMAT# linv = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 640 | double ln_thee; |
| 641 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (linv.getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & ln_thee); |
| 642 | const double ln_me = NUMdeterminant_fromSymmetricMatrixcxx . . $ NUMdeterminant_fromSymmetricMatrix(3ee797411d4a69d9). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 643 | /* |
| 644 | We need trace (A B^-1). We have A and the inverse L^(-1) of the |
| 645 | Cholesky decomposition L^T L of B in the lower triangle + diagonal. |
| 646 | Always: tr (A B) = tr (B A) |
| 647 | tr (A B^-1) = tr (A (L L^T)^-1) = tr (A L^-1 (L^T)^-1) |
| 648 | trace = sum(i=1..nrow, j=1..nrow, l=max(i,j)..nrow, A [i] [j]Lm [l] [j]Lm [l] [i], |
| 649 | where Lm = L^(-1) |
| 650 | */ |
| 651 | double trace = 0.0; |
| 652 | for (integercxx . . $ integer# i = 1; i <= nrow; i ++) { |
| 653 | for (integercxx . . $ integer# j = 1; j <= nrow; j ++) { |
| 654 | const integercxx . . $ integer# lp = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (j, i); |
| 655 | for (integercxx . . $ integer# l = lp; l <= nrow; l ++) |
| 656 | trace += mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] * linv [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).l] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] * linv [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).l] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | const double l = (numberOfObservations - 1) * fabscxx . . $ fabs(fee4f1d22bc8e617). (ln_thee - ln_me + trace - nrow); |
| 661 | const double chisq = l * fabscxx . . $ fabs(fee4f1d22bc8e617). (1.0 - (2.0 * nrow + 1.0 - 2.0 / (nrow + 1)) / (numberOfObservations - 1) / 6.0); |
| 662 | const double ndof = nrow * (nrow + 1) / 2.0; |
| 663 | |
| 664 | if (out_prob) |
| 665 | *out_prob = NUMchiSquareQcxx . . $ NUMchiSquareQ(d8e7e0733fa41c87). (chisq, ndof); |
| 666 | if (out_chisq) |
| 667 | *out_chisq = chisq; |
| 668 | if (out_ndof) |
| 669 | *out_ndof = ndof; |
| 670 | } |
| 671 | |
| 672 | static void checkOneIndexcxx . . $ checkOneIndex(11b4d88ee5467183). (TableOfRealcxx . . $ TableOfReal# me, integercxx . . $ integer# index) { |
| 673 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (index > 0 && index <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumns, |
| 674 | U"Index should be in interval [1, ", mycxx . . $ `melder/melder.h:44:9`! numberOfColumns, U"]."); |
| 675 | } |
| 676 | |
| 677 | static void checkTwoIndicescxx . . $ checkTwoIndices(cfadf70b559ed315). (TableOfRealcxx . . $ TableOfReal# me, integercxx . . $ integer# index1, integercxx . . $ integer# index2) { |
| 678 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (index1 > 0 && index1 <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumns && index2 > 0 && index2 <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumns, |
| 679 | U"Index should be in interval [1, ", mycxx . . $ `melder/melder.h:44:9`! numberOfColumns, U"]."); |
| 680 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (index1 != index2, |
| 681 | U"Indices should be different."); |
| 682 | } |
| 683 | |
| 684 | void Covariance_getSignificanceOfOneMeancxx . . $ Covariance_getSignificanceOfOneMean(9321ff4128b9e8f5). (Covariancecxx . . $ Covariance# me, integercxx . . $ integer# index, double mu, double *out_prob, double *out_t, double *out_ndof) { |
| 685 | const double var = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).index] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index]; |
| 686 | double prob = undefinedcxx . . $ undefined., t = undefinedcxx . . $ undefined.; |
| 687 | const double ndof = mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1.0; |
| 688 | |
| 689 | checkOneIndexcxx . . $ checkOneIndex(11b4d88ee5467183). (me, index); |
| 690 | |
| 691 | if (var > 0.0) { |
| 692 | t = (mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index] - mu) / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (var / mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.); |
| 693 | if (out_prob) |
| 694 | prob = 2.0 * NUMstudentQcxx . . $ NUMstudentQ(d8e7e0733fa41c87). (fabscxx . . $ fabs(fee4f1d22bc8e617). (t), ndof); |
| 695 | } |
| 696 | if (out_prob) |
| 697 | *out_prob = prob; |
| 698 | if (out_t) |
| 699 | *out_t = t; |
| 700 | if (out_ndof) |
| 701 | *out_ndof = ndof; |
| 702 | } |
| 703 | |
| 704 | void Covariance_getSignificanceOfMeansDifferencecxx . . $ Covariance_getSignificanceOfMeansDifference(870744d08ab6bf0). (Covariancecxx . . $ Covariance# me, integercxx . . $ integer# index1, integercxx . . $ integer# index2, double mu, int paired, int equalVariances, double *out_prob, double *out_t, double *out_ndof) { |
| 705 | integercxx . . $ integer# n = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.); |
| 706 | |
| 707 | double prob = undefinedcxx . . $ undefined., t = undefinedcxx . . $ undefined.; |
| 708 | double ndof = 2.0 * (n - 1); |
| 709 | |
| 710 | checkTwoIndicescxx . . $ checkTwoIndices(cfadf70b559ed315). (me, index1, index2); |
| 711 | |
| 712 | const double var1 = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).index1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index1]; |
| 713 | const double var2 = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).index2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index2]; |
| 714 | |
| 715 | double var_pooled = var1 + var2; |
| 716 | if (var_pooled == 0.0) |
| 717 | Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"The pooled variance turned out to be zero. Check your data."); |
| 718 | else { |
| 719 | if (paired) { |
| 720 | var_pooled -= 2.0 * mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).index1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index2]; |
| 721 | ndof /= 2.0; |
| 722 | } |
| 723 | if (var_pooled == 0.0) { |
| 724 | Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"The pooled variance with the paired correction turned out to be zero. "); |
| 725 | prob = 0.0; |
| 726 | } else { |
| 727 | t = (mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index1] - mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index2] - mu) / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (var_pooled / n); |
| 728 | /* |
| 729 | Return two sided probability. |
| 730 | */ |
| 731 | if (equalVariances) |
| 732 | prob = 2.0 * NUMstudentQcxx . . $ NUMstudentQ(d8e7e0733fa41c87). (fabscxx . . $ fabs(fee4f1d22bc8e617). (t), ndof); |
| 733 | else { |
| 734 | ndof = (1.0 + 2.0 * var1 * var2 / (var1 * var1 + var2 * var2)) * (n - 1); |
| 735 | prob = NUMincompleteBetacxx . . $ NUMincompleteBeta(7678bba071383ab). (ndof / 2.0, 0.5, ndof / (ndof + t * t)); |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | if (out_prob) |
| 740 | *out_prob = prob; |
| 741 | if (out_t) |
| 742 | *out_t = t; |
| 743 | if (out_ndof) |
| 744 | *out_ndof = ndof; |
| 745 | } |
| 746 | |
| 747 | void Covariance_getSignificanceOfOneVariancecxx . . $ Covariance_getSignificanceOfOneVariance(9321ff4128b9e8f5). (Covariancecxx . . $ Covariance# me, integercxx . . $ integer# index, double sigmasq, double *out_prob, double *out_chisq, double *out_ndof) { |
| 748 | const double var = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).index] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index]; |
| 749 | double prob = undefinedcxx . . $ undefined., chisq = undefinedcxx . . $ undefined.; |
| 750 | const double ndof = mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1.0; |
| 751 | |
| 752 | checkOneIndexcxx . . $ checkOneIndex(11b4d88ee5467183). (me, index); |
| 753 | |
| 754 | if (var > 0.0) { |
| 755 | chisq = ndof; |
| 756 | if (sigmasq > 0.0) |
| 757 | chisq = ndof * var / sigmasq; |
| 758 | if (out_prob) |
| 759 | prob = NUMchiSquareQcxx . . $ NUMchiSquareQ(d8e7e0733fa41c87). (chisq, ndof); |
| 760 | } |
| 761 | if (out_prob) |
| 762 | *out_prob = prob; |
| 763 | if (out_chisq) |
| 764 | *out_chisq = chisq; |
| 765 | if (out_ndof) |
| 766 | *out_ndof = ndof; |
| 767 | } |
| 768 | |
| 769 | void Covariance_getSignificanceOfVariancesRatiocxx . . $ Covariance_getSignificanceOfVariancesRatio(675793c557b0208f). (Covariancecxx . . $ Covariance# me, integercxx . . $ integer# index1, integercxx . . $ integer# index2, double ratio, double *out_prob, double *out_f, double *out_ndof) { |
| 770 | const double ndof = mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1.0; |
| 771 | double prob = undefinedcxx . . $ undefined., f = undefinedcxx . . $ undefined.; |
| 772 | checkTwoIndicescxx . . $ checkTwoIndices(cfadf70b559ed315). (me, index1, index2); |
| 773 | |
| 774 | const double var1 = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).index1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index1]; |
| 775 | const double var2 = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).index2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index2]; |
| 776 | |
| 777 | if (var1 > 0.0 && var2 > 0.0) { |
| 778 | double ratio2 = (var1 / var2) / ratio; |
| 779 | f = ratio2; |
| 780 | if (var2 > var1) |
| 781 | ratio2 = (var2 / var1) * ratio; |
| 782 | if (out_prob) { |
| 783 | prob = 2.0 * NUMfisherQcxx . . $ NUMfisherQ(7678bba071383ab). (ratio2, ndof, ndof); |
| 784 | if (prob > 1.0) |
| 785 | prob = 2.0 - prob; |
| 786 | } |
| 787 | } |
| 788 | if (out_prob) |
| 789 | *out_prob = prob; |
| 790 | if (out_ndof) |
| 791 | *out_ndof = ndof; |
| 792 | if (out_f) |
| 793 | *out_f = f; |
| 794 | } |
| 795 | |
| 796 | /* End of file Covariance.cpp */ |