Changeset 168


Ignore:
Timestamp:
Sep 23, 2004, 3:27:03 PM (19 years ago)
Author:
Peter
Message:

percentile modified (is now defined as in GSL)

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Statistics.cc

    r117 r168  
    88#include <algorithm>
    99
     10#include <cmath>
    1011#include <cstdlib>
    1112#include <iostream>
     
    3233  double Statistics::percentile(std::vector<double>& vec, double percentile)
    3334  {
    34     sort(vec.begin(), vec.end());
    35     double j = percentile/100 * vec.size();
    3635    if (percentile==100)
    37       return vec[vec.size()-1];   
    38    
    39     double k = j + 0.5 - static_cast<int> (j+0.5);
    40     if (k<.500000000001 && k>.499999999999){
    41       int i = static_cast<int>(j+0.5);
    42       return (vec[i]+vec[i-1])/2;
     36      return  vec[vec.size()-1];
     37    else{
     38      sort(vec.begin(), vec.end());
     39      double j = percentile/100 * (vec.size()-1);
     40      int i = static_cast<int>(j);
     41      return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1];
    4342    }
    44     else{
    45       int i = static_cast<int>(j);
    46       return  vec[i];
    47     }
    48    
    4943  }
    5044
    5145  double Statistics::percentile(std::vector<size_t>& vec, double percentile)
    5246  {
    53     sort(vec.begin(), vec.end());
    54     double j = percentile/100 * vec.size();
    5547    if (percentile==100)
    56       return vec[vec.size()-1];   
    57    
    58     double k = j + 0.5 - static_cast<int> (j+0.5);
    59     if (k<.500000000001 && k>.499999999999){
    60       int i = static_cast<int>(j+0.5);
    61       double r = static_cast<double>(vec[i]+vec[i-1])/2;
    62       return r;
    63      
    64     }
     48      return vec[vec.size()-1];
    6549    else{
     50      sort(vec.begin(), vec.end());
     51      double j = percentile/100 * (vec.size()-1);
    6652      int i = static_cast<int>(j);
    67       return  static_cast<double>(vec[i]);
     53      return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1];
    6854    }
    6955   
  • trunk/src/Statistics.h

    r117 r168  
    2727
    2828    ///
    29     /// @return median
     29    /// Median is defined to be value in the middle. If number of
     30    /// values is even median is the average of the two middle
     31    /// values. @return median
    3032    ///
    3133    double median(std::vector<double>&); 
    3234
    3335    ///
    34     /// @return median
     36    /// Median is defined to be value in the middle. If number of
     37    /// values is even median is the average of the two middle
     38    /// values. @return median
    3539    ///
    3640    double median(std::vector<size_t>&); 
    3741
    3842    ///
    39     /// @return \a i'th percentile
     43    /// The percentile is determined by the \a p, a number between 0
     44    /// and 100. The percentile is found by interpolation, using the
     45    /// formula \f$ percentile = (1 - \delta) x_i + \delta x_{i+1} \f$
     46    /// where \a p is floor\f$((n - 1)p/100)\f$ and \f$ \delta \f$ is
     47    /// \f$ (n-1)p/100 - i \f$.Thus the minimum value of the vector is
     48    /// given by p equal to zero, the maximum is given by p equal to
     49    /// 100 and the median value is given by p equal to 50. Since the
     50    /// algorithm for computing percentiles involves interpolation
     51    /// this function always returns a floating-point number, even for
     52    /// integer data types.  @return \a i'th percentile
    4053    ///
    41     double percentile(std::vector<double>&, double i);
     54    double percentile(std::vector<double>&, double p);
    4255
    4356    ///
    44     /// @return \a i'th percentile
     57    /// The percentile is determined by the \a p, a number between 0
     58    /// and 100. The percentile is found by interpolation, using the
     59    /// formula \f$ percentile = (1 - \delta) x_i + \delta x_{i+1} \f$
     60    /// where \a p is floor\f$((n - 1)p/100)\f$ and \f$ \delta \f$ is
     61    /// \f$ (n-1)p/100 - i \f$.Thus the minimum value of the vector is
     62    /// given by p equal to zero, the maximum is given by p equal to
     63    /// 100 and the median value is given by p equal to 50. Since the
     64    /// algorithm for computing percentiles involves interpolation
     65    /// this function always returns a floating-point number, even for
     66    /// integer data types.  @return \a i'th percentile
    4567    ///
    4668    double percentile(std::vector<size_t>&, double i);
Note: See TracChangeset for help on using the changeset viewer.