extended_p_square_quantile.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. , probability()
  72. {
  73. }
  74. template<typename Args>
  75. result_type result(Args const &args) const
  76. {
  77. typedef
  78. typename mpl::if_<
  79. is_same<Impl1, weighted>
  80. , tag::weighted_extended_p_square
  81. , tag::extended_p_square
  82. >::type
  83. extended_p_square_tag;
  84. extractor<extended_p_square_tag> const some_extended_p_square = {};
  85. array_type heights(some_extended_p_square(args).size());
  86. std::copy(some_extended_p_square(args).begin(), some_extended_p_square(args).end(), heights.begin());
  87. this->probability = args[quantile_probability];
  88. typename array_type::const_iterator iter_probs = std::lower_bound(this->probabilities.begin(), this->probabilities.end(), this->probability);
  89. std::size_t dist = std::distance(this->probabilities.begin(), iter_probs);
  90. typename array_type::const_iterator iter_heights = heights.begin() + dist;
  91. // If this->probability is not in a valid range return NaN or throw exception
  92. if (this->probability < *this->probabilities.begin() || this->probability > *(this->probabilities.end() - 1))
  93. {
  94. if (std::numeric_limits<result_type>::has_quiet_NaN)
  95. {
  96. return std::numeric_limits<result_type>::quiet_NaN();
  97. }
  98. else
  99. {
  100. std::ostringstream msg;
  101. msg << "probability = " << this->probability << " is not in valid range (";
  102. msg << *this->probabilities.begin() << ", " << *(this->probabilities.end() - 1) << ")";
  103. boost::throw_exception(std::runtime_error(msg.str()));
  104. return Sample(0);
  105. }
  106. }
  107. if (*iter_probs == this->probability)
  108. {
  109. return heights[dist];
  110. }
  111. else
  112. {
  113. result_type res;
  114. if (is_same<Impl2, linear>::value)
  115. {
  116. /////////////////////////////////////////////////////////////////////////////////
  117. // LINEAR INTERPOLATION
  118. //
  119. float_type p1 = *iter_probs;
  120. float_type p0 = *(iter_probs - 1);
  121. float_type h1 = *iter_heights;
  122. float_type h0 = *(iter_heights - 1);
  123. float_type a = numeric::fdiv(h1 - h0, p1 - p0);
  124. float_type b = h1 - p1 * a;
  125. res = a * this->probability + b;
  126. }
  127. else
  128. {
  129. /////////////////////////////////////////////////////////////////////////////////
  130. // QUADRATIC INTERPOLATION
  131. //
  132. float_type p0, p1, p2;
  133. float_type h0, h1, h2;
  134. if ( (dist == 1 || *iter_probs - this->probability <= this->probability - *(iter_probs - 1) ) && dist != this->probabilities.size() - 1 )
  135. {
  136. p0 = *(iter_probs - 1);
  137. p1 = *iter_probs;
  138. p2 = *(iter_probs + 1);
  139. h0 = *(iter_heights - 1);
  140. h1 = *iter_heights;
  141. h2 = *(iter_heights + 1);
  142. }
  143. else
  144. {
  145. p0 = *(iter_probs - 2);
  146. p1 = *(iter_probs - 1);
  147. p2 = *iter_probs;
  148. h0 = *(iter_heights - 2);
  149. h1 = *(iter_heights - 1);
  150. h2 = *iter_heights;
  151. }
  152. float_type hp21 = numeric::fdiv(h2 - h1, p2 - p1);
  153. float_type hp10 = numeric::fdiv(h1 - h0, p1 - p0);
  154. float_type p21 = numeric::fdiv(p2 * p2 - p1 * p1, p2 - p1);
  155. float_type p10 = numeric::fdiv(p1 * p1 - p0 * p0, p1 - p0);
  156. float_type a = numeric::fdiv(hp21 - hp10, p21 - p10);
  157. float_type b = hp21 - a * p21;
  158. float_type c = h2 - a * p2 * p2 - b * p2;
  159. res = a * this->probability * this-> probability + b * this->probability + c;
  160. }
  161. return res;
  162. }
  163. }
  164. public:
  165. // make this accumulator serializeable
  166. // TODO: do we need to split to load/save and verify that the parameters did not change?
  167. template<class Archive>
  168. void serialize(Archive & ar, const unsigned int file_version)
  169. {
  170. ar & probabilities;
  171. ar & probability;
  172. }
  173. private:
  174. array_type probabilities;
  175. mutable float_type probability;
  176. };
  177. } // namespace impl
  178. ///////////////////////////////////////////////////////////////////////////////
  179. // tag::extended_p_square_quantile
  180. //
  181. namespace tag
  182. {
  183. struct extended_p_square_quantile
  184. : depends_on<extended_p_square>
  185. {
  186. typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, unweighted, linear> impl;
  187. };
  188. struct extended_p_square_quantile_quadratic
  189. : depends_on<extended_p_square>
  190. {
  191. typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, unweighted, quadratic> impl;
  192. };
  193. struct weighted_extended_p_square_quantile
  194. : depends_on<weighted_extended_p_square>
  195. {
  196. typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, weighted, linear> impl;
  197. };
  198. struct weighted_extended_p_square_quantile_quadratic
  199. : depends_on<weighted_extended_p_square>
  200. {
  201. typedef accumulators::impl::extended_p_square_quantile_impl<mpl::_1, weighted, quadratic> impl;
  202. };
  203. }
  204. ///////////////////////////////////////////////////////////////////////////////
  205. // extract::extended_p_square_quantile
  206. // extract::weighted_extended_p_square_quantile
  207. //
  208. namespace extract
  209. {
  210. extractor<tag::extended_p_square_quantile> const extended_p_square_quantile = {};
  211. extractor<tag::extended_p_square_quantile_quadratic> const extended_p_square_quantile_quadratic = {};
  212. extractor<tag::weighted_extended_p_square_quantile> const weighted_extended_p_square_quantile = {};
  213. extractor<tag::weighted_extended_p_square_quantile_quadratic> const weighted_extended_p_square_quantile_quadratic = {};
  214. BOOST_ACCUMULATORS_IGNORE_GLOBAL(extended_p_square_quantile)
  215. BOOST_ACCUMULATORS_IGNORE_GLOBAL(extended_p_square_quantile_quadratic)
  216. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_extended_p_square_quantile)
  217. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_extended_p_square_quantile_quadratic)
  218. }
  219. using extract::extended_p_square_quantile;
  220. using extract::extended_p_square_quantile_quadratic;
  221. using extract::weighted_extended_p_square_quantile;
  222. using extract::weighted_extended_p_square_quantile_quadratic;
  223. // extended_p_square_quantile(linear) -> extended_p_square_quantile
  224. template<>
  225. struct as_feature<tag::extended_p_square_quantile(linear)>
  226. {
  227. typedef tag::extended_p_square_quantile type;
  228. };
  229. // extended_p_square_quantile(quadratic) -> extended_p_square_quantile_quadratic
  230. template<>
  231. struct as_feature<tag::extended_p_square_quantile(quadratic)>
  232. {
  233. typedef tag::extended_p_square_quantile_quadratic type;
  234. };
  235. // weighted_extended_p_square_quantile(linear) -> weighted_extended_p_square_quantile
  236. template<>
  237. struct as_feature<tag::weighted_extended_p_square_quantile(linear)>
  238. {
  239. typedef tag::weighted_extended_p_square_quantile type;
  240. };
  241. // weighted_extended_p_square_quantile(quadratic) -> weighted_extended_p_square_quantile_quadratic
  242. template<>
  243. struct as_feature<tag::weighted_extended_p_square_quantile(quadratic)>
  244. {
  245. typedef tag::weighted_extended_p_square_quantile_quadratic type;
  246. };
  247. // for the purposes of feature-based dependency resolution,
  248. // extended_p_square_quantile and weighted_extended_p_square_quantile
  249. // provide the same feature as quantile
  250. template<>
  251. struct feature_of<tag::extended_p_square_quantile>
  252. : feature_of<tag::quantile>
  253. {
  254. };
  255. template<>
  256. struct feature_of<tag::extended_p_square_quantile_quadratic>
  257. : feature_of<tag::quantile>
  258. {
  259. };
  260. // So that extended_p_square_quantile can be automatically substituted with
  261. // weighted_extended_p_square_quantile when the weight parameter is non-void
  262. template<>
  263. struct as_weighted_feature<tag::extended_p_square_quantile>
  264. {
  265. typedef tag::weighted_extended_p_square_quantile type;
  266. };
  267. template<>
  268. struct feature_of<tag::weighted_extended_p_square_quantile>
  269. : feature_of<tag::extended_p_square_quantile>
  270. {
  271. };
  272. // So that extended_p_square_quantile_quadratic can be automatically substituted with
  273. // weighted_extended_p_square_quantile_quadratic when the weight parameter is non-void
  274. template<>
  275. struct as_weighted_feature<tag::extended_p_square_quantile_quadratic>
  276. {
  277. typedef tag::weighted_extended_p_square_quantile_quadratic type;
  278. };
  279. template<>
  280. struct feature_of<tag::weighted_extended_p_square_quantile_quadratic>
  281. : feature_of<tag::extended_p_square_quantile_quadratic>
  282. {
  283. };
  284. }} // namespace boost::accumulators
  285. #ifdef _MSC_VER
  286. # pragma warning(pop)
  287. #endif
  288. #endif