Ignore:
Timestamp:
May 21, 2008, 2:23:18 PM (13 years ago)
Author:
Peter
Message:

moving implementation of percentile to a functor: Percentiler

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/statistics/utility.h

    r1305 r1317  
    2929*/
    3030
    31 #include "averager_traits.h"
     31#include "Percentiler.h"
     32
    3233#include "yat/classifier/DataLookupWeighted1D.h"
    3334#include "yat/classifier/Target.h"
     
    4748
    4849  /**
    49      \brief Functor to take average of a range.
    50    */
    51   struct Average
    52   {
    53     /**
    54        If range is weighted an AveragerWeighted is used else a Averager.
    55        
    56        \return average of range
    57      */
    58     template<typename ForwardIterator>
    59     inline double operator()(ForwardIterator first, ForwardIterator last) const
    60     {
    61       typename averager<ForwardIterator>::type a;
    62       add(a, first, last);
    63       return a.mean();
    64     }
    65 
    66   };
    67 
    68 
    69 
    70   //forward declarations
     50     \brief 50th percentile
     51     @see Percentiler
     52  */
    7153  template <class T>
    7254  double median(T first, T last, const bool sorted=false);
    7355
     56  /**
     57     \see Percentiler
     58  */
    7459  template <class T>
    7560  double percentile(T first, T last, double p, bool sorted=false);
     
    10186  /// @return cumulative hypergeomtric distribution functions P(k).
    10287  ///
    103   /// \deprecated
     88  /// \deprecated use gsl_cdf_hypergeometric_P
    10489  ///
    10590  double cdf_hypergeometric_P(unsigned int k, unsigned int n1,
     
    192177  double percentile(T first, T last, double p, bool sorted=false)
    193178  {
    194     utility::yat_assert<std::range_error>(first<last,
    195                                           "percentile: invalid range");
    196     utility::yat_assert<std::runtime_error>(p>=0, "percentage is negative");
    197     utility::yat_assert<std::runtime_error>(p<=100,
    198                                             "percentage is larger than 100");
    199     if (sorted){
    200       if (p>=100)
    201         return *(--last);
    202       // range is one value only is a special case
    203       if (std::distance(first, last)==1)
    204         return *first;
    205       double j = p/100 * (std::distance(first,last)-1);
    206       int i = static_cast<int>(j);
    207       return (1-j+floor(j))*first[i] + (j-floor(j))*first[i+1];
    208     }
    209 
    210     std::vector<double> v_copy;
    211     v_copy.reserve(std::distance(first,last));
    212     std::copy(first, last, std::back_inserter(v_copy));
    213     double j = p/100 * (v_copy.size()-1);
    214     size_t i = static_cast<size_t>(j);
    215     if (i+2 < v_copy.size()) {
    216       utility::yat_assert<std::out_of_range>(i+2 < v_copy.size(),
    217                                              "in utility::percentile");
    218       std::partial_sort(v_copy.begin(), v_copy.begin()+i+2, v_copy.end());
    219     }
    220     else
    221       std::sort(v_copy.begin(), v_copy.end());
    222      
    223     return percentile(v_copy.begin(), v_copy.end(), p, true);
     179    Percentiler percentiler(p, sorted);
     180    return percentiler(first, last);
    224181  }
    225182
Note: See TracChangeset for help on using the changeset viewer.