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

percentile modified (is now defined as in GSL)

File:
1 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   
Note: See TracChangeset for help on using the changeset viewer.