| 6812 | |
| 6813 | template <typename Iterator, typename Estimator> |
| 6814 | Estimate<double> bootstrap(double confidence_level, Iterator first, Iterator last, sample const& resample, Estimator&& estimator) { |
| 6815 | auto n_samples = last - first; |
| 6816 | |
| 6817 | double point = estimator(first, last); |
| 6818 | // Degenerate case with a single sample |
| 6819 | if (n_samples == 1) return { point, point, point, confidence_level }; |
| 6820 | |
| 6821 | sample jack = jackknife(estimator, first, last); |
| 6822 | double jack_mean = mean(jack.begin(), jack.end()); |
| 6823 | double sum_squares, sum_cubes; |
| 6824 | std::tie(sum_squares, sum_cubes) = std::accumulate(jack.begin(), jack.end(), std::make_pair(0., 0.), [jack_mean](std::pair<double, double> sqcb, double x) -> std::pair<double, double> { |
| 6825 | auto d = jack_mean - x; |
| 6826 | auto d2 = d * d; |
| 6827 | auto d3 = d2 * d; |
| 6828 | return { sqcb.first + d2, sqcb.second + d3 }; |
| 6829 | }); |
| 6830 | |
| 6831 | double accel = sum_cubes / (6 * std::pow(sum_squares, 1.5)); |
| 6832 | int n = static_cast<int>(resample.size()); |
| 6833 | double prob_n = std::count_if(resample.begin(), resample.end(), [point](double x) { return x < point; }) / (double)n; |
| 6834 | // degenerate case with uniform samples |
| 6835 | if (prob_n == 0) return { point, point, point, confidence_level }; |
| 6836 | |
| 6837 | double bias = normal_quantile(prob_n); |
| 6838 | double z1 = normal_quantile((1. - confidence_level) / 2.); |
| 6839 | |
| 6840 | auto cumn = [n](double x) -> int { |
| 6841 | return std::lround(normal_cdf(x) * n); }; |
| 6842 | auto a = [bias, accel](double b) { return bias + b / (1. - accel * b); }; |
| 6843 | double b1 = bias + z1; |
| 6844 | double b2 = bias - z1; |
| 6845 | double a1 = a(b1); |
| 6846 | double a2 = a(b2); |
| 6847 | auto lo = std::max(cumn(a1), 0); |
| 6848 | auto hi = std::min(cumn(a2), n - 1); |
| 6849 | |
| 6850 | return { point, resample[lo], resample[hi], confidence_level }; |
| 6851 | } |
| 6852 | |
| 6853 | double outlier_variance(Estimate<double> mean, Estimate<double> stddev, int n); |
| 6854 |
no test coverage detected