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