1 | #ifndef _theplu_yat_statistics_utility_ |
---|
2 | #define _theplu_yat_statistics_utility_ |
---|
3 | |
---|
4 | // $Id: utility.h 1486 2008-09-09 21:17:19Z jari $ |
---|
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://dev.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 3 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 | |
---|
45 | namespace theplu { |
---|
46 | namespace yat { |
---|
47 | namespace 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 | double cdf_hypergeometric_P(unsigned int k, unsigned int n1, |
---|
89 | unsigned int n2, unsigned int t); |
---|
90 | |
---|
91 | |
---|
92 | /** |
---|
93 | \brief one-sided p-value |
---|
94 | |
---|
95 | This function uses the t-distribution to calculate the one-sided |
---|
96 | p-value. Given that the true correlation is zero (Null |
---|
97 | hypothesis) the estimated correlation, r, after a transformation |
---|
98 | is t-distributed: |
---|
99 | |
---|
100 | \f$ \sqrt{(n-2)} \frac{r}{\sqrt{(1-r^2)}} \in t(n-2) \f$ |
---|
101 | |
---|
102 | \return Probability that correlation is larger than \a r by |
---|
103 | chance when having \a n samples. |
---|
104 | */ |
---|
105 | double pearson_p_value(double r, unsigned int n); |
---|
106 | |
---|
107 | /// |
---|
108 | /// @brief Computes the kurtosis of the data in a vector. |
---|
109 | /// |
---|
110 | /// The kurtosis measures how sharply peaked a distribution is, |
---|
111 | /// relative to its width. The kurtosis is normalized to zero for a |
---|
112 | /// gaussian distribution. |
---|
113 | /// |
---|
114 | double kurtosis(const utility::VectorBase&); |
---|
115 | |
---|
116 | |
---|
117 | /// |
---|
118 | /// @brief Median absolute deviation from median |
---|
119 | /// |
---|
120 | /// Function is non-mutable function |
---|
121 | /// |
---|
122 | template <class T> |
---|
123 | double mad(T first, T last, const bool sorted=false) |
---|
124 | { |
---|
125 | double m = median(first, last, sorted); |
---|
126 | std::vector<double> ad; |
---|
127 | ad.reserve(std::distance(first, last)); |
---|
128 | for( ; first!=last; ++first) |
---|
129 | ad.push_back(fabs(*first-m)); |
---|
130 | std::sort(ad.begin(), ad.end()); |
---|
131 | return median(ad.begin(), ad.end(), true); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /// |
---|
136 | /// Median is defined to be value in the middle. If number of values |
---|
137 | /// is even median is the average of the two middle values. the |
---|
138 | /// median value is given by p equal to 50. If \a sorted is false |
---|
139 | /// (default), the range is copied, the copy is sorted, and then |
---|
140 | /// used to calculate the median. |
---|
141 | /// |
---|
142 | /// Function is a non-mutable function, i.e., \a first and \a last |
---|
143 | /// can be const_iterators. |
---|
144 | /// |
---|
145 | /// Requirements: T should be an iterator over a range of doubles (or |
---|
146 | /// any type being convertable to double). |
---|
147 | /// |
---|
148 | /// @return median of range |
---|
149 | /// |
---|
150 | template <class T> |
---|
151 | double median(T first, T last, const bool sorted=false) |
---|
152 | { return percentile2(first, last, 50.0, sorted); } |
---|
153 | |
---|
154 | /** |
---|
155 | The percentile is determined by the \a p, a number between 0 and |
---|
156 | 100. The percentile is found by interpolation, using the formula |
---|
157 | \f$ percentile = (1 - \delta) x_i + \delta x_{i+1} \f$ where \a |
---|
158 | p is floor\f$((n - 1)p/100)\f$ and \f$ \delta \f$ is \f$ |
---|
159 | (n-1)p/100 - i \f$.Thus the minimum value of the vector is given |
---|
160 | by p equal to zero, the maximum is given by p equal to 100 and |
---|
161 | the median value is given by p equal to 50. If @a sorted |
---|
162 | is false (default), the vector is copied, the copy is sorted, |
---|
163 | and then used to calculate the median. |
---|
164 | |
---|
165 | Function is a non-mutable function, i.e., \a first and \a last |
---|
166 | can be const_iterators. |
---|
167 | |
---|
168 | Requirements: T should be an iterator over a range of doubles (or |
---|
169 | any type being convertable to double). |
---|
170 | |
---|
171 | @return \a p'th percentile of range |
---|
172 | |
---|
173 | \deprecated percentile2 will replace this function in the future |
---|
174 | |
---|
175 | \note the definition of percentile used here is not identical to |
---|
176 | that one used in percentile2 and Percentile. The difference is |
---|
177 | smaller for large ranges. |
---|
178 | */ |
---|
179 | template <class RandomAccessIterator> |
---|
180 | double percentile(RandomAccessIterator first, RandomAccessIterator last, |
---|
181 | double p, bool sorted=false) |
---|
182 | { |
---|
183 | // range is one value only is a special case |
---|
184 | if (first+1 == last) |
---|
185 | return utility::iterator_traits<RandomAccessIterator>().data(first); |
---|
186 | if (sorted) { |
---|
187 | // have to take care of this special case |
---|
188 | if (p>=100) |
---|
189 | return utility::iterator_traits<RandomAccessIterator>().data(--last); |
---|
190 | double j = p/100 * (std::distance(first,last)-1); |
---|
191 | int i = static_cast<int>(j); |
---|
192 | return (1-j+floor(j))*first[i] + (j-floor(j))*first[i+1]; |
---|
193 | } |
---|
194 | |
---|
195 | std::vector<typename std::iterator_traits<RandomAccessIterator>::value_type> |
---|
196 | v_copy; |
---|
197 | v_copy.reserve(std::distance(first,last)); |
---|
198 | std::copy(first, last, std::back_inserter(v_copy)); |
---|
199 | size_t i = static_cast<size_t>(p/100 * (v_copy.size()-1)); |
---|
200 | if (i+2 < v_copy.size()) { |
---|
201 | std::partial_sort(v_copy.begin(), v_copy.begin()+i+2, v_copy.end()); |
---|
202 | } |
---|
203 | else |
---|
204 | std::sort(v_copy.begin(), v_copy.end()); |
---|
205 | return percentile(v_copy.begin(), v_copy.end(), p, true); |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | /** |
---|
210 | \see Percentiler |
---|
211 | |
---|
212 | \since new in yat 0.5 |
---|
213 | */ |
---|
214 | template <class RandomAccessIterator> |
---|
215 | double percentile2(RandomAccessIterator first, RandomAccessIterator last, |
---|
216 | double p, bool sorted=false) |
---|
217 | { |
---|
218 | Percentiler percentiler(p, sorted); |
---|
219 | return percentiler(first, last); |
---|
220 | } |
---|
221 | |
---|
222 | /// |
---|
223 | /// @brief Computes the skewness of the data in a vector. |
---|
224 | /// |
---|
225 | /// The skewness measures the asymmetry of the tails of a |
---|
226 | /// distribution. |
---|
227 | /// |
---|
228 | double skewness(const utility::VectorBase&); |
---|
229 | |
---|
230 | }}} // of namespace statistics, yat, and theplu |
---|
231 | |
---|
232 | #endif |
---|