| 1 |
/* PCA.cpp |
| 2 |
* |
| 3 |
* Principal Component Analysis |
| 4 |
* |
| 5 |
* Copyright (C) 1993-2021, 2026 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 20000511 PCA_TableOfReal_to_Configuration: added centralize option. |
| 23 |
(later removed) |
| 24 |
djmw 20020327 PCA_TableOfReal_to_Configuration modified internals. |
| 25 |
djmw 20020418 Removed some causes for compiler warnings. |
| 26 |
djmw 20020502 modified call Eigen_TableOfReal_project_into. |
| 27 |
djmw 20030324 Added PCA_TableOfReal_getFractionVariance. |
| 28 |
djmw 20061212 Changed info to Melder_writeLine<x> format. |
| 29 |
djmw 20071012 Added: oo_CAN_WRITE_AS_ENCODING.h |
| 30 |
djmw 20071201 Melder_warning<n> |
| 31 |
djmw 20081119 Check in TableOfReal_to_PCA if TableOfReal_areAllCellsDefined |
| 32 |
djmw 20110304 Thing_new |
| 33 |
*/ |
| 34 |
|
| 35 |
#include "Configuration.h" |
| 36 |
#include "Eigen_and_SSCP.h" |
| 37 |
#include "Eigen_and_TableOfReal.h" |
| 38 |
#include "Matrix_extensions.h" |
| 39 |
#include "NUM2.h" |
| 40 |
#include "PCA.h" |
| 41 |
#include "TableOfReal_extensions.h" |
| 42 |
|
| 43 |
#include "oo_DESTROY.h" |
| 44 |
#include "PCA_def.h" |
| 45 |
#include "oo_COPY.h" |
| 46 |
#include "PCA_def.h" |
| 47 |
#include "oo_EQUAL.h" |
| 48 |
#include "PCA_def.h" |
| 49 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 50 |
#include "PCA_def.h" |
| 51 |
#include "oo_WRITE_TEXT.h" |
| 52 |
#include "PCA_def.h" |
| 53 |
#include "oo_READ_TEXT.h" |
| 54 |
#include "PCA_def.h" |
| 55 |
#include "oo_WRITE_BINARY.h" |
| 56 |
#include "PCA_def.h" |
| 57 |
#include "oo_READ_BINARY.h" |
| 58 |
#include "PCA_def.h" |
| 59 |
#include "oo_DESCRIPTION.h" |
| 60 |
#include "PCA_def.h" |
| 61 |
|
| 62 |
Thing_implement (PCA, Eigen, 1); |
| 63 |
|
| 64 |
void structPCA :: v1_info () { |
| 65 |
structDaata :: v1_info (); |
| 66 |
MelderInfo_writeLine (U"Number of components: ", numberOfEigenvalues); |
| 67 |
MelderInfo_writeLine (U"Number of dimensions: ", dimension); |
| 68 |
MelderInfo_writeLine (U"Number of observations: ", numberOfObservations); |
| 69 |
} |
| 70 |
|
| 71 |
autoPCA PCA_create (integer numberOfComponents, integer dimension) { |
| 72 |
try { |
| 73 |
autoPCA me = Thing_new (PCA); |
| 74 |
Eigen_init (me.get(), numberOfComponents, dimension); |
| 75 |
my labels = autoSTRVEC (dimension); |
| 76 |
my centroid = zero_VEC (dimension); |
| 77 |
return me; |
| 78 |
} catch (MelderError) { |
| 79 |
Melder_throw (U"PCA not created"); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
void PCA_setNumberOfObservations (PCA me, integer numberOfObservations) { |
| 84 |
my numberOfObservations = numberOfObservations; |
| 85 |
} |
| 86 |
|
| 87 |
integer PCA_getNumberOfObservations (PCA me) { |
| 88 |
return my numberOfObservations; |
| 89 |
} |
| 90 |
|
| 91 |
void PCA_getEqualityOfEigenvalues (PCA me, integer from, integer to, int conservative, double *out_prob, double *out_chisq, double *out_df) { |
| 92 |
double sum = 0.0, sumln = 0.0; |
| 93 |
|
| 94 |
double prob = undefined, df = undefined, chisq = undefined; |
| 95 |
|
| 96 |
if (from == 0 && to == 0) { |
| 97 |
to = 1; |
| 98 |
from = my numberOfEigenvalues; |
| 99 |
} |
| 100 |
if (from < to && from > 0 && to <= my numberOfEigenvalues) { |
| 101 |
integer i; |
| 102 |
for (i = from; i <= to; i ++) { |
| 103 |
if (my eigenvalues [i] <= 0) { |
| 104 |
break; |
| 105 |
} |
| 106 |
sum += my eigenvalues [i]; |
| 107 |
sumln += log (my eigenvalues [i]); |
| 108 |
} |
| 109 |
if (sum == 0.0) |
| 110 |
return; |
| 111 |
const integer r = i - from; |
| 112 |
double n = my numberOfObservations - 1; |
| 113 |
if (conservative) |
| 114 |
n -= from + (double) (r * (2 * r + 1) + 2) / (6.0 * r); |
| 115 |
|
| 116 |
df = r * (r + 1) / 2 - 1; |
| 117 |
chisq = n * (r * log (sum / r) - sumln); |
| 118 |
prob = NUMchiSquareQ (chisq, df); |
| 119 |
} |
| 120 |
if (out_prob) |
| 121 |
*out_prob = prob; |
| 122 |
if (out_chisq) |
| 123 |
*out_chisq = chisq; |
| 124 |
if (out_df) |
| 125 |
*out_df = df; |
| 126 |
} |
| 127 |
|
| 128 |
/* the low level routines |
| 129 |
* |
| 130 |
* The matrix M [numberOfRows, numberOfColumns] is interpreted as 'numberOfRows' vectors of dimension 'numberOfColumns' |
| 131 |
* The eigenstructure of the M'M will be calculated |
| 132 |
* |
| 133 |
*/ |
| 134 |
|
| 135 |
autoEigen PCA_to_Eigen (PCA me) { |
| 136 |
try { |
| 137 |
autoEigen thee = Eigen_create (my numberOfEigenvalues, my dimension); |
| 138 |
thy eigenvectors.all() <<= my eigenvectors.all(); |
| 139 |
thy eigenvalues.all() <<= my eigenvalues.all(); |
| 140 |
return thee; |
| 141 |
} catch (MelderError) { |
| 142 |
Melder_throw (me, U": no Eigen created."); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
static autoPCA MAT_to_PCA (constMAT m, bool byColumns) { |
| 147 |
try { |
| 148 |
Melder_require (NUMdefined (m), |
| 149 |
U"All matrix elements should be defined."); |
| 150 |
Melder_require (NUMnorm (m, 2.0) > 0.0, |
| 151 |
U"Not all values in your table should be zero."); |
| 152 |
Melder_require (m.nrow > 1, |
| 153 |
U"The number of rows should be larger than 1."); |
| 154 |
autoMAT mcopy; |
| 155 |
if (byColumns) { |
| 156 |
if (m.ncol < m.nrow) |
| 157 |
Melder_warning (U"The number of columns in your table is less than the number of rows."); |
| 158 |
mcopy = transpose_MAT (m); |
| 159 |
} else { |
| 160 |
if (m.nrow < m.ncol) |
| 161 |
Melder_warning (U"The number of rows in your table is less than the number of columns."); |
| 162 |
mcopy = copy_MAT (m); |
| 163 |
} |
| 164 |
|
| 165 |
autoPCA thee = Thing_new (PCA); |
| 166 |
thy centroid = columnMeans_VEC (mcopy.get()); |
| 167 |
mcopy.all() -= thy centroid.all(); |
| 168 |
Eigen_initFromSquareRoot (thee.get(), mcopy.get()); |
| 169 |
thy labels = autoSTRVEC (mcopy.ncol); |
| 170 |
PCA_setNumberOfObservations (thee.get(), mcopy.nrow); |
| 171 |
/* |
| 172 |
The covariance matrix C = A'A / (N-1). However, we have calculated |
| 173 |
the eigenstructure for A'A. This has no consequences for the |
| 174 |
eigenvectors, but the eigenvalues have to be divided by (N-1). |
| 175 |
*/ |
| 176 |
thy eigenvalues.all() *= 1.0 / (mcopy.nrow - 1); |
| 177 |
|
| 178 |
return thee; |
| 179 |
} catch (MelderError) { |
| 180 |
Melder_throw (U"No PCA created from ", ( byColumns ? U"columns." : U"rows." )); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
autoPCA TableOfReal_to_PCA_byRows (TableOfReal me) { |
| 185 |
try { |
| 186 |
autoPCA thee = MAT_to_PCA (my data.get(), false); |
| 187 |
Melder_assert (thy labels.size == my numberOfColumns); |
| 188 |
thy labels.all() <<= my columnLabels.all(); |
| 189 |
return thee; |
| 190 |
} catch (MelderError) { |
| 191 |
Melder_throw (me, U": PCA not created."); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
autoPCA Matrix_to_PCA_byColumns (Matrix me) { |
| 196 |
try { |
| 197 |
autoPCA thee = MAT_to_PCA (my z.get(), true); |
| 198 |
return thee; |
| 199 |
} catch (MelderError) { |
| 200 |
Melder_throw (me, U": no PCA created from columns."); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
autoPCA Matrix_to_PCA_byRows (Matrix me) { |
| 205 |
try { |
| 206 |
autoPCA thee = MAT_to_PCA (my z.get(), false); |
| 207 |
return thee; |
| 208 |
} catch (MelderError) { |
| 209 |
Melder_throw (me, U": no PCA created from rows."); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
autoTableOfReal PCA_TableOfReal_to_TableOfReal_zscores (PCA me, TableOfReal thee, integer numberOfDimensions) { |
| 214 |
try { |
| 215 |
if (numberOfDimensions == 0 || numberOfDimensions > my numberOfEigenvalues) |
| 216 |
numberOfDimensions = my numberOfEigenvalues; |
| 217 |
Melder_require (thy numberOfColumns == my eigenvectors.ncol, |
| 218 |
U"The number of columns in the TableOfReal (", thy numberOfColumns, |
| 219 |
U") should match the length of the eigenvectors of the PCA (", |
| 220 |
my eigenvectors.ncol, U").") |
| 221 |
; |
| 222 |
autoTableOfReal him = TableOfReal_create (thy numberOfRows, numberOfDimensions); |
| 223 |
for (integer i = 1; i <= thy numberOfRows; i ++) { /* row */ |
| 224 |
for (integer j = 1; j <= numberOfDimensions; j ++) { |
| 225 |
const longdouble sigma = sqrt (my eigenvalues [j]); |
| 226 |
longdouble r = 0.0; |
| 227 |
for (integer k = 1; k <= my dimension; k ++) |
| 228 |
// eigenvector in row, data in row |
| 229 |
r += my eigenvectors [j] [k] * (thy data [i] [k] - my centroid [k]) / sigma; |
| 230 |
his data [i] [j] = (double) r; |
| 231 |
} |
| 232 |
} |
| 233 |
his rowLabels.all() <<= thy rowLabels.all(); |
| 234 |
TableOfReal_setSequentialColumnLabels (him.get(), 0, 0, U"pc", 1, 1); |
| 235 |
return him; |
| 236 |
} catch (MelderError) { |
| 237 |
Melder_throw (U"TableOfReal (zscores) not created from PCA & TableOfReal."); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
autoTableOfReal PCA_TableOfReal_to_TableOfReal_projectRows (PCA me, TableOfReal thee, integer numberOfDimensionsToKeep) { |
| 242 |
try { |
| 243 |
if (numberOfDimensionsToKeep == 0 || numberOfDimensionsToKeep > my numberOfEigenvalues) |
| 244 |
numberOfDimensionsToKeep = my numberOfEigenvalues; |
| 245 |
Melder_require (thy numberOfColumns == my eigenvectors.ncol, |
| 246 |
U"The number of columns in the TableOfReal (", thy numberOfColumns, |
| 247 |
U") should match the length of the eigenvectors of the PCA (", |
| 248 |
my eigenvectors.ncol, U").") |
| 249 |
; |
| 250 |
autoTableOfReal him = TableOfReal_create (thy numberOfRows, numberOfDimensionsToKeep); |
| 251 |
mul_MAT_out (his data.get(), thy data.get(), my eigenvectors.horizontalBand (1, numberOfDimensionsToKeep).transpose()); |
| 252 |
his rowLabels.all() <<= thy rowLabels.all(); |
| 253 |
TableOfReal_setSequentialColumnLabels (him.get(), 0, 0, U"pc", 1, 1); |
| 254 |
return him; |
| 255 |
} catch (MelderError) { |
| 256 |
Melder_throw (U"TableOfReal not created from PCA & TableOfReal."); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
autoConfiguration PCA_TableOfReal_to_Configuration (PCA me, TableOfReal thee, integer numberOfDimensionsToKeep) { |
| 261 |
try { |
| 262 |
if (numberOfDimensionsToKeep == 0 || numberOfDimensionsToKeep > my numberOfEigenvalues) |
| 263 |
numberOfDimensionsToKeep = my numberOfEigenvalues; |
| 264 |
Melder_require (thy numberOfColumns == my eigenvectors.ncol, |
| 265 |
U"The number of columns in the TableOfReal (", thy numberOfColumns, |
| 266 |
U") should match the length of the eigenvectors of the PCA (", |
| 267 |
my eigenvectors.ncol, U").") |
| 268 |
; |
| 269 |
autoConfiguration him = Configuration_create (thy numberOfRows, numberOfDimensionsToKeep); |
| 270 |
mul_MAT_out (his data.get(), thy data.get(), my eigenvectors.horizontalBand(1, numberOfDimensionsToKeep).transpose()); |
| 271 |
his rowLabels.all() <<= thy rowLabels.all(); |
| 272 |
TableOfReal_setSequentialColumnLabels (him.get(), 0, 0, U"pc", 1, 1); |
| 273 |
return him; |
| 274 |
} catch (MelderError) { |
| 275 |
Melder_throw (U"Configuration not created from PCA & TableOfReal."); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
autoTableOfReal PCA_Configuration_to_TableOfReal_reconstruct (PCA me, Configuration thee) { |
| 280 |
try { |
| 281 |
Melder_require (thy numberOfColumns <= my numberOfEigenvalues, |
| 282 |
U"The number of columns in the Configuration should not exceed the number of eigenvectors in the PCA."); |
| 283 |
const integer numberOfEigenvectorsToUse = std::min (thy numberOfColumns, my numberOfEigenvalues); |
| 284 |
autoTableOfReal him = TableOfReal_create (thy numberOfRows, my dimension); |
| 285 |
Melder_assert (my labels.size == my dimension); |
| 286 |
his columnLabels.all() <<= my labels.all(); |
| 287 |
his rowLabels.all() <<= thy rowLabels.all(); |
| 288 |
|
| 289 |
mul_MAT_out (his data.get(), thy data.get (), my eigenvectors.horizontalBand (1, numberOfEigenvectorsToUse)); |
| 290 |
|
| 291 |
return him; |
| 292 |
} catch (MelderError) { |
| 293 |
Melder_throw (U"TableOfReal not reconstructed."); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
double PCA_TableOfReal_getFractionVariance (PCA me, TableOfReal thee, integer from, integer to) { |
| 298 |
try { |
| 299 |
if (from < 1 || from > to || to > thy numberOfColumns) |
| 300 |
return undefined; |
| 301 |
autoSSCP s = TableOfReal_to_SSCP (thee, 0, 0, 0, 0); |
| 302 |
autoSSCP sp = Eigen_SSCP_project (me, s.get()); |
| 303 |
const double fraction = SSCP_getFractionVariation (sp.get(), from, to); |
| 304 |
return fraction; |
| 305 |
} catch (MelderError) { |
| 306 |
return undefined; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
autoTableOfReal PCA_to_TableOfReal_reconstruct1 (PCA me, constVECVU const& coefficients) { |
| 311 |
try { |
| 312 |
Melder_require (coefficients.size == my numberOfEigenvalues, |
| 313 |
U"The number of coefficients should equal the number of eigenvectors (", my numberOfEigenvalues, U")."); |
| 314 |
autoConfiguration configuration = Configuration_create (1, coefficients.size); |
| 315 |
configuration -> data.row (1) <<= coefficients; |
| 316 |
autoTableOfReal him = PCA_Configuration_to_TableOfReal_reconstruct (me, configuration.get()); |
| 317 |
return him; |
| 318 |
} catch (MelderError) { |
| 319 |
Melder_throw (me, U" not reconstructed."); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/* End of file PCA.cpp */ |