| 1 | /* TableOfReal_and_Discriminant.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2020 David Weenink, 2020,2021,2026 Paul Boersma |
| 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 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "TableOfReal_and_Discriminant.h"cxx . . $ `<file>/dwtools/TableOfReal_and_Discriminant.h`/ |
| 20 | |
| 21 | autoDiscriminantcxx . . $ autoDiscriminant# TableOfReal_to_Discriminantcxx . . $ TableOfReal_to_Discriminant(f0615bf04d0371aa). (TableOfRealcxx . . $ TableOfReal# me) { |
| 22 | try { |
| 23 | autoDiscriminantcxx . . $ autoDiscriminant# thee = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Discriminant); |
| 24 | const integercxx . . $ integer# dimension = mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; |
| 25 | |
| 26 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (NUMdefined (mycxx . . $ `melder/melder.h:44:9`! data.get()), |
| 27 | U"There should be no undefined elements in the table."); |
| 28 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (TableOfReal_hasRowLabels (me), |
| 29 | U"All rows should be labeled."); |
| 30 | |
| 31 | autoTableOfRealcxx . . $ autoTableOfReal# mew = TableOfReal_sortOnlyByRowLabelscxx . . $ TableOfReal_sortOnlyByRowLabels(d3f65ba86ef5eddf). (me); |
| 32 | if (! TableOfReal_hasColumnLabelscxx . . $ TableOfReal_hasColumnLabels(e9b5ab7ea1aaed43). (mew.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).())) |
| 33 | TableOfReal_setSequentialColumnLabelscxx . . $ TableOfReal_setSequentialColumnLabels(8dc3139cdc86d712). (mew.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0, 0, U"c", 1, 1); |
| 34 | |
| 35 | thycxx . . $ `melder/melder.h:45:9`! groupscxx . . $ structDiscriminant#groups. =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). TableOfReal_to_SSCPList_byLabelcxx . . $ TableOfReal_to_SSCPList_byLabel(2e5f452c6e02ade1). (mew.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 36 | thycxx . . $ `melder/melder.h:45:9`! totalcxx . . $ structDiscriminant#total. =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). TableOfReal_to_SSCPcxx . . $ TableOfReal_to_SSCP(e6b6a917f4e16020). (mew.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0, 0, 0, 0); |
| 37 | |
| 38 | if ((thycxx . . $ `melder/melder.h:45:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups. = thycxx . . $ `melder/melder.h:45:9`! groupscxx . . $ structDiscriminant#groups.->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).sizecxx . . $ CollectionOf#size.) < 2) |
| 39 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Number of groups should be greater than one."); |
| 40 | |
| 41 | TableOfReal_centreColumns_byRowLabelcxx . . $ TableOfReal_centreColumns_byRowLabel(683412d750813a40). (mew.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 42 | |
| 43 | // Overall centroid and apriori probabilities and costs. |
| 44 | |
| 45 | autoVECcxx . . $ autoVEC# centroid = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (dimension); |
| 46 | autoMATcxx . . $ autoMAT# between = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups., dimension); |
| 47 | thycxx . . $ `melder/melder.h:45:9`! aprioriProbabilitiescxx . . $ structDiscriminant#aprioriProbabilities. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (thycxx . . $ `melder/melder.h:45:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups.); |
| 48 | |
| 49 | longdoublecxx . . $ longdouble# sum = 0.0; |
| 50 | for (integercxx . . $ integer# k = 1; k <= thycxx . . $ `melder/melder.h:45:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups.; k ++) { |
| 51 | const SSCPcxx . . $ SSCP# m = thycxx . . $ `melder/melder.h:45:9`! groupscxx . . $ structDiscriminant#groups.->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).k]; |
| 52 | const double scale = SSCP_getNumberOfObservationscxx . . $ SSCP_getNumberOfObservations(aff8a8bd1da17c96). (m); |
| 53 | centroid.allcxx . . $ autovector#all(a26726dad87555bb).() +=cxx . . $ `operator+=`(d166bd5b5c96d95c). scale *cxx . . $ `operator*`(7f4bd74ab5a11911). mcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). -> centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).(); |
| 54 | sum += scale; |
| 55 | } |
| 56 | centroid.allcxx . . $ autovector#all(a26726dad87555bb).() /=cxx . . $ `operator/=`(c555d170d72ff256). double (sum); |
| 57 | |
| 58 | for (integercxx . . $ integer# k = 1; k <= thycxx . . $ `melder/melder.h:45:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups.; k ++) { |
| 59 | const SSCPcxx . . $ SSCP# m = thycxx . . $ `melder/melder.h:45:9`! groupscxx . . $ structDiscriminant#groups.->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).k]; |
| 60 | const double scale = SSCP_getNumberOfObservationscxx . . $ SSCP_getNumberOfObservations(aff8a8bd1da17c96). (m); |
| 61 | thycxx . . $ `melder/melder.h:45:9`! aprioriProbabilitiescxx . . $ structDiscriminant#aprioriProbabilities. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] = scale / mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 62 | betweencxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (k) <<=cxx . . $ `operator<<=`(ce74bae6c180f663). mcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). -> centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() -cxx . . $ operator-(fcdbb1279f7b512e). centroidcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).(); |
| 63 | betweencxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (k) *=cxx . . $ `operator*=`(c555d170d72ff256). sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (scale); |
| 64 | } |
| 65 | |
| 66 | // We need to solve B'B.x = lambda W'W.x, where B'B and W'W are the between and within covariance matrices. |
| 67 | // We do not calculate these covariance matrices directly from the data but instead use the GSVD to solve for |
| 68 | // the eigenvalues and eigenvectors of the equation. |
| 69 | |
| 70 | thycxx . . $ `melder/melder.h:45:9`! eigencxx . . $ structDiscriminant#eigen. =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Thing_newcxx . . $ `sys/Thing.h:514:9`! (Eigen); |
| 71 | Eigen_initFromSquareRootPaircxx . . $ Eigen_initFromSquareRootPair(4808493b01839aeb). (thycxx . . $ `melder/melder.h:45:9`! eigencxx . . $ structDiscriminant#eigen..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), betweencxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), mewcxx . . $ constmatrix#constmatrix(eed9513d24570b3d). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 72 | |
| 73 | /* |
| 74 | Costs. |
| 75 | */ |
| 76 | thycxx . . $ `melder/melder.h:45:9`! costscxx . . $ structDiscriminant#costs. =cxx . . $ automatrix#`operator=`(a0f46581f260c488). raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups., thycxx . . $ `melder/melder.h:45:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups.); |
| 77 | |
| 78 | thycxx . . $ `melder/melder.h:45:9`! costscxx . . $ structDiscriminant#costs..allcxx . . $ automatrix#all(2114117ba8ed4224).() <<=cxx . . $ `operator<<=`(50b469ae42a42f64). 1.0; |
| 79 | thycxx . . $ `melder/melder.h:45:9`! costscxx . . $ structDiscriminant#costs..diagonalcxx . . $ automatrix#diagonal(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 80 | |
| 81 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 82 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 83 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": Discriminant not created."); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | |
| 88 | autoTableOfRealcxx . . $ autoTableOfReal# Discriminant_TableOfReal_mahalanobiscxx . . $ Discriminant_TableOfReal_mahalanobis(e59a0d6ac2121b79). (Discriminantcxx . . $ Discriminant# me, TableOfRealcxx . . $ TableOfReal# thee, integercxx . . $ integer# group, bool poolCovarianceMatrices) { |
| 89 | try { |
| 90 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (group > 0 && group <= mycxx . . $ `melder/melder.h:44:9`! numberOfGroups, |
| 91 | U"Group should be in the range [1, ", mycxx . . $ `melder/melder.h:44:9`! numberOfGroups, U"]."); |
| 92 | autoSSCPcxx . . $ autoSSCP# pool = SSCPList_to_SSCP_poolcxx . . $ SSCPList_to_SSCP_pool(b57a97f114ea42f6). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 93 | autoCovariancecxx . . $ autoCovariance# covg = SSCP_to_Covariancecxx . . $ SSCP_to_Covariance(872697cc608a64ad). (pool.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), mycxx . . $ `melder/melder.h:44:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups.); |
| 94 | autoCovariancecxx . . $ autoCovariance# cov = SSCP_to_Covariancecxx . . $ SSCP_to_Covariance(872697cc608a64ad). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups.->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).group], 1); |
| 95 | autoTableOfRealcxx . . $ autoTableOfReal# himcxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).; |
| 96 | if (poolCovarianceMatrices) { // use group mean instead of overall mean! |
| 97 | covg ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). covcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).(); |
| 98 | him =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Covariance_TableOfReal_mahalanobiscxx . . $ Covariance_TableOfReal_mahalanobis(bf87fb867ef00418). (covg.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thee, false); |
| 99 | } else { |
| 100 | him =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Covariance_TableOfReal_mahalanobiscxx . . $ Covariance_TableOfReal_mahalanobis(bf87fb867ef00418). (cov.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thee, false); |
| 101 | } |
| 102 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 103 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 104 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"TableOfReal not created."); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | autoTableOfRealcxx . . $ autoTableOfReal# Discriminant_TableOfReal_mahalanobis_allcxx . . $ Discriminant_TableOfReal_mahalanobis_all(cb77a113274d3b6). (Discriminantcxx . . $ Discriminant# me, TableOfRealcxx . . $ TableOfReal# thee, bool poolCovarianceMatrices) { |
| 109 | autoCovariancecxx . . $ autoCovariance# covgcxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).; |
| 110 | if (poolCovarianceMatrices) { |
| 111 | autoSSCPcxx . . $ autoSSCP# pool = SSCPList_to_SSCP_poolcxx . . $ SSCPList_to_SSCP_pool(b57a97f114ea42f6). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 112 | covg =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). SSCP_to_Covariancecxx . . $ SSCP_to_Covariance(872697cc608a64ad). (pool.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), mycxx . . $ `melder/melder.h:44:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups.); |
| 113 | } |
| 114 | autoTableOfRealcxx . . $ autoTableOfReal# him = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., 1); |
| 115 | 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).(); |
| 116 | for (integercxx . . $ integer# igroup = 1 ; igroup <= mycxx . . $ `melder/melder.h:44:9`! numberOfGroupscxx . . $ structDiscriminant#numberOfGroups.; igroup ++) { |
| 117 | conststring32cxx . . $ conststring32# label = Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups.->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).igroup]); |
| 118 | autoCovariancecxx . . $ autoCovariance# cov = SSCP_to_Covariancecxx . . $ SSCP_to_Covariance(872697cc608a64ad). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups.->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).igroup], 1); |
| 119 | autoTableOfRealcxx . . $ autoTableOfReal# groupMahalanobiscxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).; |
| 120 | if (poolCovarianceMatrices) { |
| 121 | covg ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). covcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). centroidcxx . . $ structSSCP#centroid..allcxx . . $ autovector#all(a26726dad87555bb).(); |
| 122 | groupMahalanobis =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Covariance_TableOfReal_mahalanobiscxx . . $ Covariance_TableOfReal_mahalanobis(bf87fb867ef00418). (covg.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thee, false); |
| 123 | } else { |
| 124 | groupMahalanobis =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Covariance_TableOfReal_mahalanobiscxx . . $ Covariance_TableOfReal_mahalanobis(bf87fb867ef00418). (cov.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thee, false); |
| 125 | } |
| 126 | for (integercxx . . $ integer# idata = 1; idata <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; idata ++) |
| 127 | if (Melder_equcxx . . $ `melder/melder_str32.h:124:9`! (label, hiscxx . . $ `melder/melder.h:47:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).idata].getcxx . . $ _autostring#get(5a2a78a048fb49a8).())) |
| 128 | hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).idata] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = groupMahalanobis->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).idata] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1]; |
| 129 | } |
| 130 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 131 | } |
| 132 | |
| 133 | autoClassificationTablecxx . . $ autoClassificationTable# Discriminant_TableOfReal_to_ClassificationTablecxx . . $ Discriminant_TableOfReal_to_ClassificationTable(371b4067fac9b1bc). (Discriminantcxx . . $ Discriminant# me, TableOfRealcxx . . $ TableOfReal# thee, bool poolCovarianceMatrices, bool useAprioriProbabilities) { |
| 134 | try { |
| 135 | const integercxx . . $ integer# numberOfGroups = Discriminant_getNumberOfGroupscxx . . $ Discriminant_getNumberOfGroups(5ee7605c36a67a8). (me); |
| 136 | const integercxx . . $ integer# dimension = Eigen_getDimensionOfComponentscxx . . $ Eigen_getDimensionOfComponents(4ba876ea4b138ca4). (mycxx . . $ `melder/melder.h:44:9`! eigencxx . . $ structDiscriminant#eigen..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 137 | |
| 138 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (dimension == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 139 | U"The number of columns should agree with the dimension of the discriminant."); |
| 140 | |
| 141 | autoVECcxx . . $ autoVEC# log_p = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfGroups); |
| 142 | autoVECcxx . . $ autoVEC# log_apriori = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfGroups); |
| 143 | autoVECcxx . . $ autoVEC# ln_determinant = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfGroups); |
| 144 | autoVECcxx . . $ autoVEC# buf = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (dimension); |
| 145 | |
| 146 | autovectorcxx . . $ autovector# <SSCPcxx . . $ SSCP#> sscpvec = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <SSCPcxx . . $ SSCP#> (numberOfGroups); |
| 147 | autoSSCPcxx . . $ autoSSCP# pool = SSCPList_to_SSCP_poolcxx . . $ SSCPList_to_SSCP_pool(b57a97f114ea42f6). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 148 | autoClassificationTablecxx . . $ autoClassificationTable# him = ClassificationTable_createcxx . . $ ClassificationTable_create(b9c68632944d67c8). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., numberOfGroups); |
| 149 | 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).(); |
| 150 | |
| 151 | /* |
| 152 | Scale the sscp to become a covariance matrix. |
| 153 | */ |
| 154 | poolcxx . . $ matrixview#matrixview(eed9513d24570b3d). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).() *=cxx . . $ `operator*=`(50b469ae42a42f64). 1.0 / (pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfObservationscxx . . $ structSSCP#numberOfObservations. - numberOfGroups); |
| 155 | |
| 156 | double lnd; |
| 157 | autoSSCPListcxx . . $ autoSSCPList# agroupscxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).; |
| 158 | SSCPListcxx . . $ SSCPList# groups; // ppgb FIXME dit kan niet goed izjn |
| 159 | if (poolCovarianceMatrices) { |
| 160 | /* |
| 161 | Covariance matrix S can be decomposed as S = L.L'. Calculate L^-1. |
| 162 | L^-1 will be used later in the Mahalanobis distance calculation: |
| 163 | v'.S^-1.v == v'.L^-1'.L^-1.v == (L^-1.v)'.(L^-1.v). |
| 164 | */ |
| 165 | if (Melder_debugcxx . . $ Melder_debug. == 52) |
| 166 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"***** before lower Cholesky inverse: \n", pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).()); |
| 167 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & lnd); |
| 168 | if (Melder_debugcxx . . $ Melder_debug. == 52) |
| 169 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"***** after lower Cholesky inverse: \n", pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..allcxx . . $ automatrix#all(2114117ba8ed4224).()); |
| 170 | for (integercxx . . $ integer# j = 1; j <= numberOfGroups; j ++) { |
| 171 | ln_determinant [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = lnd; |
| 172 | sscpvec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = pool.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 173 | } |
| 174 | groups = mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 175 | } else { |
| 176 | /* |
| 177 | Calculate the inverses of all group covariance matrices. |
| 178 | In case of a singular matrix, substitute inverse of pooled. |
| 179 | */ |
| 180 | agroups =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 181 | groups = agroups.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 182 | integercxx . . $ integer# npool = 0; |
| 183 | for (integercxx . . $ integer# j = 1; j <= numberOfGroups; j ++) { |
| 184 | const SSCPcxx . . $ SSCP# t = groups->atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).j]; |
| 185 | const integercxx . . $ integer# no = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (SSCP_getNumberOfObservationscxx . . $ SSCP_getNumberOfObservations(aff8a8bd1da17c96). (t)); |
| 186 | tcxx . . $ matrixview#matrixview(eed9513d24570b3d). -> datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).() *=cxx . . $ `operator*=`(50b469ae42a42f64). 1.0 / (no - 1); |
| 187 | |
| 188 | sscpvec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = groups->atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).j]; |
| 189 | try { |
| 190 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (t -> datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & ln_determinant [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]); |
| 191 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 192 | /* |
| 193 | Clear the error. |
| 194 | Try the alternative: the pooled covariance matrix. |
| 195 | */ |
| 196 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 197 | if (npool == 0) |
| 198 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & lnd); |
| 199 | npool ++; |
| 200 | sscpvec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = pool.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 201 | ln_determinant [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = lnd; |
| 202 | } |
| 203 | } |
| 204 | if (npool > 0) |
| 205 | Melder_warningcxx . . $ Melder_warning(6298c93241566110). (npool, U" groups use pooled covariance matrix."); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | Labels for columns in ClassificationTable |
| 210 | */ |
| 211 | for (integercxx . . $ integer# j = 1; j <= numberOfGroups; j ++) { |
| 212 | conststring32cxx . . $ conststring32# name = Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups.->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).j]); |
| 213 | if (! name) |
| 214 | name = U"?"; |
| 215 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), j, name); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | Normalize the sum of the apriori probabilities to 1. |
| 220 | Next take ln (p) because otherwise probabilities might be too small to represent. |
| 221 | */ |
| 222 | if (Melder_debugcxx . . $ Melder_debug. == 52) |
| 223 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"***** before normalizing priors: \n", mycxx . . $ `melder/melder.h:44:9`! aprioriProbabilitiescxx . . $ structDiscriminant#aprioriProbabilities..allcxx . . $ autovector#all(a26726dad87555bb).()); |
| 224 | VECnormalize_inplacecxx . . $ VECnormalize_inplace(963991e5268b1933). (mycxx . . $ `melder/melder.h:44:9`! aprioriProbabilitiescxx . . $ structDiscriminant#aprioriProbabilities..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 1.0, 1.0); |
| 225 | if (Melder_debugcxx . . $ Melder_debug. == 52) |
| 226 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"***** after normalizing priors: \n", mycxx . . $ `melder/melder.h:44:9`! aprioriProbabilitiescxx . . $ structDiscriminant#aprioriProbabilities..allcxx . . $ autovector#all(a26726dad87555bb).()); |
| 227 | const double logg = logcxx . . $ log(7759b11ec49fe502). (numberOfGroups); |
| 228 | for (integercxx . . $ integer# j = 1; j <= numberOfGroups; j ++) |
| 229 | log_apriori [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = ( useAprioriProbabilities ? logcxx . . $ log(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! aprioriProbabilitiescxx . . $ structDiscriminant#aprioriProbabilities. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]) : - logg ); |
| 230 | |
| 231 | /* |
| 232 | Generalized squared distance function: |
| 233 | D^2(x) = (x - mu)' S^-1 (x - mu) + ln (determinant(S)) - 2 ln (apriori) |
| 234 | */ |
| 235 | for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; i ++) { |
| 236 | double norm = 0.0, pt_max = -1e308; |
| 237 | for (integercxx . . $ integer# j = 1; j <= numberOfGroups; j ++) { |
| 238 | const SSCPcxx . . $ SSCP# t = groups->atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).j]; |
| 239 | const double md = NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (sscpveccxx . . $ constmatrix#constmatrix(eed9513d24570b3d). [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] -> datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (i), tcxx . . $ constvector#constvector(eac9d6ec4c4bfecd). -> centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 240 | if (Melder_debugcxx . . $ Melder_debug. == 52) |
| 241 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"***** Mahalanobis distance (squared): ", i, U" ", j, U" ", md); |
| 242 | const double pt = log_apriori [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] - 0.5 * (ln_determinant [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] + md); |
| 243 | if (pt > pt_max) |
| 244 | pt_max = pt; |
| 245 | log_p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = pt; |
| 246 | } |
| 247 | for (integercxx . . $ integer# j = 1; j <= numberOfGroups; j ++) |
| 248 | norm += log_p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = expcxx . . $ exp(fee4f1d22bc8e617). (log_p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] - pt_max); |
| 249 | for (integercxx . . $ integer# j = 1; j <= numberOfGroups; j ++) |
| 250 | hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = log_p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] / norm; |
| 251 | } |
| 252 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 253 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 254 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"ClassificationTable from Discriminant & TableOfReal not created."); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | autoClassificationTablecxx . . $ autoClassificationTable# Discriminant_TableOfReal_to_ClassificationTable_dwcxx . . $ Discriminant_TableOfReal_to_ClassificationTable_dw(9eb43969a5427dab). (Discriminantcxx . . $ Discriminant# me, TableOfRealcxx . . $ TableOfReal# thee, bool poolCovarianceMatrices, bool useAprioriProbabilities, double alpha, double minProb, autoTableOfRealcxx . . $ autoTableOfReal# *displacements) { |
| 259 | try { |
| 260 | const integercxx . . $ integer# g = Discriminant_getNumberOfGroupscxx . . $ Discriminant_getNumberOfGroups(5ee7605c36a67a8). (me); |
| 261 | const integercxx . . $ integer# p = Eigen_getDimensionOfComponentscxx . . $ Eigen_getDimensionOfComponents(4ba876ea4b138ca4). (mycxx . . $ `melder/melder.h:44:9`! eigencxx . . $ structDiscriminant#eigen..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 262 | const integercxx . . $ integer# m = thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 263 | |
| 264 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (p == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 265 | U"The number of columns does not agree with the dimension of the discriminant."); |
| 266 | |
| 267 | autoVECcxx . . $ autoVEC# log_p = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (g); |
| 268 | autoVECcxx . . $ autoVEC# log_apriori = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (g); |
| 269 | autoVECcxx . . $ autoVEC# ln_determinant = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (g); |
| 270 | autoVECcxx . . $ autoVEC# buf = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (p); |
| 271 | autoVECcxx . . $ autoVEC# displacement = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (p); |
| 272 | autoVECcxx . . $ autoVEC# x = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (p); |
| 273 | autovectorcxx . . $ autovector# <SSCPcxx . . $ SSCP#> sscpvec = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <SSCPcxx . . $ SSCP#> (g); |
| 274 | autoSSCPcxx . . $ autoSSCP# pool = SSCPList_to_SSCP_poolcxx . . $ SSCPList_to_SSCP_pool(b57a97f114ea42f6). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 275 | autoClassificationTablecxx . . $ autoClassificationTable# him = ClassificationTable_createcxx . . $ ClassificationTable_create(b9c68632944d67c8). (m, g); |
| 276 | 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).(); |
| 277 | autoTableOfRealcxx . . $ autoTableOfReal# adisplacements = Data_copycxx . . $ Data_copy(97a542a26794c80e). (thee); |
| 278 | |
| 279 | /* |
| 280 | Scale the sscp to become a covariance matrix. |
| 281 | */ |
| 282 | |
| 283 | poolcxx . . $ matrixview#matrixview(eed9513d24570b3d). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).() *=cxx . . $ `operator*=`(50b469ae42a42f64). 1.0 / (pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfObservationscxx . . $ structSSCP#numberOfObservations. - g); |
| 284 | |
| 285 | double lnd; |
| 286 | autoSSCPListcxx . . $ autoSSCPList# agroupscxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).; |
| 287 | SSCPListcxx . . $ SSCPList# groups; |
| 288 | if (poolCovarianceMatrices) { |
| 289 | |
| 290 | /* |
| 291 | Covariance matrix S can be Cholesky decomposed as S = L.L'. |
| 292 | Calculate L^-1. |
| 293 | L^-1 will be used later in the Mahalanobis distance calculation: |
| 294 | v'.S^-1.v = v'.L^-1'.L^-1.v = (L^-1.v)'.(L^-1.v). |
| 295 | */ |
| 296 | |
| 297 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & lnd); |
| 298 | for (integercxx . . $ integer# j = 1; j <= g; j ++) { |
| 299 | ln_determinant [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = lnd; |
| 300 | sscpvec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = pool.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 301 | } |
| 302 | groups = mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 303 | } else { |
| 304 | |
| 305 | /* |
| 306 | Calculate the inverses of all group covariance matrices. |
| 307 | In case of a singular matrix, substitute inverse of pooled. |
| 308 | */ |
| 309 | |
| 310 | agroups =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 311 | groups = agroups.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 312 | integercxx . . $ integer# npool = 0; |
| 313 | for (integercxx . . $ integer# j = 1; j <= g; j ++) { |
| 314 | const SSCPcxx . . $ SSCP# t = groups->atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).j]; |
| 315 | const integercxx . . $ integer# no = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (SSCP_getNumberOfObservationscxx . . $ SSCP_getNumberOfObservations(aff8a8bd1da17c96). (t)); |
| 316 | tcxx . . $ matrixview#matrixview(eed9513d24570b3d). -> datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).() *=cxx . . $ `operator*=`(50b469ae42a42f64). 1.0 / (no - 1); |
| 317 | |
| 318 | sscpvec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = groups->atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).j]; |
| 319 | try { |
| 320 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (t -> datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & ln_determinant [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]); |
| 321 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 322 | |
| 323 | /* |
| 324 | Clear the error. |
| 325 | Try the alternative: the pooled covariance matrix. |
| 326 | */ |
| 327 | |
| 328 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 329 | if (npool == 0) |
| 330 | MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (pool ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & lnd); |
| 331 | npool ++; |
| 332 | sscpvec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = pool.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 333 | ln_determinant [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = lnd; |
| 334 | } |
| 335 | } |
| 336 | if (npool > 0) |
| 337 | Melder_warningcxx . . $ Melder_warning(6298c93241566110). (npool, U" groups use pooled covariance matrix."); |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | Labels for columns in ClassificationTable |
| 342 | */ |
| 343 | |
| 344 | for (integercxx . . $ integer# j = 1; j <= g; j ++) { |
| 345 | conststring32cxx . . $ conststring32# name = Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (mycxx . . $ `melder/melder.h:44:9`! groupscxx . . $ structDiscriminant#groups.->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).j]); |
| 346 | if (! name) |
| 347 | name = U"?"; |
| 348 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), j, name); |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | Normalize the sum of the apriori probabilities to 1. |
| 353 | Next take ln (p) because otherwise probabilities might be too small to represent. |
| 354 | */ |
| 355 | |
| 356 | const double logg = logcxx . . $ log(7759b11ec49fe502). (g); |
| 357 | VECnormalize_inplacecxx . . $ VECnormalize_inplace(963991e5268b1933). (mycxx . . $ `melder/melder.h:44:9`! aprioriProbabilitiescxx . . $ structDiscriminant#aprioriProbabilities..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 1.0, 1.0); |
| 358 | for (integercxx . . $ integer# j = 1; j <= g; j ++) { |
| 359 | log_apriori [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = ( useAprioriProbabilities ? logcxx . . $ log(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! aprioriProbabilitiescxx . . $ structDiscriminant#aprioriProbabilities. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]) : - logg ); |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | Generalized squared distance function: |
| 364 | D^2(x) = (x - mu)' S^-1 (x - mu) + ln (determinant(S)) - 2 ln (apriori) |
| 365 | */ |
| 366 | |
| 367 | for (integercxx . . $ integer# i = 1; i <= m; i ++) { |
| 368 | SSCPcxx . . $ SSCP# winner; |
| 369 | double norm = 0, pt_max = -1e308; |
| 370 | integercxx . . $ integer# iwinner = 1; |
| 371 | for (integercxx . . $ integer# k = 1; k <= p; k ++) |
| 372 | x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] = thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k] + displacement [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]; |
| 373 | for (integercxx . . $ integer# j = 1; j <= g; j ++) { |
| 374 | const SSCPcxx . . $ SSCP# t = groups->atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).j]; |
| 375 | const double md = NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (sscpveccxx . . $ constmatrix#constmatrix(eed9513d24570b3d). [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] -> datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), xcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), tcxx . . $ constvector#constvector(eac9d6ec4c4bfecd). -> centroidcxx . . $ structSSCP#centroid..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 376 | const double pt = log_apriori [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] - 0.5 * (ln_determinant [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] + md); |
| 377 | if (pt > pt_max) { |
| 378 | pt_max = pt; |
| 379 | iwinner = j; |
| 380 | } |
| 381 | log_p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = pt; |
| 382 | } |
| 383 | for (integercxx . . $ integer# j = 1; j <= g; j ++) |
| 384 | norm += log_p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = expcxx . . $ exp(fee4f1d22bc8e617). (log_p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] - pt_max); |
| 385 | |
| 386 | for (integercxx . . $ integer# j = 1; j <= g; j ++) |
| 387 | hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = log_p [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] / norm; |
| 388 | |
| 389 | /* |
| 390 | Save old displacement, calculate new displacement |
| 391 | */ |
| 392 | |
| 393 | winner = groups->atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).iwinner]; |
| 394 | for (integercxx . . $ integer# k = 1; k <= p; k ++) { |
| 395 | adisplacements ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k] = displacement [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]; |
| 396 | if (hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iwinner] > minProb) { |
| 397 | double delta_k = winner -> centroidcxx . . $ structSSCP#centroid. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] - x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]; |
| 398 | displacement [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] += alpha * delta_k; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | *displacements =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). adisplacements.movecxx . . $ autoSomeThing#move(25acc3554b7d1642).(); |
| 403 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 404 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 405 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"ClassificationTable for Weenink procedure not created."); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | |
| 410 | autoConfigurationcxx . . $ autoConfiguration# Discriminant_TableOfReal_to_Configurationcxx . . $ Discriminant_TableOfReal_to_Configuration(97a7027e4523e055). (Discriminantcxx . . $ Discriminant# me, TableOfRealcxx . . $ TableOfReal# thee, integercxx . . $ integer# numberOfDimensions) { |
| 411 | try { |
| 412 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (thycxx . . $ `melder/melder.h:45:9`! numberOfColumns == mycxx . . $ `melder/melder.h:44:9`! eigen -> dimension, |
| 413 | U"The number of columns in the TableOfReal (", thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, U") should be equal to the dimension of the eigenvectors of the Discriminant (", mycxx . . $ `melder/melder.h:44:9`! eigen -> dimension, U")."); |
| 414 | if (numberOfDimensions == 0) |
| 415 | numberOfDimensions = Discriminant_getNumberOfFunctionscxx . . $ Discriminant_getNumberOfFunctions(5ee7605c36a67a8). (me); |
| 416 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfDimensions <= mycxx . . $ `melder/melder.h:44:9`! eigen -> numberOfEigenvalues, |
| 417 | U"The number of dimensions should not exceed the number of eigenvectors in the Discriminant (", mycxx . . $ `melder/melder.h:44:9`! eigen -> numberOfEigenvalues, U")."); |
| 418 | autoConfigurationcxx . . $ autoConfiguration# him = Configuration_createcxx . . $ Configuration_create(312218706738bb63). (thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows., numberOfDimensions); |
| 419 | mul_MAT_outcxx . . $ mul_MAT_out(9e368e0da0f1874a). (hiscxx . . $ `melder/melder.h:47:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), mycxx . . $ `melder/melder.h:44:9`! eigencxx . . $ structDiscriminant#eigen. ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). eigenvectorscxx . . $ structEigen#eigenvectors..horizontalBandcxx . . $ automatrix#horizontalBand(244fa7aab97a21b1). (1, numberOfDimensions).transposecxx . . $ matrixview#transpose(7bb675e35130cdb1). ()); |
| 420 | TableOfReal_copyLabelscxx . . $ TableOfReal_copyLabels(4a8870dfd6c30d44). (thee, him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1, 0); |
| 421 | TableOfReal_setSequentialColumnLabelscxx . . $ TableOfReal_setSequentialColumnLabels(8dc3139cdc86d712). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0, 0, U"Eigenvector ", 1, 1); |
| 422 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 423 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 424 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Configuration not created."); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | autoConfigurationcxx . . $ autoConfiguration# TableOfReal_to_Configuration_ldacxx . . $ TableOfReal_to_Configuration_lda(d3dcf14d2c627b2c). (TableOfRealcxx . . $ TableOfReal# me, integercxx . . $ integer# numberOfDimensions) { |
| 429 | try { |
| 430 | autoDiscriminantcxx . . $ autoDiscriminant# thee = TableOfReal_to_Discriminantcxx . . $ TableOfReal_to_Discriminant(f0615bf04d0371aa). (me); |
| 431 | autoConfigurationcxx . . $ autoConfiguration# him = Discriminant_TableOfReal_to_Configurationcxx . . $ Discriminant_TableOfReal_to_Configuration(97a7027e4523e055). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me, numberOfDimensions); |
| 432 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 433 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 434 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": Configuration with lda data not created."); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* End of file TableOfReal_and_Discriminant.cpp */ |