distribution_test_util.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_INTERNAL_DISTRIBUTION_TEST_UTIL_H_
  15. #define Y_ABSL_RANDOM_INTERNAL_DISTRIBUTION_TEST_UTIL_H_
  16. #include <cstddef>
  17. #include <iostream>
  18. #include <vector>
  19. #include "y_absl/strings/string_view.h"
  20. #include "y_absl/types/span.h"
  21. // NOTE: The functions in this file are test only, and are should not be used in
  22. // non-test code.
  23. namespace y_absl {
  24. Y_ABSL_NAMESPACE_BEGIN
  25. namespace random_internal {
  26. // http://webspace.ship.edu/pgmarr/Geo441/Lectures/Lec%205%20-%20Normality%20Testing.pdf
  27. // Compute the 1st to 4th standard moments:
  28. // mean, variance, skewness, and kurtosis.
  29. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm
  30. struct DistributionMoments {
  31. size_t n = 0;
  32. double mean = 0.0;
  33. double variance = 0.0;
  34. double skewness = 0.0;
  35. double kurtosis = 0.0;
  36. };
  37. DistributionMoments ComputeDistributionMoments(
  38. y_absl::Span<const double> data_points);
  39. std::ostream& operator<<(std::ostream& os, const DistributionMoments& moments);
  40. // Computes the Z-score for a set of data with the given distribution moments
  41. // compared against `expected_mean`.
  42. double ZScore(double expected_mean, const DistributionMoments& moments);
  43. // Returns the probability of success required for a single trial to ensure that
  44. // after `num_trials` trials, the probability of at least one failure is no more
  45. // than `p_fail`.
  46. double RequiredSuccessProbability(double p_fail, int num_trials);
  47. // Computes the maximum distance from the mean tolerable, for Z-Tests that are
  48. // expected to pass with `acceptance_probability`. Will terminate if the
  49. // resulting tolerance is zero (due to passing in 0.0 for
  50. // `acceptance_probability` or rounding errors).
  51. //
  52. // For example,
  53. // MaxErrorTolerance(0.001) = 0.0
  54. // MaxErrorTolerance(0.5) = ~0.47
  55. // MaxErrorTolerance(1.0) = inf
  56. double MaxErrorTolerance(double acceptance_probability);
  57. // Approximation to inverse of the Error Function in double precision.
  58. // (http://people.maths.ox.ac.uk/gilesm/files/gems_erfinv.pdf)
  59. double erfinv(double x);
  60. // Beta(p, q) = Gamma(p) * Gamma(q) / Gamma(p+q)
  61. double beta(double p, double q);
  62. // The inverse of the normal survival function.
  63. double InverseNormalSurvival(double x);
  64. // Returns whether actual is "near" expected, based on the bound.
  65. bool Near(y_absl::string_view msg, double actual, double expected, double bound);
  66. // Implements the incomplete regularized beta function, AS63, BETAIN.
  67. // https://www.jstor.org/stable/2346797
  68. //
  69. // BetaIncomplete(x, p, q), where
  70. // `x` is the value of the upper limit
  71. // `p` is beta parameter p, `q` is beta parameter q.
  72. //
  73. // NOTE: This is a test-only function which is only accurate to within, at most,
  74. // 1e-13 of the actual value.
  75. //
  76. double BetaIncomplete(double x, double p, double q);
  77. // Implements the inverse of the incomplete regularized beta function, AS109,
  78. // XINBTA.
  79. // https://www.jstor.org/stable/2346798
  80. // https://www.jstor.org/stable/2346887
  81. //
  82. // BetaIncompleteInv(p, q, beta, alhpa)
  83. // `p` is beta parameter p, `q` is beta parameter q.
  84. // `alpha` is the value of the lower tail area.
  85. //
  86. // NOTE: This is a test-only function and, when successful, is only accurate to
  87. // within ~1e-6 of the actual value; there are some cases where it diverges from
  88. // the actual value by much more than that. The function uses Newton's method,
  89. // and thus the runtime is highly variable.
  90. double BetaIncompleteInv(double p, double q, double alpha);
  91. } // namespace random_internal
  92. Y_ABSL_NAMESPACE_END
  93. } // namespace y_absl
  94. #endif // Y_ABSL_RANDOM_INTERNAL_DISTRIBUTION_TEST_UTIL_H_