-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial_regression.cpp
185 lines (152 loc) · 4.48 KB
/
polynomial_regression.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int size_of_row;
void print_square_matrix(double **A, double sol_vector[], int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << A[i][j] << "\t";
}
cout << "=\t" << sol_vector[i];
cout << endl;
}
}
void forward_elimination(double **A, double sol_vector[], int n) {
double factor;
for(int k = 0; k < n; k++) {
for(int i = k+1; i < n; i++) {
factor = A[i][k]/A[k][k];
for(int j = k; j < n; j++) {
A[i][j] = A[i][j] - factor*A[k][j];
}
sol_vector[i] = sol_vector[i] - factor*sol_vector[k];
}
}
}
void back_substitution(double **A, double sol_vector[], int n, double solution[]) {
double sum;
solution[n-1] = sol_vector[n-1]/A[n-1][n-1];
for(int i = n-2; i >= 0; i--) {
sum = 0;
for(int j = i+1; j < n; j++) {
sum = sum + A[i][j] * solution[j];
}
solution[i] = ( sol_vector[i] - sum ) / A[i][i];
}
}
void print_poly(double v[], int n) {
for(int i = 0; i < n; i++){
cout << v[i];
if(i != 0){
cout << "*x^" << i;
}
if(i != n-1)
cout << " + ";
}
cout << endl;
}
void print_vector(double v[], int n) {
for(int i = 0; i < n; i++){
cout << "a" << i << " = " << v[i];
cout << endl;
}
cout << endl;
}
void polynomial_regression(double x[], double y[], const int degree, const int n) {
double sum_x = 0, sum_xy = 0;
size_of_row = degree + 1;
double sol_vector[size_of_row];
double **linear_equations;
linear_equations = new double*[size_of_row];
for(int i = 0; i < size_of_row; i++){
linear_equations[i] = new double[size_of_row];
}
cout << "\nConstructing system of linear equations ..." << endl << "------------------------------------------" << endl;
for(int i = 0; i < size_of_row; i++) {
sum_xy = 0;
for(int j = 0; j < n; j++)
sum_xy += pow(x[j],i)*y[j];
sol_vector[i] = sum_xy;
for (int j = 0; j < size_of_row; j++) {
sum_x = 0;
if (i == 0 && j == 0)
linear_equations[i][j] = n;
else {
//Calculate the sum of x to a certain power.
for (int h = 0; h < n; h++)
sum_x += pow(x[h],(j+i));
linear_equations[i][j] = sum_x;
}
}
}
for(int i = 0; i < size_of_row; i++) {
for(int j = 0; j < size_of_row; j++)
cout << linear_equations[i][j] << "\t";
cout << "=\t" << sol_vector[i];
cout << endl;
}
cout << endl;
double sum_y = sol_vector[0];
cout << "\nGauss to get [a0...an] ..." << endl << "------------------------------------------" << endl;
forward_elimination(linear_equations, sol_vector, size_of_row);
print_square_matrix(linear_equations, sol_vector, size_of_row);
double x_vector[size_of_row];
back_substitution(linear_equations, sol_vector, size_of_row, x_vector);
print_vector(x_vector, size_of_row);
cout << "\nPolynomial ..." << endl << "------------------------------------------" << endl;
print_poly(x_vector, size_of_row);
cout << "\nErrors ..." << endl << "------------------------------------------" << endl;
double e[n];
for (int i = 0; i < n; i++) {
double y_calculada = 0;
for (int j = size_of_row - 1; j >= 1; j--)
y_calculada += x_vector[j]*( pow( x[i], j ) );
y_calculada += x_vector[0];
e[i] = pow(y[i] - y_calculada,2);
}
double sr = 0;
double st = 0;
for (int i = 0; i < n; i++) {
sr += e[i];
st += pow(y[i] - (sum_y/n),2);
}
cout << "Sr = " << sr << endl;
cout << sr << " / (" << n << " - (" << degree << " + 1))" << endl;
double syx = sqrt(sr/(n - (degree + 1)));
cout << "St = " << st << endl;
double r2 = (st-sr)/st;
double r = sqrt(r2);
cout << "Error Estandar (S y/x): " << syx;
if(n - degree - 1 == 0)
cout << "\tS y/x is a division by 0";
cout << endl;
cout << "Coeficiente de determinacion (r2): " << r2 << endl;
cout << "Coeficiente de correlacion (r): " << r << endl;
cout << endl;
}
int main() {
int n, degree;
cout << "n: ";
cin >> n;
cout << "degree: ";
cin >> degree;
ifstream inx("./data/x_2.txt");
ifstream iny("./data/y_2.txt");
if(!inx || !iny){
cout << "Error" << endl;
}
double X[n], Y[n];
cout << "\n x\t y" << endl << "-------------" << endl;
for(int i = 0; inx && iny && i < n; i++) {
inx >> X[i];
iny >> Y[i];
cout << X[i] << "\t" << Y[i] << endl;
}
inx.close();
iny.close();
polynomial_regression(X,Y,degree,n);
return 0;
}
//Error estandar de estimación Sy/x = sqrt(sr/(n-(m+1)))
//r^2 = (st-sr)/st