discrete_distribution.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 Y_ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_
  15. #define Y_ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_
  16. #include <cassert>
  17. #include <cmath>
  18. #include <istream>
  19. #include <limits>
  20. #include <numeric>
  21. #include <type_traits>
  22. #include <utility>
  23. #include <vector>
  24. #include "y_absl/random/bernoulli_distribution.h"
  25. #include "y_absl/random/internal/iostream_state_saver.h"
  26. #include "y_absl/random/uniform_int_distribution.h"
  27. namespace y_absl {
  28. Y_ABSL_NAMESPACE_BEGIN
  29. // y_absl::discrete_distribution
  30. //
  31. // A discrete distribution produces random integers i, where 0 <= i < n
  32. // distributed according to the discrete probability function:
  33. //
  34. // P(i|p0,...,pn−1)=pi
  35. //
  36. // This class is an implementation of discrete_distribution (see
  37. // [rand.dist.samp.discrete]).
  38. //
  39. // The algorithm used is Walker's Aliasing algorithm, described in Knuth, Vol 2.
  40. // y_absl::discrete_distribution takes O(N) time to precompute the probabilities
  41. // (where N is the number of possible outcomes in the distribution) at
  42. // construction, and then takes O(1) time for each variate generation. Many
  43. // other implementations also take O(N) time to construct an ordered sequence of
  44. // partial sums, plus O(log N) time per variate to binary search.
  45. //
  46. template <typename IntType = int>
  47. class discrete_distribution {
  48. public:
  49. using result_type = IntType;
  50. class param_type {
  51. public:
  52. using distribution_type = discrete_distribution;
  53. param_type() { init(); }
  54. template <typename InputIterator>
  55. explicit param_type(InputIterator begin, InputIterator end)
  56. : p_(begin, end) {
  57. init();
  58. }
  59. explicit param_type(std::initializer_list<double> weights) : p_(weights) {
  60. init();
  61. }
  62. template <class UnaryOperation>
  63. explicit param_type(size_t nw, double xmin, double xmax,
  64. UnaryOperation fw) {
  65. if (nw > 0) {
  66. p_.reserve(nw);
  67. double delta = (xmax - xmin) / static_cast<double>(nw);
  68. assert(delta > 0);
  69. double t = delta * 0.5;
  70. for (size_t i = 0; i < nw; ++i) {
  71. p_.push_back(fw(xmin + i * delta + t));
  72. }
  73. }
  74. init();
  75. }
  76. const std::vector<double>& probabilities() const { return p_; }
  77. size_t n() const { return p_.size() - 1; }
  78. friend bool operator==(const param_type& a, const param_type& b) {
  79. return a.probabilities() == b.probabilities();
  80. }
  81. friend bool operator!=(const param_type& a, const param_type& b) {
  82. return !(a == b);
  83. }
  84. private:
  85. friend class discrete_distribution;
  86. void init();
  87. std::vector<double> p_; // normalized probabilities
  88. std::vector<std::pair<double, size_t>> q_; // (acceptance, alternate) pairs
  89. static_assert(std::is_integral<result_type>::value,
  90. "Class-template y_absl::discrete_distribution<> must be "
  91. "parameterized using an integral type.");
  92. };
  93. discrete_distribution() : param_() {}
  94. explicit discrete_distribution(const param_type& p) : param_(p) {}
  95. template <typename InputIterator>
  96. explicit discrete_distribution(InputIterator begin, InputIterator end)
  97. : param_(begin, end) {}
  98. explicit discrete_distribution(std::initializer_list<double> weights)
  99. : param_(weights) {}
  100. template <class UnaryOperation>
  101. explicit discrete_distribution(size_t nw, double xmin, double xmax,
  102. UnaryOperation fw)
  103. : param_(nw, xmin, xmax, std::move(fw)) {}
  104. void reset() {}
  105. // generating functions
  106. template <typename URBG>
  107. result_type operator()(URBG& g) { // NOLINT(runtime/references)
  108. return (*this)(g, param_);
  109. }
  110. template <typename URBG>
  111. result_type operator()(URBG& g, // NOLINT(runtime/references)
  112. const param_type& p);
  113. const param_type& param() const { return param_; }
  114. void param(const param_type& p) { param_ = p; }
  115. result_type(min)() const { return 0; }
  116. result_type(max)() const {
  117. return static_cast<result_type>(param_.n());
  118. } // inclusive
  119. // NOTE [rand.dist.sample.discrete] returns a std::vector<double> not a
  120. // const std::vector<double>&.
  121. const std::vector<double>& probabilities() const {
  122. return param_.probabilities();
  123. }
  124. friend bool operator==(const discrete_distribution& a,
  125. const discrete_distribution& b) {
  126. return a.param_ == b.param_;
  127. }
  128. friend bool operator!=(const discrete_distribution& a,
  129. const discrete_distribution& b) {
  130. return a.param_ != b.param_;
  131. }
  132. private:
  133. param_type param_;
  134. };
  135. // --------------------------------------------------------------------------
  136. // Implementation details only below
  137. // --------------------------------------------------------------------------
  138. namespace random_internal {
  139. // Using the vector `*probabilities`, whose values are the weights or
  140. // probabilities of an element being selected, constructs the proportional
  141. // probabilities used by the discrete distribution. `*probabilities` will be
  142. // scaled, if necessary, so that its entries sum to a value sufficiently close
  143. // to 1.0.
  144. std::vector<std::pair<double, size_t>> InitDiscreteDistribution(
  145. std::vector<double>* probabilities);
  146. } // namespace random_internal
  147. template <typename IntType>
  148. void discrete_distribution<IntType>::param_type::init() {
  149. if (p_.empty()) {
  150. p_.push_back(1.0);
  151. q_.emplace_back(1.0, 0);
  152. } else {
  153. assert(n() <= (std::numeric_limits<IntType>::max)());
  154. q_ = random_internal::InitDiscreteDistribution(&p_);
  155. }
  156. }
  157. template <typename IntType>
  158. template <typename URBG>
  159. typename discrete_distribution<IntType>::result_type
  160. discrete_distribution<IntType>::operator()(
  161. URBG& g, // NOLINT(runtime/references)
  162. const param_type& p) {
  163. const auto idx = y_absl::uniform_int_distribution<result_type>(0, p.n())(g);
  164. const auto& q = p.q_[idx];
  165. const bool selected = y_absl::bernoulli_distribution(q.first)(g);
  166. return selected ? idx : static_cast<result_type>(q.second);
  167. }
  168. template <typename CharT, typename Traits, typename IntType>
  169. std::basic_ostream<CharT, Traits>& operator<<(
  170. std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
  171. const discrete_distribution<IntType>& x) {
  172. auto saver = random_internal::make_ostream_state_saver(os);
  173. const auto& probabilities = x.param().probabilities();
  174. os << probabilities.size();
  175. os.precision(random_internal::stream_precision_helper<double>::kPrecision);
  176. for (const auto& p : probabilities) {
  177. os << os.fill() << p;
  178. }
  179. return os;
  180. }
  181. template <typename CharT, typename Traits, typename IntType>
  182. std::basic_istream<CharT, Traits>& operator>>(
  183. std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
  184. discrete_distribution<IntType>& x) { // NOLINT(runtime/references)
  185. using param_type = typename discrete_distribution<IntType>::param_type;
  186. auto saver = random_internal::make_istream_state_saver(is);
  187. size_t n;
  188. std::vector<double> p;
  189. is >> n;
  190. if (is.fail()) return is;
  191. if (n > 0) {
  192. p.reserve(n);
  193. for (IntType i = 0; i < n && !is.fail(); ++i) {
  194. auto tmp = random_internal::read_floating_point<double>(is);
  195. if (is.fail()) return is;
  196. p.push_back(tmp);
  197. }
  198. }
  199. x.param(param_type(p.begin(), p.end()));
  200. return is;
  201. }
  202. Y_ABSL_NAMESPACE_END
  203. } // namespace y_absl
  204. #endif // Y_ABSL_RANDOM_DISCRETE_DISTRIBUTION_H_