-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
74 lines (55 loc) · 1.55 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
// my math funcs are better
#define SQ_(n) ((n)*(n))
#define ABS_(n) ((int)(n)>=0?(n):(-1*(n)))
double SQRT_(double x) {
double guess = x / 2.0;
double error = 0.0001;
while (ABS_(guess * guess - x) > error) {
guess = (guess + x / guess) / 2.0;
}
return guess;
}
// -----------
void calculateFormula(double *x, double *y, int n, double *m, double *b) {
// x and y are array
// find mean x and mean y
double x_sum = 0.0, y_sum = 0.0;
for (int i = 0; i < n; ++i) {
x_sum += x[i];
y_sum += y[i];
}
double x_mean = x_sum/n;
double y_mean = y_sum/n;
// y = mx + b
// find m and b
double num = 0.0, denum = 0.0;
for (int i = 0; i < n; ++i) {
num += (x[i]-x_mean)*(y[i]-y_mean);
denum += SQ_(x[i]-x_mean);
}
*m = num/denum;
*b = y_mean - (*m)*x_mean;
}
double calculateRMSE(double *x, double *y, int n, double m, double b) {
// x and y are array
double errors = 0.0;
for (int i = 0; i < n; ++i) {
errors += SQ_((m*x[i]+b)-y[i]);
}
return SQRT_(errors/n);
}
double calculateRSquare(double *x, double *y, int n, double m, double b) {
// x and y are array
// find mean y
double y_sum = 0.0;
for (int i = 0; i < n; ++i) {
y_sum += y[i];
}
double y_mean = y_sum/n;
double ssr = 0.0, sst = 0.0;
for (int i = 0; i < n; ++i) {
ssr += SQ_((m*x[i]+b)-y_mean);
sst += SQ_(y[i]-y_mean);
}
return ssr / sst;
}