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_WEIGHTED_MEDIAN_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_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/median.hpp>
21 #include <boost/accumulators/statistics/weighted_p_square_quantile.hpp>
22 #include <boost/accumulators/statistics/weighted_density.hpp>
23 #include <boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp>
25 namespace boost { namespace accumulators
30 ///////////////////////////////////////////////////////////////////////////////
31 // weighted_median_impl
34 @brief Median estimation for weighted samples based on the \f$P^2\f$ quantile estimator
36 The \f$P^2\f$ algorithm for weighted samples is invoked with a quantile probability of 0.5.
38 template<typename Sample>
39 struct weighted_median_impl
42 // for boost::result_of
43 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
45 weighted_median_impl(dont_care) {}
47 template<typename Args>
48 result_type result(Args const &args) const
50 return weighted_p_square_quantile_for_median(args);
54 ///////////////////////////////////////////////////////////////////////////////
55 // with_density_weighted_median_impl
58 @brief Median estimation for weighted samples based on the density estimator
60 The algorithm determines the bin in which the \f$0.5*cnt\f$-th sample lies, \f$cnt\f$ being
61 the total number of samples. It returns the approximate horizontal position of this sample,
62 based on a linear interpolation inside the bin.
64 template<typename Sample>
65 struct with_density_weighted_median_impl
68 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
69 typedef std::vector<std::pair<float_type, float_type> > histogram_type;
70 typedef iterator_range<typename histogram_type::iterator> range_type;
71 // for boost::result_of
72 typedef float_type result_type;
74 template<typename Args>
75 with_density_weighted_median_impl(Args const &args)
76 : sum(numeric::fdiv(args[sample | Sample()], (std::size_t)1))
81 void operator ()(dont_care)
83 this->is_dirty = true;
86 template<typename Args>
87 result_type result(Args const &args) const
91 this->is_dirty = false;
93 std::size_t cnt = count(args);
94 range_type histogram = weighted_density(args);
95 typename range_type::iterator it = histogram.begin();
96 while (this->sum < 0.5 * cnt)
98 this->sum += it->second * cnt;
102 float_type over = numeric::fdiv(this->sum - 0.5 * cnt, it->second * cnt);
103 this->median = it->first * over + (it + 1)->first * ( 1. - over );
109 // make this accumulator serializeable
110 template<class Archive>
111 void serialize(Archive & ar, const unsigned int file_version)
119 mutable float_type sum;
120 mutable bool is_dirty;
121 mutable float_type median;
124 ///////////////////////////////////////////////////////////////////////////////
125 // with_p_square_cumulative_distribution_weighted_median_impl
128 @brief Median estimation for weighted samples based on the \f$P^2\f$ cumulative distribution estimator
130 The algorithm determines the first (leftmost) bin with a height exceeding 0.5. It
131 returns the approximate horizontal position of where the cumulative distribution
132 equals 0.5, based on a linear interpolation inside the bin.
134 template<typename Sample, typename Weight>
135 struct with_p_square_cumulative_distribution_weighted_median_impl
138 typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
139 typedef typename numeric::functional::fdiv<weighted_sample, std::size_t>::result_type float_type;
140 typedef std::vector<std::pair<float_type, float_type> > histogram_type;
141 typedef iterator_range<typename histogram_type::iterator> range_type;
142 // for boost::result_of
143 typedef float_type result_type;
145 with_p_square_cumulative_distribution_weighted_median_impl(dont_care)
150 void operator ()(dont_care)
152 this->is_dirty = true;
155 template<typename Args>
156 result_type result(Args const &args) const
160 this->is_dirty = false;
162 range_type histogram = weighted_p_square_cumulative_distribution(args);
163 typename range_type::iterator it = histogram.begin();
164 while (it->second < 0.5)
168 float_type over = numeric::fdiv(it->second - 0.5, it->second - (it - 1)->second);
169 this->median = it->first * over + (it + 1)->first * ( 1. - over );
175 // make this accumulator serializeable
176 template<class Archive>
177 void serialize(Archive & ar, const unsigned int file_version)
184 mutable bool is_dirty;
185 mutable float_type median;
190 ///////////////////////////////////////////////////////////////////////////////
191 // tag::weighted_median
192 // tag::with_density_weighted_median
193 // tag::with_p_square_cumulative_distribution_weighted_median
197 struct weighted_median
198 : depends_on<weighted_p_square_quantile_for_median>
202 typedef accumulators::impl::weighted_median_impl<mpl::_1> impl;
204 struct with_density_weighted_median
205 : depends_on<count, weighted_density>
209 typedef accumulators::impl::with_density_weighted_median_impl<mpl::_1> impl;
211 struct with_p_square_cumulative_distribution_weighted_median
212 : depends_on<weighted_p_square_cumulative_distribution>
216 typedef accumulators::impl::with_p_square_cumulative_distribution_weighted_median_impl<mpl::_1, mpl::_2> impl;
221 ///////////////////////////////////////////////////////////////////////////////
222 // extract::weighted_median
226 extractor<tag::median> const weighted_median = {};
228 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_median)
231 using extract::weighted_median;
232 // weighted_median(with_p_square_quantile) -> weighted_median
234 struct as_feature<tag::weighted_median(with_p_square_quantile)>
236 typedef tag::weighted_median type;
239 // weighted_median(with_density) -> with_density_weighted_median
241 struct as_feature<tag::weighted_median(with_density)>
243 typedef tag::with_density_weighted_median type;
246 // weighted_median(with_p_square_cumulative_distribution) -> with_p_square_cumulative_distribution_weighted_median
248 struct as_feature<tag::weighted_median(with_p_square_cumulative_distribution)>
250 typedef tag::with_p_square_cumulative_distribution_weighted_median type;
253 }} // namespace boost::accumulators