extended_p_square_quantile.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // extended_p_square_quantile.hpp
  3. //
  4. // Copyright 2005 Daniel Egloff. 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_EXTENDED_SINGLE_QUANTILE_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_EXTENDED_SINGLE_QUANTILE_HPP_DE_01_01_2006
  9. #include <vector>
  10. #include <functional>
  11. #include <boost/throw_exception.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/iterator_range.hpp>
  15. #include <boost/iterator/transform_iterator.hpp>
  16. #include <boost/iterator/counting_iterator.hpp>
  17. #include <boost/iterator/permutation_iterator.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/framework/accumulator_base.hpp>
  22. #include <boost/accumulators/framework/extractor.hpp>
  23. #include <boost/accumulators/numeric/functional.hpp>
  24. #include <boost/accumulators/framework/parameters/sample.hpp>
  25. #include <boost/accumulators/framework/depends_on.hpp>
  26. #include <boost/accumulators/statistics_fwd.hpp>
  27. #include <boost/accumulators/statistics/count.hpp>
  28. #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
  29. #include <boost/accumulators/statistics/extended_p_square.hpp>
  30. #include <boost/accumulators/statistics/weighted_extended_p_square.hpp>
  31. #include <boost/accumulators/statistics/times2_iterator.hpp>
  32. #ifdef _MSC_VER
  33. # pragma warning(push)
  34. # pragma warning(disable: 4127) // conditional expression is constant
  35. #endif
  36. namespace boost { namespace accumulators
  37. {
  38. namespace impl
  39. {
  40. ///////////////////////////////////////////////////////////////////////////////
  41. // extended_p_square_quantile_impl
  42. // single quantile estimation
  43. /**
  44. @brief Quantile estimation using the extended \f$P^2\f$ algorithm for weighted and unweighted samples
  45. Uses the quantile estimates calculated by the extended \f$P^2\f$ algorithm to compute
  46. intermediate quantile estimates by means of quadratic interpolation.
  47. @param quantile_probability The probability of the quantile to be estimated.
  48. */
  49. template<typename Sample, typename Impl1, typename Impl2> // Impl1: weighted/unweighted // Impl2: linear/quadratic
  50. struct extended_p_square_quantile_impl
  51. : accumulator_base
  52. {
  53. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
  54. typedef std::vector<float_type> array_type;
  55. typedef iterator_range<
  56. detail::lvalue_index_iterator<
  57. permutation_iterator<
  58. typename array_type::const_iterator
  59. , detail::times2_iterator
  60. >
  61. >
  62. > range_type;
  63. // for boost::result_of
  64. typedef float_type result_type;
  65. template<typename Args>
  66. extended_p_square_quantile_impl(Args const &args)
  67. : probabilities(
  68. boost::begin(args[extended_p_square_probabilities])
  69. , boost::end(args[extended_p_square_probabilities])
  70. )
  71. {
  72. }
  73. template<typename Args>
  74. result_type result(Args const &args) const
  75. {
  76. typedef
  77. typename mpl::if_<
  78. is_same<Impl1, weighted>
  79. , tag::weighted_extended_p_square
  80. , tag::extended_p_square
  81. >::type
  82. extended_p_square_tag;
  83. extractor<extended_p_square_tag> const some_extended_p_square = {};
  84. array_type heights(some_extended_p_square(args).size());
  85. std::copy(some_extended_p_square(args).begin(), some_extended_p_square(args).end(), heights.begin());
  86. this->probability = args[quantile_probability];
  87. typename array_type::const_iterator iter_probs = std::lower_bound(this->probabilities.begin(), this->probabilities.end(), this->probability);
  88. std::size_t dist = std::distance(this->probabilities.begin(), iter_probs);
  89. typename array_type::const_iterator iter_heights = heights.begin() + dist;
  90. // If this->probability is not in a valid range return NaN or throw exception
  91. if (this->probability < *this->probabilities.begin() || this->probability > *(this->probabilities.end() - 1))
  92. {
  93. if (std::numeric_limits<result_type>::has_quiet_NaN)
  94. {
  95. return std::numeric_limits<result_type>::quiet_NaN();
  96. }
  97. else
  98. {
  99. std::ostringstream msg;
  100. msg << "probability = " << this->probability << " is not in valid range (";
  101. msg << *this->probabilities.begin() << ", " << *(this->probabilities.end() - 1) << ")";
  102. boost::throw_exception(std::runtime_error(msg.str()));
  103. return Sample(0);
  104. }
  105. }
  106. if (*iter_probs == this->probability)
  107. {
  108. return heights[dist];
  109. }
  110. else
  111. {
  112. result_type res;
  113. if (is_same<Impl2, linear>::value)
  114. {
  115. /////////////////////////////////////////////////////////////////////////////////
  116. // LINEAR INTERPOLATION
  117. //
  118. float_type p1 = *iter_probs;
  119. float_type p0 = *(iter_probs - 1);
  120. float_type h1 = *iter_heights;
  121. float_type h0 = *(iter_heights - 1);
  122. float_type a = numeric::fdiv(h1 - h0, p1 - p0);
  123. float_type b = h1 - p1 * a;
  124. res = a * this->probability + b;
  125. }
  126. else
  127. {
  128. /////////////////////////////////////////////////////////////////////////////////
  129. // QUADRATIC INTERPOLATION
  130. //
  131. float_type p0, p1, p2;
  132. float_type h0, h1, h2;
  133. if ( (dist == 1 || *iter_probs - this->probability <= this->probability - *(iter_probs - 1) ) && dist != this->probabilities.size() - 1 )
  134. {
  135. p0 = *(iter_probs - 1);
  136. p1 = *iter_probs;
  137. p2 = *(iter_probs + 1);
  138. h0 = *(iter_heights - 1);
  139. h1 = *iter_heights;
  140. h2 = *(iter_heights + 1);
  141. }
  142. else
  143. {
  144. p0 = *(iter_probs - 2);
  145. p1 = *(iter_probs - 1);
  146. p2 = *iter_probs;
  147. h0 = *(iter_heights - 2);
  148. h1 = *(iter_heights - 1);
  149. h2 = *iter_heights;
  150. }
  151. float_type hp21 = numeric::fdiv(h2 - h1, p2 - p1);
  152. float_type hp10 = numeric::fdiv(h1 - h0, p1 - p0);
  153. float_type p21 = numeric::fdiv(p2 * p2 - p1 * p1, p2 - p1);
  154. float_type p10 = numeric::fdiv(p1 * p1 - p0 * p0, p1 - p0);
  155. float_type a = numeric::fdiv(hp21 - hp10, p21 - p10);
  156. float_type b = hp21 - a * p21;
  157. float_type c = h2 - a * p2 * p2 - b * p2;
  158. res = a * this->probability * this-> probability + b * this->probability + c;
  159. }
  160. return res;
  161. }
  162. }
  163. public:
  164. // make this accumulator serializeable
  165. // TODO: do we need to split to load/save and verify that the parameters did not change?
  166. template<class Archive>
  167. void serialize(Archive & ar, const unsigned int file_version)
  168. {
  169. ar & probabilities;
  170. ar & probability;
  171. }
  172. private:
  173. array_type probabilities;
  174. mutable float_type probability;
  175. };
  176. } // namespace impl
  177. ///////////////////////////////////////////////////////////////////////////////
  178. // tag::extended_p_square_quantile
  179. //
  180. namespace tag
  181. {
  182. struct extended_p_square_quantile
  183. : depends_on<extended_p_square>
  184. {
  185. typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, unweighted, linear> impl;
  186. };
  187. struct extended_p_square_quantile_quadratic
  188. : depends_on<extended_p_square>
  189. {
  190. typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, unweighted, quadratic> impl;
  191. };
  192. struct weighted_extended_p_square_quantile
  193. : depends_on<weighted_extended_p_square>
  194. {
  195. typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, weighted, linear> impl;
  196. };
  197. struct weighted_extended_p_square_quantile_quadratic
  198. : depends_on<weighted_extended_p_square>
  199. {
  200. typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, weighted, quadratic> impl;
  201. };
  202. }
  203. ///////////////////////////////////////////////////////////////////////////////
  204. // extract::extended_p_square_quantile
  205. // extract::weighted_extended_p_square_quantile
  206. //
  207. namespace extract
  208. {
  209. extractor<tag::extended_p_square_quantile> const extended_p_square_quantile = {};
  210. extractor<tag::extended_p_square_quantile_quadratic> const extended_p_square_quantile_quadratic = {};
  211. extractor<tag::weighted_extended_p_square_quantile> const weighted_extended_p_square_quantile = {};
  212. extractor<tag::weighted_extended_p_square_quantile_quadratic> const weighted_extended_p_square_quantile_quadratic = {};
  213. BOOST_ACCUMULATORS_IGNORE_GLOBAL(extended_p_square_quantile)
  214. BOOST_ACCUMULATORS_IGNORE_GLOBAL(extended_p_square_quantile_quadratic)
  215. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_extended_p_square_quantile)
  216. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_extended_p_square_quantile_quadratic)
  217. }
  218. using extract::extended_p_square_quantile;
  219. using extract::extended_p_square_quantile_quadratic;
  220. using extract::weighted_extended_p_square_quantile;
  221. using extract::weighted_extended_p_square_quantile_quadratic;
  222. // extended_p_square_quantile(linear) -> extended_p_square_quantile
  223. template<>
  224. struct as_feature<tag::extended_p_square_quantile(linear)>
  225. {
  226. typedef tag::extended_p_square_quantile type;
  227. };
  228. // extended_p_square_quantile(quadratic) -> extended_p_square_quantile_quadratic
  229. template<>
  230. struct as_feature<tag::extended_p_square_quantile(quadratic)>
  231. {
  232. typedef tag::extended_p_square_quantile_quadratic type;
  233. };
  234. // weighted_extended_p_square_quantile(linear) -> weighted_extended_p_square_quantile
  235. template<>
  236. struct as_feature<tag::weighted_extended_p_square_quantile(linear)>
  237. {
  238. typedef tag::weighted_extended_p_square_quantile type;
  239. };
  240. // weighted_extended_p_square_quantile(quadratic) -> weighted_extended_p_square_quantile_quadratic
  241. template<>
  242. struct as_feature<tag::weighted_extended_p_square_quantile(quadratic)>
  243. {
  244. typedef tag::weighted_extended_p_square_quantile_quadratic type;
  245. };
  246. // for the purposes of feature-based dependency resolution,
  247. // extended_p_square_quantile and weighted_extended_p_square_quantile
  248. // provide the same feature as quantile
  249. template<>
  250. struct feature_of<tag::extended_p_square_quantile>
  251. : feature_of<tag::quantile>
  252. {
  253. };
  254. template<>
  255. struct feature_of<tag::extended_p_square_quantile_quadratic>
  256. : feature_of<tag::quantile>
  257. {
  258. };
  259. // So that extended_p_square_quantile can be automatically substituted with
  260. // weighted_extended_p_square_quantile when the weight parameter is non-void
  261. template<>
  262. struct as_weighted_feature<tag::extended_p_square_quantile>
  263. {
  264. typedef tag::weighted_extended_p_square_quantile type;
  265. };
  266. template<>
  267. struct feature_of<tag::weighted_extended_p_square_quantile>
  268. : feature_of<tag::extended_p_square_quantile>
  269. {
  270. };
  271. // So that extended_p_square_quantile_quadratic can be automatically substituted with
  272. // weighted_extended_p_square_quantile_quadratic when the weight parameter is non-void
  273. template<>
  274. struct as_weighted_feature<tag::extended_p_square_quantile_quadratic>
  275. {
  276. typedef tag::weighted_extended_p_square_quantile_quadratic type;
  277. };
  278. template<>
  279. struct feature_of<tag::weighted_extended_p_square_quantile_quadratic>
  280. : feature_of<tag::extended_p_square_quantile_quadratic>
  281. {
  282. };
  283. }} // namespace boost::accumulators
  284. #ifdef _MSC_VER
  285. # pragma warning(pop)
  286. #endif
  287. #endif