| 1 | /* GaussianMixture.cpp |
| 2 | * |
| 3 | * Copyright (C) 2011-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 | /* |
| 20 | djmw 20101021 Initial version |
| 21 | */ |
| 22 | #include "Distributions_and_Strings.h"cxx . . $ `<file>/stat/Distributions_and_Strings.h`/ |
| 23 | #include "GaussianMixture.h"cxx . . $ `<file>/dwtools/GaussianMixture.h`/ |
| 24 | #include "NUMmachar.h"cxx . . $ `<file>/dwsys/NUMmachar.h`/ |
| 25 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 26 | #include "Strings_extensions.h"cxx . . $ `<file>/dwtools/Strings_extensions.h`/ |
| 27 | |
| 28 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 29 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 30 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 31 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 32 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 33 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 34 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 35 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 36 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 37 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 38 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 39 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 40 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 41 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 42 | #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/ |
| 43 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 44 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 45 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 46 | |
| 47 | #include "enums_getText.h"cxx . . $ `<file>/melder/enums_getText.h`/ |
| 48 | #include "GaussianMixture_enums.h"cxx . . $ `<file>/dwtools/GaussianMixture_enums.h`/ |
| 49 | #include "enums_getValue.h"cxx . . $ `<file>/melder/enums_getValue.h`/ |
| 50 | #include "GaussianMixture_enums.h"cxx . . $ `<file>/dwtools/GaussianMixture_enums.h`/ |
| 51 | |
| 52 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (GaussianMixture, Daata, 0); |
| 53 | |
| 54 | void structGaussianMixture :: v1_info () { |
| 55 | structDaatacxx . . $ structDaata# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 56 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Number of components: ", ourcxx . . $ `melder/melder.h:42:9`! numberOfComponents); |
| 57 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Dimension of component: ", ourcxx . . $ `melder/melder.h:42:9`! dimension); |
| 58 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Mixing probabilities:"); |
| 59 | for (integercxx . . $ integer# im = 1; im <= numberOfComponents; im ++) |
| 60 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U" ", im, U": p = ", ourcxx . . $ `melder/melder.h:42:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).im], U" Name = \"", Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (ourcxx . . $ `melder/melder.h:42:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im]), U"\""); |
| 61 | } |
| 62 | |
| 63 | static integercxx . . $ integer# GaussianMixture_getNumberOfParametersInComponentcxx . . $ GaussianMixture_getNumberOfParametersInComponent(8a2166766d4fa51c). (GaussianMixture me) { |
| 64 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! covariances->size > 0); |
| 65 | const Covariancecxx . . $ Covariance# thee = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).1]; |
| 66 | return ( thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1 ? 2 * thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns. : thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns. * (thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns. + 3) / 2 ); |
| 67 | } |
| 68 | |
| 69 | static double GaussianMixture_getLikelihoodValuecxx . . $ GaussianMixture_getLikelihoodValue(97c1d46bb5e56950). (GaussianMixture me, constMATcxx . . $ constMAT# const& probabilities, kGaussianMixtureCriterion criterion) { |
| 70 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (probabilities.ncol == mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 71 | U"The number of columns in the probabilities should equal the number of components."); |
| 72 | const integercxx . . $ integer# numberOfData = probabilities.nrowcxx . . $ constmatrix#nrow.; |
| 73 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfData > mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 74 | U"The number of rows in the probabilities should be larger than the number of components."); |
| 75 | if (criterion == kGaussianMixtureCriterion::COMPLETE_DATA_ML) { |
| 76 | /* |
| 77 | Bishop eq. 9.40 (we rewrote ln(a)+ln(b) = ln (a*b)): |
| 78 | ln(p(X,Z|μ,S,π)= sum(n=1...N, sum (k=1...K, gamma [n] [k])*ln (π [k]*N(x [n]|μ [k],S [k])), |
| 79 | where gamma [n] [k] = mixingProbablities [k] * probabilities [n] [k] / |
| 80 | sum(1...K, mixingProbablities [k] * probabilities [n] [k]) |
| 81 | */ |
| 82 | longdoublecxx . . $ longdouble# lnpcd = 0.0; |
| 83 | for (integercxx . . $ integer# irow = 1; irow <= numberOfData; irow ++) { |
| 84 | longdoublecxx . . $ longdouble# psum = 0.0, lnsum = 0.0; |
| 85 | for (integercxx . . $ integer# icol = 1; icol <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; icol ++) { |
| 86 | const longdoublecxx . . $ longdouble# pp = mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icol] * probabilities [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).irow] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).icol]; |
| 87 | psum += pp; |
| 88 | lnsum += pp * logcxx . . $ log(cd6880bb7604d12e). (pp); // scaling outside the loop |
| 89 | } |
| 90 | if (psum > 0) |
| 91 | lnpcd += lnsum / psum; // scaling: as responsibilities |
| 92 | } |
| 93 | return lnpcd; |
| 94 | } |
| 95 | /* |
| 96 | The common factor for the following criteria is the log(likelihood), Bishop eq. 9.28 |
| 97 | */ |
| 98 | longdoublecxx . . $ longdouble# lnp = 0.0; |
| 99 | for (integercxx . . $ integer# irow = 1; irow <= numberOfData; irow ++) { |
| 100 | const double psum = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), probabilitiescxx . . $ constvectorview#constvectorview(fc39adb4395e9baf)..rowcxx . . $ constmatrix#row(aae4ad074ae650ba). (irow)); |
| 101 | if (psum > 0.0) |
| 102 | lnp += (longdoublecxx . . $ longdouble#) logcxx . . $ log(fee4f1d22bc8e617). (psum); |
| 103 | } |
| 104 | |
| 105 | if (criterion == kGaussianMixtureCriterion::LIKELIHOOD) |
| 106 | return lnp; |
| 107 | |
| 108 | const double numberOfParametersPerComponent = GaussianMixture_getNumberOfParametersInComponentcxx . . $ GaussianMixture_getNumberOfParametersInComponent(8a2166766d4fa51c). (me); |
| 109 | const double numberOfParametersTotal = numberOfParametersPerComponent * mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; |
| 110 | if (criterion == kGaussianMixtureCriterion::MESSAGE_LENGTH) { |
| 111 | /* |
| 112 | Equation (15) in |
| 113 | Figueiredo & Jain, Unsupervised Learning of Finite Mixture Models : |
| 114 | IEEE TRANSACTIONS ON PATTERN ANALYSIS AND MACHINE INTELLIGENCE, VOL. 24, NO. 3, MARCH 2002 |
| 115 | |
| 116 | L(theta,Y)= N/2*sum(m=1..k, log(n*alpha [m]/12)) +k/2*ln(n/12) +k(N+1)/2 |
| 117 | - log (sum(i=1..n, sum(m=1..k, alpha [k]*p(k)))) |
| 118 | */ |
| 119 | longdoublecxx . . $ longdouble# logmpn = 0.0; |
| 120 | integercxx . . $ integer# numberOfNonZeroComponents = 0; |
| 121 | for (integercxx . . $ integer# ic = 1; ic <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; ic ++) |
| 122 | if (mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic] > 0) { |
| 123 | logmpn += logcxx . . $ log(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic]); |
| 124 | numberOfNonZeroComponents ++; |
| 125 | } |
| 126 | /* |
| 127 | A rewritten L(theta,Y) is |
| 128 | */ |
| 129 | return lnp - 0.5 * numberOfNonZeroComponents * (numberOfParametersPerComponent + 1) * (logcxx . . $ log(fee4f1d22bc8e617). (numberOfData / 12.0) + 1.0) |
| 130 | - 0.5 * numberOfParametersPerComponent * logmpn; |
| 131 | } else if (criterion == kGaussianMixtureCriterion::BAYES_INFORMATION) |
| 132 | return 2.0 * lnp - numberOfParametersTotal * logcxx . . $ log(7759b11ec49fe502). (numberOfData); |
| 133 | else if (criterion == kGaussianMixtureCriterion::AKAIKE_INFORMATION) |
| 134 | return 2.0 * (lnp - numberOfParametersTotal); |
| 135 | else if (criterion == kGaussianMixtureCriterion::AKAIKE_CORRECTED) { |
| 136 | return 2.0 * (lnp - numberOfParametersTotal * (numberOfData / (numberOfData - numberOfParametersTotal - 1.0))); |
| 137 | } |
| 138 | return lnp; |
| 139 | } |
| 140 | |
| 141 | static void GaussianMixture_getResponsibilitiescxx . . $ GaussianMixture_getResponsibilities(b8ab51b15584a097). (GaussianMixture me, constMATVUcxx . . $ constMATVU# const& probabilities, integercxx . . $ integer# componentToUpdate, MATcxx . . $ MAT# const& responsibilities) { |
| 142 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (responsibilities.nrow == probabilities.nrow && responsibilities.ncol == probabilities.ncol, |
| 143 | U"The responsibilities and the probabilities should have the same dimensions."); |
| 144 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (responsibilities.ncol == mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 145 | U"The number of columns of the responsibilities should equal the number of components."); |
| 146 | const integercxx . . $ integer# fromComponent = componentToUpdate == 0 ? 1 : componentToUpdate; |
| 147 | const integercxx . . $ integer# toComponent = componentToUpdate == 0 ? mycxx . . $ `melder/melder.h:44:9`! numberOfComponents : componentToUpdate; |
| 148 | |
| 149 | for (integercxx . . $ integer# irow = 1; irow <= probabilities.nrowcxx . . $ constmatrixview#nrow.; irow ++) { |
| 150 | responsibilitiescxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..rowcxx . . $ matrix#row(2d545b3cb3f8e666). (irow).partcxx . . $ vector#part(ae267db9e2c04be7). (fromComponent, toComponent) <<=cxx . . $ `operator<<=`(74defdd9e9be7864). probabilities.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (irow).partcxx . . $ constvectorview#part(27d925039583bea4). (fromComponent, toComponent) *cxx . . $ `operator*`(8a83e1da43fd4c40). mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities.partcxx . . $ autovector#part(2d8d48654ca64e70). (fromComponent, toComponent); |
| 151 | } |
| 152 | /* |
| 153 | Maintain the invariant. |
| 154 | */ |
| 155 | MATnormalizeRows_inplacecxx . . $ MATnormalizeRows_inplace(2fdf026ca2b4c21). (responsibilitiescxx . . $ matrixview#matrixview(eed9513d24570b3d)., 1.0, 1.0); |
| 156 | } |
| 157 | |
| 158 | static void GaussianMixture_updateComponentcxx . . $ GaussianMixture_updateComponent(6f1ed30d7567a247). (GaussianMixture me, integercxx . . $ integer# component, MATVUcxx . . $ MATVU# const& data, MATVUcxx . . $ MATVU# const& responsibilities) { |
| 159 | integercxx . . $ integer# numberOfData = data.nrowcxx . . $ matrixview#nrow.; |
| 160 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dimension == data.ncol, |
| 161 | U"The number of columns in the data and the dimension of the GaussianMixture should be equal."); |
| 162 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents == responsibilities.ncol, |
| 163 | U"The number of components and the number of columns in the responsibilities should conform."); |
| 164 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (responsibilities.nrow == data.nrow, |
| 165 | U"The number of rows in the data and the responsibilities should conform."); |
| 166 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (component > 0 && component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 167 | U"The component number should be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, U"."); |
| 168 | |
| 169 | const Covariancecxx . . $ Covariance# thee = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 170 | /* |
| 171 | Update the means: Bishop eq. 9.24 |
| 172 | */ |
| 173 | thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 174 | for (integercxx . . $ integer# irow = 1; irow <= numberOfData; irow ++) |
| 175 | thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).() +=cxx . . $ `operator+=`(d166bd5b5c96d95c). responsibilities [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).component] *cxx . . $ `operator*`(7f4bd74ab5a11911). datacxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..rowcxx . . $ matrixview#row(a8b9553e85c2b1fc). (irow); |
| 176 | |
| 177 | const double totalComponentResponsibility = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (responsibilitiescxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ matrixview#column(a8b9553e85c2b1fc). (component)); |
| 178 | thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585). () /=cxx . . $ `operator/=`(c555d170d72ff256). totalComponentResponsibility; |
| 179 | /* |
| 180 | update covariance with the new mean: Bishop eq. 9.25 |
| 181 | */ |
| 182 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() <<=cxx . . $ `operator<<=`(50b469ae42a42f64). 0.0; |
| 183 | autoVECcxx . . $ autoVEC# dif = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 184 | if (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) { // 1xn covariance |
| 185 | autoVECcxx . . $ autoVEC# variance = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 186 | for (integercxx . . $ integer# irow = 1; irow <= numberOfData; irow ++) { |
| 187 | dif.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(ce74bae6c180f663). datacxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..rowcxx . . $ matrixview#row(a8b9553e85c2b1fc). (irow) -cxx . . $ operator-(fcdbb1279f7b512e). thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).(); |
| 188 | variance.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(74defdd9e9be7864). difcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).() *cxx . . $ `operator*`(8a83e1da43fd4c40). difcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).(); |
| 189 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) +=cxx . . $ `operator+=`(d166bd5b5c96d95c). responsibilities [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).component] *cxx . . $ `operator*`(7f4bd74ab5a11911). variancecxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(); |
| 190 | } |
| 191 | } else { // nxn covariance |
| 192 | autoMATcxx . . $ autoMAT# covar = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns., thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 193 | for (integercxx . . $ integer# irow = 1; irow <= numberOfData; irow ++) { |
| 194 | dif.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(ce74bae6c180f663). datacxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..rowcxx . . $ matrixview#row(a8b9553e85c2b1fc). (irow) -cxx . . $ operator-(fcdbb1279f7b512e). thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).(); |
| 195 | outer_MAT_outcxx . . $ outer_MAT_out(2869a0b58d3aa0f4). (covarcxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), difcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), difcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 196 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).() +=cxx . . $ `operator+=`(b78e8a4998bd3572). responsibilities [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).component] *cxx . . $ `operator*`(63c818071e7f3db9). covarcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(); |
| 197 | } |
| 198 | } |
| 199 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).() /=cxx . . $ `operator/=`(50b469ae42a42f64). totalComponentResponsibility; |
| 200 | thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. = mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component] * numberOfData; |
| 201 | } |
| 202 | |
| 203 | static void GaussianMixture_setDefaultMixtureNamescxx . . $ GaussianMixture_setDefaultMixtureNames(a29c70c932200d85). (GaussianMixture me) { |
| 204 | for (integercxx . . $ integer# component = 1; component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; component ++) { |
| 205 | Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 206 | Thing_setNamecxx . . $ Thing_setName(b5a42e5e01c267b5). (cov, Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (U"c", component)); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static void GaussianMixture_addCovarianceFractioncxx . . $ GaussianMixture_addCovarianceFraction(4b9376bd8a90c35f). (GaussianMixture me, integercxx . . $ integer# component, Covariancecxx . . $ Covariance# him, double fraction) { |
| 211 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (component > 0 && component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 212 | U"The component number should be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, U"."); |
| 213 | const Covariancecxx . . $ Covariance# thee = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 214 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (thycxx . . $ `melder/melder.h:45:9`! numberOfColumns == hiscxx . . $ `melder/melder.h:47:9`! numberOfColumns, |
| 215 | U"The dimension of the Covariance should equal the dimension of the covariances in the GaussianMixture."); |
| 216 | /* |
| 217 | Prevent instability: add lambda fraction of global covariances |
| 218 | */ |
| 219 | if (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) |
| 220 | if (hiscxx . . $ `melder/melder.h:47:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) |
| 221 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) +=cxx . . $ `operator+=`(d166bd5b5c96d95c). fraction *cxx . . $ `operator*`(7f4bd74ab5a11911). hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); |
| 222 | else |
| 223 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) +=cxx . . $ `operator+=`(d166bd5b5c96d95c). fraction *cxx . . $ `operator*`(7f4bd74ab5a11911). hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data..diagonalcxx . . $ automatrix#diagonal(a26726dad87555bb).(); |
| 224 | else |
| 225 | if (hiscxx . . $ `melder/melder.h:47:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) |
| 226 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..diagonalcxx . . $ automatrix#diagonal(a26726dad87555bb).() +=cxx . . $ `operator+=`(adea042513329390). hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); |
| 227 | else |
| 228 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).() +=cxx . . $ `operator+=`(b78e8a4998bd3572). hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).() *cxx . . $ `operator*`(3dd25102fdc24343). fraction; |
| 229 | } |
| 230 | |
| 231 | static void GaussianMixture_setColumnLabelsFromTableOfRealcxx . . $ GaussianMixture_setColumnLabelsFromTableOfReal(6d8d040d5c79cca5). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee) { |
| 232 | for (integercxx . . $ integer# component = 1; component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; component ++) { |
| 233 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 234 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! dimension; j ++) |
| 235 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (cov, j, thycxx . . $ `melder/melder.h:45:9`! columnLabelscxx . . $ structTableOfReal#columnLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).j].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // only from big to reduced or same |
| 240 | static void Covariance_into_Covariancecxx . . $ Covariance_into_Covariance(391dfbc253c76f83). (Covariancecxx . . $ Covariance# me, Covariancecxx . . $ Covariance# thee) { |
| 241 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfColumns == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 242 | U"Dimensions should be equal."); |
| 243 | SSCP_unExpandcxx . . $ SSCP_unExpand(b2bffa5b59e040d8). (thee); // to its original state |
| 244 | thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. = mycxx . . $ `melder/melder.h:44:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 245 | thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).(); |
| 246 | 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).(); |
| 247 | |
| 248 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.) { |
| 249 | thycxx . . $ `melder/melder.h:45:9`! rowLabelscxx . . $ structTableOfReal#rowLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).() <<=cxx . . $ `operator<<=`(5bcbc9f49fd394e1). mycxx . . $ `melder/melder.h:44:9`! rowLabelscxx . . $ structTableOfReal#rowLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).(); |
| 250 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).(); |
| 251 | } else { |
| 252 | for (integercxx . . $ integer# ir = 1; ir <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; ir ++) |
| 253 | for (integercxx . . $ integer# ic = ir; ic <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; ic ++) { |
| 254 | const integercxx . . $ integer# dij = ic - ir; |
| 255 | if (dij < thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.) |
| 256 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).dij + 1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ic] = mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ir] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ic]; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | conststring32cxx . . $ conststring32# GaussianMixture_criterionTextcxx . . $ GaussianMixture_criterionText(185f836f08e9b4fd). (kGaussianMixtureCriterion criterion) { |
| 262 | if (criterion == kGaussianMixtureCriterion::LIKELIHOOD) |
| 263 | return U"(1/n)*Likelihood"; |
| 264 | else if (criterion == kGaussianMixtureCriterion::MESSAGE_LENGTH) |
| 265 | return U"(1/n)*Messagelength"; |
| 266 | else if (criterion == kGaussianMixtureCriterion::BAYES_INFORMATION) |
| 267 | return U"(1/n)*BayesInformation"; |
| 268 | else if (criterion == kGaussianMixtureCriterion::AKAIKE_INFORMATION) |
| 269 | return U"(1/n)*AkaikeInformation"; |
| 270 | else if (criterion == kGaussianMixtureCriterion::AKAIKE_CORRECTED) |
| 271 | return U"(1/n)*AkaikeCorrected"; |
| 272 | else if (criterion == kGaussianMixtureCriterion::COMPLETE_DATA_ML) |
| 273 | return U"(1/n)*CompleteDataML"; |
| 274 | else |
| 275 | return U"???"; |
| 276 | } |
| 277 | |
| 278 | void GaussianMixture_removeUnsupportedComponentscxx . . $ GaussianMixture_removeUnsupportedComponents(a29c70c932200d85). (GaussianMixture me) { |
| 279 | integercxx . . $ integer# numberOfComponents = mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; |
| 280 | for (integercxx . . $ integer# component = numberOfComponents; component > 0; component --) { |
| 281 | if (mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component] <= 0.0) { |
| 282 | mycxx . . $ `melder/melder.h:44:9`! covariances ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). removeItemcxx . . $ CollectionOf#removeItem(1624ee8275b10bda). (component); |
| 283 | mycxx . . $ `melder/melder.h:44:9`! numberOfComponents --; |
| 284 | for (integercxx . . $ integer# ic = component; ic <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; ic ++) |
| 285 | mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic] = mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic + 1]; |
| 286 | } |
| 287 | } |
| 288 | mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); // they stay normalized |
| 289 | } |
| 290 | |
| 291 | autoGaussianMixture GaussianMixture_createcxx . . $ GaussianMixture_create(261625e4cf0d6340). (integercxx . . $ integer# numberOfComponents, integercxx . . $ integer# dimension, kGaussianMixtureStorage storage) { |
| 292 | try { |
| 293 | autoGaussianMixture me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (GaussianMixture); |
| 294 | mycxx . . $ `melder/melder.h:44:9`! numberOfComponents = numberOfComponents; |
| 295 | mycxx . . $ `melder/melder.h:44:9`! dimension = dimension; |
| 296 | mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfComponents); |
| 297 | mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(c555d170d72ff256). 1.0 / numberOfComponents; |
| 298 | mycxx . . $ `melder/melder.h:44:9`! covariances =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). CovarianceList_createcxx . . $ CovarianceList_create(cc9e371c8f705d5c). (); |
| 299 | kSSCPstoragecxx . . $ kSSCPstorage# sscpStorage = storage == kGaussianMixtureStorage::DIAGONALS ? kSSCPstoragecxx . . $ kSSCPstorage#::DIAGONALcxx . . $ kSSCPstorage#DIAGONAL. : kSSCPstoragecxx . . $ kSSCPstorage#::COMPLETEcxx . . $ kSSCPstorage#COMPLETE.; |
| 300 | for (integercxx . . $ integer# component = 1; component <= numberOfComponents; component ++) { |
| 301 | autoCovariancecxx . . $ autoCovariance# cov = Covariance_create_reduceStoragecxx . . $ Covariance_create_reduceStorage(205004f52690bd11). (dimension, sscpStorage); |
| 302 | mycxx . . $ `melder/melder.h:44:9`! covariances ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). addItemAtPosition_movecxx . . $ OrderedOf#addItemAtPosition_move(ca501a207ce7a6c1). (covcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).(), component); |
| 303 | } |
| 304 | GaussianMixture_setDefaultMixtureNamescxx . . $ GaussianMixture_setDefaultMixtureNames(a29c70c932200d85). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 305 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 306 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 307 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"GaussianMixture not created."); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void GaussianMixture_generateOneVector_inlinecxx . . $ GaussianMixture_generateOneVector_inline(baab5f7d4314310c). (GaussianMixture me, VECcxx . . $ VEC# const& c, autostring32cxx . . $ autostring32# *out_covname, VECcxx . . $ VEC# const& buf) { |
| 312 | try { |
| 313 | const double p = NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0.0, 1.0); |
| 314 | const integercxx . . $ integer# component = NUMgetIndexFromProbabilitycxx . . $ NUMgetIndexFromProbability(38981f9d60616ce2). (mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), p); |
| 315 | Covariancecxx . . $ Covariance# thee = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 316 | if (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) { // 1xn reduced form |
| 317 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! dimension; i ++) |
| 318 | c [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = NUMrandomGausscxx . . $ NUMrandomGauss(d8e7e0733fa41c87). (thycxx . . $ `melder/melder.h:45:9`! centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i], sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i])); |
| 319 | } else { // nxn |
| 320 | if (! thycxx . . $ `melder/melder.h:45:9`! pcacxx . . $ structSSCP#pca.) |
| 321 | SSCP_expandWithPCAcxx . . $ SSCP_expandWithPCA(b2bffa5b59e040d8). (thee); // on demand expanding |
| 322 | Covariance_PCA_generateOneVector_inlinecxx . . $ Covariance_PCA_generateOneVector_inline(8fd3da726b298de8). (thee, thycxx . . $ `melder/melder.h:45:9`! pcacxx . . $ structSSCP#pca..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), ccxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)., buf); |
| 323 | } |
| 324 | if (out_covname) { |
| 325 | autostring32cxx . . $ autostring32# name = Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (thycxx . . $ `melder/melder.h:45:9`! namecxx . . $ structThing#name..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 326 | *out_covname =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). name.movecxx . . $ _autostring#move(bd43c759570f178f).(); |
| 327 | } |
| 328 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 329 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": vector not generated."); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | autoGaussianMixture TableOfReal_to_GaussianMixture_fromRowLabelscxx . . $ TableOfReal_to_GaussianMixture_fromRowLabels(636f10e8375fac0). (TableOfRealcxx . . $ TableOfReal# me, kGaussianMixtureStorage storage) { |
| 334 | try { |
| 335 | autoStringscxx . . $ autoStrings# rowLabels = TableOfReal_extractRowLabelscxx . . $ TableOfReal_extractRowLabels(397082e846780902). (me); |
| 336 | autoDistributionscxx . . $ autoDistributions# dist = Strings_to_Distributionscxx . . $ Strings_to_Distributions(51ca0faf08ce563). (rowLabels.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 337 | const integercxx . . $ integer# numberOfComponents = dist ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 338 | |
| 339 | autoGaussianMixture thee = GaussianMixture_createcxx . . $ GaussianMixture_create(261625e4cf0d6340). (numberOfComponents, mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns., storage); |
| 340 | |
| 341 | GaussianMixture_setColumnLabelsFromTableOfRealcxx . . $ GaussianMixture_setColumnLabelsFromTableOfReal(6d8d040d5c79cca5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me); |
| 342 | |
| 343 | for (integercxx . . $ integer# component = 1; component <= numberOfComponents; component ++) { |
| 344 | autoTableOfRealcxx . . $ autoTableOfReal# tab = TableOfReal_extractRowsWhoseLabelcxx . . $ TableOfReal_extractRowsWhoseLabel(a7581f983d80c338). (me, kMelder_stringcxx . . $ kMelder_string#::EQUAL_TOcxx . . $ kMelder_string#EQUAL_TO., dist ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).component].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 345 | autoCovariancecxx . . $ autoCovariance# cov = TableOfReal_to_Covariancecxx . . $ TableOfReal_to_Covariance(3e7df3a959f2f2b3). (tab.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 346 | Covariance_into_Covariancecxx . . $ Covariance_into_Covariance(391dfbc253c76f83). (cov.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thycxx . . $ `melder/melder.h:45:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]); |
| 347 | Thing_setNamecxx . . $ Thing_setName(b5a42e5e01c267b5). (thycxx . . $ `melder/melder.h:45:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component], dist ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).component].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 348 | } |
| 349 | thycxx . . $ `melder/melder.h:45:9`! mixingProbabilities.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). distcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (1).partcxx . . $ vectorview#part(fd45ed87dd839590). (1, dist ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfRowscxx . . $ structTableOfReal#numberOfRows.); |
| 350 | thycxx . . $ `melder/melder.h:45:9`! mixingProbabilities.allcxx . . $ autovector#all(a26726dad87555bb).() /=cxx . . $ `operator/=`(c555d170d72ff256). mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 351 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 352 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 353 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no GaussianMixture created."); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | autoCovariancecxx . . $ autoCovariance# GaussianMixture_to_Covariance_betweencxx . . $ GaussianMixture_to_Covariance_between(2376c2b32bfa09b5). (GaussianMixture me) { |
| 358 | return CovarianceList_to_Covariance_betweencxx . . $ CovarianceList_to_Covariance_between(56bfcf47216a02f2). (mycxx . . $ `melder/melder.h:44:9`! covariances.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 359 | } |
| 360 | autoCovariancecxx . . $ autoCovariance# GaussianMixture_to_Covariance_withincxx . . $ GaussianMixture_to_Covariance_within(2376c2b32bfa09b5). (GaussianMixture me) { |
| 361 | return CovarianceList_to_Covariance_withincxx . . $ CovarianceList_to_Covariance_within(56bfcf47216a02f2). (mycxx . . $ `melder/melder.h:44:9`! covariances.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 362 | } |
| 363 | |
| 364 | autoCovariancecxx . . $ autoCovariance# GaussianMixture_to_Covariance_totalcxx . . $ GaussianMixture_to_Covariance_total(2376c2b32bfa09b5). (GaussianMixture me) { |
| 365 | try { |
| 366 | autoCovariancecxx . . $ autoCovariance# thee = GaussianMixture_to_Covariance_betweencxx . . $ GaussianMixture_to_Covariance_between(2376c2b32bfa09b5). (me); |
| 367 | autoCovariancecxx . . $ autoCovariance# within = GaussianMixture_to_Covariance_withincxx . . $ GaussianMixture_to_Covariance_within(2376c2b32bfa09b5). (me); |
| 368 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() +=cxx . . $ `operator+=`(a7d93ba40fe22fe0). withincxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).(); |
| 369 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 370 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 371 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Covariance (total) created."); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | autoCovariancecxx . . $ autoCovariance# GaussianMixture_extractComponentcxx . . $ GaussianMixture_extractComponent(65c3e21f6edc0039). (GaussianMixture me, integercxx . . $ integer# component) { |
| 376 | try { |
| 377 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (component > 0 && component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 378 | U"The component should be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, U"."); |
| 379 | autoCovariancecxx . . $ autoCovariance# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]); |
| 380 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 381 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 382 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no component extracted."); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_extractMixingProbabilitiescxx . . $ GaussianMixture_extractMixingProbabilities(920e598c5b3205c4). (GaussianMixture me) { |
| 387 | try { |
| 388 | autoTableOfRealcxx . . $ autoTableOfReal# thee = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, 2); |
| 389 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1, U"componentProbability"); |
| 390 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 2, U"numberOfObservations"); |
| 391 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im ++) { |
| 392 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im]; |
| 393 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).im] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).im]; |
| 394 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).im] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] = cov -> numberOfObservationscxx . . $ structSSCP#numberOfObservations.; |
| 395 | TableOfReal_setRowLabelcxx . . $ TableOfReal_setRowLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), im, Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (cov)); |
| 396 | } |
| 397 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 398 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 399 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no mixing probabilities extracted."); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_extractCentroidscxx . . $ GaussianMixture_extractCentroids(920e598c5b3205c4). (GaussianMixture me) { |
| 404 | try { |
| 405 | autoTableOfRealcxx . . $ autoTableOfReal# thee = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, mycxx . . $ `melder/melder.h:44:9`! dimension); |
| 406 | |
| 407 | for (integercxx . . $ integer# component = 1; component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; component ++) { |
| 408 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 409 | if (component == 1) |
| 410 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! dimension; j ++) |
| 411 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), j, cov -> columnLabelscxx . . $ structTableOfReal#columnLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).j].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 412 | TableOfReal_setRowLabelcxx . . $ TableOfReal_setRowLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), component, Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (cov)); |
| 413 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (component) <<=cxx . . $ `operator<<=`(adea042513329390). covcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). -> centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).(); |
| 414 | } |
| 415 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 416 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 417 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no centroid extracted."); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | autoPCAcxx . . $ autoPCA# GaussianMixture_to_PCAcxx . . $ GaussianMixture_to_PCA(2e3c11afa70d9e49). (GaussianMixture me) { |
| 422 | try { |
| 423 | autoCovariancecxx . . $ autoCovariance# him = GaussianMixture_to_Covariance_totalcxx . . $ GaussianMixture_to_Covariance_total(2376c2b32bfa09b5). (me); |
| 424 | autoPCAcxx . . $ autoPCA# thee = SSCP_to_PCAcxx . . $ SSCP_to_PCA(67c2aab67f958214). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 425 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 426 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 427 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no PCA calculated."); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void GaussianMixture_getIntervalsAlongDirectionscxx . . $ GaussianMixture_getIntervalsAlongDirections(8dcd55032863c493). (GaussianMixture me, integercxx . . $ integer# d1, integercxx . . $ integer# d2, double nsigmas, double *out_xmin, double *out_xmax, double *out_ymin, double *out_ymax) { |
| 432 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (d1 > 0 && d1 <= mycxx . . $ `melder/melder.h:44:9`! dimension && d2 > 0 && d2 <= mycxx . . $ `melder/melder.h:44:9`! dimension, |
| 433 | U"The directions should be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! dimension, U"."); |
| 434 | autoSSCPListcxx . . $ autoSSCPList# sscps = SSCPList_extractTwoDimensionscxx . . $ SSCPList_extractTwoDimensions(1217d8d0c61689e6). (mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).asSSCPListcxx . . $ structCovarianceList#asSSCPList(dceb440e36dd3f3).(), d1, d2); |
| 435 | SSCPList_getEllipsesBoundingBoxCoordinatescxx . . $ SSCPList_getEllipsesBoundingBoxCoordinates(3598c17e44b4f508). (sscps.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), -nsigmas, 0, out_xmin, out_xmax, out_ymin, out_ymax); |
| 436 | } |
| 437 | |
| 438 | void GaussianMixture_getIntervalAlongDirectioncxx . . $ GaussianMixture_getIntervalAlongDirection(de70715f3c56b76f). (GaussianMixture me, integercxx . . $ integer# d, double nsigmas, double *out_xmin, double *out_xmax) { |
| 439 | GaussianMixture_getIntervalsAlongDirectionscxx . . $ GaussianMixture_getIntervalsAlongDirections(8dcd55032863c493). (me, d, d, nsigmas, out_xmin, out_xmax, nullptr, nullptr); |
| 440 | } |
| 441 | |
| 442 | void GaussianMixture_PCA_getIntervalsAlongDirectionscxx . . $ GaussianMixture_PCA_getIntervalsAlongDirections(66d03a0598ec4d7e). (GaussianMixture me, PCAcxx . . $ PCA# thee, integercxx . . $ integer# d1, integercxx . . $ integer# d2, double nsigmas, double *out_xmin, double *out_xmax, double *out_ymin, double *out_ymax) { |
| 443 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dimension == thycxx . . $ `melder/melder.h:45:9`! dimension, |
| 444 | U"The dimensions should agree."); |
| 445 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (d1 >= 1 && d1 <= mycxx . . $ `melder/melder.h:44:9`! dimension && d2 >= 1 && d2 <= mycxx . . $ `melder/melder.h:44:9`! dimension, |
| 446 | U"The dimensions should be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! dimension, U"."); |
| 447 | |
| 448 | autoSSCPListcxx . . $ autoSSCPList# sscps = SSCPList_toTwoDimensionscxx . . $ SSCPList_toTwoDimensions(132e4bc636c03e30). (mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).asSSCPListcxx . . $ structCovarianceList#asSSCPList(dceb440e36dd3f3).(), thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (d1), thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (d2)); |
| 449 | SSCPList_getEllipsesBoundingBoxCoordinatescxx . . $ SSCPList_getEllipsesBoundingBoxCoordinates(3598c17e44b4f508). (sscps.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), -nsigmas, 0, out_xmin, out_xmax, out_ymin, out_ymax); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | void GaussianMixture_PCA_getIntervalAlongDirectioncxx . . $ GaussianMixture_PCA_getIntervalAlongDirection(768848be2bb4571d). (GaussianMixture me, PCAcxx . . $ PCA# thee, integercxx . . $ integer# d, double nsigmas, double *out_xmin, double *out_xmax) { |
| 454 | GaussianMixture_PCA_getIntervalsAlongDirectionscxx . . $ GaussianMixture_PCA_getIntervalsAlongDirections(66d03a0598ec4d7e). (me, thee, d, d, nsigmas, out_xmin, out_xmax, nullptr, nullptr); |
| 455 | } |
| 456 | |
| 457 | void GaussianMixture_PCA_drawMarginalPdfcxx . . $ GaussianMixture_PCA_drawMarginalPdf(6a7a8d0771c8334f). (GaussianMixture me, PCAcxx . . $ PCA# thee, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# d, double xmin, double xmax, double ymin, double ymax, integercxx . . $ integer# npoints, integercxx . . $ integer# nbins, bool garnish) { |
| 458 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dimension == thycxx . . $ `melder/melder.h:45:9`! dimension, |
| 459 | U"The dimensions should agree."); |
| 460 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (d >= 1 && d <= mycxx . . $ `melder/melder.h:44:9`! dimension, |
| 461 | U"The dimension must be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! dimension, U"."); |
| 462 | if (npoints <= 1) |
| 463 | npoints = 1000; |
| 464 | constexpr double nsigmas = 2; |
| 465 | if (xmax <= xmin) |
| 466 | GaussianMixture_PCA_getIntervalAlongDirectioncxx . . $ GaussianMixture_PCA_getIntervalAlongDirection(768848be2bb4571d). (me, thee, d, nsigmas, & xmin, & xmax); |
| 467 | const double dx = (xmax - xmin) / npoints, x1 = xmin + 0.5 * dx; |
| 468 | const double scalef = ( nbins <= 0 ? 1.0 : 1.0 ); // TODO |
| 469 | autoVECcxx . . $ autoVEC# p = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (npoints); |
| 470 | for (integercxx . . $ integer# i = 1; i <= npoints; i ++) { |
| 471 | const double x = x1 + (i - 1) * dx; |
| 472 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (thycxx . . $ `melder/melder.h:45:9`! eigenvectors.ncol == thycxx . . $ `melder/melder.h:45:9`! dimension); |
| 473 | p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = scalef * GaussianMixture_getMarginalProbabilityAtPositioncxx . . $ GaussianMixture_getMarginalProbabilityAtPosition(18530cb880ed5215). (me, thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (d), x); |
| 474 | } |
| 475 | const double pmax = NUMmax_ucxx . . $ NUMmax_u(4bb9c3e3a8dd5442). (pcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 476 | if (isundefcxx . . $ isundef(f8197d536af80c96). (pmax)) |
| 477 | return; |
| 478 | if (ymin >= ymax) { |
| 479 | ymin = 0.0; |
| 480 | ymax = pmax; |
| 481 | } |
| 482 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 483 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, xmin, xmax, ymin, ymax); |
| 484 | Graphics_functioncxx . . $ Graphics_function(1c6279e2c8b8beb1). (g, p.asArgumentToFunctionThatExpectsOneBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsOneBasedArray(5a2a78a048fb49a8).(), 1, npoints, x1, xmax - 0.5 * dx); |
| 485 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 486 | if (garnish) { |
| 487 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 488 | Graphics_markBottomcxx . . $ Graphics_markBottom(e82cc8dc402e8bd7). (g, xmin, true, true, false, nullptr); |
| 489 | Graphics_markBottomcxx . . $ Graphics_markBottom(e82cc8dc402e8bd7). (g, xmax, true, true, false, nullptr); |
| 490 | Graphics_markLeftcxx . . $ Graphics_markLeft(e82cc8dc402e8bd7). (g, ymin, true, true, false, nullptr); |
| 491 | Graphics_markLeftcxx . . $ Graphics_markLeft(e82cc8dc402e8bd7). (g, ymax, true, true, false, nullptr); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | void GaussianMixture_drawMarginalPdfcxx . . $ GaussianMixture_drawMarginalPdf(8c744a26bcc19324). (GaussianMixture me, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# d, double xmin, double xmax, double ymin, double ymax, integercxx . . $ integer# npoints, integercxx . . $ integer# /* nbins */, bool garnish) { |
| 496 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (d >= 1 && d <= mycxx . . $ `melder/melder.h:44:9`! dimension, |
| 497 | U"The dimension should be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! dimension, U"."); |
| 498 | |
| 499 | if (npoints <= 1) |
| 500 | npoints = 1000; |
| 501 | |
| 502 | constexpr double nsigmas = 2.0; |
| 503 | if (xmax <= xmin) |
| 504 | GaussianMixture_getIntervalAlongDirectioncxx . . $ GaussianMixture_getIntervalAlongDirection(de70715f3c56b76f). (me, d, nsigmas, & xmin, & xmax); |
| 505 | |
| 506 | const double dx = (xmax - xmin) / (npoints - 1); |
| 507 | const double scalef = 1.0; // TODO |
| 508 | autoVECcxx . . $ autoVEC# p = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (npoints); |
| 509 | autoVECcxx . . $ autoVEC# pos = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! dimension); |
| 510 | |
| 511 | for (integercxx . . $ integer# k = 1; k <= mycxx . . $ `melder/melder.h:44:9`! dimension; k++) |
| 512 | pos [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] = ( k == d ? 1.0 : 0.0 ); |
| 513 | for (integercxx . . $ integer# i = 1; i <= npoints; i++) { |
| 514 | const double x = xmin + (i - 1) * dx; |
| 515 | p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = scalef * GaussianMixture_getMarginalProbabilityAtPositioncxx . . $ GaussianMixture_getMarginalProbabilityAtPosition(18530cb880ed5215). (me, poscxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), x); |
| 516 | } |
| 517 | const double pmax = NUMmax_ucxx . . $ NUMmax_u(4bb9c3e3a8dd5442). (pcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 518 | if (isundefcxx . . $ isundef(f8197d536af80c96). (pmax)) |
| 519 | return; |
| 520 | if (ymin >= ymax) { |
| 521 | ymin = 0; |
| 522 | ymax = pmax; |
| 523 | } |
| 524 | |
| 525 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 526 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, xmin, xmax, ymin, ymax); |
| 527 | Graphics_functioncxx . . $ Graphics_function(1c6279e2c8b8beb1). (g, p.asArgumentToFunctionThatExpectsOneBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsOneBasedArray(5a2a78a048fb49a8).(), 1, npoints, xmin, xmax); |
| 528 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 529 | |
| 530 | if (garnish) { |
| 531 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 532 | Graphics_markBottomcxx . . $ Graphics_markBottom(e82cc8dc402e8bd7). (g, xmin, true, true, false, nullptr); |
| 533 | Graphics_markBottomcxx . . $ Graphics_markBottom(e82cc8dc402e8bd7). (g, xmax, true, true, false, nullptr); |
| 534 | Graphics_markLeftcxx . . $ Graphics_markLeft(e82cc8dc402e8bd7). (g, ymin, true, true, false, nullptr); |
| 535 | Graphics_markLeftcxx . . $ Graphics_markLeft(e82cc8dc402e8bd7). (g, ymax, true, true, false, nullptr); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | void GaussianMixture_PCA_drawConcentrationEllipsescxx . . $ GaussianMixture_PCA_drawConcentrationEllipses(132aa3cf1544890d). (GaussianMixture me, PCAcxx . . $ PCA# him, Graphicscxx . . $ Graphics# g, double scale, |
| 540 | bool confidence, char32cxx . . $ char32# *label, integercxx . . $ integer# d1, integercxx . . $ integer# d2, double xmin, double xmax, double ymin, double ymax, double fontSize, bool garnish) { |
| 541 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dimension == hiscxx . . $ `melder/melder.h:47:9`! dimension, |
| 542 | U"The numbers of dimensions should agree."); |
| 543 | const integercxx . . $ integer# dim1 = integer_abscxx . . $ integer_abs(8ce0d1b217db7fd1). (d1), dim2 = integer_abscxx . . $ integer_abs(8ce0d1b217db7fd1). (d2); |
| 544 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (dim1 >= 1 && dim1 <= mycxx . . $ `melder/melder.h:44:9`! dimension && dim2 >= 1 && dim2 <= mycxx . . $ `melder/melder.h:44:9`! dimension, |
| 545 | U"The dimension numbers should be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! dimension, U" (or the negative of this value for a reversed axis)."); |
| 546 | autoVECcxx . . $ autoVEC# eigenvector1 = copy_VECcxx . . $ copy_VEC(d4415e68b25283a8). (hiscxx . . $ `melder/melder.h:47:9`! eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (dim1)); |
| 547 | autoVECcxx . . $ autoVEC# eigenvector2 = copy_VECcxx . . $ copy_VEC(d4415e68b25283a8). (hiscxx . . $ `melder/melder.h:47:9`! eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (dim2)); |
| 548 | if (d1 < 0) |
| 549 | eigenvector1.allcxx . . $ autovector#all(a26726dad87555bb).() *=cxx . . $ `operator*=`(c555d170d72ff256). -1.0; |
| 550 | if (d2 < 0) |
| 551 | eigenvector2.allcxx . . $ autovector#all(a26726dad87555bb).() *=cxx . . $ `operator*=`(c555d170d72ff256). -1.0; |
| 552 | |
| 553 | autoSSCPListcxx . . $ autoSSCPList# thee = SSCPList_toTwoDimensionscxx . . $ SSCPList_toTwoDimensions(132e4bc636c03e30). (mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).asSSCPListcxx . . $ structCovarianceList#asSSCPList(dceb440e36dd3f3).(), eigenvector1cxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).(), eigenvector2cxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).()); |
| 554 | |
| 555 | SSCPList_drawConcentrationEllipsescxx . . $ SSCPList_drawConcentrationEllipses(fd6755b3615ec33c). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), g, -scale, confidence, label, 1, 2, xmin, xmax, ymin, ymax, fontSize, 0); |
| 556 | |
| 557 | if (garnish) { |
| 558 | char32cxx . . $ char32# llabel [40]; |
| 559 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 560 | Graphics_marksLeftcxx . . $ Graphics_marksLeft(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 561 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (llabel,40, U"pc ", d2); |
| 562 | Graphics_textLeftcxx . . $ Graphics_textLeft(48cf1f690ec61203). (g, true, llabel); |
| 563 | Graphics_marksBottomcxx . . $ Graphics_marksBottom(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 564 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (llabel,40, U"pc ", d1); |
| 565 | Graphics_textBottomcxx . . $ Graphics_textBottom(48cf1f690ec61203). (g, true, llabel); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void GaussianMixture_drawConcentrationEllipsescxx . . $ GaussianMixture_drawConcentrationEllipses(e7a43cfa49c51d69). (GaussianMixture me, Graphicscxx . . $ Graphics# g, double scale, bool confidence, char32cxx . . $ char32# *label, |
| 570 | bool pcaDirections, integercxx . . $ integer# d1, integercxx . . $ integer# d2, double xmin, double xmax, double ymin, double ymax, double fontSize, bool garnish) { |
| 571 | const integercxx . . $ integer# dim1 = integer_abscxx . . $ integer_abs(8ce0d1b217db7fd1). (d1), dim2 = integer_abscxx . . $ integer_abs(8ce0d1b217db7fd1). (d2); |
| 572 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (dim1 >= 1 && dim1 <= mycxx . . $ `melder/melder.h:44:9`! dimension && dim2 >= 1 && dim2 <= mycxx . . $ `melder/melder.h:44:9`! dimension, |
| 573 | U"The dimensions should be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! dimension, U" (or the negative of this value for a reversed axis)."); |
| 574 | if (! pcaDirections) { |
| 575 | SSCPList_drawConcentrationEllipsescxx . . $ SSCPList_drawConcentrationEllipses(fd6755b3615ec33c). (mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).asSSCPListcxx . . $ structCovarianceList#asSSCPList(dceb440e36dd3f3).(), g, -scale, confidence, label, |
| 576 | dim1, dim2, xmin, xmax, ymin, ymax, fontSize, garnish); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | autoPCAcxx . . $ autoPCA# him = GaussianMixture_to_PCAcxx . . $ GaussianMixture_to_PCA(2e3c11afa70d9e49). (me); |
| 581 | |
| 582 | GaussianMixture_PCA_drawConcentrationEllipsescxx . . $ GaussianMixture_PCA_drawConcentrationEllipses(132aa3cf1544890d). (me, him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), g, scale, confidence, label, d1, d2, |
| 583 | xmin, xmax, ymin, ymax, fontSize, garnish); |
| 584 | } |
| 585 | |
| 586 | void GaussianMixture_initialGuesscxx . . $ GaussianMixture_initialGuess(6d8d040d5c79cca5). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee) { |
| 587 | try { |
| 588 | autoCovariancecxx . . $ autoCovariance# cov_t = TableOfReal_to_Covariancecxx . . $ TableOfReal_to_Covariance(3e7df3a959f2f2b3). (thee); |
| 589 | for (integercxx . . $ integer# icol = 1; icol <= thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; icol ++) { |
| 590 | const double min = NUMmin_ecxx . . $ NUMmin_e(fa513f1eb826a064). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (icol)); |
| 591 | const double max = NUMmax_ecxx . . $ NUMmax_e(fa513f1eb826a064). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (icol)); |
| 592 | for (integercxx . . $ integer# component = 1; component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; component ++) { |
| 593 | const Covariancecxx . . $ Covariance# covi = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 594 | covi -> centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icol] = NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (min, max); |
| 595 | } |
| 596 | } |
| 597 | const double varianceScaleFactor = 1.0 / (10 * thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 598 | for (integercxx . . $ integer# component = 1; component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; component ++) { |
| 599 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 600 | if (cov -> numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) |
| 601 | covcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). -> datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) <<=cxx . . $ `operator<<=`(d166bd5b5c96d95c). varianceScaleFactor *cxx . . $ `operator*`(7f4bd74ab5a11911). cov_tcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..diagonalcxx . . $ automatrix#diagonal(a26726dad87555bb).(); |
| 602 | else |
| 603 | cov -> datacxx . . $ structTableOfReal#data..diagonalcxx . . $ automatrix#diagonal(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(d166bd5b5c96d95c). varianceScaleFactor *cxx . . $ `operator*`(7f4bd74ab5a11911). cov_tcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..diagonalcxx . . $ automatrix#diagonal(a26726dad87555bb).(); |
| 604 | } |
| 605 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 606 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", thee, U": no initial guess possible."); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | void GaussianMixture_initialGuess2cxx . . $ GaussianMixture_initialGuess2(ba19c7db4584cebe). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee, double nSigmas, double ru_range) { |
| 611 | try { |
| 612 | autoCovariancecxx . . $ autoCovariance# cov_t = TableOfReal_to_Covariancecxx . . $ TableOfReal_to_Covariance(3e7df3a959f2f2b3). (thee); |
| 613 | /* |
| 614 | Assume equal probabilities for mixture |
| 615 | Assume equal covariance matrices |
| 616 | Spread centroids on an ellips in pc1-pc2 plane? |
| 617 | */ |
| 618 | if (mycxx . . $ `melder/melder.h:44:9`! dimension == 1) { |
| 619 | const double dm = 2.0 * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (cov_t ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1]) / mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; |
| 620 | double m1 = cov_t ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] - dm; |
| 621 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im ++) { |
| 622 | const Covariancecxx . . $ Covariance# covi = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im]; |
| 623 | covi -> centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = m1; |
| 624 | covi -> datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = dm * dm; |
| 625 | m1 += dm; |
| 626 | covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. = thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. / mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; |
| 627 | } |
| 628 | } else { |
| 629 | autoPCAcxx . . $ autoPCA# pca = SSCP_to_PCAcxx . . $ SSCP_to_PCA(67c2aab67f958214). (cov_t.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 630 | autoSSCPcxx . . $ autoSSCP# s2d = SSCP_toTwoDimensionscxx . . $ SSCP_toTwoDimensions(98df761079133683). (cov_t.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), pcacxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1), pcacxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2)); |
| 631 | autoMATcxx . . $ autoMAT# means2d = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, 2); |
| 632 | |
| 633 | double a, b, cs, sn; |
| 634 | NUMeigencmp22cxx . . $ NUMeigencmp22(628ba330b6233ab7). (s2d ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], s2d ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2], s2d ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2], & a, & b, & cs, & sn); |
| 635 | |
| 636 | const double a_scaled = nSigmas * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (a); |
| 637 | const double b_scaled = nSigmas * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (b); |
| 638 | const double angle_inc = NUM2picxx . . $ NUM2pi. / mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; |
| 639 | double angle = 0.0; |
| 640 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im++, angle += angle_inc) { |
| 641 | const double xc = a_scaled * (1.0 + NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (-ru_range, ru_range)) * coscxx . . $ cos(fee4f1d22bc8e617). (angle); |
| 642 | const double yc = b_scaled * (1.0 + NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (-ru_range, ru_range)) * sincxx . . $ sin(fee4f1d22bc8e617). (angle); |
| 643 | means2d [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).im] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = s2d ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] + xc * cs - yc * sn; |
| 644 | means2d [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).im] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] = s2d ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] + xc * sn + yc * cs; |
| 645 | } |
| 646 | /* |
| 647 | Reconstruct the n-dimensional means from the 2-d pc's and the 2 eigenvectors |
| 648 | */ |
| 649 | autoMATcxx . . $ autoMAT# means = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (means2dcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), pcacxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors..horizontalBandcxx . . $ automatrix#horizontalBand(244fa7aab97a21b1). (1, 2)); |
| 650 | |
| 651 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im ++) { |
| 652 | const Covariancecxx . . $ Covariance# covi = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im]; |
| 653 | covi -> centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). meanscxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (im); |
| 654 | covi -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. = thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. / mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; |
| 655 | } |
| 656 | /* |
| 657 | Trick: use the new means to get the between SSCP, |
| 658 | if there is only one component the cov_b will be zero! |
| 659 | */ |
| 660 | autoCovariancecxx . . $ autoCovariance# cov_b = GaussianMixture_to_Covariance_betweencxx . . $ GaussianMixture_to_Covariance_between(2376c2b32bfa09b5). (me); |
| 661 | const double var_t = SSCP_getTotalVariancecxx . . $ SSCP_getTotalVariance(aff8a8bd1da17c96). (cov_t.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 662 | const double var_b = SSCP_getTotalVariancecxx . . $ SSCP_getTotalVariance(aff8a8bd1da17c96). (cov_b.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 663 | if (var_b >= var_t) { // we have chosen our initial values too far out |
| 664 | const double scale = 0.9 * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (var_t / var_b); |
| 665 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im ++) { |
| 666 | const Covariancecxx . . $ Covariance# covi = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im]; |
| 667 | for (integercxx . . $ integer# ic = 1; ic <= mycxx . . $ `melder/melder.h:44:9`! dimension; ic ++) |
| 668 | covi -> centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic] -= (1.0 - scale) * (covi -> centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic] - cov_b ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic]); |
| 669 | } |
| 670 | cov_b =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). GaussianMixture_to_Covariance_betweencxx . . $ GaussianMixture_to_Covariance_between(2376c2b32bfa09b5). (me); |
| 671 | } |
| 672 | /* |
| 673 | Within variances are now (total - between) / numberOfComponents; |
| 674 | */ |
| 675 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents > 1) |
| 676 | cov_t ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() *=cxx . . $ `operator*=`(50b469ae42a42f64). var_b / var_t / mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; |
| 677 | /* |
| 678 | Copy them |
| 679 | */ |
| 680 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im ++) { |
| 681 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im]; |
| 682 | if (cov -> numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) |
| 683 | covcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). -> datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) <<=cxx . . $ `operator<<=`(adea042513329390). cov_tcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..diagonalcxx . . $ automatrix#diagonal(a26726dad87555bb).(); |
| 684 | else |
| 685 | cov -> datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).() <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). cov_tcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).(); |
| 686 | } |
| 687 | } |
| 688 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 689 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", thee, U": no initial guess possible."); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | autoClassificationTablecxx . . $ autoClassificationTable# GaussianMixture_TableOfReal_to_ClassificationTablecxx . . $ GaussianMixture_TableOfReal_to_ClassificationTable(b53771290e8e341a). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee) { |
| 694 | try { |
| 695 | autoClassificationTablecxx . . $ autoClassificationTable# him = ClassificationTable_createcxx . . $ ClassificationTable_create(b9c68632944d67c8). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); |
| 696 | for (integercxx . . $ integer# ic = 1; ic <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; ic ++) { |
| 697 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ic]; |
| 698 | SSCP_expandWithLowerCholeskyInversecxx . . $ SSCP_expandWithLowerCholeskyInverse(b2bffa5b59e040d8). (cov); |
| 699 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), ic, Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (cov)); |
| 700 | } |
| 701 | |
| 702 | const double ln2pid = -0.5 * mycxx . . $ `melder/melder.h:44:9`! dimension * logcxx . . $ log(fee4f1d22bc8e617). (NUM2picxx . . $ NUM2pi.); |
| 703 | autoVECcxx . . $ autoVEC# lnN = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); |
| 704 | for (integercxx . . $ integer# irow = 1; irow <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 705 | longdoublecxx . . $ longdouble# psum = 0.0; |
| 706 | for (integercxx . . $ integer# ic = 1; ic <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; ic ++) { |
| 707 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ic]; |
| 708 | const double dsq = NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (covcxx . . $ constmatrix#constmatrix(eed9513d24570b3d). -> lowerCholeskyInversecxx . . $ structSSCP#lowerCholeskyInverse..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (irow), covcxx . . $ constvector#constvector(eac9d6ec4c4bfecd). -> centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 709 | lnN [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic] = ln2pid - 0.5 * (cov -> lndcxx . . $ structSSCP#lnd. + dsq); |
| 710 | psum += hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ic] = mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic] * expcxx . . $ exp(fee4f1d22bc8e617). (lnN [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic]); |
| 711 | } |
| 712 | if (psum == 0.0) { // p's might be too small (underflow), make the largest equal to sfmin |
| 713 | double lnmax = -1e308; |
| 714 | integercxx . . $ integer# imm = 1; |
| 715 | for (integercxx . . $ integer# ic = 1; ic <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; ic ++) { |
| 716 | if (lnN [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic] > lnmax) |
| 717 | lnmax = lnN [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic]; |
| 718 | imm = ic; |
| 719 | } |
| 720 | hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).imm] = NUMfppcxx . . $ NUMfpp. -> sfmincxx . . $ structmachar_Table#sfmin.; |
| 721 | } |
| 722 | |
| 723 | // for (integer im = 1; im <= my numberOfComponents; im++) { his data [i] [im] /= psum; } |
| 724 | |
| 725 | TableOfReal_setRowLabelcxx . . $ TableOfReal_setRowLabel(7bba53c4f5b398fa). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), irow, thycxx . . $ `melder/melder.h:45:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).irow].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 726 | } |
| 727 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 728 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 729 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"No ClassificationTable created from GaussianMixture & TableOfReal."); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | void GaussianMixture_splitComponentcxx . . $ GaussianMixture_splitComponent(2fd3f882ef0dccef). (GaussianMixture me, integercxx . . $ integer# component) { |
| 734 | try { |
| 735 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (component > 0 && component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 736 | U"The component should be in [1, ", mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, U"]."); |
| 737 | |
| 738 | Covariancecxx . . $ Covariance# thee = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 739 | // Always new PCA because we cannot be sure of data unchanged. |
| 740 | SSCP_expandWithPCAcxx . . $ SSCP_expandWithPCA(b2bffa5b59e040d8). (thee); |
| 741 | autoCovariancecxx . . $ autoCovariance# cov1 = Data_copycxx . . $ Data_copy(97a542a26794c80e). (thee); |
| 742 | autoCovariancecxx . . $ autoCovariance# cov2 = Data_copycxx . . $ Data_copy(97a542a26794c80e). (thee); |
| 743 | SSCP_unExpandPCAcxx . . $ SSCP_unExpandPCA(b2bffa5b59e040d8). (cov1.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 744 | SSCP_unExpandPCAcxx . . $ SSCP_unExpandPCA(b2bffa5b59e040d8). (cov2.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 745 | |
| 746 | // Eventually cov1 replaces component, cov2 at end |
| 747 | |
| 748 | autoVECcxx . . $ autoVEC# mixingProbabilities = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents + 1); |
| 749 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; i ++) |
| 750 | mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 751 | |
| 752 | const double gamma = 0.5, lambda = 0.5, eta = 0.5, mu = 0.5; |
| 753 | mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component] = gamma * mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component]; |
| 754 | mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).mycxx . . $ `melder/melder.h:44:9`! numberOfComponents + 1] = (1.0 - gamma) * mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component]; |
| 755 | const double mp12 = mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component] / mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).mycxx . . $ `melder/melder.h:44:9`! numberOfComponents + 1]; |
| 756 | const double factor1 = (eta - eta * lambda * lambda - 1.0) / gamma + 1.0; |
| 757 | const double factor2 = (eta * lambda * lambda - eta - lambda * lambda) / (1.0 - gamma) + 1.0; |
| 758 | constVECcxx . . $ constVEC# ev = thycxx . . $ `melder/melder.h:45:9`! pcacxx . . $ structSSCP#pca. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); |
| 759 | const double d2 = thycxx . . $ `melder/melder.h:45:9`! pcacxx . . $ structSSCP#pca. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvaluescxx . . $ structEigen#eigenvalues. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; |
| 760 | |
| 761 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! dimension; i ++) { |
| 762 | cov1 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] -= (1.0 / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mp12)) * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (d2) * mu * ev [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]; |
| 763 | cov2 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] += sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mp12) * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (d2) * mu * ev [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]; |
| 764 | |
| 765 | if (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows. == 1) { // diagonal |
| 766 | cov1 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = cov1 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] / mp12 + factor1 * d2; |
| 767 | cov1 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = cov2 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * mp12 + factor2 * d2; |
| 768 | } else { |
| 769 | for (integercxx . . $ integer# j = i; j <= mycxx . . $ `melder/melder.h:44:9`! dimension; j++) { |
| 770 | cov1 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = cov1 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = cov1 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] / mp12 + factor1 * d2 * ev [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] * ev [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j]; |
| 771 | cov2 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = cov2 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = cov2 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] * mp12 + factor2 * d2 * ev [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] * ev [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j]; |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | cov1 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfObservationscxx . . $ structSSCP#numberOfObservations. *= gamma; |
| 776 | cov2 ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfObservationscxx . . $ structSSCP#numberOfObservations. *= 1.0 - gamma; |
| 777 | |
| 778 | // Replace cov1 at component + add cov2. If something goes wrong we should be able to restore original! |
| 779 | try { |
| 780 | Thing_setNamecxx . . $ Thing_setName(b5a42e5e01c267b5). (cov2.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (cov2.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()), U"-", mycxx . . $ `melder/melder.h:44:9`! numberOfComponents + 1)); |
| 781 | mycxx . . $ `melder/melder.h:44:9`! covariances ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). addItem_movecxx . . $ CollectionOf#addItem_move(bcc0cff787be24fe). (cov2cxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).()); |
| 782 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 783 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" cannot add new component."); |
| 784 | } |
| 785 | |
| 786 | mycxx . . $ `melder/melder.h:44:9`! covariances ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). replaceItem_movecxx . . $ CollectionOf#replaceItem_move(ca501a207ce7a6c1). (cov1cxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).(), component); |
| 787 | |
| 788 | mycxx . . $ `melder/melder.h:44:9`! numberOfComponents ++; |
| 789 | mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). mixingProbabilities.movecxx . . $ autovector#move(57d90d7d77dfc011).(); |
| 790 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 791 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": component ", component, U" cannot be split."); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | void GaussianMixture_TableOfReal_getComponentProbabilitiescxx . . $ GaussianMixture_TableOfReal_getComponentProbabilities(674342a5e75573ef). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee, integercxx . . $ integer# componentToUpdate, MATcxx . . $ MAT# const& probabilities) { |
| 796 | try { |
| 797 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (probabilities.nrow == thycxx . . $ `melder/melder.h:45:9`! numberOfRows, |
| 798 | U"The number of rows in the TableOfReal and the probabilities should be equal."); |
| 799 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (probabilities.ncol == mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 800 | U"The number of columns in the TableOfReal and the probabilities should be equal."); |
| 801 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dimension == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 802 | U"The number of columns in the TableOfReal and the dimension of the GaussianMixture should be equal."); |
| 803 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (componentToUpdate >= 0 && componentToUpdate <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 804 | U"The component number should be in the interval from 0 to ", mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); |
| 805 | const double ln2pid = mycxx . . $ `melder/melder.h:44:9`! dimension * logcxx . . $ log(fee4f1d22bc8e617). (NUM2picxx . . $ NUM2pi.); |
| 806 | |
| 807 | const integercxx . . $ integer# fromComponent = componentToUpdate == 0 ? 1 : componentToUpdate; |
| 808 | const integercxx . . $ integer# toComponent = componentToUpdate == 0 ? mycxx . . $ `melder/melder.h:44:9`! numberOfComponents : componentToUpdate; |
| 809 | |
| 810 | for (integercxx . . $ integer# component = fromComponent; component <= toComponent; component ++) { |
| 811 | const Covariancecxx . . $ Covariance# covi = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 812 | SSCP_expandWithLowerCholeskyInversecxx . . $ SSCP_expandWithLowerCholeskyInverse(b2bffa5b59e040d8). (covi); |
| 813 | |
| 814 | for (integercxx . . $ integer# irow = 1; irow <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow++) { |
| 815 | const double dsq = NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (covicxx . . $ constmatrix#constmatrix(eed9513d24570b3d). -> lowerCholeskyInversecxx . . $ structSSCP#lowerCholeskyInverse..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (irow), covicxx . . $ constvector#constvector(eac9d6ec4c4bfecd). -> centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 816 | probabilities [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).component] = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (1e-300, expcxx . . $ exp(fee4f1d22bc8e617). (- 0.5 * (ln2pid + covi -> lndcxx . . $ structSSCP#lnd. + dsq))); // prevent probabilities from being zero |
| 817 | } |
| 818 | } |
| 819 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 820 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", thee, U": no component probabilies could be calculated."); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | void GaussianMixture_TableOfReal_getResponsilitiescxx . . $ GaussianMixture_TableOfReal_getResponsilities(43fdcadf1eb22f23). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee, MATcxx . . $ MAT# const& responsibilities) { |
| 825 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (responsibilities.nrow == thycxx . . $ `melder/melder.h:45:9`! numberOfRows, |
| 826 | U"The number of rows in the TableOfReal and the responsibilities should be equal."); |
| 827 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (responsibilities.ncol == mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 828 | U"The number of columns in the TableOfReal and the responsibilities should be equal."); |
| 829 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dimension == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 830 | U"The number of columns in the TableOfReal and the dimension of the GaussianMixture should be equal."); |
| 831 | autoMATcxx . . $ autoMAT# probabilities = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (responsibilities.nrowcxx . . $ matrix#nrow., responsibilities.ncolcxx . . $ matrix#ncol.); |
| 832 | GaussianMixture_TableOfReal_getComponentProbabilitiescxx . . $ GaussianMixture_TableOfReal_getComponentProbabilities(674342a5e75573ef). (me, thee, 0, probabilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 833 | GaussianMixture_getResponsibilitiescxx . . $ GaussianMixture_getResponsibilities(b8ab51b15584a097). (me, probabilitiescxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), 0, responsibilities); |
| 834 | } |
| 835 | |
| 836 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_TableOfReal_to_TableOfReal_probabilitiescxx . . $ GaussianMixture_TableOfReal_to_TableOfReal_probabilities(da7025f09333dc32). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee) { |
| 837 | try { |
| 838 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dimension == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 839 | U"The number of columns in the TableOfReal and the dimension of the GaussianMixture should be equal."); |
| 840 | autoTableOfRealcxx . . $ autoTableOfReal# him = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); |
| 841 | hiscxx . . $ `melder/melder.h:47:9`! rowLabelscxx . . $ structTableOfReal#rowLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).() <<=cxx . . $ `operator<<=`(5bcbc9f49fd394e1). thycxx . . $ `melder/melder.h:45:9`! rowLabelscxx . . $ structTableOfReal#rowLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).(); |
| 842 | TableOfReal_setSequentialColumnLabelscxx . . $ TableOfReal_setSequentialColumnLabels(8dc3139cdc86d712). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1, mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, U"c", 1, 1); |
| 843 | GaussianMixture_TableOfReal_getComponentProbabilitiescxx . . $ GaussianMixture_TableOfReal_getComponentProbabilities(674342a5e75573ef). (me, thee, 0, hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 844 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 845 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 846 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", thee, U": no probabilies could be calculated."); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_TableOfReal_to_TableOfReal_responsibilitiescxx . . $ GaussianMixture_TableOfReal_to_TableOfReal_responsibilities(da7025f09333dc32). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee) { |
| 851 | try { |
| 852 | autoTableOfRealcxx . . $ autoTableOfReal# him = GaussianMixture_TableOfReal_to_TableOfReal_probabilitiescxx . . $ GaussianMixture_TableOfReal_to_TableOfReal_probabilities(da7025f09333dc32). (me, thee); |
| 853 | GaussianMixture_getResponsibilitiescxx . . $ GaussianMixture_getResponsibilities(b8ab51b15584a097). (me, hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), 0, hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 854 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 855 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 856 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", thee, U": no responsibilities could be calculated."); |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | void GaussianMixture_expandPCAcxx . . $ GaussianMixture_expandPCA(a29c70c932200d85). (GaussianMixture me) { |
| 861 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im ++) { |
| 862 | const Covariancecxx . . $ Covariance# him = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im]; |
| 863 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (hiscxx . . $ `melder/melder.h:47:9`! numberOfRows > 1, |
| 864 | U"Nothing to expand."); |
| 865 | hiscxx . . $ `melder/melder.h:47:9`! pcacxx . . $ structSSCP#pca. =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). SSCP_to_PCAcxx . . $ SSCP_to_PCA(67c2aab67f958214). (him); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | void GaussianMixture_unExpandPCAcxx . . $ GaussianMixture_unExpandPCA(a29c70c932200d85). (GaussianMixture me) { |
| 870 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im ++) { |
| 871 | SSCP_unExpandPCAcxx . . $ SSCP_unExpandPCA(b2bffa5b59e040d8). (mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im]); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | void GaussianMixture_TableOfReal_improveLikelihoodcxx . . $ GaussianMixture_TableOfReal_improveLikelihood(4d9c0daae3862ead). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee, double delta_lnp, integercxx . . $ integer# maxNumberOfIterations, double lambda, kGaussianMixtureCriterion criterion) { |
| 876 | try { |
| 877 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (thycxx . . $ `melder/melder.h:45:9`! numberOfColumns == mycxx . . $ `melder/melder.h:44:9`! dimension, |
| 878 | U"The number of columns and the dimension of the model should agree."); |
| 879 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents < thycxx . . $ `melder/melder.h:45:9`! numberOfRows / 2, |
| 880 | U"Not enough data points."); |
| 881 | |
| 882 | const conststring32cxx . . $ conststring32# criterionText = GaussianMixture_criterionTextcxx . . $ GaussianMixture_criterionText(185f836f08e9b4fd). (criterion); |
| 883 | |
| 884 | // The global covariance matrix is added with scaling coefficient lambda during updating the |
| 885 | // mixture covariances to prevent numerical instabilities. |
| 886 | |
| 887 | autoCovariancecxx . . $ autoCovariance# covg = TableOfReal_to_Covariancecxx . . $ TableOfReal_to_Covariance(3e7df3a959f2f2b3). (thee); |
| 888 | autoMATcxx . . $ autoMAT# probabilities = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); |
| 889 | autoMATcxx . . $ autoMAT# responsibilities = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); |
| 890 | |
| 891 | GaussianMixture_TableOfReal_getComponentProbabilitiescxx . . $ GaussianMixture_TableOfReal_getComponentProbabilities(674342a5e75573ef). (me, thee, 0, probabilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 892 | |
| 893 | double lnp = GaussianMixture_getLikelihoodValuecxx . . $ GaussianMixture_getLikelihoodValue(97c1d46bb5e56950). (me, probabilitiescxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), criterion); |
| 894 | integercxx . . $ integer# iter = 0; |
| 895 | autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"Improve likelihood..."); |
| 896 | try { |
| 897 | double lnp_prev, lnp_start = lnp / thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 898 | do { |
| 899 | iter ++; |
| 900 | /* |
| 901 | E-step: get responsibilities (gamma) with current parameters |
| 902 | See C. Bishop (2006), Pattern reconition and machine learning, Springer, page 439... |
| 903 | */ |
| 904 | GaussianMixture_getResponsibilitiescxx . . $ GaussianMixture_getResponsibilities(b8ab51b15584a097). (me, probabilitiescxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), 0, responsibilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 905 | |
| 906 | lnp_prev = lnp; |
| 907 | |
| 908 | /* |
| 909 | M-step: 1. new means & covariances |
| 910 | */ |
| 911 | for (integercxx . . $ integer# component = 1; component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; component ++) { |
| 912 | GaussianMixture_updateComponentcxx . . $ GaussianMixture_updateComponent(6f1ed30d7567a247). (me, component, thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), responsibilitiescxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 913 | GaussianMixture_addCovarianceFractioncxx . . $ GaussianMixture_addCovarianceFraction(4b9376bd8a90c35f). (me, component, covg.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), lambda); |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | M-step: 2. new mixingProbabilities |
| 918 | */ |
| 919 | autoVECcxx . . $ autoVEC# totalResponsibilities = columnSums_VECcxx . . $ columnSums_VEC(81359d2d5440d947). (responsibilitiescxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 920 | mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). totalResponsibilitiescxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(); |
| 921 | mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities.allcxx . . $ autovector#all(a26726dad87555bb).() *=cxx . . $ `operator*=`(c555d170d72ff256). 1.0 / responsibilities.nrowcxx . . $ automatrix#nrow.; |
| 922 | |
| 923 | GaussianMixture_TableOfReal_getComponentProbabilitiescxx . . $ GaussianMixture_TableOfReal_getComponentProbabilities(674342a5e75573ef). (me, thee, 0, probabilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 924 | |
| 925 | lnp = GaussianMixture_getLikelihoodValuecxx . . $ GaussianMixture_getLikelihoodValue(97c1d46bb5e56950). (me, probabilitiescxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), criterion); |
| 926 | Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). ((double) iter / (double) maxNumberOfIterations, criterionText, U": ", lnp / thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., U", L0: ", lnp_start); |
| 927 | } while (fabscxx . . $ fabs(fee4f1d22bc8e617). (lnp - lnp_prev) > stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (fabscxx . . $ fabs(fee4f1d22bc8e617). (delta_lnp * lnp_prev), NUMepscxx . . $ `dwsys/NUM2.h:36:9`!) && iter < maxNumberOfIterations); |
| 928 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 929 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 930 | } |
| 931 | |
| 932 | // During EM, covariances were underestimated by a factor of (n-1)/n. Correction now. |
| 933 | |
| 934 | for (integercxx . . $ integer# component = 1; component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; component ++) { |
| 935 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 936 | if (cov -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. > 1.5) |
| 937 | covcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). -> datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) *=cxx . . $ `operator*=`(c555d170d72ff256). cov -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. / (cov -> numberOfObservationscxx . . $ structSSCP#numberOfObservations. - 1.0); |
| 938 | } |
| 939 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 940 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", thee, U": likelihood cannot be improved."); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | |
| 945 | double GaussianMixture_TableOfReal_getLikelihoodValuecxx . . $ GaussianMixture_TableOfReal_getLikelihoodValue(4d3968d8207e3e34). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee, kGaussianMixtureCriterion criterion) { |
| 946 | autoMATcxx . . $ autoMAT# probabilities = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); |
| 947 | GaussianMixture_TableOfReal_getComponentProbabilitiescxx . . $ GaussianMixture_TableOfReal_getComponentProbabilities(674342a5e75573ef). (me, thee, 0, probabilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 948 | return GaussianMixture_getLikelihoodValuecxx . . $ GaussianMixture_getLikelihoodValue(97c1d46bb5e56950). (me, probabilitiescxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), criterion); |
| 949 | } |
| 950 | |
| 951 | autoMATcxx . . $ autoMAT# newMATremoveColumncxx . . $ newMATremoveColumn(21fc5e6ea3c1260e). (constMATcxx . . $ constMAT# const& m, integercxx . . $ integer# columnToRemove) { |
| 952 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (m.ncol > 1, |
| 953 | U"We cannot remove the last column."); |
| 954 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (columnToRemove > 0 && columnToRemove <= m.ncol, |
| 955 | U"The column number should be in the range from 1 to ",m.ncol, U"."); |
| 956 | autoMATcxx . . $ autoMAT# resized = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (m.nrowcxx . . $ constmatrix#nrow., m.ncolcxx . . $ constmatrix#ncol. - 1); |
| 957 | resized.verticalBandcxx . . $ automatrix#verticalBand(244fa7aab97a21b1). (1, columnToRemove - 1) <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). m.verticalBandcxx . . $ constmatrix#verticalBand(4283795acefb2364). (1, columnToRemove - 1); |
| 958 | if (columnToRemove < m.ncolcxx . . $ constmatrix#ncol.) |
| 959 | resized.verticalBandcxx . . $ automatrix#verticalBand(244fa7aab97a21b1). (columnToRemove, m.ncolcxx . . $ constmatrix#ncol. - 1) <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). m.verticalBandcxx . . $ constmatrix#verticalBand(4283795acefb2364). (columnToRemove + 1, m.ncolcxx . . $ constmatrix#ncol.); |
| 960 | return resizedcxx . . $ automatrix#automatrix(a69681c49ef9c9eb).; |
| 961 | } |
| 962 | |
| 963 | autoGaussianMixture TableOfReal_to_GaussianMixture_CEMMcxx . . $ TableOfReal_to_GaussianMixture_CEMM(8abb6647b26b580c). (TableOfRealcxx . . $ TableOfReal# me, integercxx . . $ integer# minimumNumberOfComponents, integercxx . . $ integer# maximumNumberOfComponents, kGaussianMixtureStorage storage, integercxx . . $ integer# maximumNumberOfIterations, double tolerance, bool info) { |
| 964 | try { |
| 965 | autoGaussianMixture thee = GaussianMixture_createcxx . . $ GaussianMixture_create(261625e4cf0d6340). (maximumNumberOfComponents, mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns., storage); |
| 966 | GaussianMixture_setColumnLabelsFromTableOfRealcxx . . $ GaussianMixture_setColumnLabelsFromTableOfReal(6d8d040d5c79cca5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me); |
| 967 | GaussianMixture_initialGuesscxx . . $ GaussianMixture_initialGuess(6d8d040d5c79cca5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me); |
| 968 | if (maximumNumberOfIterations <= 0) |
| 969 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 970 | return GaussianMixture_TableOfReal_to_GaussianMixture_CEMMcxx . . $ GaussianMixture_TableOfReal_to_GaussianMixture_CEMM(f4de5d0ba8c19c45). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me, minimumNumberOfComponents, maximumNumberOfIterations, tolerance, info); |
| 971 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 972 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U"GaussianMixture not created from TableOfReal."); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | autoGaussianMixture GaussianMixture_TableOfReal_to_GaussianMixture_CEMMcxx . . $ GaussianMixture_TableOfReal_to_GaussianMixture_CEMM(f4de5d0ba8c19c45). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee, integercxx . . $ integer# minimumNumberOfComponents, integercxx . . $ integer# maxNumberOfIterations, double tolerance, bool info) { |
| 977 | try { |
| 978 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (thycxx . . $ `melder/melder.h:45:9`! numberOfColumns == mycxx . . $ `melder/melder.h:44:9`! dimension, |
| 979 | U"The number of columns in the TableOfReal and the dimension of the model should agree."); |
| 980 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents < thycxx . . $ `melder/melder.h:45:9`! numberOfRows / 2, |
| 981 | U"Not enough data points."); |
| 982 | kGaussianMixtureCriterion criterion = kGaussianMixtureCriterion::MESSAGE_LENGTH; |
| 983 | const conststring32cxx . . $ conststring32# criterionText = GaussianMixture_criterionTextcxx . . $ GaussianMixture_criterionText(185f836f08e9b4fd). (criterion); |
| 984 | const bool deleteWeakComponents = minimumNumberOfComponents > 0; |
| 985 | autoGaussianMixture him = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 986 | autoMATcxx . . $ autoMAT# probabilities = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., hiscxx . . $ `melder/melder.h:47:9`! numberOfComponents); |
| 987 | autoMATcxx . . $ autoMAT# responsibilities = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., hiscxx . . $ `melder/melder.h:47:9`! numberOfComponents); |
| 988 | |
| 989 | autoCovariancecxx . . $ autoCovariance# covg = TableOfReal_to_Covariancecxx . . $ TableOfReal_to_Covariance(3e7df3a959f2f2b3). (thee); |
| 990 | |
| 991 | const double npars = GaussianMixture_getNumberOfParametersInComponentcxx . . $ GaussianMixture_getNumberOfParametersInComponent(8a2166766d4fa51c). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 992 | const double nparsd2 = ( deleteWeakComponents ? npars / 2.0 : 0.0 ); |
| 993 | |
| 994 | // Initial E-step: Update all component probabilities. |
| 995 | |
| 996 | GaussianMixture_TableOfReal_getComponentProbabilitiescxx . . $ GaussianMixture_TableOfReal_getComponentProbabilities(674342a5e75573ef). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thee, 0, probabilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 997 | GaussianMixture_getResponsibilitiescxx . . $ GaussianMixture_getResponsibilities(b8ab51b15584a097). (me, probabilitiescxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), 0, responsibilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 998 | |
| 999 | double lnew = GaussianMixture_getLikelihoodValuecxx . . $ GaussianMixture_getLikelihoodValue(97c1d46bb5e56950). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), probabilitiescxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), criterion); |
| 1000 | |
| 1001 | autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"Gaussian mixture..."); |
| 1002 | autoGaussianMixture best = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 1003 | |
| 1004 | double lstart = lnew / thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 1005 | integercxx . . $ integer# iter = 0, numberOfNonzeroComponents = hiscxx . . $ `melder/melder.h:47:9`! numberOfComponents; |
| 1006 | double lmax = -1e308, lprev; |
| 1007 | if (info) |
| 1008 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"iter = 0, ML = ", lstart, U" ", criterionText); |
| 1009 | while (numberOfNonzeroComponents >= minimumNumberOfComponents) { |
| 1010 | do { |
| 1011 | iter ++; |
| 1012 | lprev = lnew; |
| 1013 | for (integercxx . . $ integer# icomponent = 1; icomponent <= hiscxx . . $ `melder/melder.h:47:9`! numberOfComponents; icomponent ++) { |
| 1014 | |
| 1015 | GaussianMixture_getResponsibilitiescxx . . $ GaussianMixture_getResponsibilities(b8ab51b15584a097). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), probabilitiescxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), icomponent, responsibilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 1016 | // Now check if enough support for a component exists |
| 1017 | |
| 1018 | const double componentSupport = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (responsibilitiescxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (icomponent)) - nparsd2; |
| 1019 | double totalSupport = 0.0; |
| 1020 | for (integercxx . . $ integer# component = 1; component <= hiscxx . . $ `melder/melder.h:47:9`! numberOfComponents; component ++) |
| 1021 | totalSupport += stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (0.0, NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (responsibilitiescxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (component)) - nparsd2); |
| 1022 | |
| 1023 | hiscxx . . $ `melder/melder.h:47:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icomponent] = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (0.0, componentSupport / totalSupport); |
| 1024 | |
| 1025 | VECnormalize_inplacecxx . . $ VECnormalize_inplace(963991e5268b1933). (hiscxx . . $ `melder/melder.h:47:9`! mixingProbabilities.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 1.0, 1.0); // redistribute probability mass |
| 1026 | |
| 1027 | if (hiscxx . . $ `melder/melder.h:47:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icomponent] > 0.0) { |
| 1028 | // update probabilities for component |
| 1029 | GaussianMixture_updateComponentcxx . . $ GaussianMixture_updateComponent(6f1ed30d7567a247). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), icomponent, thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), responsibilitiescxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 1030 | //if (lambda > 0) |
| 1031 | // GaussianMixture_addCovarianceFraction (him.get(), icomponent, covg.get(), lambda); |
| 1032 | GaussianMixture_TableOfReal_getComponentProbabilitiescxx . . $ GaussianMixture_TableOfReal_getComponentProbabilities(674342a5e75573ef). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thee, icomponent, probabilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 1033 | } else { |
| 1034 | /* |
| 1035 | "Remove" the component from GaussianMixture, the probabilities and responsibilities |
| 1036 | */ |
| 1037 | if (numberOfNonzeroComponents > minimumNumberOfComponents) { |
| 1038 | numberOfNonzeroComponents --; |
| 1039 | probabilities.columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (icomponent) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 1040 | responsibilities.columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (icomponent) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 1041 | MATnormalizeRows_inplacecxx . . $ MATnormalizeRows_inplace(2fdf026ca2b4c21). (responsibilitiescxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), 1.0, 1.0); // maintain invariant |
| 1042 | if (info) |
| 1043 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"iter = ", iter, U", component ", icomponent, U" removed."); |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | // L(theta,Y)=N/2 sum(m=1..k, log(n*mixingP [m]/12))+k/2log(n/12)+k/2(N+1)-loglikelihood reduces to: |
| 1049 | // k/2 (N+1){log(n/12)+1}+N/2sum(m=1..k,mixingP [m]) - loglikelihood |
| 1050 | |
| 1051 | lnew = GaussianMixture_getLikelihoodValuecxx . . $ GaussianMixture_getLikelihoodValue(97c1d46bb5e56950). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), probabilitiescxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), criterion); |
| 1052 | if (info) |
| 1053 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"iter = ", iter, U", ML = ", lnew); |
| 1054 | } while (lnew > lprev && fabscxx . . $ fabs(fee4f1d22bc8e617). (lprev - lnew) > stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (tolerance * fabscxx . . $ fabs(fee4f1d22bc8e617). (lnew), NUMepscxx . . $ `dwsys/NUM2.h:36:9`!) && iter < maxNumberOfIterations); |
| 1055 | if (lnew > lmax) { |
| 1056 | best =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 1057 | lmax = lnew; |
| 1058 | if (! deleteWeakComponents) { |
| 1059 | break; // TODO was goto end; is dat hetzelfde? |
| 1060 | } |
| 1061 | } |
| 1062 | if (numberOfNonzeroComponents > minimumNumberOfComponents) { // remove smallest component |
| 1063 | integercxx . . $ integer# componentToDelete = 1; |
| 1064 | double mpmin = 1.1; // |
| 1065 | for (integercxx . . $ integer# component = 1; component <= hiscxx . . $ `melder/melder.h:47:9`! numberOfComponents; component ++) { |
| 1066 | if (hiscxx . . $ `melder/melder.h:47:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component] > 0 && hiscxx . . $ `melder/melder.h:47:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component] < mpmin) { |
| 1067 | mpmin = hiscxx . . $ `melder/melder.h:47:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component]; |
| 1068 | componentToDelete = component; |
| 1069 | } |
| 1070 | } |
| 1071 | hiscxx . . $ `melder/melder.h:47:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).componentToDelete] = 0.0; |
| 1072 | numberOfNonzeroComponents --; |
| 1073 | probabilities.columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (componentToDelete) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 1074 | responsibilities.columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (componentToDelete) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 1075 | MATnormalizeRows_inplacecxx . . $ MATnormalizeRows_inplace(2fdf026ca2b4c21). (responsibilitiescxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), 1.0, 1.0); // maintain invariant |
| 1076 | if (info) |
| 1077 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"iter = ", iter, U", component ", componentToDelete, U" removed (after)."); |
| 1078 | } else { |
| 1079 | break; |
| 1080 | } |
| 1081 | } |
| 1082 | if (info) |
| 1083 | MelderInfo_draincxx . . $ MelderInfo_drain(49f6e7a06ebc5aa8).(); |
| 1084 | GaussianMixture_removeUnsupportedComponentscxx . . $ GaussianMixture_removeUnsupportedComponents(a29c70c932200d85). (best.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 1085 | return bestcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 1086 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 1087 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"GaussianMixture not improved."); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | // The numberOfElements per covariance needs to be updated later |
| 1092 | void GaussianMixture_removeComponentcxx . . $ GaussianMixture_removeComponent(2fd3f882ef0dccef). (GaussianMixture me, integercxx . . $ integer# component) { |
| 1093 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents == 1) |
| 1094 | return; |
| 1095 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (component > 0 && component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, |
| 1096 | U"The component number should be in the range from 1 to ", mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, U"."); |
| 1097 | |
| 1098 | mycxx . . $ `melder/melder.h:44:9`! covariances ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). removeItemcxx . . $ CollectionOf#removeItem(1624ee8275b10bda). (component); |
| 1099 | mycxx . . $ `melder/melder.h:44:9`! numberOfComponents --; |
| 1100 | |
| 1101 | for (integercxx . . $ integer# ic = component; ic <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; ic ++) |
| 1102 | mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic] = mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ic + 1]; |
| 1103 | |
| 1104 | mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); |
| 1105 | |
| 1106 | VECnormalize_inplacecxx . . $ VECnormalize_inplace(963991e5268b1933). (mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 1.0, 1.0); |
| 1107 | } |
| 1108 | |
| 1109 | autoGaussianMixture TableOfReal_to_GaussianMixturecxx . . $ TableOfReal_to_GaussianMixture(3a584962ac32c20). (TableOfRealcxx . . $ TableOfReal# me, integercxx . . $ integer# numberOfComponents, double delta_lnp, integercxx . . $ integer# maxNumberOfIterations, double lambda, kGaussianMixtureStorage storage, kGaussianMixtureCriterion criterion) { |
| 1110 | try { |
| 1111 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfRows >= 2 * numberOfComponents, |
| 1112 | U"The number of rows should at least be twice the number of components."); |
| 1113 | autoGaussianMixture thee = GaussianMixture_createcxx . . $ GaussianMixture_create(261625e4cf0d6340). (numberOfComponents, mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns., storage); |
| 1114 | GaussianMixture_setColumnLabelsFromTableOfRealcxx . . $ GaussianMixture_setColumnLabelsFromTableOfReal(6d8d040d5c79cca5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me); |
| 1115 | GaussianMixture_initialGuesscxx . . $ GaussianMixture_initialGuess(6d8d040d5c79cca5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me); |
| 1116 | if (maxNumberOfIterations <= 0) |
| 1117 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 1118 | GaussianMixture_TableOfReal_improveLikelihoodcxx . . $ GaussianMixture_TableOfReal_improveLikelihood(4d9c0daae3862ead). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me, delta_lnp, maxNumberOfIterations, lambda, criterion); |
| 1119 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 1120 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 1121 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no GaussianMixture created."); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | autoCorrelationcxx . . $ autoCorrelation# GaussianMixture_TableOfReal_to_Correlationcxx . . $ GaussianMixture_TableOfReal_to_Correlation(d429fe4c18f76d6b). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee) { |
| 1126 | try { |
| 1127 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dimension == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 1128 | U"Dimensions should be equal."); |
| 1129 | autoClassificationTablecxx . . $ autoClassificationTable# ct = GaussianMixture_TableOfReal_to_ClassificationTablecxx . . $ GaussianMixture_TableOfReal_to_ClassificationTable(b53771290e8e341a). (me, thee); |
| 1130 | autoCorrelationcxx . . $ autoCorrelation# him = ClassificationTable_to_Correlation_columnscxx . . $ ClassificationTable_to_Correlation_columns(acb4bc943cce890a). (ct.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 1131 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 1132 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 1133 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Correlation not created from GaussianMixture & TableOfReal."); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | double GaussianMixture_getProbabilityAtPosition_stringcxx . . $ GaussianMixture_getProbabilityAtPosition_string(b3e1baf1267a95da). (GaussianMixture me, conststring32cxx . . $ conststring32# vector_string) { |
| 1138 | autoSTRVECcxx . . $ autoSTRVEC# vector = splitByWhitespace_STRVECcxx . . $ splitByWhitespace_STRVEC(d3c03a92d990d008). (vector_string); |
| 1139 | autoVECcxx . . $ autoVEC# pos = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! dimension); |
| 1140 | for (integercxx . . $ integer# i = 1; i <= vector.sizecxx . . $ _autostringautovector#size.; i ++) { |
| 1141 | pos [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = Melder_atofcxx . . $ Melder_atof(13d725aae188c0cd). (vector [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 1142 | if (i == mycxx . . $ `melder/melder.h:44:9`! dimension) |
| 1143 | break; |
| 1144 | } |
| 1145 | const double p = GaussianMixture_getProbabilityAtPositioncxx . . $ GaussianMixture_getProbabilityAtPosition(564626c28c619e52). (me, poscxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 1146 | return p; |
| 1147 | } |
| 1148 | |
| 1149 | double GaussianMixture_getMarginalProbabilityAtPositioncxx . . $ GaussianMixture_getMarginalProbabilityAtPosition(18530cb880ed5215). (GaussianMixture me, constVECVUcxx . . $ constVECVU# const& pos, double x) { |
| 1150 | longdoublecxx . . $ longdouble# p = 0.0; |
| 1151 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im ++) { |
| 1152 | const double pim = Covariance_getMarginalProbabilityAtPositioncxx . . $ Covariance_getMarginalProbabilityAtPosition(add1dc726be0e071). (mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im], pos, x); |
| 1153 | p += mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).im] * pim; |
| 1154 | } |
| 1155 | return (double) p; |
| 1156 | } |
| 1157 | |
| 1158 | double GaussianMixture_getProbabilityAtPositioncxx . . $ GaussianMixture_getProbabilityAtPosition(564626c28c619e52). (GaussianMixture me, constVECcxx . . $ constVEC# const& xpos) { |
| 1159 | longdoublecxx . . $ longdouble# p = 0.0; |
| 1160 | for (integercxx . . $ integer# im = 1; im <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; im ++) { |
| 1161 | const double pim = Covariance_getProbabilityAtPositioncxx . . $ Covariance_getProbabilityAtPosition(9672a574a0a205d1). (mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).im], xpos); |
| 1162 | p += mycxx . . $ `melder/melder.h:44:9`! mixingProbabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).im] * pim; |
| 1163 | } |
| 1164 | return (double) p; |
| 1165 | } |
| 1166 | |
| 1167 | autoMatrixcxx . . $ autoMatrix# GaussianMixture_PCA_to_Matrix_densitycxx . . $ GaussianMixture_PCA_to_Matrix_density(d2c6aa4969d8cdca). (GaussianMixture me, PCAcxx . . $ PCA# thee, integercxx . . $ integer# d1, integercxx . . $ integer# d2, double xmin, double xmax, integercxx . . $ integer# nx, double ymin, double ymax, integercxx . . $ integer# ny) { |
| 1168 | try { |
| 1169 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dimension == thycxx . . $ `melder/melder.h:45:9`! dimension, |
| 1170 | U"Dimensions should be equal."); |
| 1171 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (d1 <= thycxx . . $ `melder/melder.h:45:9`! numberOfEigenvalues && d2 <= thycxx . . $ `melder/melder.h:45:9`! numberOfEigenvalues, |
| 1172 | U"Direction index too high."); |
| 1173 | |
| 1174 | autoVECcxx . . $ autoVEC# v = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! dimension); |
| 1175 | if (xmax == xmin || ymax == ymin) { |
| 1176 | double xmind, xmaxd, ymind, ymaxd, nsigmas = 2.0; |
| 1177 | |
| 1178 | GaussianMixture_PCA_getIntervalsAlongDirectionscxx . . $ GaussianMixture_PCA_getIntervalsAlongDirections(66d03a0598ec4d7e). (me, thee, d1, d2, nsigmas, &xmind, &xmaxd, &ymind, &ymaxd); |
| 1179 | if (xmax == xmin) { |
| 1180 | xmin = xmind; |
| 1181 | xmax = xmaxd; |
| 1182 | } |
| 1183 | if (ymax == ymin) { |
| 1184 | ymin = ymind; |
| 1185 | ymax = ymaxd; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | // xmin,xmax and ymin,ymax are coordinates in the pc1 vs pc2 plane |
| 1190 | |
| 1191 | const double dx = fabscxx . . $ fabs(fee4f1d22bc8e617). (xmax - xmin) / nx, dy = fabscxx . . $ fabs(fee4f1d22bc8e617). (ymax - ymin) / ny; |
| 1192 | const double x1 = xmin + 0.5 * dx, y1 = ymin + 0.5 * dy; |
| 1193 | autoMatrixcxx . . $ autoMatrix# him = Matrix_createcxx . . $ Matrix_create(494a3ae9ff7e3b60). (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1); |
| 1194 | |
| 1195 | for (integercxx . . $ integer# i = 1; i <= ny; i ++) { |
| 1196 | const double y = y1 + (i - 1) * dy; |
| 1197 | for (integercxx . . $ integer# j = 1; j <= nx; j ++) { |
| 1198 | const double x = x1 + (j - 1) * dx; |
| 1199 | for (integercxx . . $ integer# k = 1; k <= mycxx . . $ `melder/melder.h:44:9`! dimension; k ++) |
| 1200 | v [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] = x * thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).d1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k] + y * thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).d2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k]; |
| 1201 | hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = GaussianMixture_getProbabilityAtPositioncxx . . $ GaussianMixture_getProbabilityAtPosition(564626c28c619e52). (me, vcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 1202 | } |
| 1203 | } |
| 1204 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 1205 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 1206 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", thee, U": no Matrix density created."); |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_to_TableOfReal_randomSamplingcxx . . $ GaussianMixture_to_TableOfReal_randomSampling(bb47bed5c8079adb). (GaussianMixture me, integercxx . . $ integer# numberOfPoints) { |
| 1211 | try { |
| 1212 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).1]; |
| 1213 | autoTableOfRealcxx . . $ autoTableOfReal# thee = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (numberOfPoints, mycxx . . $ `melder/melder.h:44:9`! dimension); |
| 1214 | autoVECcxx . . $ autoVEC# buf = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! dimension); |
| 1215 | thycxx . . $ `melder/melder.h:45:9`! columnLabelscxx . . $ structTableOfReal#columnLabels..allcxx . . $ _autostringautovector#all(58997016dad794db).() <<=cxx . . $ `operator<<=`(5bcbc9f49fd394e1). cov -> columnLabelscxx . . $ structTableOfReal#columnLabels..partcxx . . $ _autostringautovector#part(862ae67a684f15c7). (1, mycxx . . $ `melder/melder.h:44:9`! dimension); |
| 1216 | for (integercxx . . $ integer# i = 1; i <= numberOfPoints; i ++) { |
| 1217 | autostring32cxx . . $ autostring32# covnamecxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).; |
| 1218 | GaussianMixture_generateOneVector_inlinecxx . . $ GaussianMixture_generateOneVector_inline(baab5f7d4314310c). (me, thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (i), & covname, buf.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 1219 | TableOfReal_setRowLabelcxx . . $ TableOfReal_setRowLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), i, covname.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 1220 | } |
| 1221 | GaussianMixture_unExpandPCAcxx . . $ GaussianMixture_unExpandPCA(a29c70c932200d85). (me); |
| 1222 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 1223 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 1224 | GaussianMixture_unExpandPCAcxx . . $ GaussianMixture_unExpandPCA(a29c70c932200d85). (me); |
| 1225 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"TableOfReal with random sampling not created."); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | autoTablecxx . . $ autoTable# GaussianMixture_TableOfReal_to_Table_BHEPNormalityTestscxx . . $ GaussianMixture_TableOfReal_to_Table_BHEPNormalityTests(7dad915c890a52c7). (GaussianMixture me, TableOfRealcxx . . $ TableOfReal# thee, double h) { |
| 1230 | try { |
| 1231 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (thycxx . . $ `melder/melder.h:45:9`! numberOfColumns == mycxx . . $ `melder/melder.h:44:9`! dimension, |
| 1232 | U"Dimensions should agree."); |
| 1233 | |
| 1234 | autoMATcxx . . $ autoMAT# responsibilities = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., mycxx . . $ `melder/melder.h:44:9`! numberOfComponents); |
| 1235 | GaussianMixture_TableOfReal_getResponsilitiescxx . . $ GaussianMixture_TableOfReal_getResponsilities(43fdcadf1eb22f23). (me, thee, responsibilities.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 1236 | autoVECcxx . . $ autoVEC# numberOfData = columnSums_VECcxx . . $ columnSums_VEC(81359d2d5440d947). (responsibilitiescxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 1237 | |
| 1238 | const conststring32cxx . . $ conststring32# columnNames [] = { U"component", U"probability", U"smoothing", U"statistic", |
| 1239 | U"lnmu", U"lnvar", U"numberOfData", U"dimension", U"singular" }; |
| 1240 | autoTablecxx . . $ autoTable# him = Table_createWithColumnNamescxx . . $ Table_createWithColumnNames(41b41b537590f3f4). (mycxx . . $ `melder/melder.h:44:9`! numberOfComponents, ARRAY_TO_STRVECcxx . . $ `melder/melder_strvec.h:262:9`! (columnNames)); |
| 1241 | |
| 1242 | for (integercxx . . $ integer# component = 1; component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; component ++) { |
| 1243 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 1244 | Table_setStringValuecxx . . $ Table_setStringValue(309228d4a4a76ab1). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), component, 1, Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (cov)); |
| 1245 | } |
| 1246 | |
| 1247 | for (integercxx . . $ integer# component = 1; component <= mycxx . . $ `melder/melder.h:44:9`! numberOfComponents; component ++) { |
| 1248 | const Covariancecxx . . $ Covariance# cov = mycxx . . $ `melder/melder.h:44:9`! covariances->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).component]; |
| 1249 | autoVECcxx . . $ autoVEC# componentResponsibilities = copy_VECcxx . . $ copy_VEC(d4415e68b25283a8). (responsibilitiescxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (component)); |
| 1250 | double testStatistic, lnmu, lnvar; |
| 1251 | bool isSingular; |
| 1252 | const double probability = Covariance_TableOfReal_normalityTest_BHEPcxx . . $ Covariance_TableOfReal_normalityTest_BHEP(19ee1367b729c2b3). (cov, thee, |
| 1253 | componentResponsibilitiescxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), & h, & testStatistic, & lnmu, & lnvar, & isSingular); |
| 1254 | |
| 1255 | Table_setNumericValuecxx . . $ Table_setNumericValue(adcc5027476b94a0). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), component, 2, probability); |
| 1256 | Table_setNumericValuecxx . . $ Table_setNumericValue(adcc5027476b94a0). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), component, 3, h); |
| 1257 | Table_setNumericValuecxx . . $ Table_setNumericValue(adcc5027476b94a0). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), component, 4, testStatistic); |
| 1258 | Table_setNumericValuecxx . . $ Table_setNumericValue(adcc5027476b94a0). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), component, 5, lnmu); |
| 1259 | Table_setNumericValuecxx . . $ Table_setNumericValue(adcc5027476b94a0). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), component, 6, lnvar); |
| 1260 | Table_setNumericValuecxx . . $ Table_setNumericValue(adcc5027476b94a0). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), component, 7, numberOfData [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).component]); |
| 1261 | Table_setNumericValuecxx . . $ Table_setNumericValue(adcc5027476b94a0). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), component, 8, mycxx . . $ `melder/melder.h:44:9`! dimension); |
| 1262 | } |
| 1263 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 1264 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 1265 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Table for BHEP not created."); |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | /* End of file GaussianMixture.cpp 1555*/ |