File indexing completed on 2025-01-18 09:28:22
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006
0009 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_QUANTILE_HPP_DE_01_01_2006
0010
0011 #include <vector>
0012 #include <limits>
0013 #include <functional>
0014 #include <sstream>
0015 #include <stdexcept>
0016 #include <boost/throw_exception.hpp>
0017 #include <boost/parameter/keyword.hpp>
0018 #include <boost/mpl/placeholders.hpp>
0019 #include <boost/mpl/if.hpp>
0020 #include <boost/type_traits/is_same.hpp>
0021 #include <boost/accumulators/numeric/functional.hpp>
0022 #include <boost/accumulators/framework/depends_on.hpp>
0023 #include <boost/accumulators/framework/accumulator_base.hpp>
0024 #include <boost/accumulators/framework/extractor.hpp>
0025 #include <boost/accumulators/framework/parameters/sample.hpp>
0026 #include <boost/accumulators/statistics_fwd.hpp>
0027 #include <boost/accumulators/statistics/tail.hpp>
0028 #include <boost/accumulators/statistics/tail_quantile.hpp>
0029 #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
0030
0031 #ifdef _MSC_VER
0032 # pragma warning(push)
0033 # pragma warning(disable: 4127)
0034 #endif
0035
0036 namespace boost { namespace accumulators
0037 {
0038
0039 namespace impl
0040 {
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065 template<typename Sample, typename Weight, typename LeftRight>
0066 struct weighted_tail_quantile_impl
0067 : accumulator_base
0068 {
0069 typedef typename numeric::functional::fdiv<Weight, std::size_t>::result_type float_type;
0070
0071 typedef Sample result_type;
0072
0073 weighted_tail_quantile_impl(dont_care) {}
0074
0075 template<typename Args>
0076 result_type result(Args const &args) const
0077 {
0078 float_type threshold = sum_of_weights(args)
0079 * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] );
0080
0081 std::size_t n = 0;
0082 Weight sum = Weight(0);
0083
0084 while (sum < threshold)
0085 {
0086 if (n < static_cast<std::size_t>(tail_weights(args).size()))
0087 {
0088 sum += *(tail_weights(args).begin() + n);
0089 n++;
0090 }
0091 else
0092 {
0093 if (std::numeric_limits<result_type>::has_quiet_NaN)
0094 {
0095 return std::numeric_limits<result_type>::quiet_NaN();
0096 }
0097 else
0098 {
0099 std::ostringstream msg;
0100 msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
0101 boost::throw_exception(std::runtime_error(msg.str()));
0102 return Sample(0);
0103 }
0104 }
0105 }
0106
0107
0108
0109 return *(boost::begin(tail(args)) + n - 1);
0110 }
0111 };
0112 }
0113
0114
0115
0116
0117 namespace tag
0118 {
0119 template<typename LeftRight>
0120 struct weighted_tail_quantile
0121 : depends_on<sum_of_weights, tail_weights<LeftRight> >
0122 {
0123
0124 typedef accumulators::impl::weighted_tail_quantile_impl<mpl::_1, mpl::_2, LeftRight> impl;
0125 };
0126 }
0127
0128
0129
0130
0131 namespace extract
0132 {
0133 extractor<tag::quantile> const weighted_tail_quantile = {};
0134
0135 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_tail_quantile)
0136 }
0137
0138 using extract::weighted_tail_quantile;
0139
0140 }}
0141
0142 #ifdef _MSC_VER
0143 # pragma warning(pop)
0144 #endif
0145
0146 #endif