Guest User

Untitled

a guest
Nov 18th, 2017
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. // Copyright 2017.
  2. // Author: Yafei Zhang (zhangyafeikimi@gmail.com)
  3. //
  4. // a STL-like beta distribution random number generator
  5. //
  6.  
  7. #ifndef BETA_DISTRIBUTION_H_
  8. #define BETA_DISTRIBUTION_H_
  9.  
  10. #include <istream>
  11. #include <ostream>
  12. #include <random>
  13. #include <string>
  14.  
  15. namespace std {
  16.  
  17. template <typename RealType = double>
  18. class beta_distribution {
  19. public:
  20. using result_type = RealType;
  21.  
  22. struct param_type {
  23. using distribution_type = beta_distribution<RealType>;
  24.  
  25. param_type() : a_(2), b_(2) {}
  26. param_type(RealType a, RealType b) : a_(a), b_(b) {}
  27.  
  28. RealType a() const { return a_; }
  29. RealType b() const { return b_; }
  30.  
  31. bool operator==(const param_type& other) const {
  32. return a_ == other.a_ && b_ == other.b_;
  33. }
  34. bool operator!=(const param_type& other) const { return !(*this == other); }
  35.  
  36. private:
  37. RealType a_, b_;
  38. };
  39.  
  40. private:
  41. using gamma_dist_t = std::gamma_distribution<result_type>;
  42. using gamma_dist_param_t = typename gamma_dist_t::param_type;
  43. param_type p_;
  44. gamma_dist_t g1_, g2_;
  45.  
  46. public:
  47. beta_distribution() : p_(2, 2), g1_(p_.a(), 2), g2_(p_.b(), 2) {}
  48.  
  49. explicit beta_distribution(const param_type& p)
  50. : p_(p), g1_(p_.a(), 2), g2_(p_.b(), 2) {}
  51.  
  52. beta_distribution(RealType a, RealType b)
  53. : p_(a, b), g1_(p_.a(), 2), g2_(p_.b(), 2) {}
  54.  
  55. void reset() {
  56. g1_.reset();
  57. g2_.reset();
  58. }
  59.  
  60. RealType a() const { return p_.a(); }
  61. RealType b() const { return p_.b(); }
  62.  
  63. param_type param() const { return p_; }
  64.  
  65. void param(const param_type& p) {
  66. p_ = p;
  67. gamma_dist_param_t p1(p_.a(), 2);
  68. gamma_dist_param_t p2(p_.b(), 2);
  69. g1_.param(p1);
  70. g2_.param(p2);
  71. }
  72.  
  73. result_type min() const { return 0; }
  74. result_type max() const { return 1; }
  75.  
  76. template <class Generator>
  77. result_type operator()(Generator& g) {
  78. auto x = g1_(g);
  79. auto y = g2_(g);
  80. return x / (x + y);
  81. }
  82.  
  83. template <typename Generator>
  84. result_type operator()(Generator& g, const param_type& p) {
  85. return operator()(g, p.a(), p.b());
  86. }
  87.  
  88. template <class Generator>
  89. result_type operator()(Generator& g, RealType a, RealType b) {
  90. auto x = g1_(g, gamma_dist_param_t(a, 2));
  91. auto y = g2_(g, gamma_dist_param_t(b, 2));
  92. return x / (x + y);
  93. }
  94. };
  95.  
  96. template <typename CharT, typename Traits, typename RealType>
  97. std::basic_ostream<CharT, Traits>& operator<<(
  98. std::basic_ostream<CharT, Traits>& os,
  99. const beta_distribution<RealType>& beta) {
  100. return os << "beta_distribution " << beta.a() << " " << beta.b();
  101. }
  102.  
  103. template <typename CharT, typename Traits, typename RealType>
  104. std::basic_istream<CharT, Traits>& operator>>(
  105. std::basic_istream<CharT, Traits>& is,
  106. const beta_distribution<RealType>& beta) {
  107. std::basic_string<CharT, Traits> token;
  108. RealType a, b;
  109. is << token << a << b;
  110. if (is) {
  111. typename beta_distribution<RealType>::param_type p(a, b);
  112. beta.param(p);
  113. }
  114. return is;
  115. }
  116.  
  117. } // namespace std
  118.  
  119. #endif // BETA_DISTRIBUTION_H_
Add Comment
Please, Sign In to add comment