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

http://hadesmem.googlecode.com/ · C++ Header · 246 lines · 165 code · 29 blank · 52 comment · 16 complexity · 76f52475c7e38730acceae4dd4cd364a MD5 · raw file

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