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

http://hadesmem.googlecode.com/ · C++ Header · 290 lines · 195 code · 36 blank · 59 comment · 21 complexity · 374bf6d026410d14407dbaf1bd7b4c32 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // weighted_extended_p_square.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_WEIGHTED_EXTENDED_P_SQUARE_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_EXTENDED_P_SQUARE_HPP_DE_01_01_2006
  9. #include <vector>
  10. #include <functional>
  11. #include <boost/range/begin.hpp>
  12. #include <boost/range/end.hpp>
  13. #include <boost/range/iterator_range.hpp>
  14. #include <boost/iterator/transform_iterator.hpp>
  15. #include <boost/iterator/counting_iterator.hpp>
  16. #include <boost/iterator/permutation_iterator.hpp>
  17. #include <boost/parameter/keyword.hpp>
  18. #include <boost/mpl/placeholders.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/framework/depends_on.hpp>
  24. #include <boost/accumulators/statistics_fwd.hpp>
  25. #include <boost/accumulators/statistics/count.hpp>
  26. #include <boost/accumulators/statistics/sum.hpp>
  27. #include <boost/accumulators/statistics/times2_iterator.hpp>
  28. #include <boost/accumulators/statistics/extended_p_square.hpp>
  29. namespace boost { namespace accumulators
  30. {
  31. namespace impl
  32. {
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // weighted_extended_p_square_impl
  35. // multiple quantile estimation with weighted samples
  36. /**
  37. @brief Multiple quantile estimation with the extended \f$P^2\f$ algorithm for weighted samples
  38. This version of the extended \f$P^2\f$ algorithm extends the extended \f$P^2\f$ algorithm to
  39. support weighted samples. The extended \f$P^2\f$ algorithm dynamically estimates several
  40. quantiles without storing samples. Assume that \f$m\f$ quantiles
  41. \f$\xi_{p_1}, \ldots, \xi_{p_m}\f$ are to be estimated. Instead of storing the whole sample
  42. cumulative distribution, the algorithm maintains only \f$m+2\f$ principal markers and
  43. \f$m+1\f$ middle markers, whose positions are updated with each sample and whose heights
  44. are adjusted (if necessary) using a piecewise-parablic formula. The heights of the principal
  45. markers are the current estimates of the quantiles and are returned as an iterator range.
  46. For further details, see
  47. K. E. E. Raatikainen, Simultaneous estimation of several quantiles, Simulation, Volume 49,
  48. Number 4 (October), 1986, p. 159-164.
  49. The extended \f$ P^2 \f$ algorithm generalizess the \f$ P^2 \f$ algorithm of
  50. R. Jain and I. Chlamtac, The P^2 algorithmus for dynamic calculation of quantiles and
  51. histograms without storing observations, Communications of the ACM,
  52. Volume 28 (October), Number 10, 1985, p. 1076-1085.
  53. @param extended_p_square_probabilities A vector of quantile probabilities.
  54. */
  55. template<typename Sample, typename Weight>
  56. struct weighted_extended_p_square_impl
  57. : accumulator_base
  58. {
  59. typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
  60. typedef typename numeric::functional::average<weighted_sample, std::size_t>::result_type float_type;
  61. typedef std::vector<float_type> array_type;
  62. // for boost::result_of
  63. typedef iterator_range<
  64. detail::lvalue_index_iterator<
  65. permutation_iterator<
  66. typename array_type::const_iterator
  67. , detail::times2_iterator
  68. >
  69. >
  70. > result_type;
  71. template<typename Args>
  72. weighted_extended_p_square_impl(Args const &args)
  73. : probabilities(
  74. boost::begin(args[extended_p_square_probabilities])
  75. , boost::end(args[extended_p_square_probabilities])
  76. )
  77. , heights(2 * probabilities.size() + 3)
  78. , actual_positions(heights.size())
  79. , desired_positions(heights.size())
  80. {
  81. }
  82. template<typename Args>
  83. void operator ()(Args const &args)
  84. {
  85. std::size_t cnt = count(args);
  86. std::size_t sample_cell = 1; // k
  87. std::size_t num_quantiles = this->probabilities.size();
  88. // m+2 principal markers and m+1 middle markers
  89. std::size_t num_markers = 2 * num_quantiles + 3;
  90. // first accumulate num_markers samples
  91. if(cnt <= num_markers)
  92. {
  93. this->heights[cnt - 1] = args[sample];
  94. this->actual_positions[cnt - 1] = args[weight];
  95. // complete the initialization of heights (and actual_positions) by sorting
  96. if(cnt == num_markers)
  97. {
  98. // TODO: we need to sort the initial samples (in heights) in ascending order and
  99. // sort their weights (in actual_positions) the same way. The following lines do
  100. // it, but there must be a better and more efficient way of doing this.
  101. typename array_type::iterator it_begin, it_end, it_min;
  102. it_begin = this->heights.begin();
  103. it_end = this->heights.end();
  104. std::size_t pos = 0;
  105. while (it_begin != it_end)
  106. {
  107. it_min = std::min_element(it_begin, it_end);
  108. std::size_t d = std::distance(it_begin, it_min);
  109. std::swap(*it_begin, *it_min);
  110. std::swap(this->actual_positions[pos], this->actual_positions[pos + d]);
  111. ++it_begin;
  112. ++pos;
  113. }
  114. // calculate correct initial actual positions
  115. for (std::size_t i = 1; i < num_markers; ++i)
  116. {
  117. actual_positions[i] += actual_positions[i - 1];
  118. }
  119. }
  120. }
  121. else
  122. {
  123. if(args[sample] < this->heights[0])
  124. {
  125. this->heights[0] = args[sample];
  126. this->actual_positions[0] = args[weight];
  127. sample_cell = 1;
  128. }
  129. else if(args[sample] >= this->heights[num_markers - 1])
  130. {
  131. this->heights[num_markers - 1] = args[sample];
  132. sample_cell = num_markers - 1;
  133. }
  134. else
  135. {
  136. // find cell k = sample_cell such that heights[k-1] <= sample < heights[k]
  137. typedef typename array_type::iterator iterator;
  138. iterator it = std::upper_bound(
  139. this->heights.begin()
  140. , this->heights.end()
  141. , args[sample]
  142. );
  143. sample_cell = std::distance(this->heights.begin(), it);
  144. }
  145. // update actual position of all markers above sample_cell
  146. for(std::size_t i = sample_cell; i < num_markers; ++i)
  147. {
  148. this->actual_positions[i] += args[weight];
  149. }
  150. // compute desired positions
  151. {
  152. this->desired_positions[0] = this->actual_positions[0];
  153. this->desired_positions[num_markers - 1] = sum_of_weights(args);
  154. this->desired_positions[1] = (sum_of_weights(args) - this->actual_positions[0]) * probabilities[0]
  155. / 2. + this->actual_positions[0];
  156. this->desired_positions[num_markers - 2] = (sum_of_weights(args) - this->actual_positions[0])
  157. * (probabilities[num_quantiles - 1] + 1.)
  158. / 2. + this->actual_positions[0];
  159. for (std::size_t i = 0; i < num_quantiles; ++i)
  160. {
  161. this->desired_positions[2 * i + 2] = (sum_of_weights(args) - this->actual_positions[0])
  162. * probabilities[i] + this->actual_positions[0];
  163. }
  164. for (std::size_t i = 1; i < num_quantiles; ++i)
  165. {
  166. this->desired_positions[2 * i + 1] = (sum_of_weights(args) - this->actual_positions[0])
  167. * (probabilities[i - 1] + probabilities[i])
  168. / 2. + this->actual_positions[0];
  169. }
  170. }
  171. // adjust heights and actual_positions of markers 1 to num_markers - 2 if necessary
  172. for (std::size_t i = 1; i <= num_markers - 2; ++i)
  173. {
  174. // offset to desired position
  175. float_type d = this->desired_positions[i] - this->actual_positions[i];
  176. // offset to next position
  177. float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
  178. // offset to previous position
  179. float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
  180. // height ds
  181. float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
  182. float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
  183. if((d >= 1 && dp > 1) || (d <= -1 && dm < -1))
  184. {
  185. short sign_d = static_cast<short>(d / std::abs(d));
  186. float_type h = this->heights[i] + sign_d / (dp - dm) * ((sign_d - dm)*hp + (dp - sign_d) * hm);
  187. // try adjusting heights[i] using p-squared formula
  188. if(this->heights[i - 1] < h && h < this->heights[i + 1])
  189. {
  190. this->heights[i] = h;
  191. }
  192. else
  193. {
  194. // use linear formula
  195. if(d > 0)
  196. {
  197. this->heights[i] += hp;
  198. }
  199. if(d < 0)
  200. {
  201. this->heights[i] -= hm;
  202. }
  203. }
  204. this->actual_positions[i] += sign_d;
  205. }
  206. }
  207. }
  208. }
  209. result_type result(dont_care) const
  210. {
  211. // for i in [1,probabilities.size()], return heights[i * 2]
  212. detail::times2_iterator idx_begin = detail::make_times2_iterator(1);
  213. detail::times2_iterator idx_end = detail::make_times2_iterator(this->probabilities.size() + 1);
  214. return result_type(
  215. make_permutation_iterator(this->heights.begin(), idx_begin)
  216. , make_permutation_iterator(this->heights.begin(), idx_end)
  217. );
  218. }
  219. private:
  220. array_type probabilities; // the quantile probabilities
  221. array_type heights; // q_i
  222. array_type actual_positions; // n_i
  223. array_type desired_positions; // d_i
  224. };
  225. } // namespace impl
  226. ///////////////////////////////////////////////////////////////////////////////
  227. // tag::weighted_extended_p_square
  228. //
  229. namespace tag
  230. {
  231. struct weighted_extended_p_square
  232. : depends_on<count, sum_of_weights>
  233. , extended_p_square_probabilities
  234. {
  235. typedef accumulators::impl::weighted_extended_p_square_impl<mpl::_1, mpl::_2> impl;
  236. };
  237. }
  238. ///////////////////////////////////////////////////////////////////////////////
  239. // extract::weighted_extended_p_square
  240. //
  241. namespace extract
  242. {
  243. extractor<tag::weighted_extended_p_square> const weighted_extended_p_square = {};
  244. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_extended_p_square)
  245. }
  246. using extract::weighted_extended_p_square;
  247. }} // namespace boost::accumulators
  248. #endif