1 #ifndef _theplu_yat_statistics_percentiler_
2 #define _theplu_yat_statistics_percentiler_
26 #include "yat/utility/concept_check.h"
27 #include "yat/utility/DataWeight.h"
28 #include "yat/utility/iterator_traits.h"
29 #include "yat/utility/yat_assert.h"
30 #include "yat/utility/WeightIterator.h"
32 #include <boost/concept_check.hpp>
33 #include <boost/iterator/iterator_concepts.hpp>
44 namespace statistics {
99 template<
typename RandomAccessIterator>
101 RandomAccessIterator last)
const
104 BOOST_CONCEPT_ASSERT((boost_concepts::RandomAccessTraversal<RandomAccessIterator>));
106 return std::numeric_limits<double>::quiet_NaN();
109 typedef typename weighted_iterator_traits<RandomAccessIterator>::type tag;
110 return calculate(first, last, sorted_, tag());
117 template<
typename RandomAccessIterator>
118 double calculate(RandomAccessIterator first, RandomAccessIterator last,
122 template<
typename RandomAccessIterator>
123 double calculate(RandomAccessIterator first, RandomAccessIterator last,
136 template<
typename RandomAccessIterator>
138 Percentiler::calculate(RandomAccessIterator first,
139 RandomAccessIterator last,
143 size_t n = last - first;
148 double j = n * perc_ / 100.0;
154 return *std::max_element(first, last);
159 return *std::min_element(first, last);
162 size_t i =
static_cast<size_t>(j);
164 size_t k = (i==j) ? i-1 : i;
169 return (first[i]+first[k])/2;
172 std::vector<double> vec(first, last);
174 std::nth_element(vec.begin(), vec.begin()+i, vec.end());
186 return (vec[i] + *std::max_element(vec.begin(), vec.begin()+i))/2;
191 template<
typename RandomAccessIterator>
192 double Percentiler::calculate(RandomAccessIterator first,
193 RandomAccessIterator last,
195 utility::weighted_iterator_tag tag)
const
198 utility::iterator_traits<RandomAccessIterator> trait;
199 std::vector<double> accum_w;
200 accum_w.reserve(last-first);
201 std::partial_sum(weight_iterator(first),
202 weight_iterator(last),
203 std::back_inserter(accum_w));
205 double w_bound=perc_/100.0*accum_w.back();
206 std::vector<double>::const_iterator upper(accum_w.begin());
208 while (upper!=accum_w.end() && *upper <= w_bound+margin)
210 while (upper!=accum_w.begin() &&
211 (upper==accum_w.end() ||
212 trait.weight(first+(upper-accum_w.begin()))==0.0))
214 std::vector<double>::const_iterator lower(upper);
215 while ( *(lower-1)>=w_bound-margin && lower>accum_w.begin())
218 return (trait.data(first+(upper-accum_w.begin()))+
219 trait.data(first+(lower-accum_w.begin())))/2;
222 std::vector<utility::DataWeight> v_copy(first, last);
223 std::sort(v_copy.begin(), v_copy.end());
224 return calculate(v_copy.begin(), v_copy.end(),
true, tag);
Concept check for Data Iterator.
Definition: concept_check.h:228
Definition: iterator_traits.h:47
Definition: iterator_traits.h:55
Definition: iterator_traits.h:105
Percentiler(double perc=50, bool sorted=false)
Functor to calculate percentile of a range.
Definition: Percentiler.h:51
double operator()(RandomAccessIterator first, RandomAccessIterator last) const
Definition: Percentiler.h:100