1 | #ifndef _theplu_yat_normalizer_z_score_ |
---|
2 | #define _theplu_yat_normalizer_z_score_ |
---|
3 | |
---|
4 | // $Id: Zscore.h 2119 2009-12-12 23:11:43Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2009 Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 3 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "yat/statistics/Averager.h" |
---|
27 | #include "yat/statistics/AveragerWeighted.h" |
---|
28 | |
---|
29 | #include "yat/utility/iterator_traits.h" |
---|
30 | |
---|
31 | namespace theplu { |
---|
32 | namespace yat { |
---|
33 | namespace normalizer { |
---|
34 | |
---|
35 | /** |
---|
36 | \brief Zero mean and unity variance |
---|
37 | |
---|
38 | Shift and scale the values in a range as: \f$ y_i = |
---|
39 | \frac{x_i-m}{s} \f$ where \a m is the mean and \a s is the |
---|
40 | standard deviation. After normalization, the range will have zero |
---|
41 | mean and unity variance. |
---|
42 | |
---|
43 | \since New in yat 0.5 |
---|
44 | */ |
---|
45 | class Zscore |
---|
46 | { |
---|
47 | public: |
---|
48 | /** |
---|
49 | The element in range [result, result + (last-first)) is |
---|
50 | calculated as result[i] = (first[i] - m) / s where m and std |
---|
51 | are the mean and standard deviation, respectively, of the range |
---|
52 | [first, last). |
---|
53 | |
---|
54 | It is possible to centralize a range "in place"; it is |
---|
55 | permissible for the iterators \a first and \a result to be the |
---|
56 | same. \see std::transform |
---|
57 | */ |
---|
58 | template<class InputIterator, class OutputIterator> |
---|
59 | void operator()(InputIterator first, InputIterator last, |
---|
60 | OutputIterator result) const |
---|
61 | { |
---|
62 | typename utility::weighted_iterator_traits<InputIterator>::type tag; |
---|
63 | normalize(first, last, result, tag); |
---|
64 | } |
---|
65 | |
---|
66 | private: |
---|
67 | template<class InputIterator, class OutputIterator> |
---|
68 | void normalize(InputIterator first,InputIterator last,OutputIterator result, |
---|
69 | utility::unweighted_iterator_tag tag) const |
---|
70 | { |
---|
71 | statistics::Averager a; |
---|
72 | add(a, first, last); |
---|
73 | double m = a.mean(); |
---|
74 | double std = a.std(); |
---|
75 | while (first!=last) { |
---|
76 | *result = (*first - m) / std; |
---|
77 | ++first; |
---|
78 | ++result; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | template<class InputIterator, class OutputIterator> |
---|
83 | void normalize(InputIterator first,InputIterator last,OutputIterator result, |
---|
84 | utility::weighted_iterator_tag tag) const |
---|
85 | { |
---|
86 | std::copy(utility::weight_iterator(first), |
---|
87 | utility::weight_iterator(last), |
---|
88 | utility::weight_iterator(result)); |
---|
89 | statistics::AveragerWeighted a; |
---|
90 | add(a, first, last); |
---|
91 | double m = a.mean(); |
---|
92 | double std = a.std(); |
---|
93 | utility::iterator_traits<InputIterator> trait; |
---|
94 | while (first!=last) { |
---|
95 | trait.data(result) = (trait.data(first) - m) / std; |
---|
96 | ++first; |
---|
97 | ++result; |
---|
98 | } |
---|
99 | } |
---|
100 | }; |
---|
101 | |
---|
102 | }}} // end of namespace normalizer, yat and thep |
---|
103 | #endif |
---|