File indexing completed on 2025-01-18 09:38:12
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_HISTOGRAM_UTILITY_JEFFREYS_INTERVAL_HPP
0008 #define BOOST_HISTOGRAM_UTILITY_JEFFREYS_INTERVAL_HPP
0009
0010 #include <boost/histogram/fwd.hpp>
0011 #include <boost/histogram/utility/binomial_proportion_interval.hpp>
0012 #include <boost/math/distributions/beta.hpp>
0013 #include <cmath>
0014
0015 namespace boost {
0016 namespace histogram {
0017 namespace utility {
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 template <class ValueType>
0033 class jeffreys_interval : public binomial_proportion_interval<ValueType> {
0034 public:
0035 using value_type = typename jeffreys_interval::value_type;
0036 using interval_type = typename jeffreys_interval::interval_type;
0037
0038
0039
0040
0041
0042
0043
0044 explicit jeffreys_interval(confidence_level cl = deviation{1}) noexcept
0045 : alpha_half_{static_cast<value_type>(0.5 - 0.5 * static_cast<double>(cl))} {}
0046
0047 using binomial_proportion_interval<ValueType>::operator();
0048
0049
0050
0051
0052
0053
0054 interval_type operator()(value_type successes, value_type failures) const noexcept {
0055
0056
0057 const value_type half{0.5};
0058 const value_type total = successes + failures;
0059
0060
0061 if (successes == 0) return {0, 1 - std::pow(alpha_half_, 1 / total)};
0062 if (failures == 0) return {std::pow(alpha_half_, 1 / total), 1};
0063
0064 math::beta_distribution<value_type> beta(successes + half, failures + half);
0065 const value_type a = successes == 1 ? 0 : math::quantile(beta, alpha_half_);
0066 const value_type b = failures == 1 ? 1 : math::quantile(beta, 1 - alpha_half_);
0067 return {a, b};
0068 }
0069
0070 private:
0071 value_type alpha_half_;
0072 };
0073
0074 }
0075 }
0076 }
0077
0078 #endif