-
Notifications
You must be signed in to change notification settings - Fork 1
/
CExpectation.cpp
166 lines (142 loc) · 3.23 KB
/
CExpectation.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
#include <iostream>
#include <fstream>
#include <ctime>
#include "CExpectation.h"
CExpectation::CExpectation(int idd) :
positive(idd),
negative(idd)
{
id=idd;
firstTime = -1;
lastTime = -1;
measurements = 0;
maxPeriod = 0;
numElements = 0;
type = TT_EXPECTATION;
numSamples = 0;
positives = 0;
negatives = 0;
srandom(time(0));
}
void CExpectation::init(int iMaxPeriod, int elements, int numClasses)
{
maxPeriod = iMaxPeriod;
numElements = 1;
}
CExpectation::~CExpectation()
{
}
CExpectation::TimeSample::TimeSample() :
t(),
v()
{
}
CExpectation::TimeSample::TimeSample(long t_, float v_) :
t(t_),
v(v_)
{
}
// adds new state observations at given times
int CExpectation::add(uint32_t time, float state)
{
sampleArray[numSamples] = TimeSample(time, state);
numSamples++;
if (state > 0.5) {
positive.add_time(time, 1);
positives++;
} else {
negative.add_time(time, 1);
negatives++;
}
return 0;
}
/*not required in incremental version*/
void CExpectation::update(int modelOrder, unsigned int* times, float* signal, int length)
{
positive.train();
negative.train();
ofstream myfile0("0.txt");
ofstream myfile1("1.txt");
//ofstream myfile2("2.txt");
for (int i = 0; i < numSamples; ++i) {
myfile0 << sampleArray[i].v << std::endl;
myfile1 << estimate(sampleArray[i].t) << std::endl;
//myfile1 << positive.get_density_at(sampleArray[i].t)/negatives << std::endl;
//myfile2 << negative.get_density_at(sampleArray[i].t)/positives*100 << std::endl;
}
myfile0.close();
myfile1.close();
//myfile2.close();
}
/*text representation of the fremen model*/
void CExpectation::print(bool verbose)
{
std::cout << "Positive:";
positive.print();
std::cout << std::endl << "Negative:";
negative.print();
std::cout << std::endl;
}
float CExpectation::estimate(uint32_t time)
{
double pd = positive.get_density_at(time) * positives;
double nd = negative.get_density_at(time) * negatives;
return pd / (pd + nd);
return pd;
}
float CExpectation::predict(uint32_t time)
{
return estimate(time);
}
int CExpectation::save(const char* name, bool lossy)
{
FILE* file = fopen(name,"w");
save(file);
fclose(file);
return 0;
}
int CExpectation::load(const char* name)
{
FILE* file = fopen(name,"r");
load(file);
fclose(file);
return 0;
}
int CExpectation::save(FILE* file, bool lossy)
{
positive.save(file, lossy);
negative.save(file, lossy);
fwrite(&positives, sizeof(int), 1, file);
fwrite(&negatives, sizeof(int), 1, file);
return 0;
}
int CExpectation::load(FILE* file)
{
positive.load(file);
negative.load(file);
fread(&positives, sizeof(int), 1, file);
fread(&negatives, sizeof(int), 1, file);
return 0;
}
int CExpectation::exportToArray(double* array,int maxLen)
{
int pos = 0;
array[pos++] = type;
positive.exportToArray(array, maxLen, pos);
negative.exportToArray(array, maxLen, pos);
array[pos++] = positives;
array[pos++] = negatives;
return pos;
}
int CExpectation::importFromArray(double* array,int len)
{
int pos = 0;
type = (ETemporalType)array[pos++];
if (type != TT_MEAN) std::cerr << "Error loading the model, type mismatch." << std::endl;
positive.importFromArray(array, len, pos);
negative.importFromArray(array, len, pos);
positives = array[pos++];
negatives = array[pos++];
update(0);
return pos;
}