File Coverage

/usr/local/lib/perl5/site_perl/5.26.1/x86_64-linux/XS/libcatch.x/i/catch2/benchmark/detail/catch_stats.hpp
Criterion Covered Total %
statement 0 22 0.0
branch 0 16 0.0
condition n/a
subroutine n/a
pod n/a
total 0 38 0.0


line stmt bran cond sub pod time code
1              
2             // Copyright Catch2 Authors
3             // Distributed under the Boost Software License, Version 1.0.
4             // (See accompanying file LICENSE_1_0.txt or copy at
5             // https://www.boost.org/LICENSE_1_0.txt)
6              
7             // SPDX-License-Identifier: BSL-1.0
8             // Adapted from donated nonius code.
9              
10             #ifndef CATCH_STATS_HPP_INCLUDED
11             #define CATCH_STATS_HPP_INCLUDED
12              
13             #include
14             #include
15              
16             #include
17             #include
18             #include
19             #include
20             #include
21              
22             namespace Catch {
23             namespace Benchmark {
24             namespace Detail {
25             using sample = std::vector;
26              
27             double weighted_average_quantile(int k, int q, std::vector::iterator first, std::vector::iterator last);
28              
29             template
30 0           OutlierClassification classify_outliers(Iterator first, Iterator last) {
31 0 0         std::vector copy(first, last);
32              
33 0 0         auto q1 = weighted_average_quantile(1, 4, copy.begin(), copy.end());
34 0 0         auto q3 = weighted_average_quantile(3, 4, copy.begin(), copy.end());
35 0           auto iqr = q3 - q1;
36 0           auto los = q1 - (iqr * 3.);
37 0           auto lom = q1 - (iqr * 1.5);
38 0           auto him = q3 + (iqr * 1.5);
39 0           auto his = q3 + (iqr * 3.);
40              
41 0           OutlierClassification o;
42 0 0         for (; first != last; ++first) {
43 0           auto&& t = *first;
44 0 0         if (t < los) ++o.low_severe;
45 0 0         else if (t < lom) ++o.low_mild;
46 0 0         else if (t > his) ++o.high_severe;
47 0 0         else if (t > him) ++o.high_mild;
48 0           ++o.samples_seen;
49             }
50 0           return o;
51             }
52              
53             template
54 0           double mean(Iterator first, Iterator last) {
55 0           auto count = last - first;
56 0           double sum = std::accumulate(first, last, 0.);
57 0           return sum / count;
58             }
59              
60             template
61             sample jackknife(Estimator&& estimator, Iterator first, Iterator last) {
62             auto n = last - first;
63             auto second = first;
64             ++second;
65             sample results;
66             results.reserve(n);
67              
68             for (auto it = first; it != last; ++it) {
69             std::iter_swap(it, first);
70             results.push_back(estimator(second, last));
71             }
72              
73             return results;
74             }
75              
76             inline double normal_cdf(double x) {
77             return std::erfc(-x / std::sqrt(2.0)) / 2.0;
78             }
79              
80             double erfc_inv(double x);
81              
82             double normal_quantile(double p);
83              
84             template
85             Estimate bootstrap(double confidence_level, Iterator first, Iterator last, sample const& resample, Estimator&& estimator) {
86             auto n_samples = last - first;
87              
88             double point = estimator(first, last);
89             // Degenerate case with a single sample
90             if (n_samples == 1) return { point, point, point, confidence_level };
91              
92             sample jack = jackknife(estimator, first, last);
93             double jack_mean = mean(jack.begin(), jack.end());
94             double sum_squares, sum_cubes;
95             std::tie(sum_squares, sum_cubes) = std::accumulate(jack.begin(), jack.end(), std::make_pair(0., 0.), [jack_mean](std::pair sqcb, double x) -> std::pair {
96             auto d = jack_mean - x;
97             auto d2 = d * d;
98             auto d3 = d2 * d;
99             return { sqcb.first + d2, sqcb.second + d3 };
100             });
101              
102             double accel = sum_cubes / (6 * std::pow(sum_squares, 1.5));
103             int n = static_cast(resample.size());
104             double prob_n = std::count_if(resample.begin(), resample.end(), [point](double x) { return x < point; }) / static_cast(n);
105             // degenerate case with uniform samples
106             if (prob_n == 0) return { point, point, point, confidence_level };
107              
108             double bias = normal_quantile(prob_n);
109             double z1 = normal_quantile((1. - confidence_level) / 2.);
110              
111             auto cumn = [n](double x) -> int {
112             return std::lround(normal_cdf(x) * n); };
113             auto a = [bias, accel](double b) { return bias + b / (1. - accel * b); };
114             double b1 = bias + z1;
115             double b2 = bias - z1;
116             double a1 = a(b1);
117             double a2 = a(b2);
118             auto lo = (std::max)(cumn(a1), 0);
119             auto hi = (std::min)(cumn(a2), n - 1);
120              
121             return { point, resample[lo], resample[hi], confidence_level };
122             }
123              
124             double outlier_variance(Estimate mean, Estimate stddev, int n);
125              
126             struct bootstrap_analysis {
127             Estimate mean;
128             Estimate standard_deviation;
129             double outlier_variance;
130             };
131              
132             bootstrap_analysis analyse_samples(double confidence_level, int n_resamples, std::vector::iterator first, std::vector::iterator last);
133             } // namespace Detail
134             } // namespace Benchmark
135             } // namespace Catch
136              
137             #endif // CATCH_STATS_HPP_INCLUDED