zipf_distribution.h 8.9 KB

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