Chris@16
|
1 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 // p_square_quantile.hpp
|
Chris@16
|
3 //
|
Chris@16
|
4 // Copyright 2005 Daniel Egloff. 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_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
|
Chris@16
|
9 #define BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
|
Chris@16
|
10
|
Chris@16
|
11 #include <cmath>
|
Chris@16
|
12 #include <functional>
|
Chris@16
|
13 #include <boost/array.hpp>
|
Chris@16
|
14 #include <boost/mpl/placeholders.hpp>
|
Chris@16
|
15 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
16 #include <boost/parameter/keyword.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/framework/depends_on.hpp>
|
Chris@16
|
22 #include <boost/accumulators/statistics_fwd.hpp>
|
Chris@16
|
23 #include <boost/accumulators/statistics/count.hpp>
|
Chris@16
|
24 #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 namespace boost { namespace accumulators
|
Chris@16
|
27 {
|
Chris@16
|
28
|
Chris@16
|
29 namespace impl
|
Chris@16
|
30 {
|
Chris@16
|
31 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
32 // p_square_quantile_impl
|
Chris@16
|
33 // single quantile estimation
|
Chris@16
|
34 /**
|
Chris@16
|
35 @brief Single quantile estimation with the \f$P^2\f$ algorithm
|
Chris@16
|
36
|
Chris@16
|
37 The \f$P^2\f$ algorithm estimates a quantile dynamically without storing samples. Instead of
|
Chris@16
|
38 storing the whole sample cumulative distribution, only five points (markers) are stored. The heights
|
Chris@16
|
39 of these markers are the minimum and the maximum of the samples and the current estimates of the
|
Chris@16
|
40 \f$(p/2)\f$-, \f$p\f$- and \f$(1+p)/2\f$-quantiles. Their positions are equal to the number
|
Chris@16
|
41 of samples that are smaller or equal to the markers. Each time a new samples is recorded, the
|
Chris@16
|
42 positions of the markers are updated and if necessary their heights are adjusted using a piecewise-
|
Chris@16
|
43 parabolic formula.
|
Chris@16
|
44
|
Chris@16
|
45 For further details, see
|
Chris@16
|
46
|
Chris@16
|
47 R. Jain and I. Chlamtac, The P^2 algorithm for dynamic calculation of quantiles and
|
Chris@16
|
48 histograms without storing observations, Communications of the ACM,
|
Chris@16
|
49 Volume 28 (October), Number 10, 1985, p. 1076-1085.
|
Chris@16
|
50
|
Chris@16
|
51 @param quantile_probability
|
Chris@16
|
52 */
|
Chris@16
|
53 template<typename Sample, typename Impl>
|
Chris@16
|
54 struct p_square_quantile_impl
|
Chris@16
|
55 : accumulator_base
|
Chris@16
|
56 {
|
Chris@16
|
57 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
|
Chris@16
|
58 typedef array<float_type, 5> array_type;
|
Chris@16
|
59 // for boost::result_of
|
Chris@16
|
60 typedef float_type result_type;
|
Chris@16
|
61
|
Chris@16
|
62 template<typename Args>
|
Chris@16
|
63 p_square_quantile_impl(Args const &args)
|
Chris@16
|
64 : p(is_same<Impl, for_median>::value ? 0.5 : args[quantile_probability | 0.5])
|
Chris@16
|
65 , heights()
|
Chris@16
|
66 , actual_positions()
|
Chris@16
|
67 , desired_positions()
|
Chris@16
|
68 , positions_increments()
|
Chris@16
|
69 {
|
Chris@16
|
70 for(std::size_t i = 0; i < 5; ++i)
|
Chris@16
|
71 {
|
Chris@16
|
72 this->actual_positions[i] = i + 1.;
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 this->desired_positions[0] = 1.;
|
Chris@16
|
76 this->desired_positions[1] = 1. + 2. * this->p;
|
Chris@16
|
77 this->desired_positions[2] = 1. + 4. * this->p;
|
Chris@16
|
78 this->desired_positions[3] = 3. + 2. * this->p;
|
Chris@16
|
79 this->desired_positions[4] = 5.;
|
Chris@16
|
80
|
Chris@16
|
81 this->positions_increments[0] = 0.;
|
Chris@16
|
82 this->positions_increments[1] = this->p / 2.;
|
Chris@16
|
83 this->positions_increments[2] = this->p;
|
Chris@16
|
84 this->positions_increments[3] = (1. + this->p) / 2.;
|
Chris@16
|
85 this->positions_increments[4] = 1.;
|
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 std::size_t cnt = count(args);
|
Chris@16
|
92
|
Chris@16
|
93 // accumulate 5 first samples
|
Chris@16
|
94 if(cnt <= 5)
|
Chris@16
|
95 {
|
Chris@16
|
96 this->heights[cnt - 1] = args[sample];
|
Chris@16
|
97
|
Chris@16
|
98 // complete the initialization of heights by sorting
|
Chris@16
|
99 if(cnt == 5)
|
Chris@16
|
100 {
|
Chris@16
|
101 std::sort(this->heights.begin(), this->heights.end());
|
Chris@16
|
102 }
|
Chris@16
|
103 }
|
Chris@16
|
104 else
|
Chris@16
|
105 {
|
Chris@16
|
106 std::size_t sample_cell = 1; // k
|
Chris@16
|
107
|
Chris@16
|
108 // find cell k such that heights[k-1] <= args[sample] < heights[k] and adjust extreme values
|
Chris@16
|
109 if (args[sample] < this->heights[0])
|
Chris@16
|
110 {
|
Chris@16
|
111 this->heights[0] = args[sample];
|
Chris@16
|
112 sample_cell = 1;
|
Chris@16
|
113 }
|
Chris@16
|
114 else if (this->heights[4] <= args[sample])
|
Chris@16
|
115 {
|
Chris@16
|
116 this->heights[4] = args[sample];
|
Chris@16
|
117 sample_cell = 4;
|
Chris@16
|
118 }
|
Chris@16
|
119 else
|
Chris@16
|
120 {
|
Chris@16
|
121 typedef typename array_type::iterator iterator;
|
Chris@16
|
122 iterator it = std::upper_bound(
|
Chris@16
|
123 this->heights.begin()
|
Chris@16
|
124 , this->heights.end()
|
Chris@16
|
125 , args[sample]
|
Chris@16
|
126 );
|
Chris@16
|
127
|
Chris@16
|
128 sample_cell = std::distance(this->heights.begin(), it);
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 // update positions of markers above sample_cell
|
Chris@16
|
132 for(std::size_t i = sample_cell; i < 5; ++i)
|
Chris@16
|
133 {
|
Chris@16
|
134 ++this->actual_positions[i];
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 // update desired positions of all markers
|
Chris@16
|
138 for(std::size_t i = 0; i < 5; ++i)
|
Chris@16
|
139 {
|
Chris@16
|
140 this->desired_positions[i] += this->positions_increments[i];
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 // adjust heights and actual positions of markers 1 to 3 if necessary
|
Chris@16
|
144 for(std::size_t i = 1; i <= 3; ++i)
|
Chris@16
|
145 {
|
Chris@16
|
146 // offset to desired positions
|
Chris@16
|
147 float_type d = this->desired_positions[i] - this->actual_positions[i];
|
Chris@16
|
148
|
Chris@16
|
149 // offset to next position
|
Chris@16
|
150 float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
|
Chris@16
|
151
|
Chris@16
|
152 // offset to previous position
|
Chris@16
|
153 float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
|
Chris@16
|
154
|
Chris@16
|
155 // height ds
|
Chris@16
|
156 float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
|
Chris@16
|
157 float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
|
Chris@16
|
158
|
Chris@16
|
159 if((d >= 1. && dp > 1.) || (d <= -1. && dm < -1.))
|
Chris@16
|
160 {
|
Chris@16
|
161 short sign_d = static_cast<short>(d / std::abs(d));
|
Chris@16
|
162
|
Chris@16
|
163 // try adjusting heights[i] using p-squared formula
|
Chris@16
|
164 float_type h = this->heights[i] + sign_d / (dp - dm) * ((sign_d - dm) * hp
|
Chris@16
|
165 + (dp - sign_d) * hm);
|
Chris@16
|
166
|
Chris@16
|
167 if(this->heights[i - 1] < h && h < this->heights[i + 1])
|
Chris@16
|
168 {
|
Chris@16
|
169 this->heights[i] = h;
|
Chris@16
|
170 }
|
Chris@16
|
171 else
|
Chris@16
|
172 {
|
Chris@16
|
173 // use linear formula
|
Chris@16
|
174 if(d > 0)
|
Chris@16
|
175 {
|
Chris@16
|
176 this->heights[i] += hp;
|
Chris@16
|
177 }
|
Chris@16
|
178 if(d < 0)
|
Chris@16
|
179 {
|
Chris@16
|
180 this->heights[i] -= hm;
|
Chris@16
|
181 }
|
Chris@16
|
182 }
|
Chris@16
|
183 this->actual_positions[i] += sign_d;
|
Chris@16
|
184 }
|
Chris@16
|
185 }
|
Chris@16
|
186 }
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 result_type result(dont_care) const
|
Chris@16
|
190 {
|
Chris@16
|
191 return this->heights[2];
|
Chris@16
|
192 }
|
Chris@16
|
193
|
Chris@16
|
194 private:
|
Chris@16
|
195 float_type p; // the quantile probability p
|
Chris@16
|
196 array_type heights; // q_i
|
Chris@16
|
197 array_type actual_positions; // n_i
|
Chris@16
|
198 array_type desired_positions; // n'_i
|
Chris@16
|
199 array_type positions_increments; // dn'_i
|
Chris@16
|
200 };
|
Chris@16
|
201
|
Chris@16
|
202 } // namespace detail
|
Chris@16
|
203
|
Chris@16
|
204 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
205 // tag::p_square_quantile
|
Chris@16
|
206 //
|
Chris@16
|
207 namespace tag
|
Chris@16
|
208 {
|
Chris@16
|
209 struct p_square_quantile
|
Chris@16
|
210 : depends_on<count>
|
Chris@16
|
211 {
|
Chris@16
|
212 /// INTERNAL ONLY
|
Chris@16
|
213 ///
|
Chris@16
|
214 typedef accumulators::impl::p_square_quantile_impl<mpl::_1, regular> impl;
|
Chris@16
|
215 };
|
Chris@16
|
216 struct p_square_quantile_for_median
|
Chris@16
|
217 : depends_on<count>
|
Chris@16
|
218 {
|
Chris@16
|
219 /// INTERNAL ONLY
|
Chris@16
|
220 ///
|
Chris@16
|
221 typedef accumulators::impl::p_square_quantile_impl<mpl::_1, for_median> impl;
|
Chris@16
|
222 };
|
Chris@16
|
223 }
|
Chris@16
|
224
|
Chris@16
|
225 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
226 // extract::p_square_quantile
|
Chris@16
|
227 // extract::p_square_quantile_for_median
|
Chris@16
|
228 //
|
Chris@16
|
229 namespace extract
|
Chris@16
|
230 {
|
Chris@16
|
231 extractor<tag::p_square_quantile> const p_square_quantile = {};
|
Chris@16
|
232 extractor<tag::p_square_quantile_for_median> const p_square_quantile_for_median = {};
|
Chris@16
|
233
|
Chris@16
|
234 BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_quantile)
|
Chris@16
|
235 BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_quantile_for_median)
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238 using extract::p_square_quantile;
|
Chris@16
|
239 using extract::p_square_quantile_for_median;
|
Chris@16
|
240
|
Chris@16
|
241 // So that p_square_quantile can be automatically substituted with
|
Chris@16
|
242 // weighted_p_square_quantile when the weight parameter is non-void
|
Chris@16
|
243 template<>
|
Chris@16
|
244 struct as_weighted_feature<tag::p_square_quantile>
|
Chris@16
|
245 {
|
Chris@16
|
246 typedef tag::weighted_p_square_quantile type;
|
Chris@16
|
247 };
|
Chris@16
|
248
|
Chris@16
|
249 template<>
|
Chris@16
|
250 struct feature_of<tag::weighted_p_square_quantile>
|
Chris@16
|
251 : feature_of<tag::p_square_quantile>
|
Chris@16
|
252 {
|
Chris@16
|
253 };
|
Chris@16
|
254
|
Chris@16
|
255 }} // namespace boost::accumulators
|
Chris@16
|
256
|
Chris@16
|
257 #endif
|