density.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // density.hpp
  3. //
  4. // Copyright 2006 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_DENSITY_HPP_DE_01_01_2006
  8. #define BOOST_ACCUMULATORS_STATISTICS_DENSITY_HPP_DE_01_01_2006
  9. #include <vector>
  10. #include <limits>
  11. #include <functional>
  12. #include <boost/range.hpp>
  13. #include <boost/parameter/keyword.hpp>
  14. #include <boost/mpl/placeholders.hpp>
  15. #include <boost/accumulators/accumulators_fwd.hpp>
  16. #include <boost/accumulators/framework/accumulator_base.hpp>
  17. #include <boost/accumulators/framework/extractor.hpp>
  18. #include <boost/accumulators/numeric/functional.hpp>
  19. #include <boost/accumulators/framework/parameters/sample.hpp>
  20. #include <boost/accumulators/framework/depends_on.hpp>
  21. #include <boost/accumulators/statistics_fwd.hpp>
  22. #include <boost/accumulators/statistics/count.hpp>
  23. #include <boost/accumulators/statistics/max.hpp>
  24. #include <boost/accumulators/statistics/min.hpp>
  25. #include <boost/serialization/vector.hpp>
  26. #include <boost/serialization/utility.hpp>
  27. namespace boost { namespace accumulators
  28. {
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // cache_size and num_bins named parameters
  31. //
  32. BOOST_PARAMETER_NESTED_KEYWORD(tag, density_cache_size, cache_size)
  33. BOOST_PARAMETER_NESTED_KEYWORD(tag, density_num_bins, num_bins)
  34. BOOST_ACCUMULATORS_IGNORE_GLOBAL(density_cache_size)
  35. BOOST_ACCUMULATORS_IGNORE_GLOBAL(density_num_bins)
  36. namespace impl
  37. {
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // density_impl
  40. // density histogram
  41. /**
  42. @brief Histogram density estimator
  43. The histogram density estimator returns a histogram of the sample distribution. The positions and sizes of the bins
  44. are determined using a specifiable number of cached samples (cache_size). The range between the minimum and the
  45. maximum of the cached samples is subdivided into a specifiable number of bins (num_bins) of same size. Additionally,
  46. an under- and an overflow bin is added to capture future under- and overflow samples. Once the bins are determined,
  47. the cached samples and all subsequent samples are added to the correct bins. At the end, a range of std::pair is
  48. return, where each pair contains the position of the bin (lower bound) and the samples count (normalized with the
  49. total number of samples).
  50. @param density_cache_size Number of first samples used to determine min and max.
  51. @param density_num_bins Number of bins (two additional bins collect under- and overflow samples).
  52. */
  53. template<typename Sample>
  54. struct density_impl
  55. : accumulator_base
  56. {
  57. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
  58. typedef std::vector<std::pair<float_type, float_type> > histogram_type;
  59. typedef std::vector<float_type> array_type;
  60. // for boost::result_of
  61. typedef iterator_range<typename histogram_type::iterator> result_type;
  62. template<typename Args>
  63. density_impl(Args const &args)
  64. : cache_size(args[density_cache_size])
  65. , cache(cache_size)
  66. , num_bins(args[density_num_bins])
  67. , samples_in_bin(num_bins + 2, 0.)
  68. , bin_positions(num_bins + 2)
  69. , histogram(
  70. num_bins + 2
  71. , std::make_pair(
  72. numeric::fdiv(args[sample | Sample()],(std::size_t)1)
  73. , numeric::fdiv(args[sample | Sample()],(std::size_t)1)
  74. )
  75. )
  76. , is_dirty(true)
  77. {
  78. }
  79. template<typename Args>
  80. void operator ()(Args const &args)
  81. {
  82. this->is_dirty = true;
  83. std::size_t cnt = count(args);
  84. // Fill up cache with cache_size first samples
  85. if (cnt <= this->cache_size)
  86. {
  87. this->cache[cnt - 1] = args[sample];
  88. }
  89. // Once cache_size samples have been accumulated, create num_bins bins of same size between
  90. // the minimum and maximum of the cached samples as well as under and overflow bins.
  91. // Store their lower bounds (bin_positions) and fill the bins with the cached samples (samples_in_bin).
  92. if (cnt == this->cache_size)
  93. {
  94. float_type minimum = numeric::fdiv((min)(args), (std::size_t)1);
  95. float_type maximum = numeric::fdiv((max)(args), (std::size_t)1);
  96. float_type bin_size = numeric::fdiv(maximum - minimum, this->num_bins );
  97. // determine bin positions (their lower bounds)
  98. for (std::size_t i = 0; i < this->num_bins + 2; ++i)
  99. {
  100. this->bin_positions[i] = minimum + (i - 1.) * bin_size;
  101. }
  102. for (typename array_type::const_iterator iter = this->cache.begin(); iter != this->cache.end(); ++iter)
  103. {
  104. if (*iter < this->bin_positions[1])
  105. {
  106. ++(this->samples_in_bin[0]);
  107. }
  108. else if (*iter >= this->bin_positions[this->num_bins + 1])
  109. {
  110. ++(this->samples_in_bin[this->num_bins + 1]);
  111. }
  112. else
  113. {
  114. typename array_type::iterator it = std::upper_bound(
  115. this->bin_positions.begin()
  116. , this->bin_positions.end()
  117. , *iter
  118. );
  119. std::size_t d = std::distance(this->bin_positions.begin(), it);
  120. ++(this->samples_in_bin[d - 1]);
  121. }
  122. }
  123. }
  124. // Add each subsequent sample to the correct bin
  125. else if (cnt > this->cache_size)
  126. {
  127. if (args[sample] < this->bin_positions[1])
  128. {
  129. ++(this->samples_in_bin[0]);
  130. }
  131. else if (args[sample] >= this->bin_positions[this->num_bins + 1])
  132. {
  133. ++(this->samples_in_bin[this->num_bins + 1]);
  134. }
  135. else
  136. {
  137. typename array_type::iterator it = std::upper_bound(
  138. this->bin_positions.begin()
  139. , this->bin_positions.end()
  140. , args[sample]
  141. );
  142. std::size_t d = std::distance(this->bin_positions.begin(), it);
  143. ++(this->samples_in_bin[d - 1]);
  144. }
  145. }
  146. }
  147. /**
  148. @pre The number of samples must meet or exceed the cache size
  149. */
  150. template<typename Args>
  151. result_type result(Args const &args) const
  152. {
  153. if (this->is_dirty)
  154. {
  155. this->is_dirty = false;
  156. // creates a vector of std::pair where each pair i holds
  157. // the values bin_positions[i] (x-axis of histogram) and
  158. // samples_in_bin[i] / cnt (y-axis of histogram).
  159. for (std::size_t i = 0; i < this->num_bins + 2; ++i)
  160. {
  161. this->histogram[i] = std::make_pair(this->bin_positions[i], numeric::fdiv(this->samples_in_bin[i], count(args)));
  162. }
  163. }
  164. // returns a range of pairs
  165. return make_iterator_range(this->histogram);
  166. }
  167. // make this accumulator serializeable
  168. // TODO split to save/load and check on parameters provided in ctor
  169. template<class Archive>
  170. void serialize(Archive & ar, const unsigned int file_version)
  171. {
  172. ar & cache_size;
  173. ar & cache;
  174. ar & num_bins;
  175. ar & samples_in_bin;
  176. ar & bin_positions;
  177. ar & histogram;
  178. ar & is_dirty;
  179. }
  180. private:
  181. std::size_t cache_size; // number of cached samples
  182. array_type cache; // cache to store the first cache_size samples
  183. std::size_t num_bins; // number of bins
  184. array_type samples_in_bin; // number of samples in each bin
  185. array_type bin_positions; // lower bounds of bins
  186. mutable histogram_type histogram; // histogram
  187. mutable bool is_dirty;
  188. };
  189. } // namespace impl
  190. ///////////////////////////////////////////////////////////////////////////////
  191. // tag::density
  192. //
  193. namespace tag
  194. {
  195. struct density
  196. : depends_on<count, min, max>
  197. , density_cache_size
  198. , density_num_bins
  199. {
  200. /// INTERNAL ONLY
  201. ///
  202. typedef accumulators::impl::density_impl<mpl::_1> impl;
  203. #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
  204. /// tag::density::cache_size named parameter
  205. /// tag::density::num_bins named parameter
  206. static boost::parameter::keyword<density_cache_size> const cache_size;
  207. static boost::parameter::keyword<density_num_bins> const num_bins;
  208. #endif
  209. };
  210. }
  211. ///////////////////////////////////////////////////////////////////////////////
  212. // extract::density
  213. //
  214. namespace extract
  215. {
  216. extractor<tag::density> const density = {};
  217. BOOST_ACCUMULATORS_IGNORE_GLOBAL(density)
  218. }
  219. using extract::density;
  220. // So that density can be automatically substituted
  221. // with weighted_density when the weight parameter is non-void.
  222. template<>
  223. struct as_weighted_feature<tag::density>
  224. {
  225. typedef tag::weighted_density type;
  226. };
  227. template<>
  228. struct feature_of<tag::weighted_density>
  229. : feature_of<tag::density>
  230. {
  231. };
  232. }} // namespace boost::accumulators
  233. #endif