abseil-cpp/absl/random/zipf_distribution.h
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ABSL_RANDOM_ZIPF_DISTRIBUTION_H_
16 #define ABSL_RANDOM_ZIPF_DISTRIBUTION_H_
17 
18 #include <cassert>
19 #include <cmath>
20 #include <istream>
21 #include <limits>
22 #include <ostream>
23 #include <type_traits>
24 
25 #include "absl/random/internal/iostream_state_saver.h"
26 #include "absl/random/internal/traits.h"
27 #include "absl/random/uniform_real_distribution.h"
28 
29 namespace absl {
31 
32 // absl::zipf_distribution produces random integer-values in the range [0, k],
33 // distributed according to the unnormalized discrete probability function:
34 //
35 // P(x) = (v + x) ^ -q
36 //
37 // The parameter `v` must be greater than 0 and the parameter `q` must be
38 // greater than 1. If either of these parameters take invalid values then the
39 // behavior is undefined.
40 //
41 // IntType is the result_type generated by the generator. It must be of integral
42 // type; a static_assert ensures this is the case.
43 //
44 // The implementation is based on W.Hormann, G.Derflinger:
45 //
46 // "Rejection-Inversion to Generate Variates from Monotone Discrete
47 // Distributions"
48 //
49 // http://eeyore.wu-wien.ac.at/papers/96-04-04.wh-der.ps.gz
50 //
51 template <typename IntType = int>
53  public:
54  using result_type = IntType;
55 
56  class param_type {
57  public:
59 
60  // Preconditions: k > 0, v > 0, q > 1
61  // The precondidtions are validated when NDEBUG is not defined via
62  // a pair of assert() directives.
63  // If NDEBUG is defined and either or both of these parameters take invalid
64  // values, the behavior of the class is undefined.
66  double q = 2.0, double v = 1.0);
67 
68  result_type k() const { return k_; }
69  double q() const { return q_; }
70  double v() const { return v_; }
71 
72  friend bool operator==(const param_type& a, const param_type& b) {
73  return a.k_ == b.k_ && a.q_ == b.q_ && a.v_ == b.v_;
74  }
75  friend bool operator!=(const param_type& a, const param_type& b) {
76  return !(a == b);
77  }
78 
79  private:
80  friend class zipf_distribution;
81  inline double h(double x) const;
82  inline double hinv(double x) const;
83  inline double compute_s() const;
84  inline double pow_negative_q(double x) const;
85 
86  // Parameters here are exactly the same as the parameters of Algorithm ZRI
87  // in the paper.
88  IntType k_;
89  double q_;
90  double v_;
91 
92  double one_minus_q_; // 1-q
93  double s_;
94  double one_minus_q_inv_; // 1 / 1-q
95  double hxm_; // h(k + 0.5)
96  double hx0_minus_hxm_; // h(x0) - h(k + 0.5)
97 
99  "Class-template absl::zipf_distribution<> must be "
100  "parameterized using an integral type.");
101  };
102 
104  : zipf_distribution((std::numeric_limits<IntType>::max)()) {}
105 
106  explicit zipf_distribution(result_type k, double q = 2.0, double v = 1.0)
107  : param_(k, q, v) {}
108 
109  explicit zipf_distribution(const param_type& p) : param_(p) {}
110 
111  void reset() {}
112 
113  template <typename URBG>
114  result_type operator()(URBG& g) { // NOLINT(runtime/references)
115  return (*this)(g, param_);
116  }
117 
118  template <typename URBG>
119  result_type operator()(URBG& g, // NOLINT(runtime/references)
120  const param_type& p);
121 
122  result_type k() const { return param_.k(); }
123  double q() const { return param_.q(); }
124  double v() const { return param_.v(); }
125 
126  param_type param() const { return param_; }
127  void param(const param_type& p) { param_ = p; }
128 
129  result_type(min)() const { return 0; }
130  result_type(max)() const { return k(); }
131 
132  friend bool operator==(const zipf_distribution& a,
133  const zipf_distribution& b) {
134  return a.param_ == b.param_;
135  }
136  friend bool operator!=(const zipf_distribution& a,
137  const zipf_distribution& b) {
138  return a.param_ != b.param_;
139  }
140 
141  private:
143 };
144 
145 // --------------------------------------------------------------------------
146 // Implementation details follow
147 // --------------------------------------------------------------------------
148 
149 template <typename IntType>
151  typename zipf_distribution<IntType>::result_type k, double q, double v)
152  : k_(k), q_(q), v_(v), one_minus_q_(1 - q) {
153  assert(q > 1);
154  assert(v > 0);
155  assert(k > 0);
156  one_minus_q_inv_ = 1 / one_minus_q_;
157 
158  // Setup for the ZRI algorithm (pg 17 of the paper).
159  // Compute: h(i max) => h(k + 0.5)
160  constexpr double kMax = 18446744073709549568.0;
161  double kd = static_cast<double>(k);
162  // TODO(absl-team): Determine if this check is needed, and if so, add a test
163  // that fails for k > kMax
164  if (kd > kMax) {
165  // Ensure that our maximum value is capped to a value which will
166  // round-trip back through double.
167  kd = kMax;
168  }
169  hxm_ = h(kd + 0.5);
170 
171  // Compute: h(0)
172  const bool use_precomputed = (v == 1.0 && q == 2.0);
173  const double h0x5 = use_precomputed ? (-1.0 / 1.5) // exp(-log(1.5))
174  : h(0.5);
175  const double elogv_q = (v_ == 1.0) ? 1 : pow_negative_q(v_);
176 
177  // h(0) = h(0.5) - exp(log(v) * -q)
178  hx0_minus_hxm_ = (h0x5 - elogv_q) - hxm_;
179 
180  // And s
181  s_ = use_precomputed ? 0.46153846153846123 : compute_s();
182 }
183 
184 template <typename IntType>
186  // std::exp(one_minus_q_ * std::log(v_ + x)) * one_minus_q_inv_;
187  x += v_;
188  return (one_minus_q_ == -1.0)
189  ? (-1.0 / x) // -exp(-log(x))
190  : (std::exp(std::log(x) * one_minus_q_) * one_minus_q_inv_);
191 }
192 
193 template <typename IntType>
195  // std::exp(one_minus_q_inv_ * std::log(one_minus_q_ * x)) - v_;
196  return -v_ + ((one_minus_q_ == -1.0)
197  ? (-1.0 / x) // exp(-log(-x))
198  : std::exp(one_minus_q_inv_ * std::log(one_minus_q_ * x)));
199 }
200 
201 template <typename IntType>
203  // 1 - hinv(h(1.5) - std::exp(std::log(v_ + 1) * -q_));
204  return 1.0 - hinv(h(1.5) - pow_negative_q(v_ + 1.0));
205 }
206 
207 template <typename IntType>
209  // std::exp(std::log(x) * -q_);
210  return q_ == 2.0 ? (1.0 / (x * x)) : std::exp(std::log(x) * -q_);
211 }
212 
213 template <typename IntType>
214 template <typename URBG>
217  URBG& g, const param_type& p) { // NOLINT(runtime/references)
219  double k;
220  for (;;) {
221  const double v = uniform_double(g);
222  const double u = p.hxm_ + v * p.hx0_minus_hxm_;
223  const double x = p.hinv(u);
224  k = rint(x); // std::floor(x + 0.5);
225  if (k > static_cast<double>(p.k())) continue; // reject k > max_k
226  if (k - x <= p.s_) break;
227  const double h = p.h(k + 0.5);
228  const double r = p.pow_negative_q(p.v_ + k);
229  if (u >= h - r) break;
230  }
231  IntType ki = static_cast<IntType>(k);
232  assert(ki <= p.k_);
233  return ki;
234 }
235 
236 template <typename CharT, typename Traits, typename IntType>
237 std::basic_ostream<CharT, Traits>& operator<<(
238  std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
239  const zipf_distribution<IntType>& x) {
240  using stream_type =
244  os << static_cast<stream_type>(x.k()) << os.fill() << x.q() << os.fill()
245  << x.v();
246  return os;
247 }
248 
249 template <typename CharT, typename Traits, typename IntType>
250 std::basic_istream<CharT, Traits>& operator>>(
251  std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
252  zipf_distribution<IntType>& x) { // NOLINT(runtime/references)
254  using param_type = typename zipf_distribution<IntType>::param_type;
255  using stream_type =
257  stream_type k;
258  double q;
259  double v;
260 
262  is >> k >> q >> v;
263  if (!is.fail()) {
264  x.param(param_type(static_cast<result_type>(k), q, v));
265  }
266  return is;
267 }
268 
270 } // namespace absl
271 
272 #endif // ABSL_RANDOM_ZIPF_DISTRIBUTION_H_
absl::zipf_distribution::q
double q() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:123
absl::zipf_distribution::param_type::one_minus_q_
double one_minus_q_
Definition: abseil-cpp/absl/random/zipf_distribution.h:92
absl::zipf_distribution::param_type::s_
double s_
Definition: abseil-cpp/absl/random/zipf_distribution.h:93
absl::zipf_distribution::param_type::zipf_distribution
friend class zipf_distribution
Definition: abseil-cpp/absl/random/zipf_distribution.h:80
absl::zipf_distribution::operator!=
friend bool operator!=(const zipf_distribution &a, const zipf_distribution &b)
Definition: abseil-cpp/absl/random/zipf_distribution.h:136
absl::zipf_distribution::param_type::k
result_type k() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:68
absl::zipf_distribution::param_type::v
double v() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:70
absl::random_internal::make_istream_state_saver
istream_state_saver< std::basic_istream< CharT, Traits > > make_istream_state_saver(std::basic_istream< CharT, Traits > &is, std::ios_base::fmtflags flags=std::ios_base::dec|std::ios_base::scientific|std::ios_base::skipws)
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:149
absl::zipf_distribution::reset
void reset()
Definition: abseil-cpp/absl/random/zipf_distribution.h:111
absl::operator<<
ABSL_NAMESPACE_BEGIN std::ostream & operator<<(std::ostream &os, absl::LogSeverity s)
Definition: abseil-cpp/absl/base/log_severity.cc:24
absl::zipf_distribution::param_
param_type param_
Definition: abseil-cpp/absl/random/zipf_distribution.h:142
absl::zipf_distribution::operator==
friend bool operator==(const zipf_distribution &a, const zipf_distribution &b)
Definition: abseil-cpp/absl/random/zipf_distribution.h:132
absl::zipf_distribution::param_type
Definition: abseil-cpp/absl/random/zipf_distribution.h:56
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
absl::zipf_distribution::param_type::k_
IntType k_
Definition: abseil-cpp/absl/random/zipf_distribution.h:88
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
xds_manager.p
p
Definition: xds_manager.py:60
absl::random_internal::IsIntegral
Definition: abseil-cpp/absl/random/internal/traits.h:65
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::zipf_distribution::k
result_type k() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:122
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
absl::zipf_distribution::param_type::q_
double q_
Definition: abseil-cpp/absl/random/zipf_distribution.h:89
absl::zipf_distribution::param_type::operator==
friend bool operator==(const param_type &a, const param_type &b)
Definition: abseil-cpp/absl/random/zipf_distribution.h:72
absl::zipf_distribution::param_type::v_
double v_
Definition: abseil-cpp/absl/random/zipf_distribution.h:90
absl::random_internal::stream_format_type
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:174
absl::zipf_distribution::param_type::hx0_minus_hxm_
double hx0_minus_hxm_
Definition: abseil-cpp/absl/random/zipf_distribution.h:96
absl::zipf_distribution::param_type::hinv
double hinv(double x) const
Definition: abseil-cpp/absl/random/zipf_distribution.h:194
absl::zipf_distribution::param
void param(const param_type &p)
Definition: abseil-cpp/absl/random/zipf_distribution.h:127
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::zipf_distribution::param_type::h
double h(double x) const
Definition: abseil-cpp/absl/random/zipf_distribution.h:185
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
absl::zipf_distribution::param_type::operator!=
friend bool operator!=(const param_type &a, const param_type &b)
Definition: abseil-cpp/absl/random/zipf_distribution.h:75
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
absl::uniform_real_distribution
Definition: abseil-cpp/absl/random/internal/uniform_helper.h:33
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
g
struct @717 g
absl::zipf_distribution::max
result_type() max() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:130
absl::zipf_distribution::param_type::one_minus_q_inv_
double one_minus_q_inv_
Definition: abseil-cpp/absl/random/zipf_distribution.h:94
absl::zipf_distribution::param
param_type param() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:126
absl::random_internal::stream_precision_helper
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:107
absl::operator>>
constexpr uint128 operator>>(uint128 lhs, int amount)
Definition: abseil-cpp/absl/numeric/int128.h:917
absl::zipf_distribution::result_type
IntType result_type
Definition: abseil-cpp/absl/random/zipf_distribution.h:54
absl::zipf_distribution::param_type::q
double q() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:69
absl::zipf_distribution::operator()
result_type operator()(URBG &g)
Definition: abseil-cpp/absl/random/zipf_distribution.h:114
absl::zipf_distribution::zipf_distribution
zipf_distribution()
Definition: abseil-cpp/absl/random/zipf_distribution.h:103
fix_build_deps.r
r
Definition: fix_build_deps.py:491
absl::zipf_distribution
Definition: abseil-cpp/absl/random/zipf_distribution.h:52
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
log
bool log
Definition: abseil-cpp/absl/synchronization/mutex.cc:310
k_
int k_
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3890
absl::zipf_distribution::param_type::pow_negative_q
double pow_negative_q(double x) const
Definition: abseil-cpp/absl/random/zipf_distribution.h:208
stream_type
stream_type
Definition: task.h:81
absl::zipf_distribution::param_type::param_type
param_type(result_type k=(std::numeric_limits< IntType >::max)(), double q=2.0, double v=1.0)
absl::zipf_distribution::min
result_type() min() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:129
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::zipf_distribution::param_type::hxm_
double hxm_
Definition: abseil-cpp/absl/random/zipf_distribution.h:95
absl::zipf_distribution::param_type::compute_s
double compute_s() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:202
absl::random_internal::make_ostream_state_saver
ostream_state_saver< std::basic_ostream< CharT, Traits > > make_ostream_state_saver(std::basic_ostream< CharT, Traits > &os, std::ios_base::fmtflags flags=std::ios_base::dec|std::ios_base::left|std::ios_base::scientific)
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:82
absl::zipf_distribution::zipf_distribution
zipf_distribution(const param_type &p)
Definition: abseil-cpp/absl/random/zipf_distribution.h:109
absl::zipf_distribution::v
double v() const
Definition: abseil-cpp/absl/random/zipf_distribution.h:124
absl::zipf_distribution::zipf_distribution
zipf_distribution(result_type k, double q=2.0, double v=1.0)
Definition: abseil-cpp/absl/random/zipf_distribution.h:106
s_
std::string s_
Definition: abseil-cpp/absl/container/btree_test.cc:1031


grpc
Author(s):
autogenerated on Fri May 16 2025 03:01:01