| 1 | #cxx . . $ `<file>/dwtools/GaussianMixture.h`/ifndef _GaussianMixture_h_ |
| 2 | #define _GaussianMixture_h_cxx . . $ `dwtools/GaussianMixture.h:2:9`! |
| 3 | /* GaussianMixture.h |
| 4 | * |
| 5 | * Copyright (C) 2010-2020, 2023 David Weenink |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | djmw 20101021 initial version |
| 23 | djmw 20110306 Latest modification. |
| 24 | */ |
| 25 | |
| 26 | #include "ClassificationTable.h"cxx . . $ `<file>/dwtools/ClassificationTable.h`/ |
| 27 | #include "Matrix.h"cxx . . $ `<file>/fon/Matrix.h`/ |
| 28 | #include "Covariance.h"cxx . . $ `<file>/dwtools/Covariance.h`/ |
| 29 | #include "TableOfReal_extensions.h"cxx . . $ `<file>/dwtools/TableOfReal_extensions.h`/ |
| 30 | |
| 31 | #include "GaussianMixture_def.h"cxx . . $ `<file>/dwtools/GaussianMixture_def.h`/ |
| 32 | |
| 33 | #include "GaussianMixture_enums.h"cxx . . $ `<file>/dwtools/GaussianMixture_enums.h`/ |
| 34 | |
| 35 | /* |
| 36 | Invariants for a Gaussian mixture: |
| 37 | all covariances have the same 'dimension' parameter |
| 38 | All mixingProbabilities are >= 0 and sum to 1.0 |
| 39 | */ |
| 40 | autoGaussianMixturecxx . . $ autoGaussianMixture# GaussianMixture_createcxx . . $ GaussianMixture_create(261625e4cf0d6340). (integercxx . . $ integer# numberOfComponents, integercxx . . $ integer# dimension, kGaussianMixtureStoragecxx . . $ kGaussianMixtureStorage# storage); |
| 41 | /* Start each function with expand and end with unExpand */ |
| 42 | |
| 43 | void GaussianMixture_removeUnsupportedComponentscxx . . $ GaussianMixture_removeUnsupportedComponents(a29c70c932200d85). (GaussianMixturecxx . . $ GaussianMixture# me); |
| 44 | |
| 45 | void GaussianMixture_expandPCAcxx . . $ GaussianMixture_expandPCA(a29c70c932200d85). (GaussianMixturecxx . . $ GaussianMixture# me); |
| 46 | |
| 47 | void GaussianMixture_unExpandPCAcxx . . $ GaussianMixture_unExpandPCA(a29c70c932200d85). (GaussianMixturecxx . . $ GaussianMixture# me); |
| 48 | |
| 49 | void GaussianMixture_drawConcentrationEllipsescxx . . $ GaussianMixture_drawConcentrationEllipses(e7a43cfa49c51d69). (GaussianMixturecxx . . $ GaussianMixture# me, Graphicscxx . . $ Graphics# g, |
| 50 | double scale, bool confidence, char32cxx . . $ char32# *label, bool pcaDirections, integercxx . . $ integer# d1, integercxx . . $ integer# d2, |
| 51 | double xmin, double xmax, double ymin, double ymax, double fontSize, bool garnish); |
| 52 | |
| 53 | void GaussianMixture_PCA_drawConcentrationEllipsescxx . . $ GaussianMixture_PCA_drawConcentrationEllipses(132aa3cf1544890d). (GaussianMixturecxx . . $ GaussianMixture# me, PCAcxx . . $ PCA# him, Graphicscxx . . $ Graphics# g, |
| 54 | double scale, bool confidence, char32cxx . . $ char32# *label, integercxx . . $ integer# d1, integercxx . . $ integer# d2, |
| 55 | double xmin, double xmax, double ymin, double ymax, double fontSize, bool garnish); |
| 56 | |
| 57 | void GaussianMixture_drawMarginalPdfcxx . . $ GaussianMixture_drawMarginalPdf(8c744a26bcc19324). (GaussianMixturecxx . . $ GaussianMixture# me, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# d, double xmin, |
| 58 | double xmax, double ymin, double ymax, integercxx . . $ integer# npoints, integercxx . . $ integer# nbins, bool garnish); |
| 59 | |
| 60 | void GaussianMixture_PCA_drawMarginalPdfcxx . . $ GaussianMixture_PCA_drawMarginalPdf(6a7a8d0771c8334f). (GaussianMixturecxx . . $ GaussianMixture# me, PCAcxx . . $ PCA# him, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# d, |
| 61 | double xmin, double xmax, double ymin, double ymax, integercxx . . $ integer# npoints, integercxx . . $ integer# nbins, bool garnish); |
| 62 | |
| 63 | autoGaussianMixturecxx . . $ autoGaussianMixture# TableOfReal_to_GaussianMixture_fromRowLabelscxx . . $ TableOfReal_to_GaussianMixture_fromRowLabels(636f10e8375fac0). (TableOfRealcxx . . $ TableOfReal# me, kGaussianMixtureStoragecxx . . $ kGaussianMixtureStorage# storage); |
| 64 | |
| 65 | /* |
| 66 | Give an initial guess for the centroids and covariances of the GaussianMixture based on the data in the table. |
| 67 | Position centroids on the nSigma-ellips in the pc1-pc2 plane with some random variation and the covariances as |
| 68 | a scaled down version of the total covariance. |
| 69 | The randomly varied position of a centroid on the ellipse is parametrized as: |
| 70 | x = a * (1 + randomUniform (-ru_range, ru_range)) * cos (alpha) |
| 71 | y = b * (1 + randomUniform (-ru_range, ru_range)) * sin (alpha). |
| 72 | where a and b are the axes of the ellipse and 0<= alpha <= 2pi. |
| 73 | */ |
| 74 | void GaussianMixture_initialGuess2cxx . . $ GaussianMixture_initialGuess2(ba19c7db4584cebe). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee, double nSigmas, double ru_range); |
| 75 | void GaussianMixture_initialGuesscxx . . $ GaussianMixture_initialGuess(6d8d040d5c79cca5). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee); |
| 76 | |
| 77 | /* |
| 78 | Get the component probabilities N(x|mu(k),Sigma(k)). |
| 79 | Bishop (2007): Pattern recognition and machine learning, Springer page 25: Eq. 1.52 |
| 80 | */ |
| 81 | void GaussianMixture_TableOfReal_getComponentProbabilitiescxx . . $ GaussianMixture_TableOfReal_getComponentProbabilities(674342a5e75573ef). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee, integercxx . . $ integer# componentToUpdate, MATcxx . . $ MAT# const& probabilities); |
| 82 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_TableOfReal_to_TableOfReal_probabilitiescxx . . $ GaussianMixture_TableOfReal_to_TableOfReal_probabilities(da7025f09333dc32). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee); |
| 83 | |
| 84 | /* |
| 85 | Calculate the responsibilities: mixingProbability(k) * N(x(n)|mu(k),Sigma(k)) / Normalization. |
| 86 | Bishop (2007): Pattern recognition and machine learning, Springer page 438: Eq. 9.23 |
| 87 | Invariant: |
| 88 | The row sum always equals 1. |
| 89 | */ |
| 90 | void GaussianMixture_TableOfReal_getComponentResponsibilitiescxx . . $ GaussianMixture_TableOfReal_getComponentResponsibilities(674342a5e75573ef). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee, integercxx . . $ integer# componentToUpdate, MATcxx . . $ MAT# const& responsibilities); |
| 91 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_TableOfReal_to_TableOfReal_responsibilitiescxx . . $ GaussianMixture_TableOfReal_to_TableOfReal_responsibilities(da7025f09333dc32). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee); |
| 92 | |
| 93 | conststring32cxx . . $ conststring32# GaussianMixture_criterionTextcxx . . $ GaussianMixture_criterionText(185f836f08e9b4fd). (kGaussianMixtureCriterioncxx . . $ kGaussianMixtureCriterion# criterion); |
| 94 | |
| 95 | autoGaussianMixturecxx . . $ autoGaussianMixture# TableOfReal_to_GaussianMixturecxx . . $ TableOfReal_to_GaussianMixture(3a584962ac32c20). (TableOfRealcxx . . $ TableOfReal# me, integercxx . . $ integer# numberOfComponents, double delta_lnp, integercxx . . $ integer# maxNumberOfIterations, double lambda, kGaussianMixtureStoragecxx . . $ kGaussianMixtureStorage# storage, kGaussianMixtureCriterioncxx . . $ kGaussianMixtureCriterion# criterion); |
| 96 | |
| 97 | void GaussianMixture_TableOfReal_improveLikelihoodcxx . . $ GaussianMixture_TableOfReal_improveLikelihood(4d9c0daae3862ead). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee, double delta_lnp, integercxx . . $ integer# maxNumberOfIterations, double lambda, kGaussianMixtureCriterioncxx . . $ kGaussianMixtureCriterion# criterion); |
| 98 | |
| 99 | /* |
| 100 | Learn a GaussiamMixture from multivariate data (unsupervised). |
| 101 | 1) it is capable of selecting the number of components and |
| 102 | 2) unlike the standard expectation-maximization (EM) algorithm, it does not require careful initialization. |
| 103 | 3) It avoids the possibility of convergence toward a singular estimate at the boundary of the parameter space. |
| 104 | The Component-wise Expectation Maximization for Mixures algorithm (CEMM) is described at page 387 of: |
| 105 | M.A.T. Figueiredo & A.K. Jain (2002): "Unsupervised learning of finite mixture models", |
| 106 | IEEETransactions on pattern analysis and machine intelligence 24: 381--396. |
| 107 | */ |
| 108 | autoGaussianMixturecxx . . $ autoGaussianMixture# TableOfReal_to_GaussianMixture_CEMMcxx . . $ TableOfReal_to_GaussianMixture_CEMM(8abb6647b26b580c). (TableOfRealcxx . . $ TableOfReal# me, integercxx . . $ integer# minimumNumberOfComponents, integercxx . . $ integer# maximumNumberOfComponents, kGaussianMixtureStoragecxx . . $ kGaussianMixtureStorage# storage, integercxx . . $ integer# maximumNumberOfIterations, double tolerance, bool info); |
| 109 | |
| 110 | autoGaussianMixturecxx . . $ autoGaussianMixture# GaussianMixture_TableOfReal_to_GaussianMixture_CEMMcxx . . $ GaussianMixture_TableOfReal_to_GaussianMixture_CEMM(f4de5d0ba8c19c45). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee, integercxx . . $ integer# minimumNumberOfComponents, integercxx . . $ integer# maxNumberOfIterations, double tolerance, bool info); |
| 111 | |
| 112 | void GaussianMixture_splitComponentcxx . . $ GaussianMixture_splitComponent(2fd3f882ef0dccef). (GaussianMixturecxx . . $ GaussianMixture# me, integercxx . . $ integer# component); |
| 113 | |
| 114 | void GaussianMixture_removeComponentcxx . . $ GaussianMixture_removeComponent(2fd3f882ef0dccef). (GaussianMixturecxx . . $ GaussianMixture# me, integercxx . . $ integer# component); |
| 115 | |
| 116 | autoClassificationTablecxx . . $ autoClassificationTable# GaussianMixture_TableOfReal_to_ClassificationTablecxx . . $ GaussianMixture_TableOfReal_to_ClassificationTable(b53771290e8e341a). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee); |
| 117 | |
| 118 | autoTablecxx . . $ autoTable# GaussianMixture_TableOfReal_to_Table_BHEPNormalityTestscxx . . $ GaussianMixture_TableOfReal_to_Table_BHEPNormalityTests(7dad915c890a52c7). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee, double h); |
| 119 | |
| 120 | double GaussianMixture_TableOfReal_getLikelihoodValuecxx . . $ GaussianMixture_TableOfReal_getLikelihoodValue(4d3968d8207e3e34). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee, kGaussianMixtureCriterioncxx . . $ kGaussianMixtureCriterion# criterion); |
| 121 | |
| 122 | double GaussianMixture_getProbabilityAtPositioncxx . . $ GaussianMixture_getProbabilityAtPosition(564626c28c619e52). (GaussianMixturecxx . . $ GaussianMixture# me, constVECcxx . . $ constVEC# const& v); |
| 123 | |
| 124 | double GaussianMixture_getProbabilityAtPosition_stringcxx . . $ GaussianMixture_getProbabilityAtPosition_string(b3e1baf1267a95da). (GaussianMixturecxx . . $ GaussianMixture# me, conststring32cxx . . $ conststring32# pos); |
| 125 | |
| 126 | double GaussianMixture_getMarginalProbabilityAtPositioncxx . . $ GaussianMixture_getMarginalProbabilityAtPosition(18530cb880ed5215). (GaussianMixturecxx . . $ GaussianMixture# me, constVECVUcxx . . $ constVECVU# const& pos, double x); |
| 127 | |
| 128 | autoCorrelationcxx . . $ autoCorrelation# GaussianMixture_TableOfReal_to_Correlationcxx . . $ GaussianMixture_TableOfReal_to_Correlation(d429fe4c18f76d6b). (GaussianMixturecxx . . $ GaussianMixture# me, TableOfRealcxx . . $ TableOfReal# thee); |
| 129 | /* Correlation between components based on the data in the table */ |
| 130 | |
| 131 | autoCovariancecxx . . $ autoCovariance# GaussianMixture_to_Covariance_totalcxx . . $ GaussianMixture_to_Covariance_total(2376c2b32bfa09b5). (GaussianMixturecxx . . $ GaussianMixture# me); |
| 132 | |
| 133 | autoCovariancecxx . . $ autoCovariance# GaussianMixture_to_Covariance_betweencxx . . $ GaussianMixture_to_Covariance_between(2376c2b32bfa09b5). (GaussianMixturecxx . . $ GaussianMixture# me); |
| 134 | |
| 135 | autoCovariancecxx . . $ autoCovariance# GaussianMixture_to_Covariance_withincxx . . $ GaussianMixture_to_Covariance_within(2376c2b32bfa09b5). (GaussianMixturecxx . . $ GaussianMixture# me); |
| 136 | |
| 137 | |
| 138 | autoCovariancecxx . . $ autoCovariance# GaussianMixture_extractComponentcxx . . $ GaussianMixture_extractComponent(65c3e21f6edc0039).(GaussianMixturecxx . . $ GaussianMixture# me, integercxx . . $ integer# component); |
| 139 | |
| 140 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_extractCentroidscxx . . $ GaussianMixture_extractCentroids(920e598c5b3205c4). (GaussianMixturecxx . . $ GaussianMixture# me); |
| 141 | |
| 142 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_extractMixingProbabilitiescxx . . $ GaussianMixture_extractMixingProbabilities(920e598c5b3205c4). (GaussianMixturecxx . . $ GaussianMixture# me); |
| 143 | |
| 144 | autoPCAcxx . . $ autoPCA# GaussianMixture_to_PCAcxx . . $ GaussianMixture_to_PCA(2e3c11afa70d9e49). (GaussianMixturecxx . . $ GaussianMixture# me); |
| 145 | |
| 146 | autoMatrixcxx . . $ autoMatrix# GaussianMixture_PCA_to_Matrix_densitycxx . . $ GaussianMixture_PCA_to_Matrix_density(d2c6aa4969d8cdca). (GaussianMixturecxx . . $ GaussianMixture# me, PCAcxx . . $ PCA# pca, integercxx . . $ integer# d1, integercxx . . $ integer# d2, double xmin, double xmax, integercxx . . $ integer# nx, double ymin, double ymax, integercxx . . $ integer# ny); |
| 147 | |
| 148 | void GaussianMixture_PCA_getIntervalsAlongDirectionscxx . . $ GaussianMixture_PCA_getIntervalsAlongDirections(66d03a0598ec4d7e). (GaussianMixturecxx . . $ GaussianMixture# me, PCAcxx . . $ PCA# thee, integercxx . . $ integer# d1, integercxx . . $ integer# d2, double nsigmas, double *out_xmin, double *out_xmax, double *out_ymin, double *out_ymax); |
| 149 | |
| 150 | void GaussianMixture_PCA_getIntervalAlongDirectioncxx . . $ GaussianMixture_PCA_getIntervalAlongDirection(768848be2bb4571d). (GaussianMixturecxx . . $ GaussianMixture# me, PCAcxx . . $ PCA# thee, integercxx . . $ integer# d, double nsigmas, double *out_xmin, double *out_xmax); |
| 151 | |
| 152 | void GaussianMixture_getIntervalAlongDirectioncxx . . $ GaussianMixture_getIntervalAlongDirection(de70715f3c56b76f). (GaussianMixturecxx . . $ GaussianMixture# me, integercxx . . $ integer# d, double nsigmas, double *out_xmin, double *out_xmax); |
| 153 | |
| 154 | void GaussianMixture_getIntervalsAlongDirectionscxx . . $ GaussianMixture_getIntervalsAlongDirections(8dcd55032863c493). (GaussianMixturecxx . . $ GaussianMixture# me, integercxx . . $ integer# d1, integercxx . . $ integer# d2, double nsigmas, double *out_xmin, double *out_xmax, double *out_ymin, double *out_ymax); |
| 155 | |
| 156 | /* with on demand expand of pca ! */ |
| 157 | void GaussianMixture_generateOneVector_inlinecxx . . $ GaussianMixture_generateOneVector_inline(baab5f7d4314310c). (GaussianMixturecxx . . $ GaussianMixture# me, VECcxx . . $ VEC# const& c, autostring32cxx . . $ autostring32# *out_covname, VECcxx . . $ VEC# const& buf); |
| 158 | |
| 159 | autoTableOfRealcxx . . $ autoTableOfReal# GaussianMixture_to_TableOfReal_randomSamplingcxx . . $ GaussianMixture_to_TableOfReal_randomSampling(bb47bed5c8079adb). (GaussianMixturecxx . . $ GaussianMixture# me, integercxx . . $ integer# numberOfPoints); |
| 160 | |
| 161 | #endif /* _GaussianMixture_h_ */ |