1 | #include <iostream> |
---|
2 | #include <math.h> |
---|
3 | int linear_regression(int num, float *x, float *y, int ilow, int ihigh, float &slope, float &errSlope, float &intercept, float &errIntercept, float &r) |
---|
4 | { |
---|
5 | /** |
---|
6 | * Computes the linear best fit to data: $y=a x + b$, where $x$ and |
---|
7 | * $y$ are arrays of size num, $a$ is the slope and $b$ the |
---|
8 | * y-intercept. The values used in the arrays are those from ilow |
---|
9 | * to ihigh. (ie. if the full arrays are being used, then ilow=0 |
---|
10 | * and ihigh=num-1. Returns the values of slope & intercept (with |
---|
11 | * errors) as well as r, the regression coefficient. |
---|
12 | * \param num Size of the x & y arrays. |
---|
13 | * \param x Array of abscissae. |
---|
14 | * \param y Array of ordinates. |
---|
15 | * \param ilow Minimum index of the arrays to be used (ilow=0 means |
---|
16 | * start at the beginning). |
---|
17 | * \param ihigh Maximum index of the arrays to be used (ihigh=num-1 |
---|
18 | * means finish at the end). |
---|
19 | * \param slope Returns value of the slope of the best fit line. |
---|
20 | * \param errSlope Returns value of the estimated error in the slope |
---|
21 | * value. |
---|
22 | * \param intercept Returns value of the y-intercept of the best fit |
---|
23 | * line. |
---|
24 | * \param errIntercept Returns value of the estimated error in the |
---|
25 | * value of the y-intercept. |
---|
26 | * \param r Returns the value of the regression coefficient. |
---|
27 | * \return If everything works, returns 0. If slope is infinite (eg, |
---|
28 | * all points have same x value), returns 1. |
---|
29 | */ |
---|
30 | |
---|
31 | if (ilow>ihigh) { |
---|
32 | std::cerr << "Error! linear_regression.cc :: ilow (" << ilow |
---|
33 | << ") > ihigh (" << ihigh << ")!!\n"; |
---|
34 | return 1; |
---|
35 | } |
---|
36 | if (ihigh>num-1) { |
---|
37 | std::cerr << "Error! linear_regression.cc :: ihigh (" <<ihigh |
---|
38 | << ") out of bounds of array (>" << num-1 << ")!!\n"; |
---|
39 | return 1; |
---|
40 | } |
---|
41 | if(ilow<0){ |
---|
42 | std::cerr << "Error! linear_regression.cc :: ilow (" << ilow |
---|
43 | << ") < 0. !!\n"; |
---|
44 | return 1; |
---|
45 | } |
---|
46 | |
---|
47 | double sumx,sumy,sumxx,sumxy,sumyy; |
---|
48 | sumx=0.; |
---|
49 | sumy=0.; |
---|
50 | sumxx=0.; |
---|
51 | sumxy=0.; |
---|
52 | sumyy=0.; |
---|
53 | int count=0; |
---|
54 | for (int i=ilow;i<=ihigh;i++){ |
---|
55 | count++; |
---|
56 | sumx = sumx + x[i]; |
---|
57 | sumy = sumy + y[i]; |
---|
58 | sumxx = sumxx + x[i]*x[i]; |
---|
59 | sumxy = sumxy + x[i]*y[i]; |
---|
60 | sumyy = sumyy + y[i]*y[i]; |
---|
61 | } |
---|
62 | |
---|
63 | const float SMALLTHING=1.e-6; |
---|
64 | if(fabs(count*sumxx-sumx*sumx)<SMALLTHING) return 1; |
---|
65 | else{ |
---|
66 | |
---|
67 | slope = (count*sumxy - sumx*sumy)/(count*sumxx - sumx*sumx); |
---|
68 | errSlope = count / (count*sumxx - sumx*sumx); |
---|
69 | |
---|
70 | intercept = (sumy*sumxx - sumxy*sumx)/(count*sumxx - sumx*sumx); |
---|
71 | errIntercept = sumxx / (count*sumxx - sumx*sumx); |
---|
72 | |
---|
73 | r = (count*sumxy - sumx*sumy) / |
---|
74 | (sqrt(count*sumxx-sumx*sumx) * sqrt(count*sumyy-sumy*sumy) ); |
---|
75 | |
---|
76 | return 0; |
---|
77 | |
---|
78 | } |
---|
79 | } |
---|