-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggr.h
205 lines (171 loc) · 5.55 KB
/
aggr.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
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
/* Copyright (C) 2012,2013 Krzysztof Stachowiak */
/*
* This file is part of stat-toolkit.
*
* stat-toolkit is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* stat-toolkit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with stat-toolkit; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AGGR_H
#define AGGR_H
#include <string>
using std::string;
#include <sstream>
using std::stringstream;
#include <memory>
using std::unique_ptr;
#include <limits>
using std::numeric_limits;
#include <boost/math/distributions/normal.hpp>
using boost::math::normal;
#include <boost/xpressive/xpressive.hpp>
using boost::xpressive::sregex;
using boost::xpressive::smatch;
using boost::xpressive::s1;
using boost::xpressive::_d;
using boost::xpressive::_s;
namespace aggr {
// The class defines an interface for the objects that aggregate streams
// of numbers. The way to use an aggregator is to first feed it with
// a serie of numbers and then query it for the according aggregation.
// Aggregators are supposed to be queried multiple times after any number
// of the insertions of the input numbers.
class aggregator {
public:
virtual ~aggregator() {}
// Add a value to the distribution.
virtual void put(double value) = 0;
// Gets the aggregated value.
virtual double get() const = 0;
};
// Helper typedef.
typedef unique_ptr<aggregator> ptr;
// Count of the elements put so far.
class count : public aggregator {
int _count;
public:
count() : _count(0) {}
void put(double) { ++_count; }
double get() const { return double(_count); }
};
// The minimum from the elements put so far.
class min : public aggregator {
double _min;
public:
min() : _min(numeric_limits<double>::infinity()) {}
void put(double value) { if(value < _min) _min = value; }
double get() const { return _min; }
};
// The maximum from the elements put so far.
class max : public aggregator {
double _max;
public:
max() : _max(-numeric_limits<double>::infinity()) {}
void put(double value) { if(value > _max) _max = value; }
double get() const { return _max; }
};
// Sums the numbers that have been put into it so far.
class sum : public aggregator {
double _sum;
public:
sum() : _sum(0) {}
void put(double value) { _sum += value; }
double get() const { return _sum; }
};
// Computs the mean of the values that have been put into it.
// Note that it depends on two other aggregators : sum and count.
class mean : public aggregator {
sum _sum;
count _count;
public:
void put(double value) {
_sum.put(value);
_count.put(value);
}
double get() const {
return _sum.get() / _count.get();
}
};
// Computes the standard deviation of the population
// based on the input data interpreted as the sample.
// The implementation is based on the algorithm for the
// running standard deviation from the Wikipedia.
class stdev : public aggregator {
double _a;
double _q;
double _k;
public:
stdev() : _a(0), _q(0), _k(0) {}
void put(double value) {
double new_a = _a + (value - _a) / (_k + 1);
double new_q = _q + (value - _a) * (value - new_a);
_a = new_a;
_q = new_q;
_k += 1.0;
}
double get() const {
return sqrt(_q / (_k - 1));
}
};
// Computes the gaussian confidence interval of the values that are
// put into it. Note that it depends on the boost statistical helpers.
class ci_gauss : public aggregator {
double _alpha;
count _count;
mean _mean;
stdev _stdev;
public:
ci_gauss(double alpha) : _alpha(alpha) {}
void put(double value) {
_count.put(value);
_mean.put(value);
_stdev.put(value);
}
double get() const {
normal dist(_mean.get(), _stdev.get() / sqrt(_count.get()));
double lower_p = (1.0 - _alpha) * 0.5;
double upper_p = lower_p + _alpha;
double lower = quantile(dist, lower_p);
double upper = quantile(dist, upper_p);
return upper - lower;
}
};
// The function takes a so called constructor string as an argument,
// and constructs an according aggregator implementation.
ptr create_from_string(const string& str) {
// Simple, no argument cases.
// --------------------------
if(str == "count") return unique_ptr<aggregator>(new count);
if(str == "min") return unique_ptr<aggregator>(new min);
if(str == "max") return unique_ptr<aggregator>(new max);
if(str == "sum") return unique_ptr<aggregator>(new sum);
if(str == "mean") return unique_ptr<aggregator>(new mean);
if(str == "stdev") return unique_ptr<aggregator>(new stdev);
// Cases that require parsing.
// ---------------------------
// Normal distribution based confidence interval aggregator.
sregex cig_re = "ci_gauss" >> +_s >> (s1 = +_d >> '.' >> +_d);
smatch match;
if(regex_match(str, match, cig_re)) {
double conf_lvl;
stringstream ss;
ss << match[1];
ss >> conf_lvl;
return unique_ptr<aggregator>(new ci_gauss(conf_lvl));
}
// No case satisfied. Abort.
// -------------------------
throw string("Failed recognizing aggregator in : \"" + str + "\".");
}
}
#endif