123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #ifndef BOOST_HISTOGRAM_ACCUMULATORS_FRACTION_HPP
- #define BOOST_HISTOGRAM_ACCUMULATORS_FRACTION_HPP
- #include <boost/core/nvp.hpp>
- #include <boost/histogram/fwd.hpp> // for fraction<>
- #include <boost/histogram/utility/wilson_interval.hpp>
- #include <type_traits> // for std::common_type
- namespace boost {
- namespace histogram {
- namespace accumulators {
- template <class ValueType>
- class fraction {
- public:
- using value_type = ValueType;
- using const_reference = const value_type&;
- using real_type = typename std::conditional<std::is_floating_point<value_type>::value,
- value_type, double>::type;
- using interval_type = typename utility::wilson_interval<real_type>::interval_type;
- fraction() noexcept = default;
-
- fraction(const_reference successes, const_reference failures) noexcept
- : succ_(successes), fail_(failures) {}
-
- template <class T>
- fraction(const fraction<T>& e) noexcept
- : fraction{static_cast<value_type>(e.successes()),
- static_cast<value_type>(e.failures())} {}
-
- void operator()(bool x) noexcept {
- if (x)
- ++succ_;
- else
- ++fail_;
- }
-
- fraction& operator+=(const fraction& rhs) noexcept {
- succ_ += rhs.succ_;
- fail_ += rhs.fail_;
- return *this;
- }
-
- const_reference successes() const noexcept { return succ_; }
-
- const_reference failures() const noexcept { return fail_; }
-
- value_type count() const noexcept { return succ_ + fail_; }
-
- real_type value() const noexcept { return static_cast<real_type>(succ_) / count(); }
-
- real_type variance() const noexcept {
-
-
-
-
- const real_type p = value();
- return p * (1 - p) / count();
- }
-
- interval_type confidence_interval() const noexcept {
- return utility::wilson_interval<real_type>()(static_cast<real_type>(successes()),
- static_cast<real_type>(failures()));
- }
- bool operator==(const fraction& rhs) const noexcept {
- return succ_ == rhs.succ_ && fail_ == rhs.fail_;
- }
- bool operator!=(const fraction& rhs) const noexcept { return !operator==(rhs); }
- template <class Archive>
- void serialize(Archive& ar, unsigned /* version */) {
- ar& make_nvp("successes", succ_);
- ar& make_nvp("failures", fail_);
- }
- private:
- value_type succ_{};
- value_type fail_{};
- };
- }
- }
- }
- #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
- namespace std {
- template <class T, class U>
- /// Specialization for boost::histogram::accumulators::fraction.
- struct common_type<boost::histogram::accumulators::fraction<T>,
- boost::histogram::accumulators::fraction<U>> {
- using type = boost::histogram::accumulators::fraction<common_type_t<T, U>>;
- };
- }
- #endif
- #endif
|