jeffreys_interval.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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,
  44. value_type failures) const noexcept override {
  45. // See L.D. Brown, T.T. Cai, A. DasGupta, Statistical Science 16 (2001) 101-133,
  46. // doi:10.1214/ss/1009213286, section 4.1.2.
  47. const value_type half{0.5}, one{1.0}, zero{0.0};
  48. const value_type total = successes + failures;
  49. // if successes or failures are 0, modified interval is equal to Clopper-Pearson
  50. if (successes == 0) return {zero, one - std::pow(alpha_half_, one / total)};
  51. if (failures == 0) return {std::pow(alpha_half_, one / total), one};
  52. math::beta_distribution<value_type> beta(successes + half, failures + half);
  53. const value_type a = successes == 1 ? zero : math::quantile(beta, alpha_half_);
  54. const value_type b = failures == 1 ? one : math::quantile(beta, one - alpha_half_);
  55. return {a, b};
  56. }
  57. private:
  58. value_type alpha_half_;
  59. };
  60. } // namespace utility
  61. } // namespace histogram
  62. } // namespace boost
  63. #endif