# Changeset 168

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

percentile modified (is now defined as in GSL)

Location:
trunk/src
Files:
2 edited

Unmodified
Added
Removed
• ## trunk/src/Statistics.cc

 r117 #include #include #include #include double Statistics::percentile(std::vector& vec, double percentile) { sort(vec.begin(), vec.end()); double j = percentile/100 * vec.size(); if (percentile==100) return vec[vec.size()-1]; double k = j + 0.5 - static_cast (j+0.5); if (k<.500000000001 && k>.499999999999){ int i = static_cast(j+0.5); return (vec[i]+vec[i-1])/2; return  vec[vec.size()-1]; else{ sort(vec.begin(), vec.end()); double j = percentile/100 * (vec.size()-1); int i = static_cast(j); return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1]; } else{ int i = static_cast(j); return  vec[i]; } } double Statistics::percentile(std::vector& vec, double percentile) { sort(vec.begin(), vec.end()); double j = percentile/100 * vec.size(); if (percentile==100) return vec[vec.size()-1]; double k = j + 0.5 - static_cast (j+0.5); if (k<.500000000001 && k>.499999999999){ int i = static_cast(j+0.5); double r = static_cast(vec[i]+vec[i-1])/2; return r; } return vec[vec.size()-1]; else{ sort(vec.begin(), vec.end()); double j = percentile/100 * (vec.size()-1); int i = static_cast(j); return  static_cast(vec[i]); return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1]; }
• ## trunk/src/Statistics.h

 r117 /// /// @return median /// Median is defined to be value in the middle. If number of /// values is even median is the average of the two middle /// values. @return median /// double median(std::vector&); /// /// @return median /// Median is defined to be value in the middle. If number of /// values is even median is the average of the two middle /// values. @return median /// double median(std::vector&); /// /// @return \a i'th percentile /// The percentile is determined by the \a p, a number between 0 /// and 100. The percentile is found by interpolation, using the /// formula \f$percentile = (1 - \delta) x_i + \delta x_{i+1} \f$ /// where \a p is floor\f$((n - 1)p/100)\f$ and \f$\delta \f$ is /// \f$(n-1)p/100 - i \f$.Thus the minimum value of the vector is /// given by p equal to zero, the maximum is given by p equal to /// 100 and the median value is given by p equal to 50. Since the /// algorithm for computing percentiles involves interpolation /// this function always returns a floating-point number, even for /// integer data types.  @return \a i'th percentile /// double percentile(std::vector&, double i); double percentile(std::vector&, double p); /// /// @return \a i'th percentile /// The percentile is determined by the \a p, a number between 0 /// and 100. The percentile is found by interpolation, using the /// formula \f$percentile = (1 - \delta) x_i + \delta x_{i+1} \f$ /// where \a p is floor\f$((n - 1)p/100)\f$ and \f$\delta \f$ is /// \f$(n-1)p/100 - i \f$.Thus the minimum value of the vector is /// given by p equal to zero, the maximum is given by p equal to /// 100 and the median value is given by p equal to 50. Since the /// algorithm for computing percentiles involves interpolation /// this function always returns a floating-point number, even for /// integer data types.  @return \a i'th percentile /// double percentile(std::vector&, double i);
Note: See TracChangeset for help on using the changeset viewer.