distribution_test_util_test.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "absl/random/internal/distribution_test_util.h"
  15. #include "gtest/gtest.h"
  16. namespace {
  17. TEST(TestUtil, InverseErf) {
  18. const struct {
  19. const double z;
  20. const double value;
  21. } kErfInvTable[] = {
  22. {0.0000001, 8.86227e-8},
  23. {0.00001, 8.86227e-6},
  24. {0.5, 0.4769362762044},
  25. {0.6, 0.5951160814499},
  26. {0.99999, 3.1234132743},
  27. {0.9999999, 3.7665625816},
  28. {0.999999944, 3.8403850690566985}, // = log((1-x) * (1+x)) =~ 16.004
  29. {0.999999999, 4.3200053849134452},
  30. };
  31. for (const auto& data : kErfInvTable) {
  32. auto value = absl::random_internal::erfinv(data.z);
  33. // Log using the Wolfram-alpha function name & parameters.
  34. EXPECT_NEAR(value, data.value, 1e-8)
  35. << " InverseErf[" << data.z << "] (expected=" << data.value << ") -> "
  36. << value;
  37. }
  38. }
  39. const struct {
  40. const double p;
  41. const double q;
  42. const double x;
  43. const double alpha;
  44. } kBetaTable[] = {
  45. {0.5, 0.5, 0.01, 0.06376856085851985},
  46. {0.5, 0.5, 0.1, 0.2048327646991335},
  47. {0.5, 0.5, 1, 1},
  48. {1, 0.5, 0, 0},
  49. {1, 0.5, 0.01, 0.005012562893380045},
  50. {1, 0.5, 0.1, 0.0513167019494862},
  51. {1, 0.5, 0.5, 0.2928932188134525},
  52. {1, 1, 0.5, 0.5},
  53. {2, 2, 0.1, 0.028},
  54. {2, 2, 0.2, 0.104},
  55. {2, 2, 0.3, 0.216},
  56. {2, 2, 0.4, 0.352},
  57. {2, 2, 0.5, 0.5},
  58. {2, 2, 0.6, 0.648},
  59. {2, 2, 0.7, 0.784},
  60. {2, 2, 0.8, 0.896},
  61. {2, 2, 0.9, 0.972},
  62. {5.5, 5, 0.5, 0.4361908850559777},
  63. {10, 0.5, 0.9, 0.1516409096346979},
  64. {10, 5, 0.5, 0.08978271484375},
  65. {10, 5, 1, 1},
  66. {10, 10, 0.5, 0.5},
  67. {20, 5, 0.8, 0.4598773297575791},
  68. {20, 10, 0.6, 0.2146816102371739},
  69. {20, 10, 0.8, 0.9507364826957875},
  70. {20, 20, 0.5, 0.5},
  71. {20, 20, 0.6, 0.8979413687105918},
  72. {30, 10, 0.7, 0.2241297491808366},
  73. {30, 10, 0.8, 0.7586405487192086},
  74. {40, 20, 0.7, 0.7001783247477069},
  75. {1, 0.5, 0.1, 0.0513167019494862},
  76. {1, 0.5, 0.2, 0.1055728090000841},
  77. {1, 0.5, 0.3, 0.1633399734659245},
  78. {1, 0.5, 0.4, 0.2254033307585166},
  79. {1, 2, 0.2, 0.36},
  80. {1, 3, 0.2, 0.488},
  81. {1, 4, 0.2, 0.5904},
  82. {1, 5, 0.2, 0.67232},
  83. {2, 2, 0.3, 0.216},
  84. {3, 2, 0.3, 0.0837},
  85. {4, 2, 0.3, 0.03078},
  86. {5, 2, 0.3, 0.010935},
  87. // These values test small & large points along the range of the Beta
  88. // function.
  89. //
  90. // When selecting test points, remember that if BetaIncomplete(x, p, q)
  91. // returns the same value to within the limits of precision over a large
  92. // domain of the input, x, then BetaIncompleteInv(alpha, p, q) may return an
  93. // essentially arbitrary value where BetaIncomplete(x, p, q) =~ alpha.
  94. // BetaRegularized[x, 0.00001, 0.00001],
  95. // For x in {~0.001 ... ~0.999}, => ~0.5
  96. {1e-5, 1e-5, 1e-5, 0.4999424388184638311},
  97. {1e-5, 1e-5, (1.0 - 1e-8), 0.5000920948389232964},
  98. // BetaRegularized[x, 0.00001, 10000].
  99. // For x in {~epsilon ... 1.0}, => ~1
  100. {1e-5, 1e5, 1e-6, 0.9999817708130066936},
  101. {1e-5, 1e5, (1.0 - 1e-7), 1.0},
  102. // BetaRegularized[x, 10000, 0.00001].
  103. // For x in {0 .. 1-epsilon}, => ~0
  104. {1e5, 1e-5, 1e-6, 0},
  105. {1e5, 1e-5, (1.0 - 1e-6), 1.8229186993306369e-5},
  106. };
  107. TEST(BetaTest, BetaIncomplete) {
  108. for (const auto& data : kBetaTable) {
  109. auto value = absl::random_internal::BetaIncomplete(data.x, data.p, data.q);
  110. // Log using the Wolfram-alpha function name & parameters.
  111. EXPECT_NEAR(value, data.alpha, 1e-12)
  112. << " BetaRegularized[" << data.x << ", " << data.p << ", " << data.q
  113. << "] (expected=" << data.alpha << ") -> " << value;
  114. }
  115. }
  116. TEST(BetaTest, BetaIncompleteInv) {
  117. for (const auto& data : kBetaTable) {
  118. auto value =
  119. absl::random_internal::BetaIncompleteInv(data.p, data.q, data.alpha);
  120. // Log using the Wolfram-alpha function name & parameters.
  121. EXPECT_NEAR(value, data.x, 1e-6)
  122. << " InverseBetaRegularized[" << data.alpha << ", " << data.p << ", "
  123. << data.q << "] (expected=" << data.x << ") -> " << value;
  124. }
  125. }
  126. TEST(MaxErrorTolerance, MaxErrorTolerance) {
  127. std::vector<std::pair<double, double>> cases = {
  128. {0.0000001, 8.86227e-8 * 1.41421356237},
  129. {0.00001, 8.86227e-6 * 1.41421356237},
  130. {0.5, 0.4769362762044 * 1.41421356237},
  131. {0.6, 0.5951160814499 * 1.41421356237},
  132. {0.99999, 3.1234132743 * 1.41421356237},
  133. {0.9999999, 3.7665625816 * 1.41421356237},
  134. {0.999999944, 3.8403850690566985 * 1.41421356237},
  135. {0.999999999, 4.3200053849134452 * 1.41421356237}};
  136. for (auto entry : cases) {
  137. EXPECT_NEAR(absl::random_internal::MaxErrorTolerance(entry.first),
  138. entry.second, 1e-8);
  139. }
  140. }
  141. TEST(ZScore, WithSameMean) {
  142. absl::random_internal::DistributionMoments m;
  143. m.n = 100;
  144. m.mean = 5;
  145. m.variance = 1;
  146. EXPECT_NEAR(absl::random_internal::ZScore(5, m), 0, 1e-12);
  147. m.n = 1;
  148. m.mean = 0;
  149. m.variance = 1;
  150. EXPECT_NEAR(absl::random_internal::ZScore(0, m), 0, 1e-12);
  151. m.n = 10000;
  152. m.mean = -5;
  153. m.variance = 100;
  154. EXPECT_NEAR(absl::random_internal::ZScore(-5, m), 0, 1e-12);
  155. }
  156. TEST(ZScore, DifferentMean) {
  157. absl::random_internal::DistributionMoments m;
  158. m.n = 100;
  159. m.mean = 5;
  160. m.variance = 1;
  161. EXPECT_NEAR(absl::random_internal::ZScore(4, m), 10, 1e-12);
  162. m.n = 1;
  163. m.mean = 0;
  164. m.variance = 1;
  165. EXPECT_NEAR(absl::random_internal::ZScore(-1, m), 1, 1e-12);
  166. m.n = 10000;
  167. m.mean = -5;
  168. m.variance = 100;
  169. EXPECT_NEAR(absl::random_internal::ZScore(-4, m), -10, 1e-12);
  170. }
  171. } // namespace