1 | #ifndef _theplu_yat_normalizer_z_score_ |
---|
2 | #define _theplu_yat_normalizer_z_score_ |
---|
3 | |
---|
4 | // $Id: Zscore.h 1739 2009-01-21 01:34:18Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008, 2009 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/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 3 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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "yat/statistics/Averager.h" |
---|
26 | #include "yat/statistics/AveragerWeighted.h" |
---|
27 | |
---|
28 | #include "yat/utility/iterator_traits.h" |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace normalizer { |
---|
33 | |
---|
34 | /** |
---|
35 | \brief Zero mean and unity variance |
---|
36 | |
---|
37 | Shift and scale the values in a range as: \f$ y_i = |
---|
38 | \frac{x_i-m}{s} \f$ where \a m is the mean and \a s is the |
---|
39 | standard deviation. After normalization, the range will have zero |
---|
40 | mean and unity variance. |
---|
41 | |
---|
42 | \since New in yat 0.5 |
---|
43 | */ |
---|
44 | class Zscore |
---|
45 | { |
---|
46 | public: |
---|
47 | /** |
---|
48 | The element in range [result, result + (last-first)) is |
---|
49 | calculated as result[i] = (first[i] - m) / s where m and std |
---|
50 | are the mean and standard deviation, respectively, of the range |
---|
51 | [first, last). |
---|
52 | |
---|
53 | It is possible to centralize a range "in place"; it is |
---|
54 | permissible for the iterators \a first and \a result to be the |
---|
55 | same. \see std::transform |
---|
56 | */ |
---|
57 | template<class InputIterator, class OutputIterator> |
---|
58 | void operator()(InputIterator first, InputIterator last, |
---|
59 | OutputIterator result) const |
---|
60 | { |
---|
61 | typename utility::weighted_iterator_traits<InputIterator>::type tag; |
---|
62 | normalize(first, last, result, tag); |
---|
63 | } |
---|
64 | |
---|
65 | private: |
---|
66 | template<class InputIterator, class OutputIterator> |
---|
67 | void normalize(InputIterator first,InputIterator last,OutputIterator result, |
---|
68 | utility::unweighted_iterator_tag tag) const |
---|
69 | { |
---|
70 | statistics::Averager a; |
---|
71 | add(a, first, last); |
---|
72 | double m = a.mean(); |
---|
73 | double std = a.std(); |
---|
74 | while (first!=last) { |
---|
75 | *result = (*first - m) / std; |
---|
76 | ++first; |
---|
77 | ++result; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | template<class InputIterator, class OutputIterator> |
---|
82 | void normalize(InputIterator first,InputIterator last,OutputIterator result, |
---|
83 | utility::weighted_iterator_tag tag) const |
---|
84 | { |
---|
85 | std::copy(utility::weight_iterator(first), |
---|
86 | utility::weight_iterator(last), |
---|
87 | utility::weight_iterator(result)); |
---|
88 | statistics::AveragerWeighted a; |
---|
89 | add(a, first, last); |
---|
90 | double m = a.mean(); |
---|
91 | double std = a.std(); |
---|
92 | utility::iterator_traits<InputIterator> trait; |
---|
93 | while (first!=last) { |
---|
94 | trait.data(result) = (trait.data(first) - m) / std; |
---|
95 | ++first; |
---|
96 | ++result; |
---|
97 | } |
---|
98 | } |
---|
99 | }; |
---|
100 | |
---|
101 | }}} // end of namespace normalizer, yat and thep |
---|
102 | #endif |
---|