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