c@242: /*********************************/ c@242: /* Principal Components Analysis */ c@242: /*********************************/ c@242: c@242: /*********************************************************************/ c@242: /* Principal Components Analysis or the Karhunen-Loeve expansion is a c@242: classical method for dimensionality reduction or exploratory data c@242: analysis. One reference among many is: F. Murtagh and A. Heck, c@242: Multivariate Data Analysis, Kluwer Academic, Dordrecht, 1987. c@242: c@242: Author: c@242: F. Murtagh c@242: Phone: + 49 89 32006298 (work) c@242: + 49 89 965307 (home) c@242: Earn/Bitnet: fionn@dgaeso51, fim@dgaipp1s, murtagh@stsci c@242: Span: esomc1::fionn c@242: Internet: murtagh@scivax.stsci.edu c@242: c@242: F. Murtagh, Munich, 6 June 1989 */ c@242: /*********************************************************************/ c@242: c@242: #include c@242: #include c@242: #include c@242: c@242: #include "PCA.h" c@242: c@242: #define SIGN(a, b) ( (b) < 0 ? -fabs(a) : fabs(a) ) c@242: c@242: /** Variance-covariance matrix: creation *****************************/ c@242: c@242: /* Create m * m covariance matrix from given n * m data matrix. */ c@242: void covcol(double** data, int n, int m, double** symmat) c@242: { c@242: double *mean; c@242: int i, j, j1, j2; c@242: c@242: /* Allocate storage for mean vector */ c@242: c@242: mean = (double*) malloc(m*sizeof(double)); c@242: c@242: /* Determine mean of column vectors of input data matrix */ c@242: c@242: for (j = 0; j < m; j++) c@242: { c@242: mean[j] = 0.0; c@242: for (i = 0; i < n; i++) c@242: { c@242: mean[j] += data[i][j]; c@242: } c@242: mean[j] /= (double)n; c@242: } c@242: c@242: /* c@242: printf("\nMeans of column vectors:\n"); c@242: for (j = 0; j < m; j++) { c@242: printf("%12.1f",mean[j]); } printf("\n"); c@242: */ c@242: c@242: /* Center the column vectors. */ c@242: c@242: for (i = 0; i < n; i++) c@242: { c@242: for (j = 0; j < m; j++) c@242: { c@242: data[i][j] -= mean[j]; c@242: } c@242: } c@242: c@242: /* Calculate the m * m covariance matrix. */ c@242: for (j1 = 0; j1 < m; j1++) c@242: { c@242: for (j2 = j1; j2 < m; j2++) c@242: { c@242: symmat[j1][j2] = 0.0; c@242: for (i = 0; i < n; i++) c@242: { c@242: symmat[j1][j2] += data[i][j1] * data[i][j2]; c@242: } c@242: symmat[j2][j1] = symmat[j1][j2]; c@242: } c@242: } c@242: c@242: free(mean); c@242: c@242: return; c@242: c@242: } c@242: c@242: /** Error handler **************************************************/ c@242: c@242: void erhand(char* err_msg) c@242: { c@242: fprintf(stderr,"Run-time error:\n"); c@242: fprintf(stderr,"%s\n", err_msg); c@242: fprintf(stderr,"Exiting to system.\n"); c@242: exit(1); c@242: } c@242: c@242: c@242: /** Reduce a real, symmetric matrix to a symmetric, tridiag. matrix. */ c@242: c@242: /* Householder reduction of matrix a to tridiagonal form. c@242: Algorithm: Martin et al., Num. Math. 11, 181-195, 1968. c@242: Ref: Smith et al., Matrix Eigensystem Routines -- EISPACK Guide c@242: Springer-Verlag, 1976, pp. 489-494. c@242: W H Press et al., Numerical Recipes in C, Cambridge U P, c@242: 1988, pp. 373-374. */ c@242: void tred2(double** a, int n, double* d, double* e) c@242: { c@242: int l, k, j, i; c@242: double scale, hh, h, g, f; c@242: c@242: for (i = n-1; i >= 1; i--) c@242: { c@242: l = i - 1; c@242: h = scale = 0.0; c@242: if (l > 0) c@242: { c@242: for (k = 0; k <= l; k++) c@242: scale += fabs(a[i][k]); c@242: if (scale == 0.0) c@242: e[i] = a[i][l]; c@242: else c@242: { c@242: for (k = 0; k <= l; k++) c@242: { c@242: a[i][k] /= scale; c@242: h += a[i][k] * a[i][k]; c@242: } c@242: f = a[i][l]; c@242: g = f>0 ? -sqrt(h) : sqrt(h); c@242: e[i] = scale * g; c@242: h -= f * g; c@242: a[i][l] = f - g; c@242: f = 0.0; c@242: for (j = 0; j <= l; j++) c@242: { c@242: a[j][i] = a[i][j]/h; c@242: g = 0.0; c@242: for (k = 0; k <= j; k++) c@242: g += a[j][k] * a[i][k]; c@242: for (k = j+1; k <= l; k++) c@242: g += a[k][j] * a[i][k]; c@242: e[j] = g / h; c@242: f += e[j] * a[i][j]; c@242: } c@242: hh = f / (h + h); c@242: for (j = 0; j <= l; j++) c@242: { c@242: f = a[i][j]; c@242: e[j] = g = e[j] - hh * f; c@242: for (k = 0; k <= j; k++) c@242: a[j][k] -= (f * e[k] + g * a[i][k]); c@242: } c@242: } c@242: } c@242: else c@242: e[i] = a[i][l]; c@242: d[i] = h; c@242: } c@242: d[0] = 0.0; c@242: e[0] = 0.0; c@242: for (i = 0; i < n; i++) c@242: { c@242: l = i - 1; c@242: if (d[i]) c@242: { c@242: for (j = 0; j <= l; j++) c@242: { c@242: g = 0.0; c@242: for (k = 0; k <= l; k++) c@242: g += a[i][k] * a[k][j]; c@242: for (k = 0; k <= l; k++) c@242: a[k][j] -= g * a[k][i]; c@242: } c@242: } c@242: d[i] = a[i][i]; c@242: a[i][i] = 1.0; c@242: for (j = 0; j <= l; j++) c@242: a[j][i] = a[i][j] = 0.0; c@242: } c@242: } c@242: c@242: /** Tridiagonal QL algorithm -- Implicit **********************/ c@242: c@242: void tqli(double* d, double* e, int n, double** z) c@242: { c@242: int m, l, iter, i, k; c@242: double s, r, p, g, f, dd, c, b; c@242: c@242: for (i = 1; i < n; i++) c@242: e[i-1] = e[i]; c@242: e[n-1] = 0.0; c@242: for (l = 0; l < n; l++) c@242: { c@242: iter = 0; c@242: do c@242: { c@242: for (m = l; m < n-1; m++) c@242: { c@242: dd = fabs(d[m]) + fabs(d[m+1]); c@242: if (fabs(e[m]) + dd == dd) break; c@242: } c@242: if (m != l) c@242: { c@242: if (iter++ == 30) erhand("No convergence in TLQI."); c@242: g = (d[l+1] - d[l]) / (2.0 * e[l]); c@242: r = sqrt((g * g) + 1.0); c@242: g = d[m] - d[l] + e[l] / (g + SIGN(r, g)); c@242: s = c = 1.0; c@242: p = 0.0; c@242: for (i = m-1; i >= l; i--) c@242: { c@242: f = s * e[i]; c@242: b = c * e[i]; c@242: if (fabs(f) >= fabs(g)) c@242: { c@242: c = g / f; c@242: r = sqrt((c * c) + 1.0); c@242: e[i+1] = f * r; c@242: c *= (s = 1.0/r); c@242: } c@242: else c@242: { c@242: s = f / g; c@242: r = sqrt((s * s) + 1.0); c@242: e[i+1] = g * r; c@242: s *= (c = 1.0/r); c@242: } c@242: g = d[i+1] - p; c@242: r = (d[i] - g) * s + 2.0 * c * b; c@242: p = s * r; c@242: d[i+1] = g + p; c@242: g = c * r - b; c@242: for (k = 0; k < n; k++) c@242: { c@242: f = z[k][i+1]; c@242: z[k][i+1] = s * z[k][i] + c * f; c@242: z[k][i] = c * z[k][i] - s * f; c@242: } c@242: } c@242: d[l] = d[l] - p; c@242: e[l] = g; c@242: e[m] = 0.0; c@242: } c@242: } while (m != l); c@242: } c@242: } c@242: c@242: /* In place projection onto basis vectors */ c@242: void pca_project(double** data, int n, int m, int ncomponents) c@242: { c@242: int i, j, k, k2; c@242: double **symmat, **symmat2, *evals, *interm; c@242: c@242: //TODO: assert ncomponents < m c@242: c@242: symmat = (double**) malloc(m*sizeof(double*)); c@242: for (i = 0; i < m; i++) c@242: symmat[i] = (double*) malloc(m*sizeof(double)); c@242: c@242: covcol(data, n, m, symmat); c@242: c@242: /********************************************************************* c@242: Eigen-reduction c@242: **********************************************************************/ c@242: c@242: /* Allocate storage for dummy and new vectors. */ c@242: evals = (double*) malloc(m*sizeof(double)); /* Storage alloc. for vector of eigenvalues */ c@242: interm = (double*) malloc(m*sizeof(double)); /* Storage alloc. for 'intermediate' vector */ c@242: //MALLOC_ARRAY(symmat2,m,m,double); c@242: //for (i = 0; i < m; i++) { c@242: // for (j = 0; j < m; j++) { c@242: // symmat2[i][j] = symmat[i][j]; /* Needed below for col. projections */ c@242: // } c@242: //} c@242: tred2(symmat, m, evals, interm); /* Triangular decomposition */ c@242: tqli(evals, interm, m, symmat); /* Reduction of sym. trid. matrix */ c@242: /* evals now contains the eigenvalues, c@242: columns of symmat now contain the associated eigenvectors. */ c@242: c@242: /* c@242: printf("\nEigenvalues:\n"); c@242: for (j = m-1; j >= 0; j--) { c@242: printf("%18.5f\n", evals[j]); } c@242: printf("\n(Eigenvalues should be strictly positive; limited\n"); c@242: printf("precision machine arithmetic may affect this.\n"); c@242: printf("Eigenvalues are often expressed as cumulative\n"); c@242: printf("percentages, representing the 'percentage variance\n"); c@242: printf("explained' by the associated axis or principal component.)\n"); c@242: c@242: printf("\nEigenvectors:\n"); c@242: printf("(First three; their definition in terms of original vbes.)\n"); c@242: for (j = 0; j < m; j++) { c@242: for (i = 1; i <= 3; i++) { c@242: printf("%12.4f", symmat[j][m-i]); } c@242: printf("\n"); } c@242: */ c@242: c@242: /* Form projections of row-points on prin. components. */ c@242: /* Store in 'data', overwriting original data. */ c@242: for (i = 0; i < n; i++) { c@242: for (j = 0; j < m; j++) { c@242: interm[j] = data[i][j]; } /* data[i][j] will be overwritten */ c@242: for (k = 0; k < ncomponents; k++) { c@242: data[i][k] = 0.0; c@242: for (k2 = 0; k2 < m; k2++) { c@242: data[i][k] += interm[k2] * symmat[k2][m-k-1]; } c@242: } c@242: } c@242: c@242: /* c@242: printf("\nProjections of row-points on first 3 prin. comps.:\n"); c@242: for (i = 0; i < n; i++) { c@242: for (j = 0; j < 3; j++) { c@242: printf("%12.4f", data[i][j]); } c@242: printf("\n"); } c@242: */ c@242: c@242: /* Form projections of col.-points on first three prin. components. */ c@242: /* Store in 'symmat2', overwriting what was stored in this. */ c@242: //for (j = 0; j < m; j++) { c@242: // for (k = 0; k < m; k++) { c@242: // interm[k] = symmat2[j][k]; } /*symmat2[j][k] will be overwritten*/ c@242: // for (i = 0; i < 3; i++) { c@242: // symmat2[j][i] = 0.0; c@242: // for (k2 = 0; k2 < m; k2++) { c@242: // symmat2[j][i] += interm[k2] * symmat[k2][m-i-1]; } c@242: // if (evals[m-i-1] > 0.0005) /* Guard against zero eigenvalue */ c@242: // symmat2[j][i] /= sqrt(evals[m-i-1]); /* Rescale */ c@242: // else c@242: // symmat2[j][i] = 0.0; /* Standard kludge */ c@242: // } c@242: // } c@242: c@242: /* c@242: printf("\nProjections of column-points on first 3 prin. comps.:\n"); c@242: for (j = 0; j < m; j++) { c@242: for (k = 0; k < 3; k++) { c@242: printf("%12.4f", symmat2[j][k]); } c@242: printf("\n"); } c@242: */ c@242: c@242: c@242: for (i = 0; i < m; i++) c@242: free(symmat[i]); c@242: free(symmat); c@242: //FREE_ARRAY(symmat2,m); c@242: free(evals); c@242: free(interm); c@242: c@242: } c@242: c@242: c@242: