# source:trunk/src/Statistics.cc@117

Last change on this file since 117 was 117, checked in by Peter, 18 years ago

modified to take vector<size_t>

• Property svn:eol-style set to `native`
• Property svn:keywords set to `Author Date Id Revision`
File size: 1.4 KB
Line
1// \$Id: Statistics.cc 117 2004-07-19 15:09:14Z peter \$
2
3// Thep C++ Tools
4#include "Statistics.h"
5
6// System includes
7#include <vector>
8#include <algorithm>
9
10#include <cstdlib>
11#include <iostream>
12
13
14namespace theplu {
15namespace cpptools {
16
17  Statistics::Statistics(void)
18
19  {
20  }
21
22  double Statistics::median(std::vector<double>& vec)
23  {
24    return percentile(vec, 50.0);
25  }
26
27  double Statistics::median(std::vector<size_t>& vec)
28  {
29    return percentile(vec, 50.0);
30  }
31
32  double Statistics::percentile(std::vector<double>& vec, double percentile)
33  {
34    sort(vec.begin(), vec.end());
35    double j = percentile/100 * vec.size();
36    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;
43    }
44    else{
45      int i = static_cast<int>(j);
46      return  vec[i];
47    }
48
49  }
50
51  double Statistics::percentile(std::vector<size_t>& vec, double percentile)
52  {
53    sort(vec.begin(), vec.end());
54    double j = percentile/100 * vec.size();
55    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    }
65    else{
66      int i = static_cast<int>(j);
67      return  static_cast<double>(vec[i]);
68    }
69
70  }
71
72
73}} // of namespace cpptools and namespace theplu
Note: See TracBrowser for help on using the repository browser.