/Src/Dependencies/Boost/boost/accumulators/statistics/tail_variate_means.hpp

http://hadesmem.googlecode.com/ · C++ Header · 258 lines · 165 code · 33 blank · 60 comment · 2 complexity · 9defcb229eba651f873880784f0485a6 MD5 · raw file

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