annotate DEPENDENCIES/generic/include/boost/accumulators/statistics/weighted_density.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2 // weighted_density.hpp
Chris@16 3 //
Chris@16 4 // Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
Chris@16 5 // Software License, Version 1.0. (See accompanying file
Chris@16 6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7
Chris@16 8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_DENSITY_HPP_DE_01_01_2006
Chris@16 9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_DENSITY_HPP_DE_01_01_2006
Chris@16 10
Chris@16 11 #include <vector>
Chris@16 12 #include <limits>
Chris@16 13 #include <functional>
Chris@16 14 #include <boost/range.hpp>
Chris@16 15 #include <boost/parameter/keyword.hpp>
Chris@16 16 #include <boost/mpl/placeholders.hpp>
Chris@16 17 #include <boost/accumulators/framework/accumulator_base.hpp>
Chris@16 18 #include <boost/accumulators/framework/extractor.hpp>
Chris@16 19 #include <boost/accumulators/numeric/functional.hpp>
Chris@16 20 #include <boost/accumulators/framework/parameters/sample.hpp>
Chris@16 21 #include <boost/accumulators/statistics_fwd.hpp>
Chris@16 22 #include <boost/accumulators/statistics/sum.hpp>
Chris@16 23 #include <boost/accumulators/statistics/max.hpp>
Chris@16 24 #include <boost/accumulators/statistics/min.hpp>
Chris@16 25 #include <boost/accumulators/statistics/density.hpp> // for named parameters density_cache_size and density_num_bins
Chris@16 26
Chris@16 27 namespace boost { namespace accumulators
Chris@16 28 {
Chris@16 29
Chris@16 30 namespace impl
Chris@16 31 {
Chris@16 32 ///////////////////////////////////////////////////////////////////////////////
Chris@16 33 // weighted_density_impl
Chris@16 34 // density histogram for weighted samples
Chris@16 35 /**
Chris@16 36 @brief Histogram density estimator for weighted samples
Chris@16 37
Chris@16 38 The histogram density estimator returns a histogram of the sample distribution. The positions and sizes of the bins
Chris@16 39 are determined using a specifiable number of cached samples (cache_size). The range between the minimum and the
Chris@16 40 maximum of the cached samples is subdivided into a specifiable number of bins (num_bins) of same size. Additionally,
Chris@16 41 an under- and an overflow bin is added to capture future under- and overflow samples. Once the bins are determined,
Chris@16 42 the cached samples and all subsequent samples are added to the correct bins. At the end, a range of std::pair is
Chris@16 43 returned, where each pair contains the position of the bin (lower bound) and the sum of the weights (normalized with the
Chris@16 44 sum of all weights).
Chris@16 45
Chris@16 46 @param density_cache_size Number of first samples used to determine min and max.
Chris@16 47 @param density_num_bins Number of bins (two additional bins collect under- and overflow samples).
Chris@16 48 */
Chris@16 49 template<typename Sample, typename Weight>
Chris@16 50 struct weighted_density_impl
Chris@16 51 : accumulator_base
Chris@16 52 {
Chris@16 53 typedef typename numeric::functional::fdiv<Weight, std::size_t>::result_type float_type;
Chris@16 54 typedef std::vector<std::pair<float_type, float_type> > histogram_type;
Chris@16 55 typedef std::vector<float_type> array_type;
Chris@16 56 // for boost::result_of
Chris@16 57 typedef iterator_range<typename histogram_type::iterator> result_type;
Chris@16 58
Chris@16 59 template<typename Args>
Chris@16 60 weighted_density_impl(Args const &args)
Chris@16 61 : cache_size(args[density_cache_size])
Chris@16 62 , cache(cache_size)
Chris@16 63 , num_bins(args[density_num_bins])
Chris@16 64 , samples_in_bin(num_bins + 2, 0.)
Chris@16 65 , bin_positions(num_bins + 2)
Chris@16 66 , histogram(
Chris@16 67 num_bins + 2
Chris@16 68 , std::make_pair(
Chris@16 69 numeric::fdiv(args[sample | Sample()],(std::size_t)1)
Chris@16 70 , numeric::fdiv(args[sample | Sample()],(std::size_t)1)
Chris@16 71 )
Chris@16 72 )
Chris@16 73 , is_dirty(true)
Chris@16 74 {
Chris@16 75 }
Chris@16 76
Chris@16 77 template<typename Args>
Chris@16 78 void operator ()(Args const &args)
Chris@16 79 {
Chris@16 80 this->is_dirty = true;
Chris@16 81
Chris@16 82 std::size_t cnt = count(args);
Chris@16 83
Chris@16 84 // Fill up cache with cache_size first samples
Chris@16 85 if (cnt <= this->cache_size)
Chris@16 86 {
Chris@16 87 this->cache[cnt - 1] = std::make_pair(args[sample], args[weight]);
Chris@16 88 }
Chris@16 89
Chris@16 90 // Once cache_size samples have been accumulated, create num_bins bins of same size between
Chris@16 91 // the minimum and maximum of the cached samples as well as an under- and an overflow bin.
Chris@16 92 // Store their lower bounds (bin_positions) and fill the bins with the cached samples (samples_in_bin).
Chris@16 93 if (cnt == this->cache_size)
Chris@16 94 {
Chris@16 95 float_type minimum = numeric::fdiv((min)(args),(std::size_t)1);
Chris@16 96 float_type maximum = numeric::fdiv((max)(args),(std::size_t)1);
Chris@16 97 float_type bin_size = numeric::fdiv(maximum - minimum, this->num_bins);
Chris@16 98
Chris@16 99 // determine bin positions (their lower bounds)
Chris@16 100 for (std::size_t i = 0; i < this->num_bins + 2; ++i)
Chris@16 101 {
Chris@16 102 this->bin_positions[i] = minimum + (i - 1.) * bin_size;
Chris@16 103 }
Chris@16 104
Chris@16 105 for (typename histogram_type::const_iterator iter = this->cache.begin(); iter != this->cache.end(); ++iter)
Chris@16 106 {
Chris@16 107 if (iter->first < this->bin_positions[1])
Chris@16 108 {
Chris@16 109 this->samples_in_bin[0] += iter->second;
Chris@16 110 }
Chris@16 111 else if (iter->first >= this->bin_positions[this->num_bins + 1])
Chris@16 112 {
Chris@16 113 this->samples_in_bin[this->num_bins + 1] += iter->second;
Chris@16 114 }
Chris@16 115 else
Chris@16 116 {
Chris@16 117 typename array_type::iterator it = std::upper_bound(
Chris@16 118 this->bin_positions.begin()
Chris@16 119 , this->bin_positions.end()
Chris@16 120 , iter->first
Chris@16 121 );
Chris@16 122
Chris@16 123 std::size_t d = std::distance(this->bin_positions.begin(), it);
Chris@16 124 this->samples_in_bin[d - 1] += iter->second;
Chris@16 125 }
Chris@16 126 }
Chris@16 127 }
Chris@16 128 // Add each subsequent sample to the correct bin
Chris@16 129 else if (cnt > this->cache_size)
Chris@16 130 {
Chris@16 131 if (args[sample] < this->bin_positions[1])
Chris@16 132 {
Chris@16 133 this->samples_in_bin[0] += args[weight];
Chris@16 134 }
Chris@16 135 else if (args[sample] >= this->bin_positions[this->num_bins + 1])
Chris@16 136 {
Chris@16 137 this->samples_in_bin[this->num_bins + 1] += args[weight];
Chris@16 138 }
Chris@16 139 else
Chris@16 140 {
Chris@16 141 typename array_type::iterator it = std::upper_bound(
Chris@16 142 this->bin_positions.begin()
Chris@16 143 , this->bin_positions.end()
Chris@16 144 , args[sample]
Chris@16 145 );
Chris@16 146
Chris@16 147 std::size_t d = std::distance(this->bin_positions.begin(), it);
Chris@16 148 this->samples_in_bin[d - 1] += args[weight];
Chris@16 149 }
Chris@16 150 }
Chris@16 151 }
Chris@16 152
Chris@16 153 template<typename Args>
Chris@16 154 result_type result(Args const &args) const
Chris@16 155 {
Chris@16 156 if (this->is_dirty)
Chris@16 157 {
Chris@16 158 this->is_dirty = false;
Chris@16 159
Chris@16 160 // creates a vector of std::pair where each pair i holds
Chris@16 161 // the values bin_positions[i] (x-axis of histogram) and
Chris@16 162 // samples_in_bin[i] / cnt (y-axis of histogram).
Chris@16 163
Chris@16 164 for (std::size_t i = 0; i < this->num_bins + 2; ++i)
Chris@16 165 {
Chris@16 166 this->histogram[i] = std::make_pair(this->bin_positions[i], numeric::fdiv(this->samples_in_bin[i], sum_of_weights(args)));
Chris@16 167 }
Chris@16 168 }
Chris@16 169
Chris@16 170 // returns a range of pairs
Chris@16 171 return make_iterator_range(this->histogram);
Chris@16 172 }
Chris@16 173
Chris@16 174 private:
Chris@16 175 std::size_t cache_size; // number of cached samples
Chris@16 176 histogram_type cache; // cache to store the first cache_size samples with their weights as std::pair
Chris@16 177 std::size_t num_bins; // number of bins
Chris@16 178 array_type samples_in_bin; // number of samples in each bin
Chris@16 179 array_type bin_positions; // lower bounds of bins
Chris@16 180 mutable histogram_type histogram; // histogram
Chris@16 181 mutable bool is_dirty;
Chris@16 182 };
Chris@16 183
Chris@16 184 } // namespace impl
Chris@16 185
Chris@16 186 ///////////////////////////////////////////////////////////////////////////////
Chris@16 187 // tag::weighted_density
Chris@16 188 //
Chris@16 189 namespace tag
Chris@16 190 {
Chris@16 191 struct weighted_density
Chris@16 192 : depends_on<count, sum_of_weights, min, max>
Chris@16 193 , density_cache_size
Chris@16 194 , density_num_bins
Chris@16 195 {
Chris@16 196 /// INTERNAL ONLY
Chris@16 197 ///
Chris@16 198 typedef accumulators::impl::weighted_density_impl<mpl::_1, mpl::_2> impl;
Chris@16 199
Chris@16 200 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
Chris@16 201 static boost::parameter::keyword<density_cache_size> const cache_size;
Chris@16 202 static boost::parameter::keyword<density_num_bins> const num_bins;
Chris@16 203 #endif
Chris@16 204 };
Chris@16 205 }
Chris@16 206
Chris@16 207 ///////////////////////////////////////////////////////////////////////////////
Chris@16 208 // extract::weighted_density
Chris@16 209 //
Chris@16 210 namespace extract
Chris@16 211 {
Chris@16 212 extractor<tag::density> const weighted_density = {};
Chris@16 213
Chris@16 214 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_density)
Chris@16 215 }
Chris@16 216
Chris@16 217 using extract::weighted_density;
Chris@16 218
Chris@16 219 }} // namespace boost::accumulators
Chris@16 220
Chris@16 221 #endif