GaussianMixture.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /* GaussianMixture.cpp
  2. *
  3. * Copyright (C) 2011-2018 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. djmw 20101021 Initial version
  20. */
  21. #include "Distributions_and_Strings.h"
  22. #include "GaussianMixture.h"
  23. #include "NUMmachar.h"
  24. #include "NUM2.h"
  25. #include "Strings_extensions.h"
  26. #include "oo_DESTROY.h"
  27. #include "GaussianMixture_def.h"
  28. #include "oo_COPY.h"
  29. #include "GaussianMixture_def.h"
  30. #include "oo_EQUAL.h"
  31. #include "GaussianMixture_def.h"
  32. #include "oo_CAN_WRITE_AS_ENCODING.h"
  33. #include "GaussianMixture_def.h"
  34. #include "oo_WRITE_TEXT.h"
  35. #include "GaussianMixture_def.h"
  36. #include "oo_READ_TEXT.h"
  37. #include "GaussianMixture_def.h"
  38. #include "oo_WRITE_BINARY.h"
  39. #include "GaussianMixture_def.h"
  40. #include "oo_READ_BINARY.h"
  41. #include "GaussianMixture_def.h"
  42. #include "oo_DESCRIPTION.h"
  43. #include "GaussianMixture_def.h"
  44. Thing_implement (GaussianMixture, Daata, 0);
  45. conststring32 GaussianMixture_criterionText (int criterion) {
  46. conststring32 criterionText [6] = { U"(1/n)*LLH", U"(1/n)*MML", U"(1/n)*BIC", U"(1/n)*AIC", U"(1/n)*AICc", U"(1/n)*CD_LLH" };
  47. return criterion >= 0 && criterion < 7 ? criterionText [criterion] : U"(1/n)*ln(p)";
  48. }
  49. void GaussianMixture_removeComponent (GaussianMixture me, integer component);
  50. void GaussianMixture_removeComponent_bookkeeping (GaussianMixture me, integer component, double **p, integer numberOfRows);
  51. int GaussianMixture_TableOfReal_getProbabilities (GaussianMixture me, TableOfReal thee, integer component, double **p);
  52. void GaussianMixture_TableOfReal_getGammas (GaussianMixture me, TableOfReal thee, double **gamma, double *lnp);
  53. double GaussianMixture_getLikelihoodValue (GaussianMixture me, double **p, integer numberOfRows, int onlyLikelyhood);
  54. void GaussianMixture_updateProbabilityMarginals (GaussianMixture me, double **p, integer numberOfRows);
  55. integer GaussianMixture_getNumberOfParametersInComponent (GaussianMixture me);
  56. static void NUMdvector_scaleAsProbabilities (double *v, integer n) {
  57. longdouble sum = 0.0;
  58. for (integer i = 1; i <= n; i ++) {
  59. sum += v [i];
  60. }
  61. if (sum > 0.0) {
  62. for (integer i = 1; i <= n; i ++) {
  63. v [i] /= sum;
  64. }
  65. }
  66. }
  67. static void GaussianMixture_updateCovariance (GaussianMixture me, integer component, double **data, integer numberOfRows, double **p) {
  68. if (component < 1 || component > my numberOfComponents) {
  69. return;
  70. }
  71. Covariance thee = my covariances->at [component];
  72. double mixprob = my mixingProbabilities [component];
  73. double gsum = p [numberOfRows + 1] [component];
  74. // update the means
  75. for (integer j = 1; j <= thy numberOfColumns; j ++) {
  76. thy centroid [j] = 0.0;
  77. for (integer i = 1; i <= numberOfRows; i ++) {
  78. double gamma = mixprob * p [i] [component] / p [i] [my numberOfComponents + 1];
  79. thy centroid [j] += gamma * data [i] [j] ; // eq. Bishop 9.17
  80. }
  81. thy centroid [j] /= gsum;
  82. }
  83. // update covariance with the new mean
  84. if (thy numberOfRows == 1) { // 1xn covariance
  85. for (integer j = 1; j <= thy numberOfColumns; j ++) {
  86. thy data [1] [j] = 0.0;
  87. }
  88. for (integer i = 1; i <= numberOfRows; i ++) {
  89. double gamma = mixprob * p [i] [component] / p [i] [my numberOfComponents + 1];
  90. double gdn = gamma / gsum;
  91. for (integer j = 1; j <= thy numberOfColumns; j ++) {
  92. double xj = thy centroid [j] - data [i] [j];
  93. thy data [1] [j] += gdn * xj * xj;
  94. }
  95. }
  96. } else { // nxn covariance
  97. for (integer j = 1; j <= thy numberOfRows; j ++)
  98. for (integer k = j; k <= thy numberOfColumns; k ++) {
  99. thy data [k] [j] = thy data [j] [k] = 0;
  100. }
  101. for (integer i = 1; i <= numberOfRows; i ++) {
  102. double gamma = mixprob * p [i] [component] / p [i] [my numberOfComponents + 1];
  103. double gdn = gamma / gsum; // we cannot divide by nk - 1, this could cause instability
  104. for (integer j = 1; j <= thy numberOfColumns; j ++) {
  105. double xj = thy centroid [j] - data [i] [j];
  106. for (integer k = j; k <= thy numberOfColumns; k ++) {
  107. thy data [j] [k] = thy data [k] [j] += gdn * xj * (thy centroid [k] - data [i] [k]);
  108. }
  109. }
  110. }
  111. }
  112. thy numberOfObservations = my mixingProbabilities [component] * numberOfRows;
  113. }
  114. static void GaussianMixture_addCovarianceFraction (GaussianMixture me, integer im, Covariance him, double fraction) {
  115. if (im < 1 || im > my numberOfComponents || fraction == 0) {
  116. return;
  117. }
  118. Covariance thee = my covariances->at [im];
  119. // prevent instability: add lambda fraction of global covariances
  120. if (thy numberOfRows == 1) {
  121. for (integer j = 1; j <= thy numberOfColumns; j ++) {
  122. thy data [1] [j] += fraction * his data [j] [j];
  123. }
  124. } else {
  125. for (integer j = 1; j <= thy numberOfColumns; j++) {
  126. for (integer k = j; k <= thy numberOfColumns; k++) {
  127. thy data [k] [j] = thy data [j] [k] += fraction * his data [j] [k];
  128. }
  129. }
  130. }
  131. }
  132. void structGaussianMixture :: v_info () {
  133. our structDaata :: v_info ();
  134. MelderInfo_writeLine (U"Number of components: ", our numberOfComponents);
  135. MelderInfo_writeLine (U"Dimension of component: ", our dimension);
  136. MelderInfo_writeLine (U"Mixing probabilities:");
  137. for (integer im = 1; im <= numberOfComponents; im ++) {
  138. MelderInfo_writeLine (U" ", im, U": p = ", our mixingProbabilities [im], U" Name = \"", Thing_getName (our covariances->at [im]), U"\"");
  139. }
  140. }
  141. static void GaussianMixture_setLabelsFromTableOfReal (GaussianMixture me, TableOfReal thee) {
  142. for (integer im = 1; im <= my numberOfComponents; im ++) {
  143. Covariance cov = my covariances->at [im];
  144. for (integer j = 1; j <= my dimension; j ++) {
  145. TableOfReal_setColumnLabel (cov, j, thy columnLabels [j].get());
  146. }
  147. }
  148. }
  149. // only from big to reduced or same
  150. static void Covariance_into_Covariance (Covariance me, Covariance thee) {
  151. Melder_require (my numberOfColumns == thy numberOfColumns, U"Dimensions should be equal.");
  152. SSCP_unExpand (thee); // to its original state
  153. thy numberOfObservations = my numberOfObservations;
  154. // copy centroid & column labels
  155. for (integer ic = 1; ic <= my numberOfColumns; ic ++) {
  156. thy centroid [ic] = my centroid [ic];
  157. }
  158. thy columnLabels. copyElementsFrom (my columnLabels.get());
  159. // Are the matrix sizes equal
  160. if (my numberOfRows == thy numberOfRows) {
  161. thy rowLabels. copyElementsFrom (my rowLabels.get());
  162. matrixcopy_preallocated (thy data.get(), my data.get());
  163. return;
  164. } else {
  165. for (integer ir = 1; ir <= my numberOfRows; ir ++) {
  166. for (integer ic = ir; ic <= my numberOfColumns; ic ++) {
  167. integer dij = ic - ir;
  168. if (dij < thy numberOfRows)
  169. thy data [dij + 1] [ic] = my data [ir] [ic];
  170. }
  171. }
  172. }
  173. }
  174. static void GaussianMixture_setDefaultMixtureNames (GaussianMixture me) {
  175. for (integer im = 1; im <= my numberOfComponents; im ++) {
  176. Covariance cov = my covariances->at [im];
  177. Thing_setName (cov, Melder_cat (U"m", im));
  178. }
  179. }
  180. autoGaussianMixture GaussianMixture_create (integer numberOfComponents, integer dimension, integer storage) {
  181. try {
  182. autoGaussianMixture me = Thing_new (GaussianMixture);
  183. my numberOfComponents = numberOfComponents;
  184. my dimension = dimension;
  185. my mixingProbabilities = VECzero (numberOfComponents);
  186. my covariances = CovarianceList_create ();
  187. for (integer im = 1; im <= numberOfComponents; im ++) {
  188. autoCovariance cov = Covariance_create_reduceStorage (dimension, storage);
  189. my covariances -> addItemAtPosition_move (cov.move(), im);
  190. }
  191. for (integer im = 1; im <= numberOfComponents; im ++) {
  192. my mixingProbabilities [im] = 1.0 / numberOfComponents;
  193. }
  194. GaussianMixture_setDefaultMixtureNames (me.get());
  195. return me;
  196. } catch (MelderError) {
  197. Melder_throw (U"GaussianMixture not created.");
  198. }
  199. }
  200. /* c is double vector 1..dimension !!!!!! */
  201. int GaussianMixture_generateOneVector_inline (GaussianMixture me, VEC c, char32 **covname, VEC buf) {
  202. try {
  203. double p = NUMrandomUniform (0.0, 1.0);
  204. integer im = NUMgetIndexFromProbability (my mixingProbabilities.at, my numberOfComponents, p);
  205. Covariance thee = my covariances->at [im];
  206. *covname = thy name.get(); // BUG dangle
  207. if (thy numberOfRows == 1) { // 1xn reduced form
  208. for (integer i = 1; i <= my dimension; i ++) {
  209. c [i] = NUMrandomGauss (thy centroid [i], sqrt (thy data [1] [i]));
  210. }
  211. } else { // nxn
  212. if (! thy pca) {
  213. SSCP_expandPCA (thee); // on demand expanding
  214. }
  215. Covariance_PCA_generateOneVector_inline (thee, thy pca.get(), c, buf);
  216. }
  217. return 1;
  218. } catch (MelderError) {
  219. Melder_throw (me, U": vector not generated.");
  220. }
  221. }
  222. autoGaussianMixture TableOfReal_to_GaussianMixture_fromRowLabels (TableOfReal me, integer storage) {
  223. try {
  224. autoStrings rowLabels = TableOfReal_extractRowLabels (me);
  225. autoDistributions dist = Strings_to_Distributions (rowLabels.get());
  226. integer numberOfComponents = dist -> numberOfRows;
  227. autoGaussianMixture thee = GaussianMixture_create (numberOfComponents, my numberOfColumns, storage);
  228. GaussianMixture_setLabelsFromTableOfReal (thee.get(), me);
  229. for (integer i = 1; i <= numberOfComponents; i ++) {
  230. autoTableOfReal tab = TableOfReal_extractRowsWhereLabel (me, kMelder_string::EQUAL_TO, dist -> rowLabels [i].get());
  231. autoCovariance cov = TableOfReal_to_Covariance (tab.get());
  232. Covariance_into_Covariance (cov.get(), thy covariances->at [i]);
  233. Thing_setName (thy covariances->at [i], dist -> rowLabels [i].get());
  234. }
  235. for (integer im = 1; im <= numberOfComponents; im ++) {
  236. thy mixingProbabilities [im] = dist -> data [im] [1] / my numberOfRows;
  237. }
  238. return thee;
  239. } catch (MelderError) {
  240. Melder_throw (me, U": no GaussianMixture created.");
  241. }
  242. }
  243. autoCovariance GaussianMixture_to_Covariance_between (GaussianMixture me) {
  244. try {
  245. autoCovariance thee = Covariance_create (my dimension);
  246. // First the new centroid, based on the mixture centroids
  247. double nobs_total = 0;
  248. for (integer i = 1; i <= my numberOfComponents; i ++) {
  249. Covariance him = my covariances->at [i];
  250. double nobs = his numberOfObservations; // the weighting factor
  251. for (integer ic = 1; ic <= my dimension; ic ++) {
  252. thy centroid [ic] += nobs * his centroid [ic];
  253. }
  254. nobs_total += nobs;
  255. }
  256. for (integer ic = 1; ic <= my dimension; ic ++) {
  257. thy centroid [ic] /= nobs_total;
  258. }
  259. Covariance cov = my covariances->at [1];
  260. for (integer i = 1; i <= thy numberOfColumns; i ++) {
  261. if (cov -> columnLabels [i]) {
  262. TableOfReal_setColumnLabel (thee.get(), i, cov -> columnLabels [i].get());
  263. TableOfReal_setRowLabel (thee.get(), i, cov -> columnLabels [i].get()); // if diagonal !
  264. }
  265. }
  266. // Between covariance, scale by the number of observations
  267. for (integer i = 1; i <= my numberOfComponents; i ++) {
  268. Covariance him = my covariances->at [i];
  269. double nobs = his numberOfObservations - 1; // we lose 1 degree of freedom
  270. for (integer ir = 1; ir <= my dimension; ir ++) {
  271. double dir = his centroid [ir] - thy centroid [ir];
  272. for (integer ic = ir; ic <= my dimension; ic ++) {
  273. double dic = his centroid [ic] - thy centroid [ic];
  274. thy data [ir] [ic] = thy data [ic] [ir] += nobs * dir * dic;
  275. }
  276. }
  277. }
  278. // Scale back
  279. for (integer ir = 1; ir <= my dimension; ir ++) {
  280. for (integer ic = ir; ic <= my dimension; ic ++) {
  281. thy data [ir] [ic] = thy data [ic] [ir] /= nobs_total;
  282. }
  283. }
  284. thy numberOfObservations = nobs_total;
  285. return thee;
  286. } catch (MelderError) {
  287. Melder_throw (me, U": no Covariance (between) created.");
  288. }
  289. }
  290. autoCovariance GaussianMixture_to_Covariance_within (GaussianMixture me) {
  291. try {
  292. autoCovariance thee = Covariance_create (my dimension);
  293. for (integer i = 1; i <= my numberOfComponents; i ++) {
  294. double p = my mixingProbabilities [i];
  295. Covariance him = my covariances->at [i];
  296. if (his numberOfRows == 1) {
  297. for (integer ic = 1; ic <= my dimension; ic ++) {
  298. thy data [ic] [ic] += p * his data [1] [ic];
  299. }
  300. } else {
  301. for (integer ir = 1; ir <= my dimension; ir ++) {
  302. for (integer ic = ir; ic <= my dimension; ic ++) {
  303. thy data [ir] [ic] = thy data [ic] [ir] += p * his data [ir] [ic];
  304. }
  305. }
  306. }
  307. thy numberOfObservations += his numberOfObservations - 1; // we lose a degree of freedom?
  308. }
  309. // Leave centroid at 0 so we can add the within and between covariance nicely
  310. // Copy row labels from columns, because covar might be diagonal
  311. TableOfReal_copyLabels (my covariances->at [1], thee.get(), -1, 1);
  312. return thee;
  313. } catch (MelderError) {
  314. Melder_throw (me, U": no Covariance (within) created.");
  315. }
  316. }
  317. autoCovariance GaussianMixture_to_Covariance_total (GaussianMixture me) {
  318. try {
  319. autoCovariance thee = GaussianMixture_to_Covariance_between (me);
  320. autoCovariance within = GaussianMixture_to_Covariance_within (me);
  321. for (integer ir = 1; ir <= my dimension; ir++) {
  322. for (integer ic = ir; ic <= my dimension; ic++) {
  323. thy data [ir] [ic] = thy data [ic] [ir] += within -> data [ir] [ic];
  324. }
  325. }
  326. return thee;
  327. } catch (MelderError) {
  328. Melder_throw (me, U": no Covariance (total) created.");
  329. }
  330. }
  331. autoCovariance GaussianMixture_extractComponent (GaussianMixture me, integer component) {
  332. try {
  333. Melder_require (component > 0 && component <= my numberOfComponents, U"The component should be in [1, ", my numberOfComponents, U".");
  334. autoCovariance thee = Data_copy (my covariances->at [component]);
  335. return thee;
  336. } catch (MelderError) {
  337. Melder_throw (me, U": no component extracted.");
  338. }
  339. }
  340. autoTableOfReal GaussianMixture_extractMixingProbabilities (GaussianMixture me) {
  341. try {
  342. autoTableOfReal thee = TableOfReal_create (my numberOfComponents, 2);
  343. TableOfReal_setColumnLabel (thee.get(), 1, U"p");
  344. TableOfReal_setColumnLabel (thee.get(), 2, U"n");
  345. for (integer im = 1; im <= my numberOfComponents; im ++) {
  346. Covariance cov = my covariances->at [im];
  347. thy data [im] [1] = my mixingProbabilities [im];
  348. thy data [im] [2] = cov -> numberOfObservations;
  349. TableOfReal_setRowLabel (thee.get(), im, Thing_getName (cov));
  350. }
  351. return thee;
  352. } catch (MelderError) {
  353. Melder_throw (me, U": no mixing probabilities extracted.");
  354. }
  355. }
  356. autoTableOfReal GaussianMixture_extractCentroids (GaussianMixture me) {
  357. try {
  358. autoTableOfReal thee = TableOfReal_create (my numberOfComponents, my dimension);
  359. for (integer im = 1; im <= my numberOfComponents; im ++) {
  360. Covariance cov = my covariances->at [im];
  361. if (im == 1) {
  362. for (integer j = 1; j <= my dimension; j ++) {
  363. TableOfReal_setColumnLabel (thee.get(), j, cov -> columnLabels [j].get());
  364. }
  365. }
  366. TableOfReal_setRowLabel (thee.get(), im, Thing_getName (cov));
  367. for (integer j = 1; j <= my dimension; j ++) {
  368. thy data [im] [j] = cov -> centroid [j];
  369. }
  370. }
  371. return thee;
  372. } catch (MelderError) {
  373. Melder_throw (me, U": no centroid extracted.");
  374. }
  375. }
  376. autoPCA GaussianMixture_to_PCA (GaussianMixture me) {
  377. try {
  378. autoCovariance him = GaussianMixture_to_Covariance_total (me);
  379. autoPCA thee = SSCP_to_PCA (him.get());
  380. return thee;
  381. } catch (MelderError) {
  382. Melder_throw (me, U": no PCA calculated.");
  383. }
  384. }
  385. void GaussianMixture_getIntervalsAlongDirections (GaussianMixture me, integer d1, integer d2, double nsigmas, double *xmin, double *xmax, double *ymin, double *ymax) {
  386. *xmin = *xmax = *ymin = *ymax = undefined;
  387. Melder_require (d1 > 0 && d1 <= my dimension && d2 > 0 && d2 <= my dimension, U"Incorrect directions.");
  388. autoSSCPList sscps = SSCPList_extractTwoDimensions (my covariances->asSSCPList(), d1, d2);
  389. SSCPList_getEllipsesBoundingBoxCoordinates (sscps.get(), -nsigmas, 0, xmin, xmax, ymin, ymax);
  390. }
  391. void GaussianMixture_getIntervalAlongDirection (GaussianMixture me, integer d, double nsigmas, double *xmin, double *xmax) {
  392. double ymin, ymax;
  393. GaussianMixture_getIntervalsAlongDirections (me, d, d, nsigmas, xmin, xmax, &ymin, &ymax);
  394. }
  395. void GaussianMixture_PCA_getIntervalsAlongDirections (GaussianMixture me, PCA thee, integer d1, integer d2, double nsigmas, double *xmin, double *xmax, double *ymin, double *ymax) {
  396. Melder_require (my dimension == thy dimension,
  397. U"Dimensions should be equal.");
  398. Melder_require (d1 > 0 && d1 <= my dimension && d2 > 0 && d2 <= my dimension,
  399. U"Incorrect directions.");
  400. autoSSCPList sscps = SSCPList_toTwoDimensions (my covariances->asSSCPList(), thy eigenvectors.row (d1), thy eigenvectors.row (d2));
  401. SSCPList_getEllipsesBoundingBoxCoordinates (sscps.get(), -nsigmas, 0, xmin, xmax, ymin, ymax);
  402. }
  403. void GaussianMixture_PCA_getIntervalAlongDirection (GaussianMixture me, PCA thee, integer d, double nsigmas, double *xmin, double *xmax) {
  404. GaussianMixture_PCA_getIntervalsAlongDirections (me, thee, d, d, nsigmas, xmin, xmax, nullptr, nullptr);
  405. }
  406. void GaussianMixture_PCA_drawMarginalPdf (GaussianMixture me, PCA thee, Graphics g, integer d, double xmin, double xmax, double ymin, double ymax, integer npoints, integer nbins, int garnish) {
  407. if (my dimension != thy dimension || d < 1 || d > my dimension) {
  408. Melder_warning (U"Dimensions don't agree.");
  409. return;
  410. }
  411. if (npoints <= 1)
  412. npoints = 1000;
  413. double nsigmas = 2;
  414. if (xmax <= xmin)
  415. GaussianMixture_PCA_getIntervalAlongDirection (me, thee, d, nsigmas, & xmin, & xmax);
  416. double pmax = 0.0, dx = (xmax - xmin) / npoints, x1 = xmin + 0.5 * dx;
  417. double scalef = ( nbins <= 0 ? 1.0 : 1.0 ); // TODO
  418. autoVEC p = VECraw (npoints);
  419. for (integer i = 1; i <= npoints; i ++) {
  420. double x = x1 + (i - 1) * dx;
  421. Melder_assert (thy eigenvectors.ncol == thy dimension);
  422. p [i] = scalef * GaussianMixture_getMarginalProbabilityAtPosition (me, thy eigenvectors.row (d), x);
  423. if (p [i] > pmax)
  424. pmax = p [i];
  425. }
  426. if (ymin >= ymax) {
  427. ymin = 0.0;
  428. ymax = pmax;
  429. }
  430. Graphics_setInner (g);
  431. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  432. Graphics_function (g, p.at, 1, npoints, x1, xmax - 0.5 * dx);
  433. Graphics_unsetInner (g);
  434. if (garnish) {
  435. Graphics_drawInnerBox (g);
  436. Graphics_markBottom (g, xmin, true, true, false, nullptr);
  437. Graphics_markBottom (g, xmax, true, true, false, nullptr);
  438. Graphics_markLeft (g, ymin, true, true, false, nullptr);
  439. Graphics_markLeft (g, ymax, true, true, false, nullptr);
  440. }
  441. }
  442. void GaussianMixture_drawMarginalPdf (GaussianMixture me, Graphics g, integer d, double xmin, double xmax, double ymin, double ymax, integer npoints, integer nbins, int garnish) {
  443. if (d < 1 || d > my dimension) {
  444. Melder_warning (U"Dimension doesn't agree.");
  445. return;
  446. }
  447. if (npoints <= 1) {
  448. npoints = 1000;
  449. }
  450. autoVEC p (npoints, kTensorInitializationType::RAW);
  451. autoVEC v (my dimension, kTensorInitializationType::RAW);
  452. double nsigmas = 2.0;
  453. if (xmax <= xmin) {
  454. GaussianMixture_getIntervalAlongDirection (me, d, nsigmas, &xmin, &xmax);
  455. }
  456. double pmax = 0.0, dx = (xmax - xmin) / (npoints - 1);
  457. double scalef = ( nbins <= 0 ? 1.0 : 1.0 ); // TODO
  458. for (integer i = 1; i <= npoints; i++) {
  459. double x = xmin + (i - 1) * dx;
  460. for (integer k = 1; k <= my dimension; k++) {
  461. v [k] = k == d ? 1 : 0;
  462. }
  463. p [i] = scalef * GaussianMixture_getMarginalProbabilityAtPosition (me, v.get(), x);
  464. if (p [i] > pmax) {
  465. pmax = p [i];
  466. }
  467. }
  468. if (ymin >= ymax) {
  469. ymin = 0;
  470. ymax = pmax;
  471. }
  472. Graphics_setInner (g);
  473. Graphics_setWindow (g, xmin, xmax, ymin, ymax);
  474. Graphics_function (g, p.at, 1, npoints, xmin, xmax);
  475. Graphics_unsetInner (g);
  476. if (garnish) {
  477. Graphics_drawInnerBox (g);
  478. Graphics_markBottom (g, xmin, true, true, false, nullptr);
  479. Graphics_markBottom (g, xmax, true, true, false, nullptr);
  480. Graphics_markLeft (g, ymin, true, true, false, nullptr);
  481. Graphics_markLeft (g, ymax, true, true, false, nullptr);
  482. }
  483. }
  484. void GaussianMixture_PCA_drawConcentrationEllipses (GaussianMixture me, PCA him, Graphics g, double scale, int confidence, char32 *label, integer d1, integer d2, double xmin, double xmax, double ymin, double ymax, int fontSize, int garnish) {
  485. if (my dimension != his dimension) {
  486. Melder_warning (U"Dimensions don't agree.");
  487. return;
  488. }
  489. int d1_inverted = 0, d2_inverted = 0;
  490. if (d1 < 0) {
  491. d1 = labs (d1);
  492. Eigen_invertEigenvector (him, d1);
  493. d1_inverted = 1;
  494. }
  495. if (d2 < 0) {
  496. d2 = labs (d2);
  497. Eigen_invertEigenvector (him, d2);
  498. d2_inverted = 1;
  499. }
  500. autoSSCPList thee = SSCPList_toTwoDimensions (my covariances->asSSCPList(), his eigenvectors.row(d1), his eigenvectors.row (d2));
  501. if (d1_inverted) {
  502. Eigen_invertEigenvector (him, d1);
  503. }
  504. if (d2_inverted) {
  505. Eigen_invertEigenvector (him, d2);
  506. }
  507. SSCPList_drawConcentrationEllipses (thee.get(), g, -scale, confidence, label, 1, 2, xmin, xmax, ymin, ymax, fontSize, 0);
  508. if (garnish) {
  509. char32 llabel [40];
  510. Graphics_drawInnerBox (g);
  511. Graphics_marksLeft (g, 2, true, true, false);
  512. Melder_sprint (llabel,40, U"pc ", d2);
  513. Graphics_textLeft (g, true, llabel);
  514. Graphics_marksBottom (g, 2, true, true, false);
  515. Melder_sprint (llabel,40, U"pc ", d1);
  516. Graphics_textBottom (g, true, llabel);
  517. }
  518. }
  519. void GaussianMixture_drawConcentrationEllipses (GaussianMixture me, Graphics g, double scale, int confidence, char32 *label, int pcaDirections, integer d1, integer d2, double xmin, double xmax, double ymin, double ymax, int fontSize, int garnish) {
  520. if (d1 == 0 && d2 == 0) {
  521. d1 = 1;
  522. d2 = 2;
  523. }
  524. if (labs (d1) > my dimension || labs (d2) > my dimension) {
  525. return;
  526. }
  527. if (! pcaDirections) {
  528. SSCPList_drawConcentrationEllipses (my covariances->asSSCPList(), g, -scale, confidence, label,
  529. labs (d1), labs (d2), xmin, xmax, ymin, ymax, fontSize, garnish);
  530. return;
  531. }
  532. autoPCA him = GaussianMixture_to_PCA (me);
  533. GaussianMixture_PCA_drawConcentrationEllipses (me, him.get(), g, scale, confidence, label, d1, d2,
  534. xmin, xmax, ymin, ymax, fontSize, garnish);
  535. }
  536. void GaussianMixture_initialGuess (GaussianMixture me, TableOfReal thee, double nSigmas, double ru_range) {
  537. try {
  538. autoCovariance cov_t = TableOfReal_to_Covariance (thee);
  539. // assume equal probabilities for mixture
  540. // assume equal covariance matrices
  541. // spread centroids on an ellips in pc1-pc2 plane?
  542. if (my dimension == 1) {
  543. double dm = 2.0 * sqrt (cov_t -> data [1] [1]) / my numberOfComponents;
  544. double m1 = cov_t -> centroid [1] - dm;
  545. for (integer im = 1; im <= my numberOfComponents; im ++) {
  546. Covariance covi = my covariances->at [im];
  547. covi -> centroid [1] = m1;
  548. covi -> data [1] [1] = dm * dm;
  549. m1 += dm;
  550. covi -> numberOfObservations = thy numberOfRows / my numberOfComponents;
  551. }
  552. } else {
  553. autoPCA pca = SSCP_to_PCA (cov_t.get());
  554. autoSSCP s2d = SSCP_toTwoDimensions (cov_t.get(), pca -> eigenvectors.row(1), pca -> eigenvectors.row(2));
  555. autoConfiguration means2d = Configuration_create (my numberOfComponents, 2);
  556. double a, b, cs, sn;
  557. NUMeigencmp22 (s2d -> data [1] [1], s2d -> data [1] [2], s2d -> data [2] [2], & a, & b, & cs, & sn);
  558. a = nSigmas * sqrt (a);
  559. b = nSigmas * sqrt (b);
  560. double angle = 0.0, angle_inc = NUM2pi / my numberOfComponents;
  561. for (integer im = 1; im <= my numberOfComponents; im++, angle += angle_inc) {
  562. double xc = a * (1.0 + NUMrandomUniform (-ru_range, ru_range)) * cos (angle);
  563. double yc = b * (1.0 + NUMrandomUniform (-ru_range, ru_range)) * sin (angle);
  564. means2d -> data [im] [1] = s2d -> centroid [1] + xc * cs - yc * sn;
  565. means2d -> data [im] [2] = s2d -> centroid [2] + xc * sn + yc * cs;
  566. }
  567. // reconstruct the n-dimensional means from the 2-d from the eigenvectors
  568. autoTableOfReal means = PCA_Configuration_to_TableOfReal_reconstruct (pca.get(), means2d.get());
  569. for (integer im = 1; im <= my numberOfComponents; im ++) {
  570. Covariance covi = my covariances->at [im];
  571. for (integer ic = 1; ic <= my dimension; ic ++) {
  572. covi -> centroid [ic] = means -> data [im] [ic];
  573. }
  574. covi -> numberOfObservations = thy numberOfRows / my numberOfComponents;
  575. }
  576. // Trick: use the new means to get the between SSCP,
  577. // if there is only one component the cov_b will be zero!
  578. autoCovariance cov_b = GaussianMixture_to_Covariance_between (me);
  579. double var_t = SSCP_getTotalVariance (cov_t.get());
  580. double var_b = SSCP_getTotalVariance (cov_b.get());
  581. if (var_b >= var_t) { // we have chosen our initial values too far out
  582. double scale = 0.9 * sqrt (var_t / var_b);
  583. for (integer im = 1; im <= my numberOfComponents; im ++) {
  584. Covariance covi = my covariances->at [im];
  585. for (integer ic = 1; ic <= my dimension; ic ++) {
  586. covi -> centroid [ic] -= (1.0 - scale) * (covi -> centroid [ic] - cov_b -> centroid [ic]);
  587. }
  588. }
  589. cov_b = GaussianMixture_to_Covariance_between (me);
  590. }
  591. // Within variances are now (total - between) / numberOfComponents;
  592. for (integer ir = 1; ir <= my dimension; ir ++) {
  593. for (integer ic = ir; ic <= my dimension; ic ++) {
  594. double scalef = my numberOfComponents == 1 ? 1.0 : (var_b / var_t) / my numberOfComponents;
  595. cov_t -> data [ic] [ir] = cov_t -> data [ir] [ic] *= scalef;
  596. }
  597. }
  598. // Copy them
  599. for (integer im = 1; im <= my numberOfComponents; im ++) {
  600. Covariance cov = my covariances->at [im];
  601. if (cov -> numberOfRows == 1) {
  602. for (integer ic = 1; ic <= my dimension; ic ++) {
  603. cov -> data [1] [ic] = cov_t -> data [ic] [ic];
  604. }
  605. } else {
  606. for (integer ir = 1; ir <= my dimension; ir ++) {
  607. for (integer ic = ir; ic <= my dimension; ic ++) {
  608. cov -> data [ir] [ic] = cov -> data [ic] [ir] = cov_t -> data [ir] [ic];
  609. }
  610. }
  611. }
  612. }
  613. }
  614. } catch (MelderError) {
  615. Melder_throw (me, U" & ", thee, U": no initial guess possible.");
  616. }
  617. }
  618. autoClassificationTable GaussianMixture_TableOfReal_to_ClassificationTable (GaussianMixture me, TableOfReal thee) {
  619. try {
  620. autoClassificationTable him = ClassificationTable_create (thy numberOfRows, my numberOfComponents);
  621. for (integer im = 1; im <= my numberOfComponents; im ++) {
  622. Covariance cov = my covariances->at [im];
  623. SSCP_expandLowerCholesky (cov);
  624. TableOfReal_setColumnLabel (him.get(), im, Thing_getName (cov));
  625. }
  626. double ln2pid = -0.5 * my dimension * log (NUM2pi);
  627. autoNUMvector<double> lnN (1, my numberOfComponents);
  628. for (integer i = 1; i <= thy numberOfRows; i ++) {
  629. double psum = 0.0;
  630. for (integer im = 1; im <= my numberOfComponents; im ++) {
  631. Covariance cov = my covariances->at [im];
  632. double dsq = NUMmahalanobisDistance (cov -> lowerCholesky.get(), thy data.row(i), cov -> centroid.get());
  633. lnN [im] = ln2pid - 0.5 * (cov -> lnd + dsq);
  634. psum += his data [i] [im] = my mixingProbabilities [im] * exp (lnN [im]);
  635. }
  636. if (psum == 0) { // p's might be too small (underflow), make the largest equal to sfmin
  637. double lnmax = -1e308;
  638. integer imm = 1;
  639. for (integer im = 1; im <= my numberOfComponents; im ++) {
  640. if (lnN [im] > lnmax) {
  641. lnmax = lnN [im];
  642. } imm = im;
  643. }
  644. his data [i] [imm] = NUMfpp -> sfmin;
  645. }
  646. // for (integer im = 1; im <= my numberOfComponents; im++) { his data [i] [im] /= psum; }
  647. TableOfReal_setRowLabel (him.get(), i, thy rowLabels [i].get());
  648. }
  649. return him;
  650. } catch (MelderError) {
  651. Melder_throw (U"No ClassificationTable created from GaussianMixture & TableOfReal.");
  652. }
  653. }
  654. void GaussianMixture_TableOfReal_getGammas (GaussianMixture me, TableOfReal thee, double **gamma, double *p_lnp) {
  655. try {
  656. for (integer im = 1; im <= my numberOfComponents; im ++) {
  657. Covariance cov = my covariances->at [im];
  658. SSCP_expandLowerCholesky (cov);
  659. }
  660. double *nk = gamma [thy numberOfRows + 1];
  661. for (integer im = 1; im <= my numberOfComponents; im ++) {
  662. nk [im] = 0.0;
  663. }
  664. double lnp = 0.0;
  665. double ln2pid = - 0.5 * my dimension * log (NUM2pi);
  666. autoVEC lnN = VECraw (my numberOfComponents);
  667. for (integer i = 1; i <= thy numberOfRows; i ++) {
  668. double rowsum = 0.0;
  669. for (integer im = 1; im <= my numberOfComponents; im ++) {
  670. Covariance cov = my covariances->at [im];
  671. double dsq = NUMmahalanobisDistance (cov -> lowerCholesky.get(), thy data.row(i), cov -> centroid.get());
  672. lnN [im] = ln2pid - 0.5 * (cov -> lnd + dsq);
  673. gamma [i] [im] = my mixingProbabilities [im] * exp (lnN [im]); // eq. Bishop 9.16
  674. rowsum += gamma [i] [im];
  675. }
  676. // If the gamma [i]'s are too small, their sum will be zero and the scaling will overflow
  677. if (rowsum == 0.0) {
  678. continue; // This is ok because gamma [i]'s will all be zero
  679. }
  680. // scale gamma and get log(likehood) (Bishop eq. 9.40)
  681. for (integer im = 1; im <= my numberOfComponents; im ++) {
  682. gamma [i] [im] /= rowsum; // eq. Bishop 9.16
  683. nk [im] += gamma [i] [im]; // eq. Bishop 9.18
  684. lnp += gamma [i] [im] * (log (my mixingProbabilities [im]) + lnN [im]); // eq. Bishop 9.40
  685. }
  686. }
  687. if (p_lnp) {
  688. *p_lnp = lnp;
  689. }
  690. } catch (MelderError) {
  691. Melder_throw (me, U" & ", thee, U": no gammas.");
  692. }
  693. }
  694. void GaussianMixture_splitComponent (GaussianMixture me, integer component) {
  695. try {
  696. Melder_require (component > 0 && component <= my numberOfComponents, U"The component should be in [1, ", my numberOfComponents, U"].");
  697. Covariance thee = my covariances->at [component];
  698. // Always new PCA because we cannot be sure of data unchanged.
  699. SSCP_expandPCA (thee);
  700. autoCovariance cov1 = Data_copy (thee);
  701. autoCovariance cov2 = Data_copy (thee);
  702. SSCP_unExpandPCA (cov1.get());
  703. SSCP_unExpandPCA (cov2.get());
  704. // Eventually cov1 replaces component, cov2 at end
  705. autoVEC mixingProbabilities = VECraw (my numberOfComponents + 1);
  706. for (integer i = 1; i <= my numberOfComponents; i ++) {
  707. mixingProbabilities [i] = my mixingProbabilities [i];
  708. }
  709. double gamma = 0.5, lambda = 0.5, eta = 0.5, mu = 0.5;
  710. mixingProbabilities [component] = gamma * my mixingProbabilities [component];
  711. mixingProbabilities [my numberOfComponents + 1] = (1.0 - gamma) * my mixingProbabilities [component];
  712. double mp12 = mixingProbabilities [component] / mixingProbabilities [my numberOfComponents + 1];
  713. double factor1 = (eta - eta * lambda * lambda - 1.0) / gamma + 1.0;
  714. double factor2 = (eta * lambda * lambda - eta - lambda * lambda) / (1.0 - gamma) + 1.0;
  715. double *ev = thy pca -> eigenvectors [1];
  716. double d2 = thy pca -> eigenvalues [1];
  717. for (integer i = 1; i <= my dimension; i ++) {
  718. cov1 -> centroid [i] -= (1.0 / sqrt (mp12)) * sqrt (d2) * mu * ev [i];
  719. cov2 -> centroid [i] += sqrt (mp12) * sqrt (d2) * mu * ev [i];
  720. if (thy numberOfRows == 1) { // diagonal
  721. cov1 -> data [1] [i] = cov1 -> data [1] [i] / mp12 + factor1 * d2;
  722. cov1 -> data [1] [i] = cov2 -> data [i] [i] * mp12 + factor2 * d2;
  723. } else {
  724. for (integer j = i; j <= my dimension; j++) {
  725. cov1 -> data [j] [i] = cov1 -> data [i] [j] = cov1 -> data [i] [j] / mp12 + factor1 * d2 * ev [i] * ev [j];
  726. cov2 -> data [j] [i] = cov2 -> data [i] [j] = cov2 -> data [i] [j] * mp12 + factor2 * d2 * ev [i] * ev [j];
  727. }
  728. }
  729. }
  730. cov1 -> numberOfObservations *= gamma;
  731. cov2 -> numberOfObservations *= 1.0 - gamma;
  732. // Replace cov1 at component + add cov2. If something goes wrong we should be able to restore original!
  733. try {
  734. Thing_setName (cov2.get(), Melder_cat (Thing_getName (cov2.get()), U"-", my numberOfComponents + 1));
  735. my covariances -> addItem_move (cov2.move());
  736. } catch (MelderError) {
  737. Melder_throw (me, U" cannot add new component.");
  738. }
  739. my covariances -> replaceItem_move (cov1.move(), component);
  740. my numberOfComponents ++;
  741. my mixingProbabilities = mixingProbabilities.move();
  742. } catch (MelderError) {
  743. Melder_throw (me, U": component ", component, U" cannot be split.");
  744. }
  745. }
  746. int GaussianMixture_TableOfReal_getProbabilities (GaussianMixture me, TableOfReal thee, integer component, double **p) {
  747. try {
  748. double ln2pid = my dimension * log (NUM2pi);
  749. // Update only one component or all?
  750. integer icb = 1, ice = my numberOfComponents;
  751. if (component > 0 && component <= my numberOfComponents) {
  752. icb = ice = component;
  753. }
  754. for (integer ic = icb; ic <= ice; ic ++) {
  755. Covariance him = my covariances->at [ic];
  756. SSCP_expandLowerCholesky (him);
  757. for (integer i = 1; i <= thy numberOfRows; i++) {
  758. double dsq = NUMmahalanobisDistance (his lowerCholesky.get(), thy data.row(i), his centroid.get());
  759. double prob = exp (- 0.5 * (ln2pid + his lnd + dsq));
  760. prob = prob < 1e-300 ? 1e-300 : prob; // prevent p from being zero
  761. p [i] [ic] = prob;
  762. }
  763. }
  764. GaussianMixture_updateProbabilityMarginals (me, p, thy numberOfRows);
  765. return 1;
  766. } catch (MelderError) {
  767. Melder_throw (me, U" & ", thee, U": no probabilies could be calculated.");
  768. }
  769. }
  770. void GaussianMixture_expandPCA (GaussianMixture me) {
  771. for (integer im = 1; im <= my numberOfComponents; im ++) {
  772. Covariance him = my covariances->at [im];
  773. Melder_require (his numberOfRows > 1, U"Nothing to expand.");
  774. his pca = SSCP_to_PCA (him);
  775. }
  776. }
  777. void GaussianMixture_unExpandPCA (GaussianMixture me) {
  778. for (integer im = 1; im <= my numberOfComponents; im ++) {
  779. SSCP_unExpandPCA (my covariances->at [im]);
  780. }
  781. }
  782. void GaussianMixture_TableOfReal_improveLikelihood (GaussianMixture me, TableOfReal thee, double delta_lnp, integer maxNumberOfIterations, double lambda, int criterion) {
  783. try {
  784. conststring32 criterionText = GaussianMixture_criterionText (criterion);
  785. // The global covariance matrix is added with scaling coefficient lambda during updating the
  786. // mixture covariances to prevent numerical instabilities.
  787. autoCovariance covg = TableOfReal_to_Covariance (thee);
  788. autoNUMmatrix<double> pp (1, thy numberOfRows + 1, 1, my numberOfComponents + 1);
  789. double *nk = pp [thy numberOfRows + 1]; // last row has the column marginals n(k)
  790. if (! GaussianMixture_TableOfReal_getProbabilities (me, thee, 0, pp.peek())) {
  791. Melder_throw (U"Iteration not started. Too many components?");
  792. }
  793. double lnp = GaussianMixture_getLikelihoodValue (me, pp.peek(), thy numberOfRows, criterion);
  794. integer iter = 0;
  795. autoMelderProgress progress (U"Improve likelihood...");
  796. try {
  797. double lnp_prev, lnp_start = lnp / thy numberOfRows;
  798. do {
  799. // E-step: get responsabilities (gamma) with current parameters
  800. // See C. Bishop (2006), Pattern reconition and machine learning, Springer, page 439...
  801. lnp_prev = lnp;
  802. iter ++;
  803. // M-step: 1. new means & covariances
  804. for (integer im = 1; im <= my numberOfComponents; im ++) {
  805. GaussianMixture_updateCovariance (me, im, thy data.at, thy numberOfRows, pp.peek());
  806. GaussianMixture_addCovarianceFraction (me, im, covg.get(), lambda);
  807. }
  808. // M-step: 2. new mixingProbabilities
  809. for (integer im = 1; im <= my numberOfComponents; im ++) {
  810. my mixingProbabilities [im] = nk [im] / thy numberOfRows;
  811. }
  812. if (! GaussianMixture_TableOfReal_getProbabilities (me, thee, 0, pp.peek())) {
  813. break;
  814. }
  815. lnp = GaussianMixture_getLikelihoodValue (me, pp.peek(), thy numberOfRows, criterion);
  816. Melder_progress ((double) iter / (double) maxNumberOfIterations, criterionText, U": ", lnp / thy numberOfRows, U", L0: ", lnp_start);
  817. } while (fabs ((lnp - lnp_prev) / lnp_prev) > delta_lnp && iter < maxNumberOfIterations);
  818. } catch (MelderError) {
  819. Melder_clearError ();
  820. }
  821. // During EM, covariances were underestimated by a factor of (n-1)/n. Correction now.
  822. for (integer im = 1; im <= my numberOfComponents; im ++) {
  823. Covariance cov = my covariances->at [im];
  824. if (cov -> numberOfObservations > 1.5) {
  825. if (cov -> numberOfRows == 1) {
  826. for (integer j = 1; j <= thy numberOfColumns; j ++) {
  827. cov -> data [1] [j] *= cov -> numberOfObservations / (cov -> numberOfObservations - 1);
  828. }
  829. } else {
  830. for (integer j = 1; j <= thy numberOfColumns; j ++)
  831. for (integer k = j; k <= thy numberOfColumns; k ++) {
  832. cov -> data [j] [k] = cov -> data [k] [j] *= cov -> numberOfObservations / (cov -> numberOfObservations - 1.0);
  833. }
  834. }
  835. }
  836. }
  837. } catch (MelderError) {
  838. Melder_throw (me, U" & ", thee, U": likelihood cannot be improved.");
  839. }
  840. }
  841. integer GaussianMixture_getNumberOfParametersInComponent (GaussianMixture me) {
  842. Melder_assert (my covariances->size > 0);
  843. Covariance thee = my covariances->at [1];
  844. // if diagonal) d (means) + d (stdev)
  845. // else n + n(n+1)/2
  846. return thy numberOfRows == 1 ? 2 * thy numberOfColumns : thy numberOfColumns * (thy numberOfColumns + 3) / 2;
  847. }
  848. void GaussianMixture_updateProbabilityMarginals (GaussianMixture me, double **p, integer numberOfRows) {
  849. integer nocp1 = my numberOfComponents + 1, norp1 = numberOfRows + 1;
  850. for (integer ic = 1; ic <= my numberOfComponents; ic ++) {
  851. p [norp1] [ic] = 0.0;
  852. }
  853. for (integer i = 1; i <= numberOfRows; i ++) {
  854. double rowsum = 0.0;
  855. for (integer ic = 1; ic <= my numberOfComponents; ic ++) {
  856. rowsum += my mixingProbabilities [ic] * p [i] [ic];
  857. }
  858. p [i] [nocp1] = rowsum;
  859. for (integer ic = 1; ic <= my numberOfComponents; ic ++) {
  860. p [norp1] [ic] += my mixingProbabilities [ic] * p [i] [ic] / p [i] [nocp1];
  861. }
  862. }
  863. }
  864. void GaussianMixture_removeComponent_bookkeeping (GaussianMixture me, integer component, double **p, integer numberOfRows) {
  865. // p is (NumberOfRows+1) by (numberOfComponents+1) !
  866. for (integer i = 1; i <= numberOfRows + 1; i ++) {
  867. for (integer ic = component; ic <= my numberOfComponents; ic ++) {
  868. p [i] [ic] = p [i] [ic + 1];
  869. }
  870. }
  871. GaussianMixture_updateProbabilityMarginals (me, p, numberOfRows);
  872. GaussianMixture_removeComponent (me, component);
  873. }
  874. double GaussianMixture_TableOfReal_getLikelihoodValue (GaussianMixture me, TableOfReal thee, int criterion) {
  875. double value = undefined;
  876. autoNUMmatrix<double> pp (1, thy numberOfRows + 1, 1, my numberOfComponents + 1);
  877. if (GaussianMixture_TableOfReal_getProbabilities (me, thee, 0, pp.peek())) {
  878. value = GaussianMixture_getLikelihoodValue (me, pp.peek(), thy numberOfRows, criterion);
  879. }
  880. return value;
  881. }
  882. double GaussianMixture_getLikelihoodValue (GaussianMixture me, double **p, integer numberOfRows, int criterion) {
  883. // Because we try to _maximize_ a criterion, all criteria are negative numbers.
  884. if (criterion == GaussianMixture_CD_LIKELIHOOD) {
  885. double lnpcd = 0.0;
  886. for (integer i = 1; i <= numberOfRows; i ++) {
  887. double psum = 0, lnsum = 0;
  888. for (integer ic = 1; ic <= my numberOfComponents; ic ++) {
  889. double pp = my mixingProbabilities [ic] * p [i] [ic];
  890. psum += pp;
  891. lnsum += pp * log (pp);
  892. }
  893. if (psum > 0) {
  894. lnpcd += lnsum / psum;
  895. }
  896. }
  897. return lnpcd;
  898. }
  899. // The common factor for all other criteria is the log(likelihood)
  900. double lnp = 0.0;
  901. for (integer i = 1; i <= numberOfRows; i ++) {
  902. double psum = 0.0;
  903. for (integer ic = 1; ic <= my numberOfComponents; ic ++) {
  904. psum += my mixingProbabilities [ic] * p [i] [ic];
  905. }
  906. if (psum > 0.0) {
  907. lnp += log (psum);
  908. }
  909. }
  910. if (criterion == GaussianMixture_LIKELIHOOD) {
  911. return lnp;
  912. }
  913. double npars = GaussianMixture_getNumberOfParametersInComponent (me), np = npars * my numberOfComponents;
  914. if (criterion == GaussianMixture_MML) {
  915. /* Equation (15) in
  916. Mario A.T. Figueiredo, and Anil K. Jain, Unsupervised Learning of Finite Mixture Models :
  917. IEEE TRANSACTIONS ON PATTERN ANALYSIS AND MACHINE INTELLIGENCE, VOL. 24, NO. 3, MARCH 2002
  918. L(theta,Y)= N/2*sum(m=1..k, log(n*alpha [k]/12)) +k/2*ln(n/12) +k(N+1)/2
  919. - log (sum(i=1..n, sum(m=1..k, alpha [k]*p(k))))
  920. */
  921. double logmpn = 0.0;
  922. for (integer ic = 1; ic <= my numberOfComponents; ic ++) {
  923. logmpn += log (my mixingProbabilities [ic]);
  924. }
  925. // a rewritten L(theta,Y) is
  926. return lnp - 0.5 * my numberOfComponents * (npars + 1) * (log (numberOfRows / 12.0) + 1.0)
  927. + 0.5 * npars * logmpn;
  928. } else if (criterion == GaussianMixture_BIC) {
  929. return 2.0 * lnp - np * log (numberOfRows);
  930. } else if (criterion == GaussianMixture_AIC) {
  931. return 2.0 * (lnp - np);
  932. } else if (criterion == GaussianMixture_AICC) {
  933. np = npars * my numberOfComponents;
  934. return 2.0 * (lnp - np * (numberOfRows / (numberOfRows - np - 1.0)));
  935. }
  936. return lnp;
  937. }
  938. autoGaussianMixture GaussianMixture_TableOfReal_to_GaussianMixture_CEMM (GaussianMixture gm, TableOfReal thee, integer minNumberOfComponents, double delta_l, integer maxNumberOfIterations, double lambda, int criterion) {
  939. try {
  940. conststring32 criterionText = GaussianMixture_criterionText (criterion);
  941. bool deleteWeakComponents = ( minNumberOfComponents > 0 );
  942. autoGaussianMixture me = Data_copy (gm);
  943. autoNUMmatrix<double> p (1, thy numberOfRows + 2, 1, my numberOfComponents + 1);
  944. double *gsum = p [thy numberOfRows + 1]; // convenience array with sums
  945. autoCovariance covg = TableOfReal_to_Covariance (thee);
  946. double npars = GaussianMixture_getNumberOfParametersInComponent (me.get());
  947. double nparsd2 = ( deleteWeakComponents ? npars / 2.0 : 0.0 );
  948. // Initial E-step: Update all p's.
  949. GaussianMixture_TableOfReal_getProbabilities (me.get(), thee, 0, p.peek());
  950. double lnew = GaussianMixture_getLikelihoodValue (me.get(), p.peek(), thy numberOfRows, criterion);
  951. autoMelderProgress progress (U"Gaussian mixture...");
  952. autoGaussianMixture best;
  953. try {
  954. double lstart = lnew / thy numberOfRows;
  955. integer iter = 0, component;
  956. double lmax = -1e308, lprev;
  957. while (my numberOfComponents >= minNumberOfComponents) {
  958. do {
  959. iter ++;
  960. component = 1;
  961. lprev = lnew;
  962. while (component <= my numberOfComponents) {
  963. // M-step for means and covariances
  964. GaussianMixture_updateProbabilityMarginals (me.get(), p.peek(), thy numberOfRows);
  965. GaussianMixture_updateCovariance (me.get(), component, thy data.at, thy numberOfRows, p.peek());
  966. if (lambda > 0) {
  967. GaussianMixture_addCovarianceFraction (me.get(), component, covg.get(), lambda);
  968. }
  969. // Now check if enough support for a component exists
  970. double support_im = gsum [component] - nparsd2, support = 0.0;
  971. for (integer ic = 1; ic <= my numberOfComponents; ic ++) {
  972. double support_ic = gsum [ic] - nparsd2;
  973. if (support_ic > 0.0) {
  974. support += support_ic;
  975. }
  976. }
  977. my mixingProbabilities [component] = ( support_im > 0.0 ? support_im : 0.0 );
  978. if (support > 0.0) {
  979. my mixingProbabilities [component] /= support;
  980. }
  981. NUMdvector_scaleAsProbabilities (my mixingProbabilities.at, my numberOfComponents);
  982. if (my mixingProbabilities [component] > 0.0) { // update p for component
  983. GaussianMixture_TableOfReal_getProbabilities (me.get(), thee, component, p.peek());
  984. component ++;
  985. } else {
  986. // "Remove" the component column from p by shifting row values
  987. GaussianMixture_removeComponent_bookkeeping (me.get(), component, p.peek(), thy numberOfRows);
  988. // Now numberOfComponents is one less!
  989. // MelderInfo_writeLine (U"Removed component ", component);
  990. }
  991. }
  992. // 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:
  993. // k/2 (N+1){log(n/12)+1}+N/2sum(m=1..k,mixingP [m]) - loglikelihood
  994. lnew = GaussianMixture_getLikelihoodValue (me.get(), p.peek(), thy numberOfRows, criterion);
  995. Melder_progress ((double) iter / (double) maxNumberOfIterations, U", ", criterionText, U": ",
  996. lnew / thy numberOfRows, U"\nComponents: ", my numberOfComponents, U"\nL0: ", lstart);
  997. } while (lnew > lprev && fabs ((lprev - lnew) / lnew) > delta_l && iter < maxNumberOfIterations);
  998. if (lnew > lmax) {
  999. best = Data_copy (me.get());
  1000. lmax = lnew;
  1001. if (! deleteWeakComponents) {
  1002. break; // TODO was got end; is dat hetzelfde?
  1003. }
  1004. }
  1005. if (my numberOfComponents > 1) { // remove smallest component
  1006. component = 1;
  1007. double mpmin = my mixingProbabilities [component];
  1008. for (integer ic = 2; ic <= my numberOfComponents; ic ++) {
  1009. if (my mixingProbabilities [ic] < mpmin) {
  1010. mpmin = my mixingProbabilities [ic];
  1011. component = ic;
  1012. }
  1013. }
  1014. GaussianMixture_removeComponent_bookkeeping (me.get(), component, p.peek(), thy numberOfRows);
  1015. } else {
  1016. break;
  1017. }
  1018. }
  1019. } catch (MelderError) {
  1020. Melder_clearError ();
  1021. }
  1022. return best;
  1023. } catch (MelderError) {
  1024. Melder_throw (U"GaussianMixture not improved.");
  1025. }
  1026. }
  1027. // The numberOfElemnts per covariance needs to be updated later
  1028. void GaussianMixture_removeComponent (GaussianMixture me, integer component) {
  1029. if (component < 1 || component > my numberOfComponents || my numberOfComponents == 1) {
  1030. return;
  1031. }
  1032. my covariances -> removeItem (component);
  1033. my numberOfComponents --;
  1034. for (integer ic = component; ic <= my numberOfComponents; ic ++) {
  1035. my mixingProbabilities [ic] = my mixingProbabilities [ic + 1];
  1036. }
  1037. NUMdvector_scaleAsProbabilities (my mixingProbabilities.at, my numberOfComponents);
  1038. }
  1039. autoGaussianMixture TableOfReal_to_GaussianMixture (TableOfReal me, integer numberOfComponents, double delta_lnp, integer maxNumberOfIterations, double lambda, int storage, int criterion) {
  1040. try {
  1041. Melder_require (my numberOfRows >= 2 * numberOfComponents,
  1042. U"The number of data points should at least be twice the number of components.");
  1043. autoGaussianMixture thee = GaussianMixture_create (numberOfComponents, my numberOfColumns, storage);
  1044. GaussianMixture_setLabelsFromTableOfReal (thee.get(), me);
  1045. GaussianMixture_initialGuess (thee.get(), me, 1.0, 0.05);
  1046. if (maxNumberOfIterations <= 0)
  1047. return thee;
  1048. GaussianMixture_TableOfReal_improveLikelihood (thee.get(), me, delta_lnp, maxNumberOfIterations, lambda, criterion);
  1049. return thee;
  1050. } catch (MelderError) {
  1051. Melder_throw (me, U": no GaussianMixture created.");
  1052. }
  1053. }
  1054. autoCorrelation GaussianMixture_TableOfReal_to_Correlation (GaussianMixture me, TableOfReal thee) {
  1055. try {
  1056. Melder_require (my dimension == thy numberOfColumns,
  1057. U"Dimensions should be equal.");
  1058. autoClassificationTable ct = GaussianMixture_TableOfReal_to_ClassificationTable (me, thee);
  1059. autoCorrelation him = ClassificationTable_to_Correlation_columns (ct.get());
  1060. return him;
  1061. } catch (MelderError) {
  1062. Melder_throw (U"Correlation not created from GaussianMixture & TableOfReal.");
  1063. }
  1064. }
  1065. double GaussianMixture_getProbabilityAtPosition_string (GaussianMixture me, conststring32 vector_string) {
  1066. autostring32vector vector = STRVECtokenize (vector_string);
  1067. autoVEC pos = {my dimension, kTensorInitializationType::ZERO};
  1068. for (integer i = 1; i <= vector.size; i ++) {
  1069. pos [i] = Melder_atof (vector [i].get());
  1070. if (i == my dimension)
  1071. break;
  1072. }
  1073. double p = GaussianMixture_getProbabilityAtPosition (me, pos.get());
  1074. return p;
  1075. }
  1076. double GaussianMixture_getMarginalProbabilityAtPosition (GaussianMixture me, constVEC pos, double x) {
  1077. longdouble p = 0.0;
  1078. for (integer im = 1; im <= my numberOfComponents; im ++) {
  1079. double pim = Covariance_getMarginalProbabilityAtPosition (my covariances->at [im], pos, x);
  1080. p += my mixingProbabilities [im] * pim;
  1081. }
  1082. return (double) p;
  1083. }
  1084. double GaussianMixture_getProbabilityAtPosition (GaussianMixture me, constVEC xpos) {
  1085. longdouble p = 0.0;
  1086. for (integer im = 1; im <= my numberOfComponents; im ++) {
  1087. double pim = Covariance_getProbabilityAtPosition (my covariances->at [im], xpos);
  1088. p += my mixingProbabilities [im] * pim;
  1089. }
  1090. return (double) p;
  1091. }
  1092. autoMatrix GaussianMixture_PCA_to_Matrix_density (GaussianMixture me, PCA thee, integer d1, integer d2, double xmin, double xmax, integer nx, double ymin, double ymax, integer ny) {
  1093. try {
  1094. Melder_require (my dimension == thy dimension, U"Dimensions should be equal.");
  1095. Melder_require (d1 <= thy numberOfEigenvalues && d2 <= thy numberOfEigenvalues, U"Direction index too high.");
  1096. autoVEC v = { my dimension, kTensorInitializationType::ZERO };
  1097. if (xmax == xmin || ymax == ymin) {
  1098. double xmind, xmaxd, ymind, ymaxd, nsigmas = 2.0;
  1099. GaussianMixture_PCA_getIntervalsAlongDirections (me, thee, d1, d2, nsigmas, &xmind, &xmaxd, &ymind, &ymaxd);
  1100. if (xmax == xmin) {
  1101. xmin = xmind;
  1102. xmax = xmaxd;
  1103. }
  1104. if (ymax == ymin) {
  1105. ymin = ymind;
  1106. ymax = ymaxd;
  1107. }
  1108. }
  1109. // xmin,xmax and ymin,ymax are coordinates in the pc1 vs pc2 plane
  1110. double dx = fabs (xmax - xmin) / nx, dy = fabs (ymax - ymin) / ny;
  1111. double x1 = xmin + 0.5 * dx, y1 = ymin + 0.5 * dy;
  1112. autoMatrix him = Matrix_create (xmin, xmax, nx, dx, x1, ymin, ymax, ny, dy, y1);
  1113. for (integer i = 1; i <= ny; i ++) {
  1114. double y = y1 + (i - 1) * dy;
  1115. for (integer j = 1; j <= nx; j ++) {
  1116. double x = x1 + (j - 1) * dx;
  1117. for (integer k = 1; k <= my dimension; k ++) {
  1118. v [k] = x * thy eigenvectors [d1] [k] + y * thy eigenvectors [d2] [k];
  1119. }
  1120. his z [i] [j] = GaussianMixture_getProbabilityAtPosition (me, v.get());
  1121. }
  1122. }
  1123. return him;
  1124. } catch (MelderError) {
  1125. Melder_throw (me, U" & ", thee, U": no Matrix density created.");
  1126. }
  1127. }
  1128. autoTableOfReal GaussianMixture_to_TableOfReal_randomSampling (GaussianMixture me, integer numberOfPoints) {
  1129. try {
  1130. Covariance cov = my covariances->at [1];
  1131. autoTableOfReal thee = TableOfReal_create (numberOfPoints, my dimension);
  1132. autoVEC buf (my dimension, kTensorInitializationType::RAW);
  1133. thy columnLabels. copyElementsFrom_upTo (cov -> columnLabels.get(), my dimension);
  1134. // ppgb FIXME: is the number of column labels in the covariance equal to the number of dimensions? If so, document or assert.
  1135. for (integer i = 1; i <= numberOfPoints; i ++) {
  1136. char32 *covname;
  1137. GaussianMixture_generateOneVector_inline (me, thy data.row (i), & covname, buf.get());
  1138. TableOfReal_setRowLabel (thee.get(), i, covname);
  1139. }
  1140. GaussianMixture_unExpandPCA (me);
  1141. return thee;
  1142. } catch (MelderError) {
  1143. GaussianMixture_unExpandPCA (me);
  1144. Melder_throw (U"TableOfReal with random sampling not created.");
  1145. }
  1146. }
  1147. autoTableOfReal GaussianMixture_TableOfReal_to_TableOfReal_BHEPNormalityTests (GaussianMixture me, TableOfReal thee, double h) {
  1148. try {
  1149. integer n = thy numberOfRows, d = thy numberOfColumns, nocp1 = my numberOfComponents + 1;
  1150. Melder_require (d == my dimension, U"Dimensions should agree.");
  1151. // We cannot use a classification table because this could weigh a far-off data point with high probability
  1152. autoNUMmatrix<double> p (1, thy numberOfRows + 1, 1, my numberOfComponents + 1);
  1153. GaussianMixture_TableOfReal_getProbabilities (me, thee, 0, p.peek());
  1154. // prob, beta, tnbo, lnmu, lnvar, ndata, ncol
  1155. autoTableOfReal him = TableOfReal_create (my numberOfComponents, 7);
  1156. // labels
  1157. integer iprob = 1, ih = 2, itnb = 3, ilnmu = 4, ilnvar = 5, indata = 6, id = 7;
  1158. conststring32 label [8] = { U"", U"p", U"h", U"tnb", U"lnmu", U"lnvar", U"ndata", U"d" };
  1159. for (integer icol = 1; icol <= 7; icol ++) {
  1160. TableOfReal_setColumnLabel (him.get(), icol, label [icol]);
  1161. }
  1162. for (integer irow = 1; irow <= my numberOfComponents; irow ++) {
  1163. Covariance cov = my covariances->at [irow];
  1164. TableOfReal_setRowLabel (him.get(), irow, Thing_getName (cov));
  1165. }
  1166. for (integer icol = 1 ; icol <= my numberOfComponents; icol ++) {
  1167. his data [icol] [indata] = p [n + 1] [icol];
  1168. }
  1169. for (integer im = 1; im <= my numberOfComponents; im ++) {
  1170. Covariance cov = my covariances->at [im];
  1171. double mixingP = my mixingProbabilities [im];
  1172. double nd = his data [im] [indata], d2 = d / 2.0;
  1173. double beta = h > 0.0 ? NUMsqrt1_2 / h : NUMsqrt1_2 * pow ( (1.0 + 2.0 * d) / 4.0, 1.0 / (d + 4.0)) * pow (nd, 1.0 / (d + 4.0));
  1174. double beta2 = beta * beta, beta4 = beta2 * beta2, beta8 = beta4 * beta4;
  1175. double gamma = 1.0 + 2.0 * beta2, gamma2 = gamma * gamma, gamma4 = gamma2 * gamma2;
  1176. double delta = 1.0 + beta2 * (4.0 + 3.0 * beta2), delta2 = delta * delta;
  1177. double mu = 1.0 - pow (gamma, -d2) * (1.0 + d * beta2 / gamma + d * (d + 2.0) * beta4 / (2.0 * gamma2));
  1178. double var = 2.0 * pow (1.0 + 4.0 * beta2, -d2)
  1179. + 2.0 * pow (gamma, -d) * (1.0 + 2.0 * d * beta4 / gamma2 + 3.0 * d * (d + 2.0) * beta8 / (4.0 * gamma4))
  1180. - 4.0 * pow (delta, -d2) * (1.0 + 3.0 * d * beta4 / (2.0 * delta) + d * (d + 2.0) * beta8 / (2.0 * delta2));
  1181. double mu2 = mu * mu;
  1182. double prob = undefined, tnb = undefined, lnmu = undefined, lnvar = undefined;
  1183. try {
  1184. SSCP_expandLowerCholesky (cov);
  1185. } catch (MelderError) {
  1186. tnb = 4.0 * nd;
  1187. }
  1188. double djk, djj, sumjk = 0.0, sumj = 0.0;
  1189. double b1 = beta2 / 2.0, b2 = b1 / (1.0 + beta2);
  1190. /* Heinze & Wagner (1997), page 3
  1191. We use d [j] [k] = ||Y [j]-Y [k]||^2 = (Y [j]-Y [k])'S^(-1)(Y [j]-Y [k])
  1192. So d [j] [k]= d [k] [j] and d [j] [j] = 0
  1193. */
  1194. for (integer j = 1; j <= n; j ++) {
  1195. double wj = p [j] [nocp1] > 0.0 ? mixingP * p [j] [im] / p [j] [nocp1] : 0.0;
  1196. for (integer k = 1; k < j; k ++) {
  1197. djk = NUMmahalanobisDistance_chi (cov -> lowerCholesky.at, thy data [j], thy data [k], d, d);
  1198. double w = p [k] [nocp1] > 0.0 ? wj * mixingP * p [k] [im] / p [k] [nocp1] : 0.0;
  1199. sumjk += 2.0 * w * exp (-b1 * djk); // factor 2 because d [j] [k] == d [k] [j]
  1200. }
  1201. sumjk += wj * wj; // for k == j. Is this ok now for probability weighing ????
  1202. djj = NUMmahalanobisDistance (cov -> lowerCholesky.get(), thy data.row(j), cov -> centroid.get());
  1203. sumj += wj * exp (-b2 * djj);
  1204. }
  1205. tnb = (1.0 / nd) * sumjk - 2.0 * pow (1.0 + beta2, - d2) * sumj + nd * pow (gamma, - d2); // n *
  1206. his data [im] [ilnmu] = lnmu = 0.5 * log (mu2 * mu2 / (mu2 + var)); //log (sqrt (mu2 * mu2 /(mu2 + var)));
  1207. his data [im] [ilnvar] = lnvar = sqrt (log ( (mu2 + var) / mu2));
  1208. his data [im] [iprob] = prob = NUMlogNormalQ (tnb, lnmu, lnvar);
  1209. his data [im] [ih] = NUMsqrt1_2 / beta;
  1210. his data [im] [id] = d;
  1211. his data [im] [itnb] = tnb;
  1212. }
  1213. return him;
  1214. } catch (MelderError) {
  1215. Melder_throw (U"TableOfReal for BHEP not created.");
  1216. }
  1217. }
  1218. /* End of file GaussianMixture.cpp 1555*/