annotate DEPENDENCIES/generic/include/boost/accumulators/statistics/weighted_tail_mean.hpp @ 133:4acb5d8d80b6 tip

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