zipf_distribution.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #ifndef ABSL_RANDOM_ZIPF_DISTRIBUTION_H_
  15. #define ABSL_RANDOM_ZIPF_DISTRIBUTION_H_
  16. #include <cassert>
  17. #include <cmath>
  18. #include <istream>
  19. #include <limits>
  20. #include <ostream>
  21. #include <type_traits>
  22. #include "absl/base/config.h"
  23. #include "absl/random/internal/iostream_state_saver.h"
  24. #include "absl/random/internal/traits.h"
  25. #include "absl/random/uniform_real_distribution.h"
  26. namespace absl {
  27. ABSL_NAMESPACE_BEGIN
  28. // absl::zipf_distribution produces random integer-values in the range [0, k],
  29. // distributed according to the unnormalized discrete probability function:
  30. //
  31. // P(x) = (v + x) ^ -q
  32. //
  33. // The parameter `v` must be greater than 0 and the parameter `q` must be
  34. // greater than 1. If either of these parameters take invalid values then the
  35. // behavior is undefined.
  36. //
  37. // IntType is the result_type generated by the generator. It must be of integral
  38. // type; a static_assert ensures this is the case.
  39. //
  40. // The implementation is based on W.Hormann, G.Derflinger:
  41. //
  42. // "Rejection-Inversion to Generate Variates from Monotone Discrete
  43. // Distributions"
  44. //
  45. // http://eeyore.wu-wien.ac.at/papers/96-04-04.wh-der.ps.gz
  46. //
  47. template <typename IntType = int>
  48. class zipf_distribution {
  49. public:
  50. using result_type = IntType;
  51. class param_type {
  52. public:
  53. using distribution_type = zipf_distribution;
  54. // Preconditions: k >= 0, v > 0, q > 1
  55. // The preconditions are validated when NDEBUG is not defined via
  56. // a pair of assert() directives.
  57. // If NDEBUG is defined and either or both of these parameters take invalid
  58. // values, the behavior of the class is undefined.
  59. explicit param_type(result_type k = (std::numeric_limits<IntType>::max)(),
  60. double q = 2.0, double v = 1.0);
  61. result_type k() const { return k_; }
  62. double q() const { return q_; }
  63. double v() const { return v_; }
  64. friend bool operator==(const param_type& a, const param_type& b) {
  65. return a.k_ == b.k_ && a.q_ == b.q_ && a.v_ == b.v_;
  66. }
  67. friend bool operator!=(const param_type& a, const param_type& b) {
  68. return !(a == b);
  69. }
  70. private:
  71. friend class zipf_distribution;
  72. inline double h(double x) const;
  73. inline double hinv(double x) const;
  74. inline double compute_s() const;
  75. inline double pow_negative_q(double x) const;
  76. // Parameters here are exactly the same as the parameters of Algorithm ZRI
  77. // in the paper.
  78. IntType k_;
  79. double q_;
  80. double v_;
  81. double one_minus_q_; // 1-q
  82. double s_;
  83. double one_minus_q_inv_; // 1 / 1-q
  84. double hxm_; // h(k + 0.5)
  85. double hx0_minus_hxm_; // h(x0) - h(k + 0.5)
  86. static_assert(random_internal::IsIntegral<IntType>::value,
  87. "Class-template absl::zipf_distribution<> must be "
  88. "parameterized using an integral type.");
  89. };
  90. zipf_distribution()
  91. : zipf_distribution((std::numeric_limits<IntType>::max)()) {}
  92. explicit zipf_distribution(result_type k, double q = 2.0, double v = 1.0)
  93. : param_(k, q, v) {}
  94. explicit zipf_distribution(const param_type& p) : param_(p) {}
  95. void reset() {}
  96. template <typename URBG>
  97. result_type operator()(URBG& g) { // NOLINT(runtime/references)
  98. return (*this)(g, param_);
  99. }
  100. template <typename URBG>
  101. result_type operator()(URBG& g, // NOLINT(runtime/references)
  102. const param_type& p);
  103. result_type k() const { return param_.k(); }
  104. double q() const { return param_.q(); }
  105. double v() const { return param_.v(); }
  106. param_type param() const { return param_; }
  107. void param(const param_type& p) { param_ = p; }
  108. result_type(min)() const { return 0; }
  109. result_type(max)() const { return k(); }
  110. friend bool operator==(const zipf_distribution& a,
  111. const zipf_distribution& b) {
  112. return a.param_ == b.param_;
  113. }
  114. friend bool operator!=(const zipf_distribution& a,
  115. const zipf_distribution& b) {
  116. return a.param_ != b.param_;
  117. }
  118. private:
  119. param_type param_;
  120. };
  121. // --------------------------------------------------------------------------
  122. // Implementation details follow
  123. // --------------------------------------------------------------------------
  124. template <typename IntType>
  125. zipf_distribution<IntType>::param_type::param_type(
  126. typename zipf_distribution<IntType>::result_type k, double q, double v)
  127. : k_(k), q_(q), v_(v), one_minus_q_(1 - q) {
  128. assert(q > 1);
  129. assert(v > 0);
  130. assert(k >= 0);
  131. one_minus_q_inv_ = 1 / one_minus_q_;
  132. // Setup for the ZRI algorithm (pg 17 of the paper).
  133. // Compute: h(i max) => h(k + 0.5)
  134. constexpr double kMax = 18446744073709549568.0;
  135. double kd = static_cast<double>(k);
  136. // TODO(absl-team): Determine if this check is needed, and if so, add a test
  137. // that fails for k > kMax
  138. if (kd > kMax) {
  139. // Ensure that our maximum value is capped to a value which will
  140. // round-trip back through double.
  141. kd = kMax;
  142. }
  143. hxm_ = h(kd + 0.5);
  144. // Compute: h(0)
  145. const bool use_precomputed = (v == 1.0 && q == 2.0);
  146. const double h0x5 = use_precomputed ? (-1.0 / 1.5) // exp(-log(1.5))
  147. : h(0.5);
  148. const double elogv_q = (v_ == 1.0) ? 1 : pow_negative_q(v_);
  149. // h(0) = h(0.5) - exp(log(v) * -q)
  150. hx0_minus_hxm_ = (h0x5 - elogv_q) - hxm_;
  151. // And s
  152. s_ = use_precomputed ? 0.46153846153846123 : compute_s();
  153. }
  154. template <typename IntType>
  155. double zipf_distribution<IntType>::param_type::h(double x) const {
  156. // std::exp(one_minus_q_ * std::log(v_ + x)) * one_minus_q_inv_;
  157. x += v_;
  158. return (one_minus_q_ == -1.0)
  159. ? (-1.0 / x) // -exp(-log(x))
  160. : (std::exp(std::log(x) * one_minus_q_) * one_minus_q_inv_);
  161. }
  162. template <typename IntType>
  163. double zipf_distribution<IntType>::param_type::hinv(double x) const {
  164. // std::exp(one_minus_q_inv_ * std::log(one_minus_q_ * x)) - v_;
  165. return -v_ + ((one_minus_q_ == -1.0)
  166. ? (-1.0 / x) // exp(-log(-x))
  167. : std::exp(one_minus_q_inv_ * std::log(one_minus_q_ * x)));
  168. }
  169. template <typename IntType>
  170. double zipf_distribution<IntType>::param_type::compute_s() const {
  171. // 1 - hinv(h(1.5) - std::exp(std::log(v_ + 1) * -q_));
  172. return 1.0 - hinv(h(1.5) - pow_negative_q(v_ + 1.0));
  173. }
  174. template <typename IntType>
  175. double zipf_distribution<IntType>::param_type::pow_negative_q(double x) const {
  176. // std::exp(std::log(x) * -q_);
  177. return q_ == 2.0 ? (1.0 / (x * x)) : std::exp(std::log(x) * -q_);
  178. }
  179. template <typename IntType>
  180. template <typename URBG>
  181. typename zipf_distribution<IntType>::result_type
  182. zipf_distribution<IntType>::operator()(
  183. URBG& g, const param_type& p) { // NOLINT(runtime/references)
  184. absl::uniform_real_distribution<double> uniform_double;
  185. double k;
  186. for (;;) {
  187. const double v = uniform_double(g);
  188. const double u = p.hxm_ + v * p.hx0_minus_hxm_;
  189. const double x = p.hinv(u);
  190. k = rint(x); // std::floor(x + 0.5);
  191. if (k > static_cast<double>(p.k())) continue; // reject k > max_k
  192. if (k - x <= p.s_) break;
  193. const double h = p.h(k + 0.5);
  194. const double r = p.pow_negative_q(p.v_ + k);
  195. if (u >= h - r) break;
  196. }
  197. IntType ki = static_cast<IntType>(k);
  198. assert(ki <= p.k_);
  199. return ki;
  200. }
  201. template <typename CharT, typename Traits, typename IntType>
  202. std::basic_ostream<CharT, Traits>& operator<<(
  203. std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
  204. const zipf_distribution<IntType>& x) {
  205. using stream_type =
  206. typename random_internal::stream_format_type<IntType>::type;
  207. auto saver = random_internal::make_ostream_state_saver(os);
  208. os.precision(random_internal::stream_precision_helper<double>::kPrecision);
  209. os << static_cast<stream_type>(x.k()) << os.fill() << x.q() << os.fill()
  210. << x.v();
  211. return os;
  212. }
  213. template <typename CharT, typename Traits, typename IntType>
  214. std::basic_istream<CharT, Traits>& operator>>(
  215. std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
  216. zipf_distribution<IntType>& x) { // NOLINT(runtime/references)
  217. using result_type = typename zipf_distribution<IntType>::result_type;
  218. using param_type = typename zipf_distribution<IntType>::param_type;
  219. using stream_type =
  220. typename random_internal::stream_format_type<IntType>::type;
  221. stream_type k;
  222. double q;
  223. double v;
  224. auto saver = random_internal::make_istream_state_saver(is);
  225. is >> k >> q >> v;
  226. if (!is.fail()) {
  227. x.param(param_type(static_cast<result_type>(k), q, v));
  228. }
  229. return is;
  230. }
  231. ABSL_NAMESPACE_END
  232. } // namespace absl
  233. #endif // ABSL_RANDOM_ZIPF_DISTRIBUTION_H_