testsuite_random.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // -*- C++ -*-
  2. // Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // You should have received a copy of the GNU General Public License along
  14. // with this library; see the file COPYING3. If not see
  15. // <http://www.gnu.org/licenses/>.
  16. /**
  17. * @file testsuite_random.h
  18. */
  19. #ifndef _GLIBCXX_TESTSUITE_RANDOM_H
  20. #define _GLIBCXX_TESTSUITE_RANDOM_H
  21. #include <cmath>
  22. #include <initializer_list>
  23. #include <testsuite_hooks.h>
  24. namespace __gnu_test
  25. {
  26. // Adapted for libstdc++ from GNU gsl-1.14/randist/test.c
  27. // Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007, 2010
  28. // James Theiler, Brian Gough
  29. template<unsigned long BINS = 100,
  30. unsigned long N = 100000,
  31. typename Distribution, typename Pdf>
  32. void
  33. testDiscreteDist(Distribution& f, Pdf pdf)
  34. {
  35. double count[BINS], p[BINS];
  36. for (unsigned long i = 0; i < BINS; i++)
  37. count[i] = 0;
  38. for (unsigned long i = 0; i < N; i++)
  39. {
  40. auto r = f();
  41. if (r >= 0 && (unsigned long)r < BINS)
  42. count[r]++;
  43. }
  44. for (unsigned long i = 0; i < BINS; i++)
  45. p[i] = pdf(i);
  46. for (unsigned long i = 0; i < BINS; i++)
  47. {
  48. bool status_i;
  49. double d = std::abs(count[i] - N * p[i]);
  50. if (p[i] != 0)
  51. {
  52. double s = d / std::sqrt(N * p[i]);
  53. status_i = (s > 5) && (d > 1);
  54. }
  55. else
  56. status_i = (count[i] != 0);
  57. VERIFY( !status_i );
  58. }
  59. }
  60. inline double
  61. bernoulli_pdf(int k, double p)
  62. {
  63. if (k == 0)
  64. return 1 - p;
  65. else if (k == 1)
  66. return p;
  67. else
  68. return 0.0;
  69. }
  70. #ifdef _GLIBCXX_USE_C99_MATH_TR1
  71. inline double
  72. binomial_pdf(int k, int n, double p)
  73. {
  74. if (k < 0 || k > n)
  75. return 0.0;
  76. else
  77. {
  78. double q;
  79. if (p == 0.0)
  80. q = (k == 0) ? 1.0 : 0.0;
  81. else if (p == 1.0)
  82. q = (k == n) ? 1.0 : 0.0;
  83. else
  84. {
  85. double ln_Cnk = (std::lgamma(n + 1.0) - std::lgamma(k + 1.0)
  86. - std::lgamma(n - k + 1.0));
  87. q = ln_Cnk + k * std::log(p) + (n - k) * std::log1p(-p);
  88. q = std::exp(q);
  89. }
  90. return q;
  91. }
  92. }
  93. #endif
  94. inline double
  95. discrete_pdf(int k, std::initializer_list<double> wl)
  96. {
  97. if (!wl.size())
  98. {
  99. static std::initializer_list<double> one = { 1.0 };
  100. wl = one;
  101. }
  102. if (k < 0 || (std::size_t)k >= wl.size())
  103. return 0.0;
  104. else
  105. {
  106. double sum = 0.0;
  107. for (auto it = wl.begin(); it != wl.end(); ++it)
  108. sum += *it;
  109. return wl.begin()[k] / sum;
  110. }
  111. }
  112. inline double
  113. geometric_pdf(int k, double p)
  114. {
  115. if (k < 0)
  116. return 0.0;
  117. else if (k == 0)
  118. return p;
  119. else
  120. return p * std::pow(1 - p, k);
  121. }
  122. #ifdef _GLIBCXX_USE_C99_MATH_TR1
  123. inline double
  124. negative_binomial_pdf(int k, int n, double p)
  125. {
  126. if (k < 0)
  127. return 0.0;
  128. else
  129. {
  130. double f = std::lgamma(k + (double)n);
  131. double a = std::lgamma(n);
  132. double b = std::lgamma(k + 1.0);
  133. return std::exp(f - a - b) * std::pow(p, n) * std::pow(1 - p, k);
  134. }
  135. }
  136. inline double
  137. poisson_pdf(int k, double mu)
  138. {
  139. if (k < 0)
  140. return 0.0;
  141. else
  142. {
  143. double lf = std::lgamma(k + 1.0);
  144. return std::exp(std::log(mu) * k - lf - mu);
  145. }
  146. }
  147. #endif
  148. inline double
  149. uniform_int_pdf(int k, int a, int b)
  150. {
  151. if (k < 0 || k < a || k > b)
  152. return 0.0;
  153. else
  154. return 1.0 / (b - a + 1.0);
  155. }
  156. #ifdef _GLIBCXX_USE_C99_MATH_TR1
  157. inline double
  158. lbincoef(int n, int k)
  159. {
  160. return std::lgamma(double(1 + n))
  161. - std::lgamma(double(1 + k))
  162. - std::lgamma(double(1 + n - k));
  163. }
  164. inline double
  165. hypergeometric_pdf(int k, int N, int K, int n)
  166. {
  167. if (k < 0 || k < std::max(0, n - (N - K)) || k > std::min(K, n))
  168. return 0.0;
  169. else
  170. return lbincoef(K, k) + lbincoef(N - K, n - k) - lbincoef(N, n);
  171. }
  172. #endif
  173. // Check whether TOKEN can construct a std::random_device successfully.
  174. inline bool
  175. random_device_available(const std::string& token) noexcept
  176. {
  177. try {
  178. std::random_device dev(token);
  179. return true;
  180. } catch (...) {
  181. return false;
  182. }
  183. }
  184. } // namespace __gnu_test
  185. #endif // #ifndef _GLIBCXX_TESTSUITE_RANDOM_H