-
Notifications
You must be signed in to change notification settings - Fork 2
/
program-utils.c
146 lines (124 loc) · 3.36 KB
/
program-utils.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <math.h>
#include "program-utils.h"
void least_squares_method(char coordinates[LENGTH], char line[BLOCK])
{
double X_sum = 0,
Y_sum = 0,
X_2_sum = 0,
XY_sum = 0;
char line_estimation[BLOCK];
int X, Y;
for (int i = 0 ; i < LENGTH ; i++)
{
X = (int)((unsigned char) coordinates[i]);
Y = (int)((unsigned char) coordinates[++i]);
X_sum += X;
Y_sum += Y;
XY_sum += X * Y;
X_2_sum += (X * X);
}
double a = 0, b = 0;
a = ((TOT_CRDNT * XY_sum) - (X_sum * Y_sum)) / ((TOT_CRDNT * X_2_sum) - (X_sum * Y_sum));
b = (Y_sum - (a * X_sum)) / TOT_CRDNT;
sprintf(line_estimation, " %.3lfx+%.3lf\n", a, b);
strcat(line, line_estimation);
}
void set_metrics(char line[],
double mean_absolute_err[],
double mean_squared_err[],
double root_mean_squared_err[],
int index)
{
char temp[BLOCK] = "";
int count = 0;
int x_coordinate[10];
int y_coordinate[10];
double mean_absolute_error = 0;
double mean_squared_error = 0;
double root_mean_squared_error = 0;
double a = 0, b = 0, nuy = 0;
for(int i = 0; i < strlen(line); i++)
{
if(count >= LENGTH)
{
if(line[i] != ' ')
{
if(line[i] == 'x' || i == strlen(line) - 1)
{
if(count % 2 == 0) a = atof(temp);
else b = atof(temp);
strcpy(temp, "");
count++;
}
else strncat(temp, &line[i], 1);
}
}
else if(line[i] != '(' && line[i] != ')' && line[i] != ' ')
{
if(line[i] == ',')
{
if(count % 2 == 0) x_coordinate[count/2] = atoi(temp);
else y_coordinate[count/2] = atoi(temp);
strcpy(temp, "");
count++;
}
else strncat(temp, &line[i], 1);
}
}
for(int i = 0 ; i < TOT_CRDNT; i++)
{
nuy = (a * x_coordinate[i]) + b;
mean_absolute_error += abs(nuy - y_coordinate[i]);
mean_squared_error += ((nuy - y_coordinate[i]) * (nuy - y_coordinate[i]));
}
mean_absolute_error /= TOT_CRDNT;
mean_squared_error /= TOT_CRDNT;
root_mean_squared_error = sqrt(mean_squared_error);
mean_absolute_err[index] = round(mean_absolute_error * 1000) / 1000;
mean_squared_err[index] = round(mean_squared_error * 1000) / 1000;
root_mean_squared_err[index] = round(root_mean_squared_error * 1000) / 1000;
strcpy(temp, "");
sprintf(temp, ", %.3lf, %.3lf, %.3lf\n", mean_absolute_error,
mean_squared_error,
root_mean_squared_error);
strcat(line,temp);
}
double standard_deviation(double metric[], double mean, int count)
{
double standard_deviation = 0;
for (int i = 0; i < count; i++)
{
standard_deviation += ((metric[i] - mean) * (metric[i] - mean));
}
standard_deviation = sqrt(standard_deviation / count);
standard_deviation = round(standard_deviation * 1000) / 1000;
return standard_deviation;
}
double median_deviation(double metric[], double mean, int count)
{
double total = 0;
for(int i = 0; i < count; i++)
{
total = total + abs(mean - metric[i]);
}
total = total / count;
total = round(total * 1000) / 1000;
return total;
}
double mean(double metric[], int count)
{
double total = 0;
for(int i = 0; i < count; i++)
{
total += metric[i];
}
total /= count;
total = round(total * 1000) / 1000;
return total;
}