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

http://hadesmem.googlecode.com/ · C++ Header · 221 lines · 153 code · 26 blank · 42 comment · 16 complexity · 3b957753b0377da9a8f7d42c352d6a50 MD5 · raw file

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