-
Notifications
You must be signed in to change notification settings - Fork 1
/
Regression.cpp
81 lines (69 loc) · 1.31 KB
/
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
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<cmath>
#include "Regression.h"
using namespace std;
Regression::Regression(const int n, double *x, double *y) {
_n = n;
_x = x;
_y = y;
_a = 0.0;
_b = 0.0;
}
double Regression::SumX() const {
double sum = 0;
for (int i=0; i<_n; i++) {
sum = sum + _x[i];
}
return sum;
}
double Regression::SumY() const {
double sum = 0;
for (int i=0; i<_n; i++) {
sum = sum + _y[i];
}
return sum;
}
double Regression::SumX2() const {
double sum = 0;
for (int i=0; i<_n; i++) {
sum = sum + _x[i]*_x[i];
}
return sum;
}
double Regression::SumY2() const {
double sum = 0;
for (int i=0; i<_n; i++) {
sum = sum + _y[i]*_y[i];
}
return sum;
}
double Regression::SumXY() const {
double sum = 0;
for (int i=0; i<_n; i++) {
sum = sum + _x[i]*_y[i];
}
return sum;
}
void Regression::ShowData() {
for(int i=0; i<_n; i++) {
cout<<"Point"<<i<<": "<<_x[i]<<" "<<_y[i]<<endl;
}
}
void Regression::Evaluate() {
double sumx, sumy;
double sumx2, sumy2;
double sumxy;
sumx = SumX();
sumx2 = SumX2();
sumy = SumY();
sumy2 = SumY2();
sumxy = SumXY();
_b = (_n*sumxy-sumx*sumy)/(_n*sumx2-sumx*sumx);
_a = (sumy-_b*sumx)/_n;
}
double Regression::Calculate(double x) {
return _a+_b*x;
}