jeffreys_interval.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2022 Jay Gohil, Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_UTILITY_JEFFREYS_INTERVAL_HPP
  7. #define BOOST_HISTOGRAM_UTILITY_JEFFREYS_INTERVAL_HPP
  8. #include <boost/histogram/fwd.hpp>
  9. #include <boost/histogram/utility/binomial_proportion_interval.hpp>
  10. #include <boost/math/distributions/beta.hpp>
  11. #include <cmath>
  12. namespace boost {
  13. namespace histogram {
  14. namespace utility {
  15. /**
  16. Jeffreys interval.
  17. This is the Bayesian credible interval with a Jeffreys prior. Although it has a
  18. Bayesian derivation, it has good coverage. The interval boundaries are close to the
  19. Wilson interval. A special property of this interval is that it is equal-tailed; the
  20. probability of the true value to be above or below the interval is approximately equal.
  21. To avoid coverage probability tending to zero when the fraction approaches 0 or 1,
  22. this implementation uses a modification described in section 4.1.2 of the
  23. paper by L.D. Brown, T.T. Cai, A. DasGupta, Statistical Science 16 (2001) 101-133,
  24. doi:10.1214/ss/1009213286.
  25. */
  26. template <class ValueType>
  27. class jeffreys_interval : public binomial_proportion_interval<ValueType> {
  28. public:
  29. using value_type = typename jeffreys_interval::value_type;
  30. using interval_type = typename jeffreys_interval::interval_type;
  31. /** Construct Jeffreys interval computer.
  32. @param cl Confidence level for the interval. The default value produces a
  33. confidence level of 68 % equivalent to one standard deviation. Both `deviation` and
  34. `confidence_level` objects can be used to initialize the interval.
  35. */
  36. explicit jeffreys_interval(confidence_level cl = deviation{1}) noexcept
  37. : alpha_half_{static_cast<value_type>(0.5 - 0.5 * static_cast<double>(cl))} {}
  38. using binomial_proportion_interval<ValueType>::operator();
  39. /** Compute interval for given number of successes and failures.
  40. @param successes Number of successful trials.
  41. @param failures Number of failed trials.
  42. */
  43. interval_type operator()(value_type successes, value_type failures) const noexcept {
  44. // See L.D. Brown, T.T. Cai, A. DasGupta, Statistical Science 16 (2001) 101-133,
  45. // doi:10.1214/ss/1009213286, section 4.1.2.
  46. const value_type half{0.5};
  47. const value_type total = successes + failures;
  48. // if successes or failures are 0, modified interval is equal to Clopper-Pearson
  49. if (successes == 0) return {0, 1 - std::pow(alpha_half_, 1 / total)};
  50. if (failures == 0) return {std::pow(alpha_half_, 1 / total), 1};
  51. math::beta_distribution<value_type> beta(successes + half, failures + half);
  52. const value_type a = successes == 1 ? 0 : math::quantile(beta, alpha_half_);
  53. const value_type b = failures == 1 ? 1 : math::quantile(beta, 1 - alpha_half_);
  54. return {a, b};
  55. }
  56. private:
  57. value_type alpha_half_;
  58. };
  59. } // namespace utility
  60. } // namespace histogram
  61. } // namespace boost
  62. #endif