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

Last change on this file since 511 was 511, checked in by Peter, 17 years ago

added percentile function for gslapi::vector

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.4 KB
RevLine 
[115]1// $Id: utility.cc 511 2006-02-18 14:46:46Z peter $
2
3
[298]4#include <c++_tools/statistics/utility.h>
5
[169]6#include <gsl/gsl_randist.h>
[500]7#include <gsl/gsl_statistics_double.h>
[115]8
9namespace theplu {
[196]10namespace statistics { 
[115]11
[169]12  double cdf_hypergeometric_P(u_int k, u_int n1, u_int n2, u_int t)
13  {
14    double p=0;
15    for (u_int i=0; i<=k; i++)
16      p+= gsl_ran_hypergeometric_pdf(i, n1, n2, t);
17    return p;
18  }
19
[500]20  double median(const gslapi::vector& vec, const bool sorted)
21  {
22    if (!sorted){
23      gslapi::vector vec_copy(vec);
24      vec_copy.sort();
25      return gsl_stats_median_from_sorted_data (vec_copy.gsl_vector_p()->data, 
26                                                vec_copy.gsl_vector_p()->stride,
27                                                vec_copy.gsl_vector_p()->size);
28    }
29    return gsl_stats_median_from_sorted_data (vec.gsl_vector_p()->data, 
30                                              vec.gsl_vector_p()->stride,
31                                              vec.gsl_vector_p()->size);
32  }
33
[511]34  double percentile(const gslapi::vector& vec, const double p, 
35                    const bool sorted)
36  {
37    if (!sorted){
38      gslapi::vector vec_c(vec);
39      vec_c.sort();
40      return gsl_stats_quantile_from_sorted_data(vec_c.gsl_vector_p()->data,
41                                                 vec_c.gsl_vector_p()->stride,
42                                                 vec_c.gsl_vector_p()->size,
43                                                 p);
44    }
45    return gsl_stats_quantile_from_sorted_data (vec.gsl_vector_p()->data, 
46                                              vec.gsl_vector_p()->stride,
47                                              vec.gsl_vector_p()->size,
48                                              p);
49  }
50
[196]51}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.