source: trunk/lib/statistics/utility.cc @ 298

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

fixed include problems

  • 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 298 2005-04-29 12:16:21Z peter $
2
3
4#include <c++_tools/statistics/utility.h>
5
6#include <vector>
7#include <algorithm>
8
9#include <cmath>
10#include <cstdlib>
11#include <gsl/gsl_randist.h>
12#include <iostream>
13
14
15namespace theplu {
16namespace statistics { 
17
18  double cdf_hypergeometric_P(u_int k, u_int n1, u_int n2, u_int t)
19  {
20    double p=0;
21    for (u_int i=0; i<=k; i++)
22      p+= gsl_ran_hypergeometric_pdf(i, n1, n2, t);
23    return p;
24  }
25
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
63}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.