-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmf.cpp
376 lines (315 loc) · 9.35 KB
/
pmf.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include "pmf.hpp"
#include <string.h>
#include <math.h>
#include <cmath>
using namespace std;
namespace PrositAux {
int distr::load(const string &filename) throw(Exc) {
ifstream myfile(filename.c_str());
string line;
stringstream sstr;
int size;
long double d;
long double i;
if (!myfile.is_open())
EXC_PRINT_2("unable to open file", filename);
getline(myfile, line);
sstr << line;
size = 0;
while (!sstr.eof()) {
if (!(sstr >> d))
EXC_PRINT("Incorrect file format 1");
size++;
}
if (size > 2)
EXC_PRINT_2("format unknown for file", filename);
sstr.clear();
sstr << line;
int j = 1;
if (!(sstr >> i))
throw Exc("Incorrect file format 2");
if (!(sstr >> d))
throw Exc("Incorrect file format 3");
set(i, d);
while (myfile.good()) {
getline(myfile, line);
if (line.find_first_not_of(' ') == std::string::npos)
break;
sstr.clear();
sstr << line;
if (!(sstr >> i))
EXC_PRINT_2("Incorrect format for file", filename);
if (!(sstr >> d))
EXC_PRINT_2("Incorrect format for file", filename);
set(i, d);
j++;
}
myfile.close();
return j;
}
void distr::print() const {
for (double i = get_min(); i <= get_max(); i++) {
cout << i << ": " << get(i) << endl;
}
}
void distr::dump(const char * filename){
ofstream dumpFile;
dumpFile.open(filename);
if(dumpFile.is_open()){
for (double i = get_min(); i <= get_max(); i++) {
dumpFile << i << " " << get(i) << '\n';
}
dumpFile.close();
} else {
cout << "Unable to open file " << dumpFile << endl;
}
}
double pmf::avg() const {
unsigned int i;
double avg = 0;
for (i = 0; i < size; i++) {
avg += ((i - offset) * elems(i));
}
return avg;
}
double pmf::std() const {
double var = pmf::var();
return sqrt(var);
}
double pmf::var() const {
unsigned int i;
double var = 0;
double avg = pmf::avg();
for (i = 0; i < size; i++) {
var += (pow(((i - offset) - avg), 2) * elems(i));
};
return var;
}
pmf::pmf(unsigned int sz, unsigned int offs, double epsilon)
: distr(sz, offs, epsilon), tail(0.0) {
elems.setZero();
return;
}
int pmf::set(int val, double p) throw(Exc) {
if (val > (int)size - (int)offset) {
EXC_PRINT("access out of range while setting");
}
// if equal??
if (p > 1e-10) {
if (val > max) {
max = val;
// cerr<<"setting max to"<<max<<endl;
}
if (val < min) {
min = val;
// cerr<<"setting min to"<<min<<endl;
}
}
elems(val + offset) = p;
// cerr<<"SET: Min: "<<get_min()<<", Max: "<<get_max()<<endl;
return 1;
}
double pmf::get(int el) const throw(Exc) {
if (el + offset > size - 1){
EXC_PRINT("access out of range while getting");
} else {
return elems(el + offset);
}
}
int pmf::set_samples(int samples) {
unsigned int i;
if (tail > 0) {
return -1;
}
tail = 1.0 / samples; // epsilon = 1/samples
for (i = 0; i < size; i++) {
if (elems(i) > epsilon) {
elems(i) = elems(i) * samples / (samples + 1);
}
}
return 1;
}
double pmf::sum() const {
double p;
p = tail;
p += elems.sum();
return p;
}
pmf::ERR_CODES pmf::check() const {
double p = sum();
for (int i = get_min(); i <= get_max(); i++) {
if (get(i) < -epsilon)
return PMF_NEG;
if (p < 1.0 - epsilon) {
cerr << "warning: pmf::check, sum:" << p << endl;
return PMF_SMALLER_ONE;
}
if (p > 1.0 + epsilon) {
cerr << "warning: pmf::check, sum:" << p << endl;
return PMF_GREATER_ONE;
}
}
return PMF_OK;
}
int pmf::load(const string &filename) throw(Exc) {
int j = distr::load(filename);
ERR_CODES e = check();
switch (e) {
case PMF_NEG:
EXC_PRINT_2("ill formed pmf: negative value, file", filename);
break;
case PMF_SMALLER_ONE:
EXC_PRINT_2("ill formed pmf: sum smaller than one, file", filename);
break;
case PMF_GREATER_ONE:
EXC_PRINT_2("ill formed pmf: sum greater than one, file", filename);
break;
default:
;
};
return j;
}
pmf *pmf::resample(int q, bool growing) const {
double sum = 0; //were we accumulate the probability
int low_lim; //lower bound (conservative)
int upper_lim; //upper bound (conservative)
pmf *resampledPmf = new pmf(this->get_size(), this->get_offset() / q);
//calculate conservative bounds for the two different cases
if(growing){ //computation time approximated to the greater value
low_lim = ceil(this->get_min() / q) * q;
upper_lim = ceil(this->get_max() / q) * q;
} else { //interarrival time approximated to the lower value
low_lim = floor(this->get_min() / q) * q;
upper_lim = floor(this->get_min() / q) * q;
}
//for (int i = get_min(); i <= (get_max() / q) * q + q; i++) {
for (int i = low_lim; i <= upper_lim; i++) {
sum += get(i);
if (i % q == 0) {
resampledPmf->set(i / q, sum);
sum = 0;
}
}
return resampledPmf;
}
void pmf2cdf(const pmf &p, cdf &c) {
double sum = 0.0;
c.elems = p.elems;
c.elems.setZero();
c.size = p.size;
c.offset = p.offset;
c.min = c.size - c.offset;
c.max = -c.offset;
for (int i = p.get_min(); i <= p.get_max(); i++) {
sum += p.get(i);
c.set(i, sum);
}
if (c.get(c.get_max()) < 1.0 - c.get_epsilon()) {
cerr << "PMF2CDF warning: pdf does not sum to 1" << endl;
cerr << "Sum is " << c.get(c.get_max()) << endl;
c.set(c.get_max(), 1.0);
}
c.check();
return;
}
void cdf2pmf(const cdf &c, pmf &p) {
double sum = 0;
p.elems = c.elems;
p.elems.setZero();
p.size = c.size;
p.offset = c.offset;
p.min = p.size - p.offset;
p.max = -p.offset;
for (int i = c.get_min(); i <= c.get_max(); i++) {
p.set(i, c.get(i) - sum);
sum += c.get(i);
}
return;
}
unique_ptr<PrositAux::beta> beta::create_beta_computation(XMLElement *e) throw (Exc) {
XMLElement * betaElement = e->FirstChildElement("pmfComputation");
const char * type;
if(!(type = betaElement->Attribute("type")))
EXC_PRINT("Type for pmf computation not specified");
if(strcmp(type, "beta") != 0) //if the type specified is not "beta", then die
EXC_PRINT("Specified type is different from beta");
//Parse parameters needed to create the distribution
XMLElement * internal;
if(!(internal = betaElement->FirstChildElement("a")))
EXC_PRINT("a parameter missing");
internal->QueryDoubleText(&a); //set alpha
if(!(internal = betaElement->FirstChildElement("b")))
EXC_PRINT("b parameter missing");
internal->QueryDoubleText(&b); //set beta
if(!(internal = betaElement->FirstChildElement("cmin")))
EXC_PRINT("cmin missing");
internal->QueryIntText(&b_min); //set min
if(!(internal = betaElement->FirstChildElement("cmax")))
EXC_PRINT("cmax missing");
internal->QueryIntText(&b_max); //set max
if(!(internal = betaElement->FirstChildElement("step")))
EXC_PRINT("step missing");
internal->QueryIntText(&b_step); //set step
if(!(internal = betaElement->FirstChildElement("size")))
EXC_PRINT("size missing");
internal->QueryIntText(&b_size); //set size
// Initialization of the distribution function
unique_ptr<PrositAux::beta> x = unique_ptr<PrositAux::beta>(new beta(a, b, b_min, b_max, b_step, b_size));
double total_prob = 0.0;
for(int i = b_min; i <= b_max; i += b_step){
double p = pow(double(i-b_min)/double(b_max-b_min), a-1) *
pow(1-(double(i-b_min)/double(b_max-b_min)), b-1);
total_prob += p;
x->set(i, p);
}
if(total_prob <= 1){
for(int i = b_min; i <= b_max; i += b_step){
x->set(i, x->get(i)/total_prob);
}
}
return std::move(x);
}
unique_ptr<PrositAux::beta> beta::create_beta_interarrival(XMLElement *e) throw (Exc) {
XMLElement * betaElement = e->FirstChildElement("pmfInterarrival");
const char * type;
if(!(type = betaElement->Attribute("type")))
EXC_PRINT("Type for pmf interarrival not specified");
if(strcmp(type, "beta") != 0) //if the type specified is not "beta", then die
EXC_PRINT("Specified type is different from beta");
//Parse parameters needed to create the distribution
XMLElement * internal;
if(!(internal = betaElement->FirstChildElement("a")))
EXC_PRINT("a parameter missing");
internal->QueryDoubleText(&a); //set alpha
if(!(internal = betaElement->FirstChildElement("b")))
EXC_PRINT("b parameter missing");
internal->QueryDoubleText(&b); //set beta
if(!(internal = betaElement->FirstChildElement("cmin")))
EXC_PRINT("cmin missing");
internal->QueryIntText(&b_min); //set min
if(!(internal = betaElement->FirstChildElement("cmax")))
EXC_PRINT("cmax missing");
internal->QueryIntText(&b_max); //set max
if(!(internal = betaElement->FirstChildElement("step")))
EXC_PRINT("step missing");
internal->QueryIntText(&b_step); //set step
if(!(internal = betaElement->FirstChildElement("size")))
EXC_PRINT("size missing");
internal->QueryIntText(&b_size); //set size
// Initialization of the distribution function
unique_ptr<PrositAux::beta> x = unique_ptr<PrositAux::beta>(new beta(a, b, b_min, b_max, b_step, b_size));
double total_prob = 0.0;
for(int i = b_min; i <= b_max; i += b_step){
double p = pow(double(i-b_min)/double(b_max-b_min), a-1) *
pow(1-(double(i-b_min)/double(b_max-b_min)), b-1);
total_prob += p;
x->set(i, p);
}
if(total_prob <= 1){
for(int i = b_min; i <= b_max; i += b_step){
x->set(i, x->get(i)/total_prob);
}
}
return std::move(x);
}
}