source: trunk/yat/normalizer/Zscore.h @ 1546

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

fixes #443

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1#ifndef _theplu_yat_normalizer_z_score_
2#define _theplu_yat_normalizer_z_score_
3
4/*
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "yat/statistics/Averager.h"
24#include "yat/statistics/AveragerWeighted.h"
25
26#include "yat/utility/iterator_traits.h"
27
28namespace theplu {
29namespace yat {
30namespace normalizer {
31
32  /**
33     \brief Zero mean and unity variance
34
35     Create a range that has zero mean and unity variance
36
37     \since New in yat 0.5
38   */
39  class Zscore
40  {
41  public:
42    /**
43       The element in range [result, result + (last-first)) is
44       calculated as result[i] = (first[i] - m) / std where m and std
45       are the mean and standard deviation, respectively, of the range
46       [first, last).
47
48       It is possible to centralize a range "in place"; it is
49       permissible for the iterators \a first and \a result to be the
50       same. \see std::transform
51
52       \return result + (last-first)
53     */
54    template<class InputIterator, class OutputIterator>
55    OutputIterator operator()(InputIterator first, InputIterator last,
56                              OutputIterator result) const
57    {
58      typename utility::weighted_iterator_traits<InputIterator>::type tag;
59      return normalize(first, last, result, tag);
60    }
61
62  private:
63    template<class InputIterator, class OutputIterator>
64    OutputIterator normalize(InputIterator first, InputIterator last,
65                             OutputIterator result,
66                             utility::unweighted_iterator_tag tag) const
67    {
68      statistics::Averager a;
69      add(a, first, last);
70      double m = a.mean();
71      double std = a.std();
72      while (first!=last) {
73        *result = (*first - m) / std;
74        ++first;
75        ++result;
76      }
77      return result;
78    }
79
80    template<class InputIterator, class OutputIterator>
81    OutputIterator normalize(InputIterator first, InputIterator last,
82                             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      return result;
99    }
100
101  };
102
103}}} // end of namespace normalizer, yat and thep
104#endif
Note: See TracBrowser for help on using the repository browser.