-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistic.h
More file actions
220 lines (187 loc) · 5.43 KB
/
statistic.h
File metadata and controls
220 lines (187 loc) · 5.43 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
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
#ifndef __STATISTIC_H__
#define __STATISTIC_H__
#include <cmath>
#include <cstdint>
#include <random>
#include <stdexcept>
#include <vector>
template <typename T, typename Iter> T summation(Iter first, Iter last) {
T sum = 0;
for (; first != last; ++first)
sum += *first;
return sum;
}
template <typename T, typename Iter, typename BinaryOperation>
auto summation(Iter first, Iter last, BinaryOperation op) {
T sum = 0;
for (; first != last; ++first)
sum = op(*first, sum);
return sum;
}
template <typename Iter> double average(Iter first, Iter last) {
return summation<double>(first, last) / std::distance(first, last);
}
template <typename Iter, typename BinaryOperation>
double average(Iter first, Iter last, BinaryOperation op) {
return summation<double>(first, last, op) / std::distance(first, last);
}
template <typename Iter> double variance(Iter first, Iter last) {
using T = decltype(*first);
double avg = average(first, last);
return average(first, last, [avg](const T x, const double p) {
double tmp = x - avg;
return p + tmp * tmp;
});
}
template <typename Iter, typename BinaryOperation>
double variance(Iter first, Iter last, BinaryOperation op) {
using T = decltype(*first);
double avg = average(first, last, op);
return average(first, last, [avg, &op](const T x, const double p) {
double tmp = op(x, -avg);
return p + tmp * tmp;
});
}
template <typename Iter> double stddeviation(Iter first, Iter last) {
return std::sqrt(variance(first, last));
}
template <typename Iter, typename BinaryOperation>
double stddeviation(Iter first, Iter last, BinaryOperation op) {
return std::sqrt(variance(first, last, op));
}
/**
* @brief Generates random number according zipfian distribution.
* It is defined as: P(X=k)= C / k^q, 1 <= k <= n
*/
template <typename IntType = int> class zipf_distribution {
public:
typedef IntType result_type;
zipf_distribution(IntType max, double theta)
: m_max(max), m_theta(theta), m_dist(0.0, 1.0) {
m_c = std::pow(m_max, -m_theta) / zeta(m_theta, m_max);
m_q = std::pow(2.0, -m_theta);
m_h = harmonic(m_max);
m_v = m_dist(m_gen);
}
/**
* @brief 返回zipf分布随机数[0, max)
*
* @tparam Generator
* @param g
* @return IntType
*/
template <typename Generator> IntType operator()(Generator &g) {
while (true) {
double u = m_dist(g) - 0.5;
double y = std::floor(std::pow(m_max + 0.5, m_v - u) - 0.5);
if (y < 1 || y > m_max)
continue;
double k = std::floor(y);
m_v = m_dist(g);
if (m_v >= m_q * std::pow(k + 1, m_theta) / (m_h + k))
continue;
return static_cast<IntType>(k) - 1;
}
}
private:
IntType m_max;
double m_theta;
double m_c;
double m_q;
double m_h;
double m_v;
std::mt19937 m_gen;
std::uniform_real_distribution<double> m_dist;
static double zeta(double theta, IntType n) {
double sum = 0.0;
for (IntType i = 1; i <= n; ++i)
sum += std::pow(i, -theta);
return sum;
}
double harmonic(IntType n) const { return m_c * zeta(m_theta, n); }
};
class Histogram {
public:
Histogram(int nr_buckets, double min_value, double max_value)
: nr_buckets(nr_buckets), min_value(min_value),
max_value(max_value),
bucket_width((max_value - min_value) / nr_buckets),
buckets(nr_buckets, 0) {}
~Histogram() = default;
void add(double value) {
if (value < min_value || value > max_value) {
return;
}
int bucket = get_bucket(value);
++buckets[bucket];
}
void clear() { std::fill(buckets.begin(), buckets.end(), 0); }
int size() const {
int total_count = 0;
for (int count : buckets) {
total_count += count;
}
return total_count;
}
int percentile(double p) const {
if (p < 0 || p > 100) {
// Invalid percentile value
return -1;
}
int total_count = size();
int count_so_far = 0;
for (int i = 0; i < nr_buckets; ++i) {
count_so_far += buckets[i];
if (count_so_far / (double)total_count * 100 >= p) {
return i;
}
}
// Should not reach here
return -1;
}
double average() const {
double sum = 0;
for (int i = 0; i < nr_buckets; ++i) {
sum += get_value(i) * get_count(i);
}
return sum / size();
}
friend Histogram merge(Histogram &a, Histogram &b) {
Histogram new_histogram(std::max(a.nr_buckets, b.nr_buckets),
std::min(a.min_value, b.max_value),
std::max(a.max_value, b.max_value));
for (int i = 0; i < a.nr_buckets; ++i) {
new_histogram.buckets[new_histogram.get_bucket(a.get_value(i))] +=
a.get_count(i);
}
for (int i = 0; i < b.nr_buckets; ++i) {
new_histogram.buckets[new_histogram.get_bucket(b.get_value(i))] +=
b.get_count(i);
}
return new_histogram;
}
private:
int get_bucket(double value) const {
return (value - min_value) / bucket_width;
}
double get_value(int bucket) const {
if (bucket < 0 || bucket >= nr_buckets) {
// Invalid bucket index
return -1;
}
return min_value + bucket_width * bucket;
}
int get_count(int bucket) const {
if (bucket < 0 || bucket >= nr_buckets) {
// Invalid bucket index
return -1;
}
return buckets[bucket];
}
const int nr_buckets;
const double min_value;
const double max_value;
const double bucket_width;
std::vector<int> buckets;
};
#endif // __STATISTIC_H__