Changeset 168
- Timestamp:
- Sep 23, 2004, 3:27:03 PM (19 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/Statistics.cc
r117 r168 8 8 #include <algorithm> 9 9 10 #include <cmath> 10 11 #include <cstdlib> 11 12 #include <iostream> … … 32 33 double Statistics::percentile(std::vector<double>& vec, double percentile) 33 34 { 34 sort(vec.begin(), vec.end());35 double j = percentile/100 * vec.size();36 35 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]; 43 42 } 44 else{45 int i = static_cast<int>(j);46 return vec[i];47 }48 49 43 } 50 44 51 45 double Statistics::percentile(std::vector<size_t>& vec, double percentile) 52 46 { 53 sort(vec.begin(), vec.end());54 double j = percentile/100 * vec.size();55 47 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]; 65 49 else{ 50 sort(vec.begin(), vec.end()); 51 double j = percentile/100 * (vec.size()-1); 66 52 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]; 68 54 } 69 55 -
trunk/src/Statistics.h
r117 r168 27 27 28 28 /// 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 30 32 /// 31 33 double median(std::vector<double>&); 32 34 33 35 /// 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 35 39 /// 36 40 double median(std::vector<size_t>&); 37 41 38 42 /// 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 40 53 /// 41 double percentile(std::vector<double>&, double i);54 double percentile(std::vector<double>&, double p); 42 55 43 56 /// 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 45 67 /// 46 68 double percentile(std::vector<size_t>&, double i);
Note: See TracChangeset
for help on using the changeset viewer.