-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab.h
77 lines (69 loc) · 1.69 KB
/
lab.h
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
#pragma once
#include "help.h"
class lab
{
private:
static float avg(vectorNode v)
{
float sum=0;
for(unsigned int i=0;i<v.getl();i++)
sum+=v.v[i];
return sum/v.getl();
}
static float err(vectorNode v, float avg=-1)
{
if(avg==-1)
{
avg=lab::avg(v);
}
float sum=0;
for(unsigned int i=0;i<v.getl();i++)
{
float num=v.v[i]-avg;
sum+=num*num;
}
return sum;
}
public:
static float avg(vector<string>com)
{
//vectorNode v(com.size()-1);
vectorNode v=help::toVectorNode(com);
return avg(v);
}
static float var(vector<string>com)
{
auto v=help::toVectorNode(com);
return err(v)/v.getl();
}
static float r(vectorNode &x, vectorNode &y)
{
float avgx=avg(x);
float avgy=avg(y);
float lxx=err(x,avgx);
float lyy=err(y,avgy);
float lxy=0;
for(unsigned int i=0;i<x.getl();i++)
{
float num1=x.v[i]-avgx;
float num2=y.v[i]-avgy;
lxy+=abs(num1*num2);
}
return lxy/(sqrt(lxx*lyy));
}
static tuple<double,double> LeastSquare(vectorNode &x, vectorNode &y)
{
double t1=0, t2=0, t3=0, t4=0;
for(unsigned int i=0; i<x.getl(); ++i)
{
t1 += x.v[i]*x.v[i];
t2 += x.v[i];
t3 += x.v[i]*y.v[i];
t4 += y.v[i];
}
double a = (t3*x.getl() - t2*t4) / (t1*x.getl() - t2*t2);
//b = (t4 - a*t2) / x.size();
double b = (t1*t4 - t2*t3) / (t1*x.getl() - t2*t2);
return make_tuple(a,b);
}
};