mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 07:16:10 +01:00
Internal linkage for outlier_variance
This commit is contained in:
parent
10f0a58643
commit
51fdbedd13
@ -21,28 +21,34 @@
|
|||||||
#include <future>
|
#include <future>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace Catch {
|
||||||
|
namespace Benchmark {
|
||||||
|
namespace Detail {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using Catch::Benchmark::Detail::sample;
|
|
||||||
|
|
||||||
template <typename URng, typename Estimator>
|
template <typename URng, typename Estimator>
|
||||||
static sample resample(URng& rng, unsigned int resamples,
|
static sample
|
||||||
|
resample( URng& rng,
|
||||||
|
unsigned int resamples,
|
||||||
std::vector<double>::const_iterator first,
|
std::vector<double>::const_iterator first,
|
||||||
std::vector<double>::const_iterator last,
|
std::vector<double>::const_iterator last,
|
||||||
Estimator& estimator ) {
|
Estimator& estimator ) {
|
||||||
auto n = static_cast<size_t>( last - first );
|
auto n = static_cast<size_t>( last - first );
|
||||||
std::uniform_int_distribution<decltype(n)> dist(0, n - 1);
|
std::uniform_int_distribution<decltype( n )> dist( 0,
|
||||||
|
n - 1 );
|
||||||
|
|
||||||
sample out;
|
sample out;
|
||||||
out.reserve( resamples );
|
out.reserve( resamples );
|
||||||
// We allocate the vector outside the loop to avoid realloc per resample
|
// We allocate the vector outside the loop to avoid realloc
|
||||||
|
// per resample
|
||||||
std::vector<double> resampled;
|
std::vector<double> resampled;
|
||||||
resampled.reserve( n );
|
resampled.reserve( n );
|
||||||
for ( size_t i = 0; i < resamples; ++i ) {
|
for ( size_t i = 0; i < resamples; ++i ) {
|
||||||
resampled.clear();
|
resampled.clear();
|
||||||
for ( size_t s = 0; s < n; ++s ) {
|
for ( size_t s = 0; s < n; ++s ) {
|
||||||
resampled.push_back(
|
resampled.push_back(
|
||||||
first[static_cast<std::ptrdiff_t>( dist( rng ) )] );
|
first[static_cast<std::ptrdiff_t>(
|
||||||
|
dist( rng ) )] );
|
||||||
}
|
}
|
||||||
const auto estimate =
|
const auto estimate =
|
||||||
estimator( resampled.begin(), resampled.end() );
|
estimator( resampled.begin(), resampled.end() );
|
||||||
@ -52,9 +58,42 @@ using Catch::Benchmark::Detail::sample;
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static double outlier_variance( Estimate<double> mean,
|
||||||
|
Estimate<double> stddev,
|
||||||
|
int n ) {
|
||||||
|
double sb = stddev.point;
|
||||||
|
double mn = mean.point / n;
|
||||||
|
double mg_min = mn / 2.;
|
||||||
|
double sg = (std::min)( mg_min / 4., sb / std::sqrt( n ) );
|
||||||
|
double sg2 = sg * sg;
|
||||||
|
double sb2 = sb * sb;
|
||||||
|
|
||||||
double erf_inv(double x) {
|
auto c_max = [n, mn, sb2, sg2]( double x ) -> double {
|
||||||
// Code accompanying the article "Approximating the erfinv function" in GPU Computing Gems, Volume 2
|
double k = mn - x;
|
||||||
|
double d = k * k;
|
||||||
|
double nd = n * d;
|
||||||
|
double k0 = -n * nd;
|
||||||
|
double k1 = sb2 - n * sg2 + nd;
|
||||||
|
double det = k1 * k1 - 4 * sg2 * k0;
|
||||||
|
return static_cast<int>( -2. * k0 /
|
||||||
|
( k1 + std::sqrt( det ) ) );
|
||||||
|
};
|
||||||
|
|
||||||
|
auto var_out = [n, sb2, sg2]( double c ) {
|
||||||
|
double nc = n - c;
|
||||||
|
return ( nc / n ) * ( sb2 - nc * sg2 );
|
||||||
|
};
|
||||||
|
|
||||||
|
return (std::min)( var_out( 1 ),
|
||||||
|
var_out(
|
||||||
|
(std::min)( c_max( 0. ),
|
||||||
|
c_max( mg_min ) ) ) ) /
|
||||||
|
sb2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double erf_inv( double x ) {
|
||||||
|
// Code accompanying the article "Approximating the erfinv
|
||||||
|
// function" in GPU Computing Gems, Volume 2
|
||||||
double w, p;
|
double w, p;
|
||||||
|
|
||||||
w = -log( ( 1.0 - x ) * ( 1.0 + x ) );
|
w = -log( ( 1.0 - x ) * ( 1.0 + x ) );
|
||||||
@ -128,10 +167,12 @@ using Catch::Benchmark::Detail::sample;
|
|||||||
return p * x;
|
return p * x;
|
||||||
}
|
}
|
||||||
|
|
||||||
double standard_deviation(std::vector<double>::const_iterator first,
|
static double
|
||||||
|
standard_deviation( std::vector<double>::const_iterator first,
|
||||||
std::vector<double>::const_iterator last ) {
|
std::vector<double>::const_iterator last ) {
|
||||||
auto m = Catch::Benchmark::Detail::mean( first, last );
|
auto m = Catch::Benchmark::Detail::mean( first, last );
|
||||||
double variance = std::accumulate( first,
|
double variance =
|
||||||
|
std::accumulate( first,
|
||||||
last,
|
last,
|
||||||
0.,
|
0.,
|
||||||
[m]( double a, double b ) {
|
[m]( double a, double b ) {
|
||||||
@ -142,7 +183,10 @@ using Catch::Benchmark::Detail::sample;
|
|||||||
return std::sqrt( variance );
|
return std::sqrt( variance );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace
|
||||||
|
} // namespace Detail
|
||||||
|
} // namespace Benchmark
|
||||||
|
} // namespace Catch
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
namespace Benchmark {
|
namespace Benchmark {
|
||||||
@ -234,34 +278,6 @@ namespace Catch {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
double outlier_variance(Estimate<double> mean, Estimate<double> stddev, int n) {
|
|
||||||
double sb = stddev.point;
|
|
||||||
double mn = mean.point / n;
|
|
||||||
double mg_min = mn / 2.;
|
|
||||||
double sg = (std::min)(mg_min / 4., sb / std::sqrt(n));
|
|
||||||
double sg2 = sg * sg;
|
|
||||||
double sb2 = sb * sb;
|
|
||||||
|
|
||||||
auto c_max = [n, mn, sb2, sg2](double x) -> double {
|
|
||||||
double k = mn - x;
|
|
||||||
double d = k * k;
|
|
||||||
double nd = n * d;
|
|
||||||
double k0 = -n * nd;
|
|
||||||
double k1 = sb2 - n * sg2 + nd;
|
|
||||||
double det = k1 * k1 - 4 * sg2 * k0;
|
|
||||||
return static_cast<int>(-2. * k0 / (k1 + std::sqrt(det)));
|
|
||||||
};
|
|
||||||
|
|
||||||
auto var_out = [n, sb2, sg2](double c) {
|
|
||||||
double nc = n - c;
|
|
||||||
return (nc / n) * (sb2 - nc * sg2);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (std::min)(var_out(1), var_out((std::min)(c_max(0.), c_max(mg_min)))) / sb2;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bootstrap_analysis analyse_samples(double confidence_level,
|
bootstrap_analysis analyse_samples(double confidence_level,
|
||||||
unsigned int n_resamples,
|
unsigned int n_resamples,
|
||||||
std::vector<double>::iterator first,
|
std::vector<double>::iterator first,
|
||||||
|
@ -108,8 +108,6 @@ namespace Catch {
|
|||||||
return { point, resample[lo], resample[hi], confidence_level };
|
return { point, resample[lo], resample[hi], confidence_level };
|
||||||
}
|
}
|
||||||
|
|
||||||
double outlier_variance(Estimate<double> mean, Estimate<double> stddev, int n);
|
|
||||||
|
|
||||||
struct bootstrap_analysis {
|
struct bootstrap_analysis {
|
||||||
Estimate<double> mean;
|
Estimate<double> mean;
|
||||||
Estimate<double> standard_deviation;
|
Estimate<double> standard_deviation;
|
||||||
|
Loading…
Reference in New Issue
Block a user