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

http://hadesmem.googlecode.com/ · C++ Header · 257 lines · 171 code · 32 blank · 54 comment · 18 complexity · 3aca4125adf25510506e69b1a5fad761 MD5 · raw file

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