Ignore:
Timestamp:
Dec 16, 2005, 11:15:25 PM (17 years ago)
Author:
Peter
Message:

made median template

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/statistics/utility.cc

    r298 r464  
    44#include <c++_tools/statistics/utility.h>
    55
    6 #include <vector>
    7 #include <algorithm>
    8 
    9 #include <cmath>
    10 #include <cstdlib>
    116#include <gsl/gsl_randist.h>
    12 #include <iostream>
    137
    148
     
    2418  }
    2519
    26 
    27   double median(std::vector<double>& vec)
    28   {
    29     return percentile(vec, 50.0);
    30   }
    31 
    32   double median(std::vector<size_t>& vec)
    33   {
    34     return percentile(vec, 50.0);
    35   }
    36 
    37   double percentile(std::vector<double>& vec, double percentile)
    38   {
    39     if (percentile==100)
    40       return  vec[vec.size()-1];
    41     else{
    42       sort(vec.begin(), vec.end());
    43       double j = percentile/100 * (vec.size()-1);
    44       int i = static_cast<int>(j);
    45       return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1];
    46     }
    47   }
    48 
    49   double percentile(std::vector<size_t>& vec, double percentile)
    50   {
    51     if (percentile==100)
    52       return vec[vec.size()-1];
    53     else{
    54       sort(vec.begin(), vec.end());
    55       double j = percentile/100 * (vec.size()-1);
    56       int i = static_cast<int>(j);
    57       return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1];
    58     }
    59    
    60   }
    61 
    62 
    6320}} // of namespace statistics and namespace theplu
Note: See TracChangeset for help on using the changeset viewer.