catch_stats.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Created by Martin on 15/06/2019.
  3. * Adapted from donated nonius code.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. // Statistical analysis tools
  9. #if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
  10. #include "catch_stats.hpp"
  11. #include "../../catch_compiler_capabilities.h"
  12. #include <cassert>
  13. #include <random>
  14. #if defined(CATCH_CONFIG_USE_ASYNC)
  15. #include <future>
  16. #endif
  17. namespace {
  18. double erf_inv(double x) {
  19. // Code accompanying the article "Approximating the erfinv function" in GPU Computing Gems, Volume 2
  20. double w, p;
  21. w = -log((1.0 - x) * (1.0 + x));
  22. if (w < 6.250000) {
  23. w = w - 3.125000;
  24. p = -3.6444120640178196996e-21;
  25. p = -1.685059138182016589e-19 + p * w;
  26. p = 1.2858480715256400167e-18 + p * w;
  27. p = 1.115787767802518096e-17 + p * w;
  28. p = -1.333171662854620906e-16 + p * w;
  29. p = 2.0972767875968561637e-17 + p * w;
  30. p = 6.6376381343583238325e-15 + p * w;
  31. p = -4.0545662729752068639e-14 + p * w;
  32. p = -8.1519341976054721522e-14 + p * w;
  33. p = 2.6335093153082322977e-12 + p * w;
  34. p = -1.2975133253453532498e-11 + p * w;
  35. p = -5.4154120542946279317e-11 + p * w;
  36. p = 1.051212273321532285e-09 + p * w;
  37. p = -4.1126339803469836976e-09 + p * w;
  38. p = -2.9070369957882005086e-08 + p * w;
  39. p = 4.2347877827932403518e-07 + p * w;
  40. p = -1.3654692000834678645e-06 + p * w;
  41. p = -1.3882523362786468719e-05 + p * w;
  42. p = 0.0001867342080340571352 + p * w;
  43. p = -0.00074070253416626697512 + p * w;
  44. p = -0.0060336708714301490533 + p * w;
  45. p = 0.24015818242558961693 + p * w;
  46. p = 1.6536545626831027356 + p * w;
  47. } else if (w < 16.000000) {
  48. w = sqrt(w) - 3.250000;
  49. p = 2.2137376921775787049e-09;
  50. p = 9.0756561938885390979e-08 + p * w;
  51. p = -2.7517406297064545428e-07 + p * w;
  52. p = 1.8239629214389227755e-08 + p * w;
  53. p = 1.5027403968909827627e-06 + p * w;
  54. p = -4.013867526981545969e-06 + p * w;
  55. p = 2.9234449089955446044e-06 + p * w;
  56. p = 1.2475304481671778723e-05 + p * w;
  57. p = -4.7318229009055733981e-05 + p * w;
  58. p = 6.8284851459573175448e-05 + p * w;
  59. p = 2.4031110387097893999e-05 + p * w;
  60. p = -0.0003550375203628474796 + p * w;
  61. p = 0.00095328937973738049703 + p * w;
  62. p = -0.0016882755560235047313 + p * w;
  63. p = 0.0024914420961078508066 + p * w;
  64. p = -0.0037512085075692412107 + p * w;
  65. p = 0.005370914553590063617 + p * w;
  66. p = 1.0052589676941592334 + p * w;
  67. p = 3.0838856104922207635 + p * w;
  68. } else {
  69. w = sqrt(w) - 5.000000;
  70. p = -2.7109920616438573243e-11;
  71. p = -2.5556418169965252055e-10 + p * w;
  72. p = 1.5076572693500548083e-09 + p * w;
  73. p = -3.7894654401267369937e-09 + p * w;
  74. p = 7.6157012080783393804e-09 + p * w;
  75. p = -1.4960026627149240478e-08 + p * w;
  76. p = 2.9147953450901080826e-08 + p * w;
  77. p = -6.7711997758452339498e-08 + p * w;
  78. p = 2.2900482228026654717e-07 + p * w;
  79. p = -9.9298272942317002539e-07 + p * w;
  80. p = 4.5260625972231537039e-06 + p * w;
  81. p = -1.9681778105531670567e-05 + p * w;
  82. p = 7.5995277030017761139e-05 + p * w;
  83. p = -0.00021503011930044477347 + p * w;
  84. p = -0.00013871931833623122026 + p * w;
  85. p = 1.0103004648645343977 + p * w;
  86. p = 4.8499064014085844221 + p * w;
  87. }
  88. return p * x;
  89. }
  90. double standard_deviation(std::vector<double>::iterator first, std::vector<double>::iterator last) {
  91. auto m = Catch::Benchmark::Detail::mean(first, last);
  92. double variance = std::accumulate(first, last, 0., [m](double a, double b) {
  93. double diff = b - m;
  94. return a + diff * diff;
  95. }) / (last - first);
  96. return std::sqrt(variance);
  97. }
  98. }
  99. namespace Catch {
  100. namespace Benchmark {
  101. namespace Detail {
  102. double weighted_average_quantile(int k, int q, std::vector<double>::iterator first, std::vector<double>::iterator last) {
  103. auto count = last - first;
  104. double idx = (count - 1) * k / static_cast<double>(q);
  105. int j = static_cast<int>(idx);
  106. double g = idx - j;
  107. std::nth_element(first, first + j, last);
  108. auto xj = first[j];
  109. if (g == 0) return xj;
  110. auto xj1 = *std::min_element(first + (j + 1), last);
  111. return xj + g * (xj1 - xj);
  112. }
  113. double erfc_inv(double x) {
  114. return erf_inv(1.0 - x);
  115. }
  116. double normal_quantile(double p) {
  117. static const double ROOT_TWO = std::sqrt(2.0);
  118. double result = 0.0;
  119. assert(p >= 0 && p <= 1);
  120. if (p < 0 || p > 1) {
  121. return result;
  122. }
  123. result = -erfc_inv(2.0 * p);
  124. // result *= normal distribution standard deviation (1.0) * sqrt(2)
  125. result *= /*sd * */ ROOT_TWO;
  126. // result += normal disttribution mean (0)
  127. return result;
  128. }
  129. double outlier_variance(Estimate<double> mean, Estimate<double> stddev, int n) {
  130. double sb = stddev.point;
  131. double mn = mean.point / n;
  132. double mg_min = mn / 2.;
  133. double sg = (std::min)(mg_min / 4., sb / std::sqrt(n));
  134. double sg2 = sg * sg;
  135. double sb2 = sb * sb;
  136. auto c_max = [n, mn, sb2, sg2](double x) -> double {
  137. double k = mn - x;
  138. double d = k * k;
  139. double nd = n * d;
  140. double k0 = -n * nd;
  141. double k1 = sb2 - n * sg2 + nd;
  142. double det = k1 * k1 - 4 * sg2 * k0;
  143. return (int)(-2. * k0 / (k1 + std::sqrt(det)));
  144. };
  145. auto var_out = [n, sb2, sg2](double c) {
  146. double nc = n - c;
  147. return (nc / n) * (sb2 - nc * sg2);
  148. };
  149. return (std::min)(var_out(1), var_out((std::min)(c_max(0.), c_max(mg_min)))) / sb2;
  150. }
  151. bootstrap_analysis analyse_samples(double confidence_level, int n_resamples, std::vector<double>::iterator first, std::vector<double>::iterator last) {
  152. CATCH_INTERNAL_START_WARNINGS_SUPPRESSION
  153. CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS
  154. static std::random_device entropy;
  155. CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION
  156. auto n = static_cast<int>(last - first); // seriously, one can't use integral types without hell in C++
  157. auto mean = &Detail::mean<std::vector<double>::iterator>;
  158. auto stddev = &standard_deviation;
  159. #if defined(CATCH_CONFIG_USE_ASYNC)
  160. auto Estimate = [=](double(*f)(std::vector<double>::iterator, std::vector<double>::iterator)) {
  161. auto seed = entropy();
  162. return std::async(std::launch::async, [=] {
  163. std::mt19937 rng(seed);
  164. auto resampled = resample(rng, n_resamples, first, last, f);
  165. return bootstrap(confidence_level, first, last, resampled, f);
  166. });
  167. };
  168. auto mean_future = Estimate(mean);
  169. auto stddev_future = Estimate(stddev);
  170. auto mean_estimate = mean_future.get();
  171. auto stddev_estimate = stddev_future.get();
  172. #else
  173. auto Estimate = [=](double(*f)(std::vector<double>::iterator, std::vector<double>::iterator)) {
  174. auto seed = entropy();
  175. std::mt19937 rng(seed);
  176. auto resampled = resample(rng, n_resamples, first, last, f);
  177. return bootstrap(confidence_level, first, last, resampled, f);
  178. };
  179. auto mean_estimate = Estimate(mean);
  180. auto stddev_estimate = Estimate(stddev);
  181. #endif // CATCH_USE_ASYNC
  182. double outlier_variance = Detail::outlier_variance(mean_estimate, stddev_estimate, n);
  183. return { mean_estimate, stddev_estimate, outlier_variance };
  184. }
  185. } // namespace Detail
  186. } // namespace Benchmark
  187. } // namespace Catch
  188. #endif // CATCH_CONFIG_ENABLE_BENCHMARKING