source: trunk/yat/statistics/utility.cc @ 865

Last change on this file since 865 was 865, checked in by Peter, 14 years ago

changing URL to http://trac.thep.lu.se/trac/yat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.0 KB
Line 
1// $Id: utility.cc 865 2007-09-10 19:41:04Z peter $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
7  Copyright (C) 2007 Jari Häkkinen
8
9  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "utility.h"
28
29#include <gsl/gsl_randist.h>
30#include <gsl/gsl_statistics_double.h>
31
32namespace theplu {
33namespace yat {
34namespace statistics { 
35
36  double cdf_hypergeometric_P(u_int k, u_int n1, u_int n2, u_int t)
37  {
38    double p=0;
39    for (u_int i=0; i<=k; i++)
40      p+= gsl_ran_hypergeometric_pdf(i, n1, n2, t);
41    return p;
42  }
43
44  double kurtosis(const utility::vector& v)
45  {
46    const gsl_vector* gvp=v.gsl_vector_p();
47    return gsl_stats_kurtosis(gvp->data,gvp->stride,gvp->size);
48  }
49
50  double mad(const utility::vector& vec, const bool sorted)
51  {
52    double m = median(vec, sorted);
53    std::vector<double> ad;
54    ad.reserve(vec.size());
55    for (size_t i = 0; i<vec.size(); ++i)
56      ad.push_back(fabs(vec[i]-m));
57    std::sort(ad.begin(), ad.end());
58    return median(ad,true);
59  }
60 
61
62  double median(const utility::vector& vec, const bool sorted)
63  {
64    if (!sorted){
65      utility::vector vec_copy(vec);
66      utility::sort(vec_copy);
67      return gsl_stats_median_from_sorted_data (vec_copy.gsl_vector_p()->data, 
68                                                vec_copy.gsl_vector_p()->stride,
69                                                vec_copy.gsl_vector_p()->size);
70    }
71    return gsl_stats_median_from_sorted_data (vec.gsl_vector_p()->data, 
72                                              vec.gsl_vector_p()->stride,
73                                              vec.gsl_vector_p()->size);
74  }
75
76  double percentile(const utility::vector& vec, const double p, 
77                    const bool sorted)
78  {
79    if (!sorted){
80      utility::vector vec_c(vec);
81      utility::sort(vec_c);
82      return gsl_stats_quantile_from_sorted_data(vec_c.gsl_vector_p()->data,
83                                                 vec_c.gsl_vector_p()->stride,
84                                                 vec_c.gsl_vector_p()->size,
85                                                 p);
86    }
87    return gsl_stats_quantile_from_sorted_data (vec.gsl_vector_p()->data, 
88                                              vec.gsl_vector_p()->stride,
89                                              vec.gsl_vector_p()->size,
90                                              p);
91  }
92
93  double skewness(const utility::vector& v)
94  {
95    const gsl_vector* gvp=v.gsl_vector_p();
96    return gsl_stats_skew(gvp->data,gvp->stride,gvp->size);
97  }
98
99}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.