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 {
101 template<
typename RandomAccessIterator>
103 RandomAccessIterator last)
const 106 BOOST_CONCEPT_ASSERT((boost_concepts::RandomAccessTraversal<RandomAccessIterator>));
109 return std::numeric_limits<double>::quiet_NaN();
112 typedef typename weighted_iterator_traits<RandomAccessIterator>::type tag;
113 return calculate(first, last, sorted_, tag());
120 template<
typename RandomAccessIterator>
121 double calculate(RandomAccessIterator first, RandomAccessIterator last,
125 template<
typename RandomAccessIterator>
126 double calculate(RandomAccessIterator first, RandomAccessIterator last,
139 template<
typename RandomAccessIterator>
141 Percentiler::calculate(RandomAccessIterator first,
142 RandomAccessIterator last,
146 BOOST_CONCEPT_ASSERT((boost_concepts::LvalueIterator<RandomAccessIterator>));
147 size_t n = last - first;
152 double j = n * perc_ / 100.0;
158 return *std::max_element(first, last);
163 return *std::min_element(first, last);
166 size_t i =
static_cast<size_t>(j);
168 size_t k = (i==j) ? i-1 : i;
173 return (first[i]+first[k])/2;
176 std::vector<double> vec(first, last);
178 std::nth_element(vec.begin(), vec.begin()+i, vec.end());
190 return (vec[i] + *std::max_element(vec.begin(), vec.begin()+i))/2;
195 template<
typename RandomAccessIterator>
196 double Percentiler::calculate(RandomAccessIterator first,
197 RandomAccessIterator last,
203 std::vector<double> accum_w;
204 accum_w.reserve(last-first);
205 std::partial_sum(weight_iterator(first),
206 weight_iterator(last),
207 std::back_inserter(accum_w));
209 double w_bound=perc_/100.0*accum_w.back();
210 std::vector<double>::const_iterator upper(accum_w.begin());
212 while (upper!=accum_w.end() && *upper <= w_bound+margin)
214 while (upper!=accum_w.begin() &&
215 (upper==accum_w.end() ||
216 trait.
weight(first+(upper-accum_w.begin()))==0.0))
218 std::vector<double>::const_iterator lower(upper);
219 while ( *(lower-1)>=w_bound-margin && lower>accum_w.begin())
222 return (trait.
data(first+(upper-accum_w.begin()))+
223 trait.
data(first+(lower-accum_w.begin())))/2;
226 std::vector<utility::DataWeight> v_copy(first, last);
227 std::sort(v_copy.begin(), v_copy.end());
228 return calculate(v_copy.begin(), v_copy.end(),
true, tag);
data_reference data(Iter iter) const
Definition: iterator_traits.h:440
Concept check for Data Iterator.
Definition: concept_check.h:240
Definition: iterator_traits.h:412
The Department of Theoretical Physics namespace as we define it.
Definition: iterator_traits.h:47
Definition: iterator_traits.h:55
Definition: iterator_traits.h:105
weight_reference weight(Iter iter) const
Definition: iterator_traits.h:446
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:102