# source:trunk/lib/statistics/utility.cc@295

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

file structure modifications. NOTE, this revision is not working, please wait for the next...

• Property svn:eol-style set to `native`
• Property svn:keywords set to `Author Date Id Revision`
File size: 1.2 KB
Line
1// \$Id: utility.cc 295 2005-04-29 09:15:58Z peter \$
2
3// Thep C++ Tools
4#include "Statistics.h"
5
6// System includes
7#include <vector>
8#include <algorithm>
9
10#include <cmath>
11#include <cstdlib>
12#include <gsl/gsl_randist.h>
13#include <iostream>
14
15
16namespace theplu {
17namespace statistics {
18
19  double cdf_hypergeometric_P(u_int k, u_int n1, u_int n2, u_int t)
20  {
21    double p=0;
22    for (u_int i=0; i<=k; i++)
23      p+= gsl_ran_hypergeometric_pdf(i, n1, n2, t);
24    return p;
25  }
26
27
28  double median(std::vector<double>& vec)
29  {
30    return percentile(vec, 50.0);
31  }
32
33  double median(std::vector<size_t>& vec)
34  {
35    return percentile(vec, 50.0);
36  }
37
38  double percentile(std::vector<double>& vec, double percentile)
39  {
40    if (percentile==100)
41      return  vec[vec.size()-1];
42    else{
43      sort(vec.begin(), vec.end());
44      double j = percentile/100 * (vec.size()-1);
45      int i = static_cast<int>(j);
46      return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1];
47    }
48  }
49
50  double percentile(std::vector<size_t>& vec, double percentile)
51  {
52    if (percentile==100)
53      return vec[vec.size()-1];
54    else{
55      sort(vec.begin(), vec.end());
56      double j = percentile/100 * (vec.size()-1);
57      int i = static_cast<int>(j);
58      return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1];
59    }
60
61  }
62
63
64}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.