Project revived from Feb2017
[EroSomnia.git] / deps / boost_1_63_0 / boost / accumulators / statistics / density.hpp
blob88ca17df79d8a1df6cfacb720ff4f9fba52eccef
2 ///////////////////////////////////////////////////////////////////////////////
3 // density.hpp
4 //
5 // Copyright 2006 Daniel Egloff, Olivier Gygi. Distributed under the Boost
6 // Software License, Version 1.0. (See accompanying file
7 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_ACCUMULATORS_STATISTICS_DENSITY_HPP_DE_01_01_2006
10 #define BOOST_ACCUMULATORS_STATISTICS_DENSITY_HPP_DE_01_01_2006
12 #include <vector>
13 #include <limits>
14 #include <functional>
15 #include <boost/range.hpp>
16 #include <boost/parameter/keyword.hpp>
17 #include <boost/mpl/placeholders.hpp>
18 #include <boost/accumulators/accumulators_fwd.hpp>
19 #include <boost/accumulators/framework/accumulator_base.hpp>
20 #include <boost/accumulators/framework/extractor.hpp>
21 #include <boost/accumulators/numeric/functional.hpp>
22 #include <boost/accumulators/framework/parameters/sample.hpp>
23 #include <boost/accumulators/framework/depends_on.hpp>
24 #include <boost/accumulators/statistics_fwd.hpp>
25 #include <boost/accumulators/statistics/count.hpp>
26 #include <boost/accumulators/statistics/max.hpp>
27 #include <boost/accumulators/statistics/min.hpp>
29 namespace boost { namespace accumulators
32 ///////////////////////////////////////////////////////////////////////////////
33 // cache_size and num_bins named parameters
35 BOOST_PARAMETER_NESTED_KEYWORD(tag, density_cache_size, cache_size)
36 BOOST_PARAMETER_NESTED_KEYWORD(tag, density_num_bins, num_bins)
38 BOOST_ACCUMULATORS_IGNORE_GLOBAL(density_cache_size)
39 BOOST_ACCUMULATORS_IGNORE_GLOBAL(density_num_bins)
41 namespace impl
43 ///////////////////////////////////////////////////////////////////////////////
44 // density_impl
45 // density histogram
46 /**
47 @brief Histogram density estimator
49 The histogram density estimator returns a histogram of the sample distribution. The positions and sizes of the bins
50 are determined using a specifiable number of cached samples (cache_size). The range between the minimum and the
51 maximum of the cached samples is subdivided into a specifiable number of bins (num_bins) of same size. Additionally,
52 an under- and an overflow bin is added to capture future under- and overflow samples. Once the bins are determined,
53 the cached samples and all subsequent samples are added to the correct bins. At the end, a range of std::pair is
54 return, where each pair contains the position of the bin (lower bound) and the samples count (normalized with the
55 total number of samples).
57 @param density_cache_size Number of first samples used to determine min and max.
58 @param density_num_bins Number of bins (two additional bins collect under- and overflow samples).
60 template<typename Sample>
61 struct density_impl
62 : accumulator_base
64 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
65 typedef std::vector<std::pair<float_type, float_type> > histogram_type;
66 typedef std::vector<float_type> array_type;
67 // for boost::result_of
68 typedef iterator_range<typename histogram_type::iterator> result_type;
70 template<typename Args>
71 density_impl(Args const &args)
72 : cache_size(args[density_cache_size])
73 , cache(cache_size)
74 , num_bins(args[density_num_bins])
75 , samples_in_bin(num_bins + 2, 0.)
76 , bin_positions(num_bins + 2)
77 , histogram(
78 num_bins + 2
79 , std::make_pair(
80 numeric::fdiv(args[sample | Sample()],(std::size_t)1)
81 , numeric::fdiv(args[sample | Sample()],(std::size_t)1)
84 , is_dirty(true)
88 template<typename Args>
89 void operator ()(Args const &args)
91 this->is_dirty = true;
93 std::size_t cnt = count(args);
95 // Fill up cache with cache_size first samples
96 if (cnt <= this->cache_size)
98 this->cache[cnt - 1] = args[sample];
101 // Once cache_size samples have been accumulated, create num_bins bins of same size between
102 // the minimum and maximum of the cached samples as well as under and overflow bins.
103 // Store their lower bounds (bin_positions) and fill the bins with the cached samples (samples_in_bin).
104 if (cnt == this->cache_size)
106 float_type minimum = numeric::fdiv((min)(args), (std::size_t)1);
107 float_type maximum = numeric::fdiv((max)(args), (std::size_t)1);
108 float_type bin_size = numeric::fdiv(maximum - minimum, this->num_bins );
110 // determine bin positions (their lower bounds)
111 for (std::size_t i = 0; i < this->num_bins + 2; ++i)
113 this->bin_positions[i] = minimum + (i - 1.) * bin_size;
116 for (typename array_type::const_iterator iter = this->cache.begin(); iter != this->cache.end(); ++iter)
118 if (*iter < this->bin_positions[1])
120 ++(this->samples_in_bin[0]);
122 else if (*iter >= this->bin_positions[this->num_bins + 1])
124 ++(this->samples_in_bin[this->num_bins + 1]);
126 else
128 typename array_type::iterator it = std::upper_bound(
129 this->bin_positions.begin()
130 , this->bin_positions.end()
131 , *iter
134 std::size_t d = std::distance(this->bin_positions.begin(), it);
135 ++(this->samples_in_bin[d - 1]);
139 // Add each subsequent sample to the correct bin
140 else if (cnt > this->cache_size)
142 if (args[sample] < this->bin_positions[1])
144 ++(this->samples_in_bin[0]);
146 else if (args[sample] >= this->bin_positions[this->num_bins + 1])
148 ++(this->samples_in_bin[this->num_bins + 1]);
150 else
152 typename array_type::iterator it = std::upper_bound(
153 this->bin_positions.begin()
154 , this->bin_positions.end()
155 , args[sample]
158 std::size_t d = std::distance(this->bin_positions.begin(), it);
159 ++(this->samples_in_bin[d - 1]);
165 @pre The number of samples must meet or exceed the cache size
167 template<typename Args>
168 result_type result(Args const &args) const
170 if (this->is_dirty)
172 this->is_dirty = false;
174 // creates a vector of std::pair where each pair i holds
175 // the values bin_positions[i] (x-axis of histogram) and
176 // samples_in_bin[i] / cnt (y-axis of histogram).
178 for (std::size_t i = 0; i < this->num_bins + 2; ++i)
180 this->histogram[i] = std::make_pair(this->bin_positions[i], numeric::fdiv(this->samples_in_bin[i], count(args)));
183 // returns a range of pairs
184 return make_iterator_range(this->histogram);
187 private:
188 std::size_t cache_size; // number of cached samples
189 array_type cache; // cache to store the first cache_size samples
190 std::size_t num_bins; // number of bins
191 array_type samples_in_bin; // number of samples in each bin
192 array_type bin_positions; // lower bounds of bins
193 mutable histogram_type histogram; // histogram
194 mutable bool is_dirty;
197 } // namespace impl
199 ///////////////////////////////////////////////////////////////////////////////
200 // tag::density
202 namespace tag
204 struct density
205 : depends_on<count, min, max>
206 , density_cache_size
207 , density_num_bins
209 /// INTERNAL ONLY
211 typedef accumulators::impl::density_impl<mpl::_1> impl;
213 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
214 /// tag::density::cache_size named parameter
215 /// tag::density::num_bins named parameter
216 static boost::parameter::keyword<density_cache_size> const cache_size;
217 static boost::parameter::keyword<density_num_bins> const num_bins;
218 #endif
222 ///////////////////////////////////////////////////////////////////////////////
223 // extract::density
225 namespace extract
227 extractor<tag::density> const density = {};
229 BOOST_ACCUMULATORS_IGNORE_GLOBAL(density)
232 using extract::density;
234 // So that density can be automatically substituted
235 // with weighted_density when the weight parameter is non-void.
236 template<>
237 struct as_weighted_feature<tag::density>
239 typedef tag::weighted_density type;
242 template<>
243 struct feature_of<tag::weighted_density>
244 : feature_of<tag::density>
248 }} // namespace boost::accumulators
250 #endif