1 | #ifndef _theplu_yat_statistics_utility_ |
---|
2 | #define _theplu_yat_statistics_utility_ |
---|
3 | |
---|
4 | // $Id: utility.h 839 2007-04-25 07:26:27Z 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, Markus Ringnér, Peter Johansson |
---|
10 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
11 | |
---|
12 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
13 | |
---|
14 | The yat library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU General Public License as |
---|
16 | published by the Free Software Foundation; either version 2 of the |
---|
17 | License, or (at your option) any later version. |
---|
18 | |
---|
19 | The yat library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with this program; if not, write to the Free Software |
---|
26 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
27 | 02111-1307, USA. |
---|
28 | */ |
---|
29 | |
---|
30 | #include "yat/classifier/DataLookupWeighted1D.h" |
---|
31 | #include "yat/classifier/Target.h" |
---|
32 | #include "yat/utility/vector.h" |
---|
33 | |
---|
34 | #include <algorithm> |
---|
35 | #include <cmath> |
---|
36 | #include <vector> |
---|
37 | |
---|
38 | #include <gsl/gsl_statistics_double.h> |
---|
39 | |
---|
40 | namespace theplu { |
---|
41 | namespace yat { |
---|
42 | namespace statistics { |
---|
43 | |
---|
44 | //forward declarations |
---|
45 | template <class T> |
---|
46 | double median(const std::vector<T>& v, const bool sorted=false); |
---|
47 | |
---|
48 | template <class T> |
---|
49 | double percentile(const std::vector<T>& vec, const double p, |
---|
50 | const bool sorted=false); |
---|
51 | |
---|
52 | /** |
---|
53 | Adding each value in an array \a v \a to an object \a o. The |
---|
54 | requirements for the type T1 is to have an add(double, bool) |
---|
55 | function, and for T2 of the array \a v are: operator[] returning |
---|
56 | an element and function size() returning the number of elements. |
---|
57 | */ |
---|
58 | template <typename T1, typename T2> |
---|
59 | void add(T1& o, const T2& v, const classifier::Target& target) |
---|
60 | { |
---|
61 | for (size_t i=0; i<v.size(); ++i) |
---|
62 | o.add(v[i],target.binary(i)); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | Adding each value in an array \a v \a to an object \a o. The |
---|
67 | requirements for the type T1 is to have an add(double, bool) |
---|
68 | function, and for T2 of the array \a v are: operator[] returning |
---|
69 | an element and function size() returning the number of elements. |
---|
70 | */ |
---|
71 | template <typename T1> |
---|
72 | void add(T1& o, const classifier::DataLookupWeighted1D& v, |
---|
73 | const classifier::Target& target) |
---|
74 | { |
---|
75 | for (size_t i=0; i<v.size(); ++i) |
---|
76 | o.add(v.data(i),target.binary(i),v.weight(i)); |
---|
77 | } |
---|
78 | |
---|
79 | /// |
---|
80 | /// Calculates the probability to get \a k or smaller from a |
---|
81 | /// hypergeometric distribution with parameters \a n1 \a n2 \a |
---|
82 | /// t. Hypergeomtric situation you get in the following situation: |
---|
83 | /// Let there be \a n1 ways for a "good" selection and \a n2 ways |
---|
84 | /// for a "bad" selection out of a total of possibilities. Take \a |
---|
85 | /// t samples without replacement and \a k of those are "good" |
---|
86 | /// samples. \a k will follow a hypergeomtric distribution. |
---|
87 | /// |
---|
88 | /// @return cumulative hypergeomtric distribution functions P(k). |
---|
89 | /// |
---|
90 | double cdf_hypergeometric_P(u_int k, u_int n1, u_int n2, u_int t); |
---|
91 | |
---|
92 | |
---|
93 | /// |
---|
94 | /// @brief Computes the kurtosis of the data in a vector. |
---|
95 | /// |
---|
96 | /// The kurtosis measures how sharply peaked a distribution is, |
---|
97 | /// relative to its width. The kurtosis is normalized to zero for a |
---|
98 | /// gaussian distribution. |
---|
99 | /// |
---|
100 | double kurtosis(const utility::vector&); |
---|
101 | |
---|
102 | |
---|
103 | /// |
---|
104 | /// @brief Median absolute deviation from median |
---|
105 | /// |
---|
106 | template <class T> |
---|
107 | double mad(const std::vector<T>& vec, const bool sorted=false) |
---|
108 | { |
---|
109 | double m = median(vec, sorted); |
---|
110 | std::vector<double> ad; |
---|
111 | ad.reserve(vec.size()); |
---|
112 | for (size_t i = 0; i<vec.size(); ++i) |
---|
113 | ad.push_back(fabs(vec[i]-m)); |
---|
114 | std::sort(ad.begin(), ad.end()); |
---|
115 | return median(ad,true); |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | /// |
---|
120 | /// @brief Median absolute deviation from median |
---|
121 | /// |
---|
122 | double mad(const utility::vector& vec, const bool sorted=false); |
---|
123 | |
---|
124 | |
---|
125 | /// |
---|
126 | /// Median is defined to be value in the middle. If number of values |
---|
127 | /// is even median is the average of the two middle values. the |
---|
128 | /// median value is given by p equal to 50. If @a sorted is false |
---|
129 | /// (default), the vector is copied, the copy is sorted, and then |
---|
130 | /// used to calculate the median. |
---|
131 | /// |
---|
132 | /// @return median |
---|
133 | /// |
---|
134 | /// @note interface will change |
---|
135 | /// |
---|
136 | template <class T> |
---|
137 | double median(const std::vector<T>& v, const bool sorted=false) |
---|
138 | { return percentile(v, 50.0, sorted); } |
---|
139 | |
---|
140 | /// |
---|
141 | /// Median is defined to be value in the middle. If number of values |
---|
142 | /// is even median is the average of the two middle values. If @a |
---|
143 | /// sorted is true, the function assumes vector @a vec to be |
---|
144 | /// sorted. If @a sorted is false, the vector is copied, the copy is |
---|
145 | /// sorted (default), and then used to calculate the median. |
---|
146 | /// |
---|
147 | /// @return median |
---|
148 | /// |
---|
149 | double median(const utility::vector& vec, const bool sorted=false); |
---|
150 | |
---|
151 | /** |
---|
152 | The percentile is determined by the \a p, a number between 0 and |
---|
153 | 100. The percentile is found by interpolation, using the formula |
---|
154 | \f$ percentile = (1 - \delta) x_i + \delta x_{i+1} \f$ where \a |
---|
155 | p is floor\f$((n - 1)p/100)\f$ and \f$ \delta \f$ is \f$ |
---|
156 | (n-1)p/100 - i \f$.Thus the minimum value of the vector is given |
---|
157 | by p equal to zero, the maximum is given by p equal to 100 and |
---|
158 | the median value is given by p equal to 50. If @a sorted |
---|
159 | is false (default), the vector is copied, the copy is sorted, |
---|
160 | and then used to calculate the median. |
---|
161 | |
---|
162 | @return \a p'th percentile |
---|
163 | */ |
---|
164 | template <class T> |
---|
165 | double percentile(const std::vector<T>& vec, const double p, |
---|
166 | const bool sorted=false) |
---|
167 | { |
---|
168 | if (sorted){ |
---|
169 | if (p>=100) |
---|
170 | return vec.back(); |
---|
171 | double j = p/100 * (vec.size()-1); |
---|
172 | int i = static_cast<int>(j); |
---|
173 | return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1]; |
---|
174 | } |
---|
175 | if (p==100) |
---|
176 | return *std::max_element(vec.begin(),vec.end()); |
---|
177 | std::vector<T> v_copy(vec); |
---|
178 | double j = p/100 * (v_copy.size()-1); |
---|
179 | int i = static_cast<int>(j); |
---|
180 | std::partial_sort(v_copy.begin(),v_copy.begin()+i+2 , v_copy.end()); |
---|
181 | return (1-j+floor(j))*v_copy[i] + (j-floor(j))*v_copy[i+1]; |
---|
182 | |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | The percentile is determined by the \a p, a number between 0 and |
---|
187 | 100. The percentile is found by interpolation, using the formula |
---|
188 | \f$ percentile = (1 - \delta) x_i + \delta x_{i+1} \f$ where \a |
---|
189 | p is floor\f$((n - 1)p/100)\f$ and \f$ \delta \f$ is \f$ |
---|
190 | (n-1)p/100 - i \f$.Thus the minimum value of the vector is given |
---|
191 | by p equal to zero, the maximum is given by p equal to 100 and |
---|
192 | the median value is given by p equal to 50. If @a sorted |
---|
193 | is false (default), the vector is copied, the copy is sorted, |
---|
194 | and then used to calculate the median. |
---|
195 | |
---|
196 | @return \a p'th percentile |
---|
197 | */ |
---|
198 | double percentile(const utility::vector& vec, const double, |
---|
199 | const bool sorted=false); |
---|
200 | |
---|
201 | /// |
---|
202 | /// @brief Computes the skewness of the data in a vector. |
---|
203 | /// |
---|
204 | /// The skewness measures the asymmetry of the tails of a |
---|
205 | /// distribution. |
---|
206 | /// |
---|
207 | double skewness(const utility::vector&); |
---|
208 | |
---|
209 | }}} // of namespace statistics, yat, and theplu |
---|
210 | |
---|
211 | #endif |
---|