Revision: | 2.4 |
Committed: | Tue Feb 25 02:47:21 2003 UTC (22 years, 2 months ago) by greg |
Content type: | text/plain |
Branch: | MAIN |
CVS Tags: | rad5R4, rad5R2, rad4R2P2, rad5R0, rad5R1, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R5, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1, rad5R3, HEAD |
Changes since 2.3: | +1 -56 lines |
Log Message: | Replaced inline copyright notice with #include "copyright.h" |
# | User | Rev | Content |
---|---|---|---|
1 | greg | 1.1 | #ifndef lint |
2 | greg | 2.3 | static const char RCSid[] = "$Id$"; |
3 | greg | 1.1 | #endif |
4 | /* | ||
5 | * Basic linear regression calculation. | ||
6 | */ | ||
7 | |||
8 | greg | 2.4 | #include "copyright.h" |
9 | greg | 2.3 | |
10 | greg | 2.2 | #include <math.h> |
11 | |||
12 | greg | 1.1 | #include "linregr.h" |
13 | |||
14 | |||
15 | greg | 2.3 | void |
16 | greg | 1.1 | lrclear(l) /* initialize sum */ |
17 | register LRSUM *l; | ||
18 | { | ||
19 | l->xs = l->ys = l->xxs = l->yys = l->xys = 0.0; | ||
20 | l->n = 0; | ||
21 | } | ||
22 | |||
23 | |||
24 | greg | 2.3 | int |
25 | greg | 1.1 | flrpoint(x, y, l) /* add point (x,y) to sum */ |
26 | double x, y; | ||
27 | register LRSUM *l; | ||
28 | { | ||
29 | l->xs += x; | ||
30 | l->ys += y; | ||
31 | l->xxs += x*x; | ||
32 | l->yys += y*y; | ||
33 | l->xys += x*y; | ||
34 | return(++l->n); | ||
35 | } | ||
36 | |||
37 | |||
38 | greg | 2.3 | int |
39 | greg | 1.1 | lrfit(r, l) /* compute linear regression */ |
40 | register LRLIN *r; | ||
41 | register LRSUM *l; | ||
42 | { | ||
43 | double nxvar, nyvar; | ||
44 | |||
45 | if (l->n < 2) | ||
46 | return(-1); | ||
47 | nxvar = l->xxs - l->xs*l->xs/l->n; | ||
48 | nyvar = l->yys - l->ys*l->ys/l->n; | ||
49 | if (nxvar == 0.0 || nyvar == 0.0) | ||
50 | return(-1); | ||
51 | r->slope = (l->xys - l->xs*l->ys/l->n) / nxvar; | ||
52 | r->intercept = (l->ys - r->slope*l->xs) / l->n; | ||
53 | r->correlation = r->slope*sqrt(nxvar/nyvar); | ||
54 | return(0); | ||
55 | } |