-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
zipf_distribution.h
273 lines (233 loc) · 9 KB
/
zipf_distribution.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
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
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_RANDOM_ZIPF_DISTRIBUTION_H_
#define ABSL_RANDOM_ZIPF_DISTRIBUTION_H_
#include <cassert>
#include <cmath>
#include <istream>
#include <limits>
#include <ostream>
#include <type_traits>
#include "absl/base/config.h"
#include "absl/random/internal/iostream_state_saver.h"
#include "absl/random/internal/traits.h"
#include "absl/random/uniform_real_distribution.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
// absl::zipf_distribution produces random integer-values in the range [0, k],
// distributed according to the unnormalized discrete probability function:
//
// P(x) = (v + x) ^ -q
//
// The parameter `v` must be greater than 0 and the parameter `q` must be
// greater than 1. If either of these parameters take invalid values then the
// behavior is undefined.
//
// IntType is the result_type generated by the generator. It must be of integral
// type; a static_assert ensures this is the case.
//
// The implementation is based on W.Hormann, G.Derflinger:
//
// "Rejection-Inversion to Generate Variates from Monotone Discrete
// Distributions"
//
// http://eeyore.wu-wien.ac.at/papers/96-04-04.wh-der.ps.gz
//
template <typename IntType = int>
class zipf_distribution {
public:
using result_type = IntType;
class param_type {
public:
using distribution_type = zipf_distribution;
// Preconditions: k >= 0, v > 0, q > 1
// The preconditions are validated when NDEBUG is not defined via
// a pair of assert() directives.
// If NDEBUG is defined and either or both of these parameters take invalid
// values, the behavior of the class is undefined.
explicit param_type(result_type k = (std::numeric_limits<IntType>::max)(),
double q = 2.0, double v = 1.0);
result_type k() const { return k_; }
double q() const { return q_; }
double v() const { return v_; }
friend bool operator==(const param_type& a, const param_type& b) {
return a.k_ == b.k_ && a.q_ == b.q_ && a.v_ == b.v_;
}
friend bool operator!=(const param_type& a, const param_type& b) {
return !(a == b);
}
private:
friend class zipf_distribution;
inline double h(double x) const;
inline double hinv(double x) const;
inline double compute_s() const;
inline double pow_negative_q(double x) const;
// Parameters here are exactly the same as the parameters of Algorithm ZRI
// in the paper.
IntType k_;
double q_;
double v_;
double one_minus_q_; // 1-q
double s_;
double one_minus_q_inv_; // 1 / 1-q
double hxm_; // h(k + 0.5)
double hx0_minus_hxm_; // h(x0) - h(k + 0.5)
static_assert(random_internal::IsIntegral<IntType>::value,
"Class-template absl::zipf_distribution<> must be "
"parameterized using an integral type.");
};
zipf_distribution()
: zipf_distribution((std::numeric_limits<IntType>::max)()) {}
explicit zipf_distribution(result_type k, double q = 2.0, double v = 1.0)
: param_(k, q, v) {}
explicit zipf_distribution(const param_type& p) : param_(p) {}
void reset() {}
template <typename URBG>
result_type operator()(URBG& g) { // NOLINT(runtime/references)
return (*this)(g, param_);
}
template <typename URBG>
result_type operator()(URBG& g, // NOLINT(runtime/references)
const param_type& p);
result_type k() const { return param_.k(); }
double q() const { return param_.q(); }
double v() const { return param_.v(); }
param_type param() const { return param_; }
void param(const param_type& p) { param_ = p; }
result_type(min)() const { return 0; }
result_type(max)() const { return k(); }
friend bool operator==(const zipf_distribution& a,
const zipf_distribution& b) {
return a.param_ == b.param_;
}
friend bool operator!=(const zipf_distribution& a,
const zipf_distribution& b) {
return a.param_ != b.param_;
}
private:
param_type param_;
};
// --------------------------------------------------------------------------
// Implementation details follow
// --------------------------------------------------------------------------
template <typename IntType>
zipf_distribution<IntType>::param_type::param_type(
typename zipf_distribution<IntType>::result_type k, double q, double v)
: k_(k), q_(q), v_(v), one_minus_q_(1 - q) {
assert(q > 1);
assert(v > 0);
assert(k >= 0);
one_minus_q_inv_ = 1 / one_minus_q_;
// Setup for the ZRI algorithm (pg 17 of the paper).
// Compute: h(i max) => h(k + 0.5)
constexpr double kMax = 18446744073709549568.0;
double kd = static_cast<double>(k);
// TODO(absl-team): Determine if this check is needed, and if so, add a test
// that fails for k > kMax
if (kd > kMax) {
// Ensure that our maximum value is capped to a value which will
// round-trip back through double.
kd = kMax;
}
hxm_ = h(kd + 0.5);
// Compute: h(0)
const bool use_precomputed = (v == 1.0 && q == 2.0);
const double h0x5 = use_precomputed ? (-1.0 / 1.5) // exp(-log(1.5))
: h(0.5);
const double elogv_q = (v_ == 1.0) ? 1 : pow_negative_q(v_);
// h(0) = h(0.5) - exp(log(v) * -q)
hx0_minus_hxm_ = (h0x5 - elogv_q) - hxm_;
// And s
s_ = use_precomputed ? 0.46153846153846123 : compute_s();
}
template <typename IntType>
double zipf_distribution<IntType>::param_type::h(double x) const {
// std::exp(one_minus_q_ * std::log(v_ + x)) * one_minus_q_inv_;
x += v_;
return (one_minus_q_ == -1.0)
? (-1.0 / x) // -exp(-log(x))
: (std::exp(std::log(x) * one_minus_q_) * one_minus_q_inv_);
}
template <typename IntType>
double zipf_distribution<IntType>::param_type::hinv(double x) const {
// std::exp(one_minus_q_inv_ * std::log(one_minus_q_ * x)) - v_;
return -v_ + ((one_minus_q_ == -1.0)
? (-1.0 / x) // exp(-log(-x))
: std::exp(one_minus_q_inv_ * std::log(one_minus_q_ * x)));
}
template <typename IntType>
double zipf_distribution<IntType>::param_type::compute_s() const {
// 1 - hinv(h(1.5) - std::exp(std::log(v_ + 1) * -q_));
return 1.0 - hinv(h(1.5) - pow_negative_q(v_ + 1.0));
}
template <typename IntType>
double zipf_distribution<IntType>::param_type::pow_negative_q(double x) const {
// std::exp(std::log(x) * -q_);
return q_ == 2.0 ? (1.0 / (x * x)) : std::exp(std::log(x) * -q_);
}
template <typename IntType>
template <typename URBG>
typename zipf_distribution<IntType>::result_type
zipf_distribution<IntType>::operator()(
URBG& g, const param_type& p) { // NOLINT(runtime/references)
absl::uniform_real_distribution<double> uniform_double;
double k;
for (;;) {
const double v = uniform_double(g);
const double u = p.hxm_ + v * p.hx0_minus_hxm_;
const double x = p.hinv(u);
k = rint(x); // std::floor(x + 0.5);
if (k > static_cast<double>(p.k())) continue; // reject k > max_k
if (k - x <= p.s_) break;
const double h = p.h(k + 0.5);
const double r = p.pow_negative_q(p.v_ + k);
if (u >= h - r) break;
}
IntType ki = static_cast<IntType>(k);
assert(ki <= p.k_);
return ki;
}
template <typename CharT, typename Traits, typename IntType>
std::basic_ostream<CharT, Traits>& operator<<(
std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
const zipf_distribution<IntType>& x) {
using stream_type =
typename random_internal::stream_format_type<IntType>::type;
auto saver = random_internal::make_ostream_state_saver(os);
os.precision(random_internal::stream_precision_helper<double>::kPrecision);
os << static_cast<stream_type>(x.k()) << os.fill() << x.q() << os.fill()
<< x.v();
return os;
}
template <typename CharT, typename Traits, typename IntType>
std::basic_istream<CharT, Traits>& operator>>(
std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
zipf_distribution<IntType>& x) { // NOLINT(runtime/references)
using result_type = typename zipf_distribution<IntType>::result_type;
using param_type = typename zipf_distribution<IntType>::param_type;
using stream_type =
typename random_internal::stream_format_type<IntType>::type;
stream_type k;
double q;
double v;
auto saver = random_internal::make_istream_state_saver(is);
is >> k >> q >> v;
if (!is.fail()) {
x.param(param_type(static_cast<result_type>(k), q, v));
}
return is;
}
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_RANDOM_ZIPF_DISTRIBUTION_H_