source: trunk/yat/statistics/utility.h @ 1317

Last change on this file since 1317 was 1317, checked in by Peter, 13 years ago

moving implementation of percentile to a functor: Percentiler

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1#ifndef _theplu_yat_statistics_utility_
2#define _theplu_yat_statistics_utility_
3
4// $Id: utility.h 1317 2008-05-21 12:23:18Z peter $
5
6/*
7  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2005 Peter Johansson
9  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
10  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
11  Copyright (C) 2008 Peter Johansson
12
13  This file is part of the yat library, http://trac.thep.lu.se/yat
14
15  The yat library is free software; you can redistribute it and/or
16  modify it under the terms of the GNU General Public License as
17  published by the Free Software Foundation; either version 2 of the
18  License, or (at your option) any later version.
19
20  The yat library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  General Public License for more details.
24
25  You should have received a copy of the GNU General Public License
26  along with this program; if not, write to the Free Software
27  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28  02111-1307, USA.
29*/
30
31#include "Percentiler.h"
32
33#include "yat/classifier/DataLookupWeighted1D.h"
34#include "yat/classifier/Target.h"
35#include "yat/utility/VectorBase.h"
36#include "yat/utility/yat_assert.h"
37
38#include <algorithm>
39#include <cmath>
40#include <stdexcept>
41#include <vector>
42
43#include <gsl/gsl_statistics_double.h>
44
45namespace theplu {
46namespace yat {
47namespace statistics { 
48
49  /**
50     \brief 50th percentile
51     @see Percentiler
52  */
53  template <class T> 
54  double median(T first, T last, const bool sorted=false); 
55
56  /**
57     \see Percentiler
58  */
59  template <class T>
60  double percentile(T first, T last, double p, bool sorted=false);
61 
62  /**
63     Adding a range [\a first, \a last) into an object of type T. The
64     requirements for the type T is to have an add(double, bool, double)
65     function.
66  */
67  template <typename T, typename ForwardIterator>
68  void add(T& o, ForwardIterator first, ForwardIterator last,
69           const classifier::Target& target)
70  {
71    for (size_t i=0; first!=last; ++i, ++first)
72      o.add(utility::iterator_traits<ForwardIterator>().data(first),
73            target.binary(i), 
74            utility::iterator_traits<ForwardIterator>().weight(first));
75  } 
76
77  ///
78  /// Calculates the probability to get \a k or smaller from a
79  /// hypergeometric distribution with parameters \a n1 \a n2 \a
80  /// t. Hypergeomtric situation you get in the following situation:
81  /// Let there be \a n1 ways for a "good" selection and \a n2 ways
82  /// for a "bad" selection out of a total of possibilities. Take \a
83  /// t samples without replacement and \a k of those are "good"
84  /// samples. \a k will follow a hypergeomtric distribution.
85  ///
86  /// @return cumulative hypergeomtric distribution functions P(k).
87  ///
88  /// \deprecated use gsl_cdf_hypergeometric_P
89  ///
90  double cdf_hypergeometric_P(unsigned int k, unsigned int n1, 
91                              unsigned int n2, unsigned int t);
92
93
94  /**
95     \brief one-sided p-value
96
97     This function uses the t-distribution to calculate the one-sided
98     p-value. Given that the true correlation is zero (Null
99     hypothesis) the estimated correlation, r, after a transformation
100     is t-distributed:
101
102     \f$ \sqrt{(n-2)} \frac{r}{\sqrt{(1-r^2)}} \in t(n-2) \f$
103
104     \return Probability that correlation is larger than \a r by
105     chance when having \a n samples.
106   */
107  double pearson_p_value(double r, unsigned int n);
108
109  ///
110  /// @brief Computes the kurtosis of the data in a vector.
111  ///
112  /// The kurtosis measures how sharply peaked a distribution is,
113  /// relative to its width. The kurtosis is normalized to zero for a
114  /// gaussian distribution.
115  ///
116  double kurtosis(const utility::VectorBase&);
117
118
119  ///
120  /// @brief Median absolute deviation from median
121  ///
122  /// Function is non-mutable function
123  ///
124  template <class T>
125  double mad(T first, T last, const bool sorted=false)
126  {
127    double m = median(first, last, sorted);
128    std::vector<double> ad;
129    ad.reserve(std::distance(first, last));
130    for( ; first!=last; ++first)
131      ad.push_back(fabs(*first-m));
132    std::sort(ad.begin(), ad.end());
133    return median(ad.begin(), ad.end(), true);
134  }
135 
136
137  ///
138  /// Median is defined to be value in the middle. If number of values
139  /// is even median is the average of the two middle values.  the
140  /// median value is given by p equal to 50. If \a sorted is false
141  /// (default), the range is copied, the copy is sorted, and then
142  /// used to calculate the median.
143  ///
144  /// Function is a non-mutable function, i.e., \a first and \a last
145  /// can be const_iterators.
146  ///
147  /// Requirements: T should be an iterator over a range of doubles (or
148  /// any type being convertable to double).
149  ///
150  /// @return median of range
151  ///
152  template <class T> 
153  double median(T first, T last, const bool sorted=false) 
154  { return percentile(first, last, 50.0, sorted); }
155
156  /**
157     The percentile is determined by the \a p, a number between 0 and
158     100. The percentile is found by interpolation, using the formula
159     \f$ percentile = (1 - \delta) x_i + \delta x_{i+1} \f$ where \a
160     p is floor\f$((n - 1)p/100)\f$ and \f$ \delta \f$ is \f$
161     (n-1)p/100 - i \f$.Thus the minimum value of the vector is given
162     by p equal to zero, the maximum is given by p equal to 100 and
163     the median value is given by p equal to 50. If @a sorted
164     is false (default), the vector is copied, the copy is sorted,
165     and then used to calculate the median.
166
167     Function is a non-mutable function, i.e., \a first and \a last
168     can be const_iterators.
169     
170     Requirements: T should be an iterator over a range of doubles (or
171     any type being convertable to double). If \a sorted is false
172     iterator must be mutable, else read-only iterator is also ok.
173     
174     @return \a p'th percentile of range
175  */
176  template <class T>
177  double percentile(T first, T last, double p, bool sorted=false)
178  {
179    Percentiler percentiler(p, sorted);
180    return percentiler(first, last);
181  }
182
183  ///
184  /// @brief Computes the skewness of the data in a vector.
185  ///
186  /// The skewness measures the asymmetry of the tails of a
187  /// distribution.
188  ///
189  double skewness(const utility::VectorBase&);
190 
191}}} // of namespace statistics, yat, and theplu
192
193#endif
Note: See TracBrowser for help on using the repository browser.