lib/correlate.c

Go to the documentation of this file.
00001 /* correlate - calculate r, also known as Pearson's correlation
00002  * coefficient.  r*r has the nice property that it explains
00003  * how much of one variable's variation can be explained as
00004  * a linear function of the other variable's variation. */
00005 
00006 #include "common.h"
00007 #include "correlate.h"
00008 
00009 struct correlate *correlateNew()
00010 /* Return new correlation handler. */
00011 {
00012 struct correlate *c;
00013 return AllocVar(c);
00014 }
00015 
00016 void correlateFree(struct correlate **pC)
00017 /* Free up correlator. */
00018 {
00019 freez(pC);
00020 }
00021 
00022 void correlateNext(struct correlate *c, double x, double y)
00023 /* Add next sample to correlation. */
00024 {
00025 c->sumX += x;
00026 c->sumXX += x*x;
00027 c->sumXY += x*y;
00028 c->sumY += y;
00029 c->sumYY += y*y;
00030 c->n += 1; 
00031 }
00032 
00033 double correlateResult(struct correlate *c)
00034 /* Returns correlation (aka R) */
00035 {
00036 double r = 0;
00037 if (c->n > 0)
00038     {
00039     double sp = c->sumXY - c->sumX*c->sumY/c->n;
00040     double ssx = c->sumXX - c->sumX*c->sumX/c->n;
00041     double ssy = c->sumYY - c->sumY*c->sumY/c->n;
00042     double q = ssx*ssy;
00043     if (q != 0)
00044         r = sp/sqrt(q);
00045     }
00046 return r;
00047 }
00048 
00049 double correlateArrays(double *x, double *y, int size)
00050 /* Return correlation of two arrays of doubles. */
00051 {
00052 struct correlate *c = correlateNew();
00053 double r;
00054 int i;
00055 for (i=0; i<size; ++i)
00056      correlateNext(c, x[i], y[i]);
00057 r = correlateResult(c);
00058 correlateFree(&c);
00059 return r;
00060 }
00061 

Generated on Tue Dec 25 18:39:30 2007 for blat by  doxygen 1.5.2