File indexing completed on 2025-01-18 09:28:21
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_ACCUMULATORS_STATISTICS_TAIL_QUANTILE_HPP_DE_01_01_2006
0009 #define BOOST_ACCUMULATORS_STATISTICS_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/config/no_tr1/cmath.hpp> // For ceil
0017 #include <boost/throw_exception.hpp>
0018 #include <boost/parameter/keyword.hpp>
0019 #include <boost/mpl/placeholders.hpp>
0020 #include <boost/mpl/if.hpp>
0021 #include <boost/type_traits/is_same.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/numeric/functional.hpp>
0026 #include <boost/accumulators/framework/parameters/sample.hpp>
0027 #include <boost/accumulators/statistics_fwd.hpp>
0028 #include <boost/accumulators/statistics/tail.hpp>
0029 #include <boost/accumulators/statistics/count.hpp>
0030 #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
0031
0032 #ifdef _MSC_VER
0033 # pragma warning(push)
0034 # pragma warning(disable: 4127)
0035 #endif
0036
0037 namespace boost { namespace accumulators
0038 {
0039
0040 namespace impl
0041 {
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 template<typename Sample, typename LeftRight>
0060 struct tail_quantile_impl
0061 : accumulator_base
0062 {
0063
0064 typedef Sample result_type;
0065
0066 tail_quantile_impl(dont_care) {}
0067
0068 template<typename Args>
0069 result_type result(Args const &args) const
0070 {
0071 std::size_t cnt = count(args);
0072
0073 std::size_t n = static_cast<std::size_t>(
0074 std::ceil(
0075 cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
0076 )
0077 );
0078
0079
0080 if ( n < static_cast<std::size_t>(tail(args).size()))
0081 {
0082
0083
0084 return *(boost::begin(tail(args)) + n - 1);
0085 }
0086 else
0087 {
0088 if (std::numeric_limits<result_type>::has_quiet_NaN)
0089 {
0090 return std::numeric_limits<result_type>::quiet_NaN();
0091 }
0092 else
0093 {
0094 std::ostringstream msg;
0095 msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
0096 boost::throw_exception(std::runtime_error(msg.str()));
0097 return Sample(0);
0098 }
0099 }
0100 }
0101
0102
0103 template<class Archive>
0104 void serialize(Archive & ar, const unsigned int file_version) {}
0105 };
0106 }
0107
0108
0109
0110
0111 namespace tag
0112 {
0113 template<typename LeftRight>
0114 struct tail_quantile
0115 : depends_on<count, tail<LeftRight> >
0116 {
0117
0118
0119 typedef accumulators::impl::tail_quantile_impl<mpl::_1, LeftRight> impl;
0120 };
0121 }
0122
0123
0124
0125
0126 namespace extract
0127 {
0128 extractor<tag::quantile> const tail_quantile = {};
0129
0130 BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_quantile)
0131 }
0132
0133 using extract::tail_quantile;
0134
0135
0136
0137 template<typename LeftRight>
0138 struct feature_of<tag::tail_quantile<LeftRight> >
0139 : feature_of<tag::quantile>
0140 {
0141 };
0142
0143
0144
0145 template<typename LeftRight>
0146 struct as_weighted_feature<tag::tail_quantile<LeftRight> >
0147 {
0148 typedef tag::weighted_tail_quantile<LeftRight> type;
0149 };
0150
0151 template<typename LeftRight>
0152 struct feature_of<tag::weighted_tail_quantile<LeftRight> >
0153 : feature_of<tag::tail_quantile<LeftRight> >
0154 {};
0155
0156 }}
0157
0158 #ifdef _MSC_VER
0159 # pragma warning(pop)
0160 #endif
0161
0162 #endif