-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_regression.c
61 lines (54 loc) · 1.81 KB
/
linear_regression.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void linear_regression(int num, float x[], float y[], int ilow, int ihigh, float *a, float *err_a, float *b, float *err_b, float *r)
{
/* Computes the linear best fit to data - y = a*x + b, where x and y are
arrays of size num. The values used in the arrays are those from ilow
to ihigh. (ie. if the full arrays are being used, then ilow=0 and
ihigh=num-1.
Returns the values of a & b (with errors)
as well as r, the regression coefficient.
*/
float sumx,sumy,sumxx,sumxy,sumyy;
int i,count;
*a=0.;
*b=0.;
*r=0.;
if (ilow>ihigh) {
printf("Bollocks! linear_regression.c :: ilow (%d) > ihigh (%d)!!\n",ilow,ihigh);
exit(1);
}
if (ihigh>num-1) {
printf("Bollocks! linear_regression.c :: ihigh (%d) out of bounds of array!!\n",ihigh);
exit(1);
}
if(ilow<0){
printf("Bollocks! linear_regression.c :: ilow (%d) < 0. !!\n",ilow);
exit(1);
}
sumx=0.;
sumy=0.;
sumxx=0.;
sumxy=0.;
sumyy=0.;
count=0;
for (i=ilow;i<=ihigh;i++){
count++;
sumx = sumx + x[i];
sumy = sumy + y[i];
sumxx = sumxx + x[i]*x[i];
sumxy = sumxy + x[i]*y[i];
sumyy = sumyy + y[i]*y[i];
// printf("num = %d, ilow = %d, ihigh = %d, x[%d] = %1.2e, sum = %1.2e y[%d] = %1.2e sum = %1.2e count = %d \n", num, ilow, ihigh, i, x[i], sumx, i, y[i], sumy, count);
}
if(count!=(ihigh-ilow+1)){
printf("Whoops!! count (%d) doesn't equal what it should (%d)!!\n",count,(ihigh-ilow+1));
exit(1);
}
*a = (count*sumxy - sumx*sumy)/(count*sumxx - sumx*sumx);
*err_a = count / (count*sumxx - sumx*sumx);
*b = (sumy*sumxx - sumxy*sumx)/(count*sumxx - sumx*sumx);
*err_b = sumxx / (count*sumxx - sumx*sumx);
*r = (count*sumxy - sumx*sumy)/(sqrt(count*sumxx-sumx*sumx)*sqrt(count*sumyy-sumy*sumy));
}