Back to index

radiance  4R0+20100331
linregr.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: linregr.c,v 2.4 2003/02/25 02:47:21 greg Exp $";
00003 #endif
00004 /*
00005  * Basic linear regression calculation.
00006  */
00007 
00008 #include "copyright.h"
00009 
00010 #include <math.h>
00011 
00012 #include "linregr.h"
00013 
00014 
00015 void
00016 lrclear(l)                  /* initialize sum */
00017 register LRSUM       *l;
00018 {
00019        l->xs = l->ys = l->xxs = l->yys = l->xys = 0.0;
00020        l->n = 0;
00021 }
00022 
00023 
00024 int
00025 flrpoint(x, y, l)           /* add point (x,y) to sum */
00026 double x, y;
00027 register LRSUM       *l;
00028 {
00029        l->xs += x;
00030        l->ys += y;
00031        l->xxs += x*x;
00032        l->yys += y*y;
00033        l->xys += x*y;
00034        return(++l->n);
00035 }
00036 
00037 
00038 int
00039 lrfit(r, l)                 /* compute linear regression */
00040 register LRLIN       *r;
00041 register LRSUM       *l;
00042 {
00043        double nxvar, nyvar;
00044 
00045        if (l->n < 2)
00046               return(-1);
00047        nxvar = l->xxs - l->xs*l->xs/l->n;
00048        nyvar = l->yys - l->ys*l->ys/l->n;
00049        if (nxvar == 0.0 || nyvar == 0.0)
00050               return(-1);
00051        r->slope = (l->xys - l->xs*l->ys/l->n) / nxvar;
00052        r->intercept = (l->ys - r->slope*l->xs) / l->n;
00053        r->correlation = r->slope*sqrt(nxvar/nyvar);
00054        return(0);
00055 }