p_square_quantile.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // 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_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
  9. #include <cmath>
  10. #include <functional>
  11. #include <boost/array.hpp>
  12. #include <boost/mpl/placeholders.hpp>
  13. #include <boost/type_traits/is_same.hpp>
  14. #include <boost/parameter/keyword.hpp>
  15. #include <boost/accumulators/framework/accumulator_base.hpp>
  16. #include <boost/accumulators/framework/extractor.hpp>
  17. #include <boost/accumulators/numeric/functional.hpp>
  18. #include <boost/accumulators/framework/parameters/sample.hpp>
  19. #include <boost/accumulators/framework/depends_on.hpp>
  20. #include <boost/accumulators/statistics_fwd.hpp>
  21. #include <boost/accumulators/statistics/count.hpp>
  22. #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
  23. #include <boost/serialization/boost_array.hpp>
  24. namespace boost { namespace accumulators
  25. {
  26. namespace impl
  27. {
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // p_square_quantile_impl
  30. // single quantile estimation
  31. /**
  32. @brief Single quantile estimation with the \f$P^2\f$ algorithm
  33. The \f$P^2\f$ algorithm estimates a quantile dynamically without storing samples. Instead of
  34. storing the whole sample cumulative distribution, only five points (markers) are stored. The heights
  35. of these markers are the minimum and the maximum of the samples and the current estimates of the
  36. \f$(p/2)\f$-, \f$p\f$- and \f$(1+p)/2\f$-quantiles. Their positions are equal to the number
  37. of samples that are smaller or equal to the markers. Each time a new samples is recorded, the
  38. positions of the markers are updated and if necessary their heights are adjusted using a piecewise-
  39. parabolic formula.
  40. For further details, see
  41. R. Jain and I. Chlamtac, The P^2 algorithm for dynamic calculation of quantiles and
  42. histograms without storing observations, Communications of the ACM,
  43. Volume 28 (October), Number 10, 1985, p. 1076-1085.
  44. @param quantile_probability
  45. */
  46. template<typename Sample, typename Impl>
  47. struct p_square_quantile_impl
  48. : accumulator_base
  49. {
  50. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
  51. typedef array<float_type, 5> array_type;
  52. // for boost::result_of
  53. typedef float_type result_type;
  54. template<typename Args>
  55. p_square_quantile_impl(Args const &args)
  56. : p(is_same<Impl, for_median>::value ? float_type(0.5) : args[quantile_probability | float_type(0.5)])
  57. , heights()
  58. , actual_positions()
  59. , desired_positions()
  60. , positions_increments()
  61. {
  62. for(std::size_t i = 0; i < 5; ++i)
  63. {
  64. this->actual_positions[i] = i + float_type(1.);
  65. }
  66. this->desired_positions[0] = float_type(1.);
  67. this->desired_positions[1] = float_type(1.) + float_type(2.) * this->p;
  68. this->desired_positions[2] = float_type(1.) + float_type(4.) * this->p;
  69. this->desired_positions[3] = float_type(3.) + float_type(2.) * this->p;
  70. this->desired_positions[4] = float_type(5.);
  71. this->positions_increments[0] = float_type(0.);
  72. this->positions_increments[1] = this->p / float_type(2.);
  73. this->positions_increments[2] = this->p;
  74. this->positions_increments[3] = (float_type(1.) + this->p) / float_type(2.);
  75. this->positions_increments[4] = float_type(1.);
  76. }
  77. template<typename Args>
  78. void operator ()(Args const &args)
  79. {
  80. std::size_t cnt = count(args);
  81. // accumulate 5 first samples
  82. if(cnt <= 5)
  83. {
  84. this->heights[cnt - 1] = args[sample];
  85. // complete the initialization of heights by sorting
  86. if(cnt == 5)
  87. {
  88. std::sort(this->heights.begin(), this->heights.end());
  89. }
  90. }
  91. else
  92. {
  93. std::size_t sample_cell = 1; // k
  94. // find cell k such that heights[k-1] <= args[sample] < heights[k] and adjust extreme values
  95. if (args[sample] < this->heights[0])
  96. {
  97. this->heights[0] = args[sample];
  98. sample_cell = 1;
  99. }
  100. else if (this->heights[4] <= args[sample])
  101. {
  102. this->heights[4] = args[sample];
  103. sample_cell = 4;
  104. }
  105. else
  106. {
  107. typedef typename array_type::iterator iterator;
  108. iterator it = std::upper_bound(
  109. this->heights.begin()
  110. , this->heights.end()
  111. , args[sample]
  112. );
  113. sample_cell = std::distance(this->heights.begin(), it);
  114. }
  115. // update positions of markers above sample_cell
  116. for(std::size_t i = sample_cell; i < 5; ++i)
  117. {
  118. ++this->actual_positions[i];
  119. }
  120. // update desired positions of all markers
  121. for(std::size_t i = 0; i < 5; ++i)
  122. {
  123. this->desired_positions[i] += this->positions_increments[i];
  124. }
  125. // adjust heights and actual positions of markers 1 to 3 if necessary
  126. for(std::size_t i = 1; i <= 3; ++i)
  127. {
  128. // offset to desired positions
  129. float_type d = this->desired_positions[i] - this->actual_positions[i];
  130. // offset to next position
  131. float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
  132. // offset to previous position
  133. float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
  134. // height ds
  135. float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
  136. float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
  137. if((d >= float_type(1.) && dp > float_type(1.)) || (d <= float_type(-1.) && dm < float_type(-1.)))
  138. {
  139. short sign_d = static_cast<short>(d / std::abs(d));
  140. // try adjusting heights[i] using p-squared formula
  141. float_type h = this->heights[i] + sign_d / (dp - dm) * ((sign_d - dm) * hp
  142. + (dp - sign_d) * hm);
  143. if(this->heights[i - 1] < h && h < this->heights[i + 1])
  144. {
  145. this->heights[i] = h;
  146. }
  147. else
  148. {
  149. // use linear formula
  150. if(d > float_type(0))
  151. {
  152. this->heights[i] += hp;
  153. }
  154. if(d < float_type(0))
  155. {
  156. this->heights[i] -= hm;
  157. }
  158. }
  159. this->actual_positions[i] += sign_d;
  160. }
  161. }
  162. }
  163. }
  164. result_type result(dont_care) const
  165. {
  166. return this->heights[2];
  167. }
  168. // make this accumulator serializeable
  169. // TODO: do we need to split to load/save and verify that P did not change?
  170. template<class Archive>
  171. void serialize(Archive & ar, const unsigned int file_version)
  172. {
  173. ar & p;
  174. ar & heights;
  175. ar & actual_positions;
  176. ar & desired_positions;
  177. ar & positions_increments;
  178. }
  179. private:
  180. float_type p; // the quantile probability p
  181. array_type heights; // q_i
  182. array_type actual_positions; // n_i
  183. array_type desired_positions; // n'_i
  184. array_type positions_increments; // dn'_i
  185. };
  186. } // namespace detail
  187. ///////////////////////////////////////////////////////////////////////////////
  188. // tag::p_square_quantile
  189. //
  190. namespace tag
  191. {
  192. struct p_square_quantile
  193. : depends_on<count>
  194. {
  195. /// INTERNAL ONLY
  196. ///
  197. typedef accumulators::impl::p_square_quantile_impl<mpl::_1, regular> impl;
  198. };
  199. struct p_square_quantile_for_median
  200. : depends_on<count>
  201. {
  202. /// INTERNAL ONLY
  203. ///
  204. typedef accumulators::impl::p_square_quantile_impl<mpl::_1, for_median> impl;
  205. };
  206. }
  207. ///////////////////////////////////////////////////////////////////////////////
  208. // extract::p_square_quantile
  209. // extract::p_square_quantile_for_median
  210. //
  211. namespace extract
  212. {
  213. extractor<tag::p_square_quantile> const p_square_quantile = {};
  214. extractor<tag::p_square_quantile_for_median> const p_square_quantile_for_median = {};
  215. BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_quantile)
  216. BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_quantile_for_median)
  217. }
  218. using extract::p_square_quantile;
  219. using extract::p_square_quantile_for_median;
  220. // So that p_square_quantile can be automatically substituted with
  221. // weighted_p_square_quantile when the weight parameter is non-void
  222. template<>
  223. struct as_weighted_feature<tag::p_square_quantile>
  224. {
  225. typedef tag::weighted_p_square_quantile type;
  226. };
  227. template<>
  228. struct feature_of<tag::weighted_p_square_quantile>
  229. : feature_of<tag::p_square_quantile>
  230. {
  231. };
  232. }} // namespace boost::accumulators
  233. #endif