annotate maths/pca/PCA.c @ 242:6060110dc3c6

* Build PCA and HMM
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 09 Jan 2008 10:37:06 +0000
parents
children
rev   line source
c@242 1 /*********************************/
c@242 2 /* Principal Components Analysis */
c@242 3 /*********************************/
c@242 4
c@242 5 /*********************************************************************/
c@242 6 /* Principal Components Analysis or the Karhunen-Loeve expansion is a
c@242 7 classical method for dimensionality reduction or exploratory data
c@242 8 analysis. One reference among many is: F. Murtagh and A. Heck,
c@242 9 Multivariate Data Analysis, Kluwer Academic, Dordrecht, 1987.
c@242 10
c@242 11 Author:
c@242 12 F. Murtagh
c@242 13 Phone: + 49 89 32006298 (work)
c@242 14 + 49 89 965307 (home)
c@242 15 Earn/Bitnet: fionn@dgaeso51, fim@dgaipp1s, murtagh@stsci
c@242 16 Span: esomc1::fionn
c@242 17 Internet: murtagh@scivax.stsci.edu
c@242 18
c@242 19 F. Murtagh, Munich, 6 June 1989 */
c@242 20 /*********************************************************************/
c@242 21
c@242 22 #include <stdio.h>
c@242 23 #include <stdlib.h>
c@242 24 #include <math.h>
c@242 25
c@242 26 #include "PCA.h"
c@242 27
c@242 28 #define SIGN(a, b) ( (b) < 0 ? -fabs(a) : fabs(a) )
c@242 29
c@242 30 /** Variance-covariance matrix: creation *****************************/
c@242 31
c@242 32 /* Create m * m covariance matrix from given n * m data matrix. */
c@242 33 void covcol(double** data, int n, int m, double** symmat)
c@242 34 {
c@242 35 double *mean;
c@242 36 int i, j, j1, j2;
c@242 37
c@242 38 /* Allocate storage for mean vector */
c@242 39
c@242 40 mean = (double*) malloc(m*sizeof(double));
c@242 41
c@242 42 /* Determine mean of column vectors of input data matrix */
c@242 43
c@242 44 for (j = 0; j < m; j++)
c@242 45 {
c@242 46 mean[j] = 0.0;
c@242 47 for (i = 0; i < n; i++)
c@242 48 {
c@242 49 mean[j] += data[i][j];
c@242 50 }
c@242 51 mean[j] /= (double)n;
c@242 52 }
c@242 53
c@242 54 /*
c@242 55 printf("\nMeans of column vectors:\n");
c@242 56 for (j = 0; j < m; j++) {
c@242 57 printf("%12.1f",mean[j]); } printf("\n");
c@242 58 */
c@242 59
c@242 60 /* Center the column vectors. */
c@242 61
c@242 62 for (i = 0; i < n; i++)
c@242 63 {
c@242 64 for (j = 0; j < m; j++)
c@242 65 {
c@242 66 data[i][j] -= mean[j];
c@242 67 }
c@242 68 }
c@242 69
c@242 70 /* Calculate the m * m covariance matrix. */
c@242 71 for (j1 = 0; j1 < m; j1++)
c@242 72 {
c@242 73 for (j2 = j1; j2 < m; j2++)
c@242 74 {
c@242 75 symmat[j1][j2] = 0.0;
c@242 76 for (i = 0; i < n; i++)
c@242 77 {
c@242 78 symmat[j1][j2] += data[i][j1] * data[i][j2];
c@242 79 }
c@242 80 symmat[j2][j1] = symmat[j1][j2];
c@242 81 }
c@242 82 }
c@242 83
c@242 84 free(mean);
c@242 85
c@242 86 return;
c@242 87
c@242 88 }
c@242 89
c@242 90 /** Error handler **************************************************/
c@242 91
c@242 92 void erhand(char* err_msg)
c@242 93 {
c@242 94 fprintf(stderr,"Run-time error:\n");
c@242 95 fprintf(stderr,"%s\n", err_msg);
c@242 96 fprintf(stderr,"Exiting to system.\n");
c@242 97 exit(1);
c@242 98 }
c@242 99
c@242 100
c@242 101 /** Reduce a real, symmetric matrix to a symmetric, tridiag. matrix. */
c@242 102
c@242 103 /* Householder reduction of matrix a to tridiagonal form.
c@242 104 Algorithm: Martin et al., Num. Math. 11, 181-195, 1968.
c@242 105 Ref: Smith et al., Matrix Eigensystem Routines -- EISPACK Guide
c@242 106 Springer-Verlag, 1976, pp. 489-494.
c@242 107 W H Press et al., Numerical Recipes in C, Cambridge U P,
c@242 108 1988, pp. 373-374. */
c@242 109 void tred2(double** a, int n, double* d, double* e)
c@242 110 {
c@242 111 int l, k, j, i;
c@242 112 double scale, hh, h, g, f;
c@242 113
c@242 114 for (i = n-1; i >= 1; i--)
c@242 115 {
c@242 116 l = i - 1;
c@242 117 h = scale = 0.0;
c@242 118 if (l > 0)
c@242 119 {
c@242 120 for (k = 0; k <= l; k++)
c@242 121 scale += fabs(a[i][k]);
c@242 122 if (scale == 0.0)
c@242 123 e[i] = a[i][l];
c@242 124 else
c@242 125 {
c@242 126 for (k = 0; k <= l; k++)
c@242 127 {
c@242 128 a[i][k] /= scale;
c@242 129 h += a[i][k] * a[i][k];
c@242 130 }
c@242 131 f = a[i][l];
c@242 132 g = f>0 ? -sqrt(h) : sqrt(h);
c@242 133 e[i] = scale * g;
c@242 134 h -= f * g;
c@242 135 a[i][l] = f - g;
c@242 136 f = 0.0;
c@242 137 for (j = 0; j <= l; j++)
c@242 138 {
c@242 139 a[j][i] = a[i][j]/h;
c@242 140 g = 0.0;
c@242 141 for (k = 0; k <= j; k++)
c@242 142 g += a[j][k] * a[i][k];
c@242 143 for (k = j+1; k <= l; k++)
c@242 144 g += a[k][j] * a[i][k];
c@242 145 e[j] = g / h;
c@242 146 f += e[j] * a[i][j];
c@242 147 }
c@242 148 hh = f / (h + h);
c@242 149 for (j = 0; j <= l; j++)
c@242 150 {
c@242 151 f = a[i][j];
c@242 152 e[j] = g = e[j] - hh * f;
c@242 153 for (k = 0; k <= j; k++)
c@242 154 a[j][k] -= (f * e[k] + g * a[i][k]);
c@242 155 }
c@242 156 }
c@242 157 }
c@242 158 else
c@242 159 e[i] = a[i][l];
c@242 160 d[i] = h;
c@242 161 }
c@242 162 d[0] = 0.0;
c@242 163 e[0] = 0.0;
c@242 164 for (i = 0; i < n; i++)
c@242 165 {
c@242 166 l = i - 1;
c@242 167 if (d[i])
c@242 168 {
c@242 169 for (j = 0; j <= l; j++)
c@242 170 {
c@242 171 g = 0.0;
c@242 172 for (k = 0; k <= l; k++)
c@242 173 g += a[i][k] * a[k][j];
c@242 174 for (k = 0; k <= l; k++)
c@242 175 a[k][j] -= g * a[k][i];
c@242 176 }
c@242 177 }
c@242 178 d[i] = a[i][i];
c@242 179 a[i][i] = 1.0;
c@242 180 for (j = 0; j <= l; j++)
c@242 181 a[j][i] = a[i][j] = 0.0;
c@242 182 }
c@242 183 }
c@242 184
c@242 185 /** Tridiagonal QL algorithm -- Implicit **********************/
c@242 186
c@242 187 void tqli(double* d, double* e, int n, double** z)
c@242 188 {
c@242 189 int m, l, iter, i, k;
c@242 190 double s, r, p, g, f, dd, c, b;
c@242 191
c@242 192 for (i = 1; i < n; i++)
c@242 193 e[i-1] = e[i];
c@242 194 e[n-1] = 0.0;
c@242 195 for (l = 0; l < n; l++)
c@242 196 {
c@242 197 iter = 0;
c@242 198 do
c@242 199 {
c@242 200 for (m = l; m < n-1; m++)
c@242 201 {
c@242 202 dd = fabs(d[m]) + fabs(d[m+1]);
c@242 203 if (fabs(e[m]) + dd == dd) break;
c@242 204 }
c@242 205 if (m != l)
c@242 206 {
c@242 207 if (iter++ == 30) erhand("No convergence in TLQI.");
c@242 208 g = (d[l+1] - d[l]) / (2.0 * e[l]);
c@242 209 r = sqrt((g * g) + 1.0);
c@242 210 g = d[m] - d[l] + e[l] / (g + SIGN(r, g));
c@242 211 s = c = 1.0;
c@242 212 p = 0.0;
c@242 213 for (i = m-1; i >= l; i--)
c@242 214 {
c@242 215 f = s * e[i];
c@242 216 b = c * e[i];
c@242 217 if (fabs(f) >= fabs(g))
c@242 218 {
c@242 219 c = g / f;
c@242 220 r = sqrt((c * c) + 1.0);
c@242 221 e[i+1] = f * r;
c@242 222 c *= (s = 1.0/r);
c@242 223 }
c@242 224 else
c@242 225 {
c@242 226 s = f / g;
c@242 227 r = sqrt((s * s) + 1.0);
c@242 228 e[i+1] = g * r;
c@242 229 s *= (c = 1.0/r);
c@242 230 }
c@242 231 g = d[i+1] - p;
c@242 232 r = (d[i] - g) * s + 2.0 * c * b;
c@242 233 p = s * r;
c@242 234 d[i+1] = g + p;
c@242 235 g = c * r - b;
c@242 236 for (k = 0; k < n; k++)
c@242 237 {
c@242 238 f = z[k][i+1];
c@242 239 z[k][i+1] = s * z[k][i] + c * f;
c@242 240 z[k][i] = c * z[k][i] - s * f;
c@242 241 }
c@242 242 }
c@242 243 d[l] = d[l] - p;
c@242 244 e[l] = g;
c@242 245 e[m] = 0.0;
c@242 246 }
c@242 247 } while (m != l);
c@242 248 }
c@242 249 }
c@242 250
c@242 251 /* In place projection onto basis vectors */
c@242 252 void pca_project(double** data, int n, int m, int ncomponents)
c@242 253 {
c@242 254 int i, j, k, k2;
c@242 255 double **symmat, **symmat2, *evals, *interm;
c@242 256
c@242 257 //TODO: assert ncomponents < m
c@242 258
c@242 259 symmat = (double**) malloc(m*sizeof(double*));
c@242 260 for (i = 0; i < m; i++)
c@242 261 symmat[i] = (double*) malloc(m*sizeof(double));
c@242 262
c@242 263 covcol(data, n, m, symmat);
c@242 264
c@242 265 /*********************************************************************
c@242 266 Eigen-reduction
c@242 267 **********************************************************************/
c@242 268
c@242 269 /* Allocate storage for dummy and new vectors. */
c@242 270 evals = (double*) malloc(m*sizeof(double)); /* Storage alloc. for vector of eigenvalues */
c@242 271 interm = (double*) malloc(m*sizeof(double)); /* Storage alloc. for 'intermediate' vector */
c@242 272 //MALLOC_ARRAY(symmat2,m,m,double);
c@242 273 //for (i = 0; i < m; i++) {
c@242 274 // for (j = 0; j < m; j++) {
c@242 275 // symmat2[i][j] = symmat[i][j]; /* Needed below for col. projections */
c@242 276 // }
c@242 277 //}
c@242 278 tred2(symmat, m, evals, interm); /* Triangular decomposition */
c@242 279 tqli(evals, interm, m, symmat); /* Reduction of sym. trid. matrix */
c@242 280 /* evals now contains the eigenvalues,
c@242 281 columns of symmat now contain the associated eigenvectors. */
c@242 282
c@242 283 /*
c@242 284 printf("\nEigenvalues:\n");
c@242 285 for (j = m-1; j >= 0; j--) {
c@242 286 printf("%18.5f\n", evals[j]); }
c@242 287 printf("\n(Eigenvalues should be strictly positive; limited\n");
c@242 288 printf("precision machine arithmetic may affect this.\n");
c@242 289 printf("Eigenvalues are often expressed as cumulative\n");
c@242 290 printf("percentages, representing the 'percentage variance\n");
c@242 291 printf("explained' by the associated axis or principal component.)\n");
c@242 292
c@242 293 printf("\nEigenvectors:\n");
c@242 294 printf("(First three; their definition in terms of original vbes.)\n");
c@242 295 for (j = 0; j < m; j++) {
c@242 296 for (i = 1; i <= 3; i++) {
c@242 297 printf("%12.4f", symmat[j][m-i]); }
c@242 298 printf("\n"); }
c@242 299 */
c@242 300
c@242 301 /* Form projections of row-points on prin. components. */
c@242 302 /* Store in 'data', overwriting original data. */
c@242 303 for (i = 0; i < n; i++) {
c@242 304 for (j = 0; j < m; j++) {
c@242 305 interm[j] = data[i][j]; } /* data[i][j] will be overwritten */
c@242 306 for (k = 0; k < ncomponents; k++) {
c@242 307 data[i][k] = 0.0;
c@242 308 for (k2 = 0; k2 < m; k2++) {
c@242 309 data[i][k] += interm[k2] * symmat[k2][m-k-1]; }
c@242 310 }
c@242 311 }
c@242 312
c@242 313 /*
c@242 314 printf("\nProjections of row-points on first 3 prin. comps.:\n");
c@242 315 for (i = 0; i < n; i++) {
c@242 316 for (j = 0; j < 3; j++) {
c@242 317 printf("%12.4f", data[i][j]); }
c@242 318 printf("\n"); }
c@242 319 */
c@242 320
c@242 321 /* Form projections of col.-points on first three prin. components. */
c@242 322 /* Store in 'symmat2', overwriting what was stored in this. */
c@242 323 //for (j = 0; j < m; j++) {
c@242 324 // for (k = 0; k < m; k++) {
c@242 325 // interm[k] = symmat2[j][k]; } /*symmat2[j][k] will be overwritten*/
c@242 326 // for (i = 0; i < 3; i++) {
c@242 327 // symmat2[j][i] = 0.0;
c@242 328 // for (k2 = 0; k2 < m; k2++) {
c@242 329 // symmat2[j][i] += interm[k2] * symmat[k2][m-i-1]; }
c@242 330 // if (evals[m-i-1] > 0.0005) /* Guard against zero eigenvalue */
c@242 331 // symmat2[j][i] /= sqrt(evals[m-i-1]); /* Rescale */
c@242 332 // else
c@242 333 // symmat2[j][i] = 0.0; /* Standard kludge */
c@242 334 // }
c@242 335 // }
c@242 336
c@242 337 /*
c@242 338 printf("\nProjections of column-points on first 3 prin. comps.:\n");
c@242 339 for (j = 0; j < m; j++) {
c@242 340 for (k = 0; k < 3; k++) {
c@242 341 printf("%12.4f", symmat2[j][k]); }
c@242 342 printf("\n"); }
c@242 343 */
c@242 344
c@242 345
c@242 346 for (i = 0; i < m; i++)
c@242 347 free(symmat[i]);
c@242 348 free(symmat);
c@242 349 //FREE_ARRAY(symmat2,m);
c@242 350 free(evals);
c@242 351 free(interm);
c@242 352
c@242 353 }
c@242 354
c@242 355
c@242 356