Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Keep bootstrap confidence interval indices in range

When all jackknife estimates are equal, the acceleration was computed as
0 / 0, the adjusted quantiles became NaN and std::lround(NaN) gave
LONG_MIN. The upper index was only clamped from above, so it ended up as
a huge size_t and resample[hi] read out of bounds. With a 64-bit long
this happens to wrap around to resample[0], with a 32-bit long (MinGW)
it crashes.

Treat zero jackknife variance as zero acceleration, and clamp both
indices to [0, n - 1], falling back to the ends of the resample if the
quantile is still NaN.

Fixes #3214

Upstream-commit: catchorg/Catch2@a1a991fee5f217b40035cb2f8ebc83864d66408d
Commit
e20068a17d90a6a7f4c8ba723b3089793682b1e8
Author
fhgffy ·
Committer
Leitwolf11
Parents
16cf55a45b4f

Files · Download .tar.gz

Changes

 src/Progmasoft/Catch3/Benchmark/Detail/Stats.cpp   | 25 +++++++++++-----
 .../IntrospectiveTests/InternalBenchmark.tests.cpp | 35 ++++++++++++++++++++++
 2 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/src/Progmasoft/Catch3/Benchmark/Detail/Stats.cpp b/src/Progmasoft/Catch3/Benchmark/Detail/Stats.cpp
index 377a1cb0..11c7c363 100644
--- a/src/Progmasoft/Catch3/Benchmark/Detail/Stats.cpp
+++ b/src/Progmasoft/Catch3/Benchmark/Detail/Stats.cpp
@@ -314,7 +314,12 @@ namespace Catch {
                     sum_cubes += cube;
                 }
 
-                double accel = sum_cubes / ( 6 * std::pow( sum_squares, 1.5 ) );
+                // With no spread in the jackknife estimates there is nothing
+                // to estimate the acceleration from (and 0 / 0 would be NaN)
+                double accel =
+                    sum_squares > 0
+                        ? sum_cubes / ( 6 * std::pow( sum_squares, 1.5 ) )
+                        : 0.;
                 long n = static_cast<long>( resample.size() );
                 double prob_n = static_cast<double>(
                     std::count_if( resample.begin(),
@@ -329,9 +334,16 @@ namespace Catch {
                 double bias = normal_quantile( prob_n );
                 double z1 = normal_quantile( ( 1. - confidence_level ) / 2. );
 
-                auto cumn = [n]( double x ) -> long {
-                    return std::lround( normal_cdf( x ) *
-                                        static_cast<double>( n ) );
+                // a1 and a2 can still end up NaN or infinite for degenerate
+                // inputs, so make sure the index always stays inside resample
+                auto cumn = [n]( double x, long fallback ) -> size_t {
+                    if ( std::isnan( x ) ) {
+                        return static_cast<size_t>( fallback );
+                    }
+                    long idx = std::lround( normal_cdf( x ) *
+                                            static_cast<double>( n ) );
+                    return static_cast<size_t>(
+                        (std::min)( (std::max)( idx, 0l ), n - 1 ) );
                 };
                 auto a = [bias, accel]( double b ) {
                     return bias + b / ( 1. - accel * b );
@@ -340,9 +352,8 @@ namespace Catch {
                 double b2 = bias - z1;
                 double a1 = a( b1 );
                 double a2 = a( b2 );
-                auto lo = static_cast<size_t>( (std::max)( cumn( a1 ), 0l ) );
-                auto hi =
-                    static_cast<size_t>( (std::min)( cumn( a2 ), n - 1 ) );
+                auto lo = cumn( a1, 0 );
+                auto hi = cumn( a2, n - 1 );
 
                 return { point, resample[lo], resample[hi], confidence_level };
             }
diff --git a/tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp b/tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp
index 45707baf..96481c9e 100644
--- a/tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp
+++ b/tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp
@@ -171,6 +171,41 @@ TEST_CASE("uniform samples", "[benchmark]") {
     CHECK(e.confidence_interval == 0.95);
 }
 
+TEST_CASE("bootstrap keeps the interval inside the resample", "[benchmark]") {
+    auto mean = []( double const* a, double const* b ) {
+        return std::accumulate( a, b, 0. ) / ( b - a );
+    };
+
+    SECTION( "no spread in the jackknife estimates" ) {
+        // All jackknife estimates are equal, which used to make the
+        // acceleration 0 / 0 and the upper index garbage
+        std::vector<double> samples( 100, 23 );
+        std::vector<double> resample{ 22, 23, 23, 24 };
+        auto e = Catch::Benchmark::Detail::bootstrap(
+            0.95,
+            samples.data(),
+            samples.data() + samples.size(),
+            resample,
+            mean );
+        CHECK( e.point == 23 );
+        CHECK( e.lower_bound == 22 );
+        CHECK( e.upper_bound == 24 );
+    }
+    SECTION( "every resampled estimate below the point estimate" ) {
+        std::vector<double> samples{ 1, 2, 3, 10 };
+        std::vector<double> resample{ 0.5, 1, 1.5, 2 };
+        auto e = Catch::Benchmark::Detail::bootstrap(
+            0.95,
+            samples.data(),
+            samples.data() + samples.size(),
+            resample,
+            mean );
+        CHECK( e.point == 4 );
+        CHECK( e.lower_bound == 0.5 );
+        CHECK( e.upper_bound == 2 );
+    }
+}
+
 
 TEST_CASE("normal_cdf", "[benchmark][approvals]") {
     using Catch::Benchmark::Detail::normal_cdf;