| 1 | #ifndef _GaussianMixture_h_ |
| 2 | #define _GaussianMixture_h_ |
| 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" |
| 27 | #include "Matrix.h" |
| 28 | #include "Covariance.h" |
| 29 | #include "TableOfReal_extensions.h" |
| 30 | |
| 31 | #include "GaussianMixture_def.h" |
| 32 | |
| 33 | #include "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 | autoGaussianMixture GaussianMixture_create (integer numberOfComponents, integer dimension, kGaussianMixtureStorage storage); |
| 41 | /* Start each function with expand and end with unExpand */ |
| 42 | |
| 43 | void GaussianMixture_removeUnsupportedComponents (GaussianMixture me); |
| 44 | |
| 45 | void GaussianMixture_expandPCA (GaussianMixture me); |
| 46 | |
| 47 | void GaussianMixture_unExpandPCA (GaussianMixture me); |
| 48 | |
| 49 | void GaussianMixture_drawConcentrationEllipses (GaussianMixture me, Graphics g, |
| 50 | double scale, bool confidence, char32 *label, bool pcaDirections, integer d1, integer d2, |
| 51 | double xmin, double xmax, double ymin, double ymax, double fontSize, bool garnish); |
| 52 | |
| 53 | void GaussianMixture_PCA_drawConcentrationEllipses (GaussianMixture me, PCA him, Graphics g, |
| 54 | double scale, bool confidence, char32 *label, integer d1, integer d2, |
| 55 | double xmin, double xmax, double ymin, double ymax, double fontSize, bool garnish); |
| 56 | |
| 57 | void GaussianMixture_drawMarginalPdf (GaussianMixture me, Graphics g, integer d, double xmin, |
| 58 | double xmax, double ymin, double ymax, integer npoints, integer nbins, bool garnish); |
| 59 | |
| 60 | void GaussianMixture_PCA_drawMarginalPdf (GaussianMixture me, PCA him, Graphics g, integer d, |
| 61 | double xmin, double xmax, double ymin, double ymax, integer npoints, integer nbins, bool garnish); |
| 62 | |
| 63 | autoGaussianMixture TableOfReal_to_GaussianMixture_fromRowLabels (TableOfReal me, 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_initialGuess2 (GaussianMixture me, TableOfReal thee, double nSigmas, double ru_range); |
| 75 | void GaussianMixture_initialGuess (GaussianMixture me, 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_getComponentProbabilities (GaussianMixture me, TableOfReal thee, integer componentToUpdate, MAT const& probabilities); |
| 82 | autoTableOfReal GaussianMixture_TableOfReal_to_TableOfReal_probabilities (GaussianMixture me, 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_getComponentResponsibilities (GaussianMixture me, TableOfReal thee, integer componentToUpdate, MAT const& responsibilities); |
| 91 | autoTableOfReal GaussianMixture_TableOfReal_to_TableOfReal_responsibilities (GaussianMixture me, TableOfReal thee); |
| 92 | |
| 93 | conststring32 GaussianMixture_criterionText (kGaussianMixtureCriterion criterion); |
| 94 | |
| 95 | autoGaussianMixture TableOfReal_to_GaussianMixture (TableOfReal me, integer numberOfComponents, double delta_lnp, integer maxNumberOfIterations, double lambda, kGaussianMixtureStorage storage, kGaussianMixtureCriterion criterion); |
| 96 | |
| 97 | void GaussianMixture_TableOfReal_improveLikelihood (GaussianMixture me, TableOfReal thee, double delta_lnp, integer maxNumberOfIterations, double lambda, 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 | autoGaussianMixture TableOfReal_to_GaussianMixture_CEMM (TableOfReal me, integer minimumNumberOfComponents, integer maximumNumberOfComponents, kGaussianMixtureStorage storage, integer maximumNumberOfIterations, double tolerance, bool info); |
| 109 | |
| 110 | autoGaussianMixture GaussianMixture_TableOfReal_to_GaussianMixture_CEMM (GaussianMixture me, TableOfReal thee, integer minimumNumberOfComponents, integer maxNumberOfIterations, double tolerance, bool info); |
| 111 | |
| 112 | void GaussianMixture_splitComponent (GaussianMixture me, integer component); |
| 113 | |
| 114 | void GaussianMixture_removeComponent (GaussianMixture me, integer component); |
| 115 | |
| 116 | autoClassificationTable GaussianMixture_TableOfReal_to_ClassificationTable (GaussianMixture me, TableOfReal thee); |
| 117 | |
| 118 | autoTable GaussianMixture_TableOfReal_to_Table_BHEPNormalityTests (GaussianMixture me, TableOfReal thee, double h); |
| 119 | |
| 120 | double GaussianMixture_TableOfReal_getLikelihoodValue (GaussianMixture me, TableOfReal thee, kGaussianMixtureCriterion criterion); |
| 121 | |
| 122 | double GaussianMixture_getProbabilityAtPosition (GaussianMixture me, constVEC const& v); |
| 123 | |
| 124 | double GaussianMixture_getProbabilityAtPosition_string (GaussianMixture me, conststring32 pos); |
| 125 | |
| 126 | double GaussianMixture_getMarginalProbabilityAtPosition (GaussianMixture me, constVECVU const& pos, double x); |
| 127 | |
| 128 | autoCorrelation GaussianMixture_TableOfReal_to_Correlation (GaussianMixture me, TableOfReal thee); |
| 129 | /* Correlation between components based on the data in the table */ |
| 130 | |
| 131 | autoCovariance GaussianMixture_to_Covariance_total (GaussianMixture me); |
| 132 | |
| 133 | autoCovariance GaussianMixture_to_Covariance_between (GaussianMixture me); |
| 134 | |
| 135 | autoCovariance GaussianMixture_to_Covariance_within (GaussianMixture me); |
| 136 | |
| 137 | |
| 138 | autoCovariance GaussianMixture_extractComponent(GaussianMixture me, integer component); |
| 139 | |
| 140 | autoTableOfReal GaussianMixture_extractCentroids (GaussianMixture me); |
| 141 | |
| 142 | autoTableOfReal GaussianMixture_extractMixingProbabilities (GaussianMixture me); |
| 143 | |
| 144 | autoPCA GaussianMixture_to_PCA (GaussianMixture me); |
| 145 | |
| 146 | autoMatrix GaussianMixture_PCA_to_Matrix_density (GaussianMixture me, PCA pca, integer d1, integer d2, double xmin, double xmax, integer nx, double ymin, double ymax, integer ny); |
| 147 | |
| 148 | void GaussianMixture_PCA_getIntervalsAlongDirections (GaussianMixture me, PCA thee, integer d1, integer d2, double nsigmas, double *out_xmin, double *out_xmax, double *out_ymin, double *out_ymax); |
| 149 | |
| 150 | void GaussianMixture_PCA_getIntervalAlongDirection (GaussianMixture me, PCA thee, integer d, double nsigmas, double *out_xmin, double *out_xmax); |
| 151 | |
| 152 | void GaussianMixture_getIntervalAlongDirection (GaussianMixture me, integer d, double nsigmas, double *out_xmin, double *out_xmax); |
| 153 | |
| 154 | void GaussianMixture_getIntervalsAlongDirections (GaussianMixture me, integer d1, 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_inline (GaussianMixture me, VEC const& c, autostring32 *out_covname, VEC const& buf); |
| 158 | |
| 159 | autoTableOfReal GaussianMixture_to_TableOfReal_randomSampling (GaussianMixture me, integer numberOfPoints); |
| 160 | |
| 161 | #endif /* _GaussianMixture_h_ */ |