File indexing completed on 2025-01-30 09:43:43
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_HISTOGRAM_ACCUMULATORS_FRACTION_HPP
0008 #define BOOST_HISTOGRAM_ACCUMULATORS_FRACTION_HPP
0009
0010 #include <boost/core/nvp.hpp>
0011 #include <boost/histogram/fwd.hpp> // for fraction<>
0012 #include <boost/histogram/utility/wilson_interval.hpp>
0013 #include <type_traits> // for std::common_type
0014
0015 namespace boost {
0016 namespace histogram {
0017 namespace accumulators {
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 template <class ValueType>
0033 class fraction {
0034 public:
0035 using value_type = ValueType;
0036 using const_reference = const value_type&;
0037 using real_type = typename std::conditional<std::is_floating_point<value_type>::value,
0038 value_type, double>::type;
0039 using interval_type = typename utility::wilson_interval<real_type>::interval_type;
0040
0041 fraction() noexcept = default;
0042
0043
0044 fraction(const_reference successes, const_reference failures) noexcept
0045 : succ_(successes), fail_(failures) {}
0046
0047
0048 template <class T>
0049 fraction(const fraction<T>& e) noexcept
0050 : fraction{static_cast<value_type>(e.successes()),
0051 static_cast<value_type>(e.failures())} {}
0052
0053
0054 void operator()(bool x) noexcept {
0055 if (x)
0056 ++succ_;
0057 else
0058 ++fail_;
0059 }
0060
0061
0062 fraction& operator+=(const fraction& rhs) noexcept {
0063 succ_ += rhs.succ_;
0064 fail_ += rhs.fail_;
0065 return *this;
0066 }
0067
0068
0069 const_reference successes() const noexcept { return succ_; }
0070
0071
0072 const_reference failures() const noexcept { return fail_; }
0073
0074
0075 value_type count() const noexcept { return succ_ + fail_; }
0076
0077
0078 real_type value() const noexcept { return static_cast<real_type>(succ_) / count(); }
0079
0080
0081 real_type variance() const noexcept {
0082
0083
0084
0085
0086 const real_type p = value();
0087 return p * (1 - p) / count();
0088 }
0089
0090
0091 interval_type confidence_interval() const noexcept {
0092 return utility::wilson_interval<real_type>()(static_cast<real_type>(successes()),
0093 static_cast<real_type>(failures()));
0094 }
0095
0096 bool operator==(const fraction& rhs) const noexcept {
0097 return succ_ == rhs.succ_ && fail_ == rhs.fail_;
0098 }
0099
0100 bool operator!=(const fraction& rhs) const noexcept { return !operator==(rhs); }
0101
0102 template <class Archive>
0103 void serialize(Archive& ar, unsigned ) {
0104 ar& make_nvp("successes", succ_);
0105 ar& make_nvp("failures", fail_);
0106 }
0107
0108 private:
0109 value_type succ_{};
0110 value_type fail_{};
0111 };
0112
0113 }
0114 }
0115 }
0116
0117 #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
0118
0119 namespace std {
0120 template <class T, class U>
0121
0122 struct common_type<boost::histogram::accumulators::fraction<T>,
0123 boost::histogram::accumulators::fraction<U>> {
0124 using type = boost::histogram::accumulators::fraction<common_type_t<T, U>>;
0125 };
0126 }
0127
0128 #endif
0129
0130 #endif