1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2006 Eric Niebler, 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)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_MEDIAN_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_MEDIAN_HPP_EAN_28_10_2005
11 #include <boost/mpl/placeholders.hpp>
12 #include <boost/range/iterator_range.hpp>
13 #include <boost/accumulators/framework/accumulator_base.hpp>
14 #include <boost/accumulators/framework/extractor.hpp>
15 #include <boost/accumulators/numeric/functional.hpp>
16 #include <boost/accumulators/framework/parameters/sample.hpp>
17 #include <boost/accumulators/framework/depends_on.hpp>
18 #include <boost/accumulators/statistics_fwd.hpp>
19 #include <boost/accumulators/statistics/count.hpp>
20 #include <boost/accumulators/statistics/p_square_quantile.hpp>
21 #include <boost/accumulators/statistics/density.hpp>
22 #include <boost/accumulators/statistics/p_square_cumul_dist.hpp>
24 namespace boost { namespace accumulators
29 ///////////////////////////////////////////////////////////////////////////////
33 @brief Median estimation based on the \f$P^2\f$ quantile estimator
35 The \f$P^2\f$ algorithm is invoked with a quantile probability of 0.5.
37 template<typename Sample>
41 // for boost::result_of
42 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
44 median_impl(dont_care) {}
46 template<typename Args>
47 result_type result(Args const &args) const
49 return p_square_quantile_for_median(args);
52 ///////////////////////////////////////////////////////////////////////////////
53 // with_density_median_impl
56 @brief Median estimation based on the density estimator
58 The algorithm determines the bin in which the \f$0.5*cnt\f$-th sample lies, \f$cnt\f$ being
59 the total number of samples. It returns the approximate horizontal position of this sample,
60 based on a linear interpolation inside the bin.
62 template<typename Sample>
63 struct with_density_median_impl
66 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
67 typedef std::vector<std::pair<float_type, float_type> > histogram_type;
68 typedef iterator_range<typename histogram_type::iterator> range_type;
69 // for boost::result_of
70 typedef float_type result_type;
72 template<typename Args>
73 with_density_median_impl(Args const &args)
74 : sum(numeric::fdiv(args[sample | Sample()], (std::size_t)1))
79 void operator ()(dont_care)
81 this->is_dirty = true;
85 template<typename Args>
86 result_type result(Args const &args) const
90 this->is_dirty = false;
92 std::size_t cnt = count(args);
93 range_type histogram = density(args);
94 typename range_type::iterator it = histogram.begin();
95 while (this->sum < 0.5 * cnt)
97 this->sum += it->second * cnt;
101 float_type over = numeric::fdiv(this->sum - 0.5 * cnt, it->second * cnt);
102 this->median = it->first * over + (it + 1)->first * (1. - over);
109 mutable float_type sum;
110 mutable bool is_dirty;
111 mutable float_type median;
114 ///////////////////////////////////////////////////////////////////////////////
115 // with_p_square_cumulative_distribution_median_impl
118 @brief Median estimation based on the \f$P^2\f$ cumulative distribution estimator
120 The algorithm determines the first (leftmost) bin with a height exceeding 0.5. It
121 returns the approximate horizontal position of where the cumulative distribution
122 equals 0.5, based on a linear interpolation inside the bin.
124 template<typename Sample>
125 struct with_p_square_cumulative_distribution_median_impl
128 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
129 typedef std::vector<std::pair<float_type, float_type> > histogram_type;
130 typedef iterator_range<typename histogram_type::iterator> range_type;
131 // for boost::result_of
132 typedef float_type result_type;
134 with_p_square_cumulative_distribution_median_impl(dont_care)
139 void operator ()(dont_care)
141 this->is_dirty = true;
144 template<typename Args>
145 result_type result(Args const &args) const
149 this->is_dirty = false;
151 range_type histogram = p_square_cumulative_distribution(args);
152 typename range_type::iterator it = histogram.begin();
153 while (it->second < 0.5)
157 float_type over = numeric::fdiv(it->second - 0.5, it->second - (it - 1)->second);
158 this->median = it->first * over + (it + 1)->first * ( 1. - over );
165 mutable bool is_dirty;
166 mutable float_type median;
171 ///////////////////////////////////////////////////////////////////////////////
173 // tag::with_densisty_median
174 // tag::with_p_square_cumulative_distribution_median
179 : depends_on<p_square_quantile_for_median>
183 typedef accumulators::impl::median_impl<mpl::_1> impl;
185 struct with_density_median
186 : depends_on<count, density>
190 typedef accumulators::impl::with_density_median_impl<mpl::_1> impl;
192 struct with_p_square_cumulative_distribution_median
193 : depends_on<p_square_cumulative_distribution>
197 typedef accumulators::impl::with_p_square_cumulative_distribution_median_impl<mpl::_1> impl;
201 ///////////////////////////////////////////////////////////////////////////////
203 // extract::with_density_median
204 // extract::with_p_square_cumulative_distribution_median
208 extractor<tag::median> const median = {};
209 extractor<tag::with_density_median> const with_density_median = {};
210 extractor<tag::with_p_square_cumulative_distribution_median> const with_p_square_cumulative_distribution_median = {};
212 BOOST_ACCUMULATORS_IGNORE_GLOBAL(median)
213 BOOST_ACCUMULATORS_IGNORE_GLOBAL(with_density_median)
214 BOOST_ACCUMULATORS_IGNORE_GLOBAL(with_p_square_cumulative_distribution_median)
217 using extract::median;
218 using extract::with_density_median;
219 using extract::with_p_square_cumulative_distribution_median;
221 // median(with_p_square_quantile) -> median
223 struct as_feature<tag::median(with_p_square_quantile)>
225 typedef tag::median type;
228 // median(with_density) -> with_density_median
230 struct as_feature<tag::median(with_density)>
232 typedef tag::with_density_median type;
235 // median(with_p_square_cumulative_distribution) -> with_p_square_cumulative_distribution_median
237 struct as_feature<tag::median(with_p_square_cumulative_distribution)>
239 typedef tag::with_p_square_cumulative_distribution_median type;
242 // for the purposes of feature-based dependency resolution,
243 // with_density_median and with_p_square_cumulative_distribution_median
244 // provide the same feature as median
246 struct feature_of<tag::with_density_median>
247 : feature_of<tag::median>
252 struct feature_of<tag::with_p_square_cumulative_distribution_median>
253 : feature_of<tag::median>
257 // So that median can be automatically substituted with
258 // weighted_median when the weight parameter is non-void.
260 struct as_weighted_feature<tag::median>
262 typedef tag::weighted_median type;
266 struct feature_of<tag::weighted_median>
267 : feature_of<tag::median>
271 // So that with_density_median can be automatically substituted with
272 // with_density_weighted_median when the weight parameter is non-void.
274 struct as_weighted_feature<tag::with_density_median>
276 typedef tag::with_density_weighted_median type;
280 struct feature_of<tag::with_density_weighted_median>
281 : feature_of<tag::with_density_median>
285 // So that with_p_square_cumulative_distribution_median can be automatically substituted with
286 // with_p_square_cumulative_distribution_weighted_median when the weight parameter is non-void.
288 struct as_weighted_feature<tag::with_p_square_cumulative_distribution_median>
290 typedef tag::with_p_square_cumulative_distribution_weighted_median type;
294 struct feature_of<tag::with_p_square_cumulative_distribution_weighted_median>
295 : feature_of<tag::with_p_square_cumulative_distribution_median>
299 }} // namespace boost::accumulators