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


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:16