-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCVM-TS-SD-EWMA.cpp
More file actions
162 lines (122 loc) · 3.44 KB
/
CVM-TS-SD-EWMA.cpp
File metadata and controls
162 lines (122 loc) · 3.44 KB
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
#include<iostream>
#include<vector>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
# define BETA 0.1
# define lamda 0.08
# define zeta 0.01
#define L 3
#define m 20
struct CSVRow {
int column1;
int column2;
int column3;
};
// 55 90 170
CSVRow parseCSV(const string& line) {
CSVRow row;
stringstream ss(line);
char comma; // to read the comma
ss >> row.column1 >> comma >> row.column2 >> comma >> row.column3;
return row;
}
double calculatecvmstatistics(const vector<double>& fx) {
int n = fx.size();
double cvmstat = 0.0;
for (int i = 0; i < n; ++i) {
double x = fx[i];
double y = static_cast<double>(i + 1) / n;
cvmstat += pow(x - y, 2);
}
cvmstat /= n;
return cvmstat;
}
int main()
{
string filename = "data.csv";
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error opening file: " << filename << endl;
return 1;
}
double time[200]={0};
double heartbeat[200]={0};
double num[200]={0};
string line;
int p=0;
while (getline(file, line)) {
CSVRow row = parseCSV(line);
num[p]=row.column1;
time[p]=row.column2;
heartbeat[p]=row.column3;
p++;
}
file.close();
// training phase for 50
double mean=0;
for(int i=0;i<5;i++)
{
mean=heartbeat[i]+mean;
}
mean=mean/5;
double z[200]={0};
z[0]=mean;
double error[200]={0};
for(int i=1;i<=5;i++)
{
z[i]=lamda*heartbeat[i]+(1-lamda)*z[i-1];
error[i]=heartbeat[i]-z[i-1];
}
double variance=0;
for(int i=1;i<5;i++)
{
variance=variance+(error[i]*error[i])/5;
}
double var[200]={0};
var[5]=variance;
double ucl[200]={0};
double lcl[200]={0};
for(int i=6;i<200;i++)
{
z[i]=lamda*heartbeat[i]+(1-lamda)*z[i-1];
error[i]=abs(heartbeat[i]-z[i-1]);
var[i]=abs(zeta*error[i]*error[i]+(1-zeta)*var[i-1]);
ucl[i]=z[i-1]+L*abs((abs(var[i-1])));
lcl[i]=z[i-1]-L*abs((abs(var[i-1])));
if(heartbeat[i]>lcl[i] && heartbeat[i]<ucl[i])
{
continue;
}
else{
vector<double> f1;
vector<double> f2;
for(int j=i-(m-1);j<i;j++)
{
f1.push_back(heartbeat[j]);
}
for(int j=i+1;j<i+m;j++)
{
f2.push_back(heartbeat[j]);
}
sort(f1.begin(),f1.end());
sort(f2.begin(),f2.end());
double f1ans= calculatecvmstatistics(f1);
double f2ans=calculatecvmstatistics(f2);
double diff=abs(f1ans-f2ans);
cout<<f1ans<<" "<<f2ans<<" "<<diff<<" "<<f1ans*0.05<<" "<<f2ans*0.05<<endl;
if(diff<0.05*f1ans || diff<0.05*f2ans){
cout<<"covariate shift is found at time:"<<time[i]<<endl;
}
else{
cout<<"covariate detected and rejected at time:"<<time[i]<<endl;
}
}
}
for(int i=0;i<200;i++)
{
cout<<time[i]<<" "<<heartbeat[i]<<" "<<error[i]<<" "<<var[i]<<" "<<z[i]<<" "<<ucl[i]<<" "<<lcl[i]<<" "<<endl;
}
}