tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

distribution_test_util.h (4144B)


      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 <ostream>
     20 
     21 #include "absl/base/config.h"
     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 {
     29 ABSL_NAMESPACE_BEGIN
     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
     37 struct DistributionMoments {
     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 };
     44 DistributionMoments ComputeDistributionMoments(
     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, alpha)
    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
    110 ABSL_NAMESPACE_END
    111 }  // namespace absl
    112 
    113 #endif  // ABSL_RANDOM_INTERNAL_DISTRIBUTION_TEST_UTIL_H_