p_square_cumul_dist.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // p_square_cumulative_distribution.hpp
  3. //
  4. // Copyright 2005 Daniel Egloff, Olivier Gygi. 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_CUMUL_DIST_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_CUMUL_DIST_HPP_DE_01_01_2006
  9. #include <vector>
  10. #include <functional>
  11. #include <boost/parameter/keyword.hpp>
  12. #include <boost/range.hpp>
  13. #include <boost/mpl/placeholders.hpp>
  14. #include <boost/accumulators/accumulators_fwd.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/statistics_fwd.hpp>
  20. #include <boost/accumulators/statistics/count.hpp>
  21. #include <boost/serialization/vector.hpp>
  22. #include <boost/serialization/utility.hpp>
  23. namespace boost { namespace accumulators
  24. {
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // num_cells named parameter
  27. //
  28. BOOST_PARAMETER_NESTED_KEYWORD(tag, p_square_cumulative_distribution_num_cells, num_cells)
  29. BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_cumulative_distribution_num_cells)
  30. namespace impl
  31. {
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // p_square_cumulative_distribution_impl
  34. // cumulative_distribution calculation (as histogram)
  35. /**
  36. @brief Histogram calculation of the cumulative distribution with the \f$P^2\f$ algorithm
  37. A histogram of the sample cumulative distribution is computed dynamically without storing samples
  38. based on the \f$ P^2 \f$ algorithm. The returned histogram has a specifiable amount (num_cells)
  39. equiprobable (and not equal-sized) cells.
  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 p_square_cumulative_distribution_num_cells.
  45. */
  46. template<typename Sample>
  47. struct p_square_cumulative_distribution_impl
  48. : accumulator_base
  49. {
  50. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
  51. typedef std::vector<float_type> array_type;
  52. typedef std::vector<std::pair<float_type, float_type> > histogram_type;
  53. // for boost::result_of
  54. typedef iterator_range<typename histogram_type::iterator> result_type;
  55. template<typename Args>
  56. p_square_cumulative_distribution_impl(Args const &args)
  57. : num_cells(args[p_square_cumulative_distribution_num_cells])
  58. , heights(num_cells + 1)
  59. , actual_positions(num_cells + 1)
  60. , desired_positions(num_cells + 1)
  61. , positions_increments(num_cells + 1)
  62. , histogram(num_cells + 1)
  63. , is_dirty(true)
  64. {
  65. std::size_t b = this->num_cells;
  66. for (std::size_t i = 0; i < b + 1; ++i)
  67. {
  68. this->actual_positions[i] = i + 1.;
  69. this->desired_positions[i] = i + 1.;
  70. this->positions_increments[i] = numeric::fdiv(i, b);
  71. }
  72. }
  73. template<typename Args>
  74. void operator ()(Args const &args)
  75. {
  76. this->is_dirty = true;
  77. std::size_t cnt = count(args);
  78. std::size_t sample_cell = 1; // k
  79. std::size_t b = this->num_cells;
  80. // accumulate num_cells + 1 first samples
  81. if (cnt <= b + 1)
  82. {
  83. this->heights[cnt - 1] = args[sample];
  84. // complete the initialization of heights by sorting
  85. if (cnt == b + 1)
  86. {
  87. std::sort(this->heights.begin(), this->heights.end());
  88. }
  89. }
  90. else
  91. {
  92. // find cell k such that heights[k-1] <= args[sample] < heights[k] and adjust extreme values
  93. if (args[sample] < this->heights[0])
  94. {
  95. this->heights[0] = args[sample];
  96. sample_cell = 1;
  97. }
  98. else if (this->heights[b] <= args[sample])
  99. {
  100. this->heights[b] = args[sample];
  101. sample_cell = b;
  102. }
  103. else
  104. {
  105. typename array_type::iterator it;
  106. it = std::upper_bound(
  107. this->heights.begin()
  108. , this->heights.end()
  109. , args[sample]
  110. );
  111. sample_cell = std::distance(this->heights.begin(), it);
  112. }
  113. // increment positions of markers above sample_cell
  114. for (std::size_t i = sample_cell; i < b + 1; ++i)
  115. {
  116. ++this->actual_positions[i];
  117. }
  118. // update desired position of markers 2 to num_cells + 1
  119. // (desired position of first marker is always 1)
  120. for (std::size_t i = 1; i < b + 1; ++i)
  121. {
  122. this->desired_positions[i] += this->positions_increments[i];
  123. }
  124. // adjust heights of markers 2 to num_cells if necessary
  125. for (std::size_t i = 1; i < b; ++i)
  126. {
  127. // offset to desire position
  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 + (dp - sign_d) * hm );
  141. if ( this->heights[i - 1] < h && h < this->heights[i + 1] )
  142. {
  143. this->heights[i] = h;
  144. }
  145. else
  146. {
  147. // use linear formula
  148. if (d>0)
  149. {
  150. this->heights[i] += hp;
  151. }
  152. if (d<0)
  153. {
  154. this->heights[i] -= hm;
  155. }
  156. }
  157. this->actual_positions[i] += sign_d;
  158. }
  159. }
  160. }
  161. }
  162. template<typename Args>
  163. result_type result(Args const &args) const
  164. {
  165. if (this->is_dirty)
  166. {
  167. this->is_dirty = false;
  168. // creates a vector of std::pair where each pair i holds
  169. // the values heights[i] (x-axis of histogram) and
  170. // actual_positions[i] / cnt (y-axis of histogram)
  171. std::size_t cnt = count(args);
  172. for (std::size_t i = 0; i < this->histogram.size(); ++i)
  173. {
  174. this->histogram[i] = std::make_pair(this->heights[i], numeric::fdiv(this->actual_positions[i], cnt));
  175. }
  176. }
  177. //return histogram;
  178. return make_iterator_range(this->histogram);
  179. }
  180. // make this accumulator serializeable
  181. // TODO split to save/load and check on parameters provided in ctor
  182. template<class Archive>
  183. void serialize(Archive & ar, const unsigned int file_version)
  184. {
  185. ar & num_cells;
  186. ar & heights;
  187. ar & actual_positions;
  188. ar & desired_positions;
  189. ar & positions_increments;
  190. ar & histogram;
  191. ar & is_dirty;
  192. }
  193. private:
  194. std::size_t num_cells; // number of cells b
  195. array_type heights; // q_i
  196. array_type actual_positions; // n_i
  197. array_type desired_positions; // n'_i
  198. array_type positions_increments; // dn'_i
  199. mutable histogram_type histogram; // histogram
  200. mutable bool is_dirty;
  201. };
  202. } // namespace detail
  203. ///////////////////////////////////////////////////////////////////////////////
  204. // tag::p_square_cumulative_distribution
  205. //
  206. namespace tag
  207. {
  208. struct p_square_cumulative_distribution
  209. : depends_on<count>
  210. , p_square_cumulative_distribution_num_cells
  211. {
  212. /// INTERNAL ONLY
  213. ///
  214. typedef accumulators::impl::p_square_cumulative_distribution_impl<mpl::_1> impl;
  215. };
  216. }
  217. ///////////////////////////////////////////////////////////////////////////////
  218. // extract::p_square_cumulative_distribution
  219. //
  220. namespace extract
  221. {
  222. extractor<tag::p_square_cumulative_distribution> const p_square_cumulative_distribution = {};
  223. BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_cumulative_distribution)
  224. }
  225. using extract::p_square_cumulative_distribution;
  226. // So that p_square_cumulative_distribution can be automatically substituted with
  227. // weighted_p_square_cumulative_distribution when the weight parameter is non-void
  228. template<>
  229. struct as_weighted_feature<tag::p_square_cumulative_distribution>
  230. {
  231. typedef tag::weighted_p_square_cumulative_distribution type;
  232. };
  233. template<>
  234. struct feature_of<tag::weighted_p_square_cumulative_distribution>
  235. : feature_of<tag::p_square_cumulative_distribution>
  236. {
  237. };
  238. }} // namespace boost::accumulators
  239. #endif