tail_variate_means.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // tail_variate_means.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. #ifndef BOOST_ACCUMULATORS_STATISTICS_TAIL_VARIATE_MEANS_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_TAIL_VARIATE_MEANS_HPP_DE_01_01_2006
  9. #include <numeric>
  10. #include <vector>
  11. #include <limits>
  12. #include <functional>
  13. #include <sstream>
  14. #include <stdexcept>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/parameter/keyword.hpp>
  17. #include <boost/mpl/placeholders.hpp>
  18. #include <boost/type_traits/is_same.hpp>
  19. #include <boost/accumulators/framework/accumulator_base.hpp>
  20. #include <boost/accumulators/framework/extractor.hpp>
  21. #include <boost/accumulators/numeric/functional.hpp>
  22. #include <boost/accumulators/framework/parameters/sample.hpp>
  23. #include <boost/accumulators/statistics_fwd.hpp>
  24. #include <boost/accumulators/statistics/tail.hpp>
  25. #include <boost/accumulators/statistics/tail_variate.hpp>
  26. #include <boost/accumulators/statistics/tail_mean.hpp>
  27. #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
  28. #include <boost/serialization/vector.hpp>
  29. #ifdef _MSC_VER
  30. # pragma warning(push)
  31. # pragma warning(disable: 4127) // conditional expression is constant
  32. #endif
  33. namespace boost { namespace accumulators
  34. {
  35. namespace impl
  36. {
  37. /**
  38. @brief Estimation of the absolute and relative tail variate means (for both left and right tails)
  39. For all \f$j\f$-th variates associated to the \f$\lceil n(1-\alpha)\rceil\f$ largest samples (or the
  40. \f$\lceil n(1-\alpha)\rceil\f$ smallest samples in case of the left tail), the absolute tail means
  41. \f$\widehat{ATM}_{n,\alpha}(X, j)\f$ are computed and returned as an iterator range. Alternatively,
  42. the relative tail means \f$\widehat{RTM}_{n,\alpha}(X, j)\f$ are returned, which are the absolute
  43. tail means normalized with the (non-coherent) sample tail mean \f$\widehat{NCTM}_{n,\alpha}(X)\f$.
  44. \f[
  45. \widehat{ATM}_{n,\alpha}^{\mathrm{right}}(X, j) =
  46. \frac{1}{\lceil n(1-\alpha) \rceil}
  47. \sum_{i=\lceil \alpha n \rceil}^n \xi_{j,i}
  48. \f]
  49. \f[
  50. \widehat{ATM}_{n,\alpha}^{\mathrm{left}}(X, j) =
  51. \frac{1}{\lceil n\alpha \rceil}
  52. \sum_{i=1}^{\lceil n\alpha \rceil} \xi_{j,i}
  53. \f]
  54. \f[
  55. \widehat{RTM}_{n,\alpha}^{\mathrm{right}}(X, j) =
  56. \frac{\sum_{i=\lceil n\alpha \rceil}^n \xi_{j,i}}
  57. {\lceil n(1-\alpha)\rceil\widehat{NCTM}_{n,\alpha}^{\mathrm{right}}(X)}
  58. \f]
  59. \f[
  60. \widehat{RTM}_{n,\alpha}^{\mathrm{left}}(X, j) =
  61. \frac{\sum_{i=1}^{\lceil n\alpha \rceil} \xi_{j,i}}
  62. {\lceil n\alpha\rceil\widehat{NCTM}_{n,\alpha}^{\mathrm{left}}(X)}
  63. \f]
  64. */
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // tail_variate_means_impl
  67. // by default: absolute tail_variate_means
  68. template<typename Sample, typename Impl, typename LeftRight, typename VariateTag>
  69. struct tail_variate_means_impl
  70. : accumulator_base
  71. {
  72. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
  73. typedef std::vector<float_type> array_type;
  74. // for boost::result_of
  75. typedef iterator_range<typename array_type::iterator> result_type;
  76. tail_variate_means_impl(dont_care) {}
  77. template<typename Args>
  78. result_type result(Args const &args) const
  79. {
  80. std::size_t cnt = count(args);
  81. std::size_t n = static_cast<std::size_t>(
  82. std::ceil(
  83. cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
  84. )
  85. );
  86. std::size_t num_variates = tail_variate(args).begin()->size();
  87. this->tail_means_.clear();
  88. this->tail_means_.resize(num_variates, Sample(0));
  89. // If n is in a valid range, return result, otherwise return NaN or throw exception
  90. if (n < static_cast<std::size_t>(tail(args).size()))
  91. {
  92. this->tail_means_ = std::accumulate(
  93. tail_variate(args).begin()
  94. , tail_variate(args).begin() + n
  95. , this->tail_means_
  96. , numeric::plus
  97. );
  98. float_type factor = n * ( (is_same<Impl, relative>::value) ? non_coherent_tail_mean(args) : 1. );
  99. std::transform(
  100. this->tail_means_.begin()
  101. , this->tail_means_.end()
  102. , this->tail_means_.begin()
  103. #ifdef BOOST_NO_CXX98_BINDERS
  104. , std::bind(std::divides<float_type>(), std::placeholders::_1, factor)
  105. #else
  106. , std::bind2nd(std::divides<float_type>(), factor)
  107. #endif
  108. );
  109. }
  110. else
  111. {
  112. if (std::numeric_limits<float_type>::has_quiet_NaN)
  113. {
  114. std::fill(
  115. this->tail_means_.begin()
  116. , this->tail_means_.end()
  117. , std::numeric_limits<float_type>::quiet_NaN()
  118. );
  119. }
  120. else
  121. {
  122. std::ostringstream msg;
  123. msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
  124. boost::throw_exception(std::runtime_error(msg.str()));
  125. }
  126. }
  127. return make_iterator_range(this->tail_means_);
  128. }
  129. // make this accumulator serializeable
  130. template<class Archive>
  131. void serialize(Archive & ar, const unsigned int file_version)
  132. {
  133. ar & tail_means_;
  134. }
  135. private:
  136. mutable array_type tail_means_;
  137. };
  138. } // namespace impl
  139. ///////////////////////////////////////////////////////////////////////////////
  140. // tag::absolute_tail_variate_means
  141. // tag::relative_tail_variate_means
  142. //
  143. namespace tag
  144. {
  145. template<typename LeftRight, typename VariateType, typename VariateTag>
  146. struct absolute_tail_variate_means
  147. : depends_on<count, non_coherent_tail_mean<LeftRight>, tail_variate<VariateType, VariateTag, LeftRight> >
  148. {
  149. typedef accumulators::impl::tail_variate_means_impl<mpl::_1, absolute, LeftRight, VariateTag> impl;
  150. };
  151. template<typename LeftRight, typename VariateType, typename VariateTag>
  152. struct relative_tail_variate_means
  153. : depends_on<count, non_coherent_tail_mean<LeftRight>, tail_variate<VariateType, VariateTag, LeftRight> >
  154. {
  155. typedef accumulators::impl::tail_variate_means_impl<mpl::_1, relative, LeftRight, VariateTag> impl;
  156. };
  157. struct abstract_absolute_tail_variate_means
  158. : depends_on<>
  159. {
  160. };
  161. struct abstract_relative_tail_variate_means
  162. : depends_on<>
  163. {
  164. };
  165. }
  166. ///////////////////////////////////////////////////////////////////////////////
  167. // extract::tail_variate_means
  168. // extract::relative_tail_variate_means
  169. //
  170. namespace extract
  171. {
  172. extractor<tag::abstract_absolute_tail_variate_means> const tail_variate_means = {};
  173. extractor<tag::abstract_relative_tail_variate_means> const relative_tail_variate_means = {};
  174. BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_variate_means)
  175. BOOST_ACCUMULATORS_IGNORE_GLOBAL(relative_tail_variate_means)
  176. }
  177. using extract::tail_variate_means;
  178. using extract::relative_tail_variate_means;
  179. // tail_variate_means<LeftRight, VariateType, VariateTag>(absolute) -> absolute_tail_variate_means<LeftRight, VariateType, VariateTag>
  180. template<typename LeftRight, typename VariateType, typename VariateTag>
  181. struct as_feature<tag::tail_variate_means<LeftRight, VariateType, VariateTag>(absolute)>
  182. {
  183. typedef tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> type;
  184. };
  185. // tail_variate_means<LeftRight, VariateType, VariateTag>(relative) ->relative_tail_variate_means<LeftRight, VariateType, VariateTag>
  186. template<typename LeftRight, typename VariateType, typename VariateTag>
  187. struct as_feature<tag::tail_variate_means<LeftRight, VariateType, VariateTag>(relative)>
  188. {
  189. typedef tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> type;
  190. };
  191. // Provides non-templatized extractor
  192. template<typename LeftRight, typename VariateType, typename VariateTag>
  193. struct feature_of<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> >
  194. : feature_of<tag::abstract_absolute_tail_variate_means>
  195. {
  196. };
  197. // Provides non-templatized extractor
  198. template<typename LeftRight, typename VariateType, typename VariateTag>
  199. struct feature_of<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> >
  200. : feature_of<tag::abstract_relative_tail_variate_means>
  201. {
  202. };
  203. // So that absolute_tail_means can be automatically substituted
  204. // with absolute_weighted_tail_means when the weight parameter is non-void.
  205. template<typename LeftRight, typename VariateType, typename VariateTag>
  206. struct as_weighted_feature<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> >
  207. {
  208. typedef tag::absolute_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> type;
  209. };
  210. template<typename LeftRight, typename VariateType, typename VariateTag>
  211. struct feature_of<tag::absolute_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> >
  212. : feature_of<tag::absolute_tail_variate_means<LeftRight, VariateType, VariateTag> >
  213. {
  214. };
  215. // So that relative_tail_means can be automatically substituted
  216. // with relative_weighted_tail_means when the weight parameter is non-void.
  217. template<typename LeftRight, typename VariateType, typename VariateTag>
  218. struct as_weighted_feature<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> >
  219. {
  220. typedef tag::relative_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> type;
  221. };
  222. template<typename LeftRight, typename VariateType, typename VariateTag>
  223. struct feature_of<tag::relative_weighted_tail_variate_means<LeftRight, VariateType, VariateTag> >
  224. : feature_of<tag::relative_tail_variate_means<LeftRight, VariateType, VariateTag> >
  225. {
  226. };
  227. }} // namespace boost::accumulators
  228. #ifdef _MSC_VER
  229. # pragma warning(pop)
  230. #endif
  231. #endif