wald_interval.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_WALD_INTERVAL_HPP
  7. #define BOOST_HISTOGRAM_UTILITY_WALD_INTERVAL_HPP
  8. #include <boost/histogram/fwd.hpp>
  9. #include <boost/histogram/utility/binomial_proportion_interval.hpp>
  10. #include <cmath>
  11. #include <utility>
  12. namespace boost {
  13. namespace histogram {
  14. namespace utility {
  15. /**
  16. Wald interval or normal approximation interval.
  17. The Wald interval is a symmetric interval. It is simple to compute, but has poor
  18. statistical properties and is universally rejected by statisticians. It should always be
  19. replaced by another iternal, for example, the Wilson interval.
  20. The Wald interval can be derived easily using the plug-in estimate of the variance for
  21. the binomial distribution, which is likely a reason for its omnipresence. Without
  22. further insight into statistical theory, it is not obvious that this derivation is
  23. flawed and that better alternatives exist.
  24. The Wald interval undercovers on average. It is unsuitable when the sample size is small
  25. or when the fraction is close to 0 or 1. e. Its limits are not naturally bounded by 0
  26. or 1. It produces empty intervals if the number of successes or failures is zero.
  27. For a critique of the Wald interval, see (a selection):
  28. L.D. Brown, T.T. Cai, A. DasGupta, Statistical Science 16 (2001) 101-133.
  29. R. D. Cousins, K. E. Hymes, J. Tucker, Nucl. Instrum. Meth. A 612 (2010) 388-398.
  30. */
  31. template <class ValueType>
  32. class wald_interval : public binomial_proportion_interval<ValueType> {
  33. public:
  34. using value_type = typename wald_interval::value_type;
  35. using interval_type = typename wald_interval::interval_type;
  36. /** Construct Wald interval computer.
  37. @param d Number of standard deviations for the interval. The default value 1
  38. corresponds to a confidence level of 68 %. Both `deviation` and `confidence_level`
  39. objects can be used to initialize the interval.
  40. */
  41. explicit wald_interval(deviation d = deviation{1.0}) noexcept
  42. : z_{static_cast<value_type>(d)} {}
  43. using binomial_proportion_interval<ValueType>::operator();
  44. /** Compute interval for given number of successes and failures.
  45. @param successes Number of successful trials.
  46. @param failures Number of failed trials.
  47. */
  48. interval_type operator()(value_type successes, value_type failures) const noexcept {
  49. // See https://en.wikipedia.org/wiki/
  50. // Binomial_proportion_confidence_interval
  51. // #Normal_approximation_interval_or_Wald_interval
  52. const value_type total_inv = 1 / (successes + failures);
  53. const value_type a = successes * total_inv;
  54. const value_type b = (z_ * total_inv) * std::sqrt(successes * failures * total_inv);
  55. return {a - b, a + b};
  56. }
  57. private:
  58. value_type z_;
  59. };
  60. } // namespace utility
  61. } // namespace histogram
  62. } // namespace boost
  63. #endif