1 /////////////////////////////////////////////////////////////////////////////// 2 // weighted_tail_mean.hpp 3 // 4 // Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost 5 // Software License, Version 1.0. (See accompanying file 6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7 8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_MEAN_HPP_DE_01_01_2006 9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_TAIL_MEAN_HPP_DE_01_01_2006 10 11 #include <numeric> 12 #include <vector> 13 #include <limits> 14 #include <functional> 15 #include <sstream> 16 #include <stdexcept> 17 #include <boost/throw_exception.hpp> 18 #include <boost/parameter/keyword.hpp> 19 #include <boost/mpl/placeholders.hpp> 20 #include <boost/type_traits/is_same.hpp> 21 #include <boost/accumulators/numeric/functional.hpp> 22 #include <boost/accumulators/framework/accumulator_base.hpp> 23 #include <boost/accumulators/framework/extractor.hpp> 24 #include <boost/accumulators/framework/parameters/sample.hpp> 25 #include <boost/accumulators/statistics_fwd.hpp> 26 #include <boost/accumulators/statistics/tail.hpp> 27 #include <boost/accumulators/statistics/tail_mean.hpp> 28 #include <boost/accumulators/statistics/parameters/quantile_probability.hpp> 29 30 #ifdef _MSC_VER 31 # pragma warning(push) 32 # pragma warning(disable: 4127) // conditional expression is constant 33 #endif 34 35 namespace boost { namespace accumulators 36 { 37 38 namespace impl 39 { 40 41 /////////////////////////////////////////////////////////////////////////////// 42 // coherent_weighted_tail_mean_impl 43 // 44 // TODO 45 46 /////////////////////////////////////////////////////////////////////////////// 47 // non_coherent_weighted_tail_mean_impl 48 // 49 /** 50 @brief Estimation of the (non-coherent) weighted tail mean based on order statistics (for both left and right tails) 51 52 53 54 An estimation of the non-coherent, weighted tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$ is given by the weighted mean 55 of the 56 57 \f[ 58 \lambda = \inf\left\{ l \left| \frac{1}{\bar{w}_n}\sum_{i=1}^{l} w_i \geq \alpha \right. \right\} 59 \f] 60 61 smallest samples (left tail) or the weighted mean of the 62 63 \f[ 64 n + 1 - \rho = n + 1 - \sup\left\{ r \left| \frac{1}{\bar{w}_n}\sum_{i=r}^{n} w_i \geq (1 - \alpha) \right. \right\} 65 \f] 66 67 largest samples (right tail) above a quantile \f$\hat{q}_{\alpha}\f$ of level \f$\alpha\f$, \f$n\f$ being the total number of sample 68 and \f$\bar{w}_n\f$ the sum of all \f$n\f$ weights: 69 70 \f[ 71 \widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X) = \frac{\sum_{i=1}^{\lambda} w_i X_{i:n}}{\sum_{i=1}^{\lambda} w_i}, 72 \f] 73 74 \f[ 75 \widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X) = \frac{\sum_{i=\rho}^n w_i X_{i:n}}{\sum_{i=\rho}^n w_i}. 76 \f] 77 78 @param quantile_probability 79 */ 80 template<typename Sample, typename Weight, typename LeftRight> 81 struct non_coherent_weighted_tail_mean_impl 82 : accumulator_base 83 { 84 typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample; 85 typedef typename numeric::functional::fdiv<Weight, std::size_t>::result_type float_type; 86 // for boost::result_of 87 typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type result_type; 88 non_coherent_weighted_tail_mean_implboost::accumulators::impl::non_coherent_weighted_tail_mean_impl89 non_coherent_weighted_tail_mean_impl(dont_care) {} 90 91 template<typename Args> resultboost::accumulators::impl::non_coherent_weighted_tail_mean_impl92 result_type result(Args const &args) const 93 { 94 float_type threshold = sum_of_weights(args) 95 * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] ); 96 97 std::size_t n = 0; 98 Weight sum = Weight(0); 99 100 while (sum < threshold) 101 { 102 if (n < static_cast<std::size_t>(tail_weights(args).size())) 103 { 104 sum += *(tail_weights(args).begin() + n); 105 n++; 106 } 107 else 108 { 109 if (std::numeric_limits<result_type>::has_quiet_NaN) 110 { 111 return std::numeric_limits<result_type>::quiet_NaN(); 112 } 113 else 114 { 115 std::ostringstream msg; 116 msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")"; 117 boost::throw_exception(std::runtime_error(msg.str())); 118 return result_type(0); 119 } 120 } 121 } 122 123 return numeric::fdiv( 124 std::inner_product( 125 tail(args).begin() 126 , tail(args).begin() + n 127 , tail_weights(args).begin() 128 , weighted_sample(0) 129 ) 130 , sum 131 ); 132 } 133 }; 134 135 } // namespace impl 136 137 138 /////////////////////////////////////////////////////////////////////////////// 139 // tag::non_coherent_weighted_tail_mean<> 140 // 141 namespace tag 142 { 143 template<typename LeftRight> 144 struct non_coherent_weighted_tail_mean 145 : depends_on<sum_of_weights, tail_weights<LeftRight> > 146 { 147 typedef accumulators::impl::non_coherent_weighted_tail_mean_impl<mpl::_1, mpl::_2, LeftRight> impl; 148 }; 149 } 150 151 /////////////////////////////////////////////////////////////////////////////// 152 // extract::non_coherent_weighted_tail_mean; 153 // 154 namespace extract 155 { 156 extractor<tag::abstract_non_coherent_tail_mean> const non_coherent_weighted_tail_mean = {}; 157 158 BOOST_ACCUMULATORS_IGNORE_GLOBAL(non_coherent_weighted_tail_mean) 159 } 160 161 using extract::non_coherent_weighted_tail_mean; 162 163 }} // namespace boost::accumulators 164 165 #ifdef _MSC_VER 166 # pragma warning(pop) 167 #endif 168 169 #endif 170