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

Last change on this file since 1575 was 1575, checked in by Jari Häkkinen, 12 years ago

• Property svn:eol-style set to `native`
• Property svn:keywords set to `Id`
File size: 3.0 KB
Line
1#ifndef _theplu_yat_normalizer_z_score_
2#define _theplu_yat_normalizer_z_score_
3
4// \$Id: Zscore.h 1575 2008-10-14 07:19:48Z jari \$
5
6/*
7  Copyright (C) 2008 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
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
30namespace theplu {
31namespace yat {
32namespace normalizer {
33
34  /**
35     \brief Zero mean and unity variance
36
37     Create a range that has zero mean and unity variance
38
39     \since New in yat 0.5
40   */
41  class Zscore
42  {
43  public:
44    /**
45       The element in range [result, result + (last-first)) is
46       calculated as result[i] = (first[i] - m) / std where m and std
47       are the mean and standard deviation, respectively, of the range
48       [first, last).
49
50       It is possible to centralize a range "in place"; it is
51       permissible for the iterators \a first and \a result to be the
52       same. \see std::transform
53
54       \return result + (last-first)
55     */
56    template<class InputIterator, class OutputIterator>
57    OutputIterator operator()(InputIterator first, InputIterator last,
58                              OutputIterator result) const
59    {
60      typename utility::weighted_iterator_traits<InputIterator>::type tag;
61      return normalize(first, last, result, tag);
62    }
63
64  private:
65    template<class InputIterator, class OutputIterator>
66    OutputIterator normalize(InputIterator first, InputIterator last,
67                             OutputIterator result,
68                             utility::unweighted_iterator_tag tag) const
69    {
70      statistics::Averager a;
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      return result;
80    }
81
82    template<class InputIterator, class OutputIterator>
83    OutputIterator normalize(InputIterator first, InputIterator last,
84                             OutputIterator result,
85                             utility::weighted_iterator_tag tag) const
86    {
87      std::copy(utility::weight_iterator(first),
88                utility::weight_iterator(last),
89                utility::weight_iterator(result));
90      statistics::AveragerWeighted a;
92      double m = a.mean();
93      double std = a.std();
94      utility::iterator_traits<InputIterator> trait;
95      while (first!=last) {
96        trait.data(result) = (trait.data(first) - m) / std;
97        ++first;
98        ++result;
99      }
100      return result;
101    }
102
103  };
104
105}}} // end of namespace normalizer, yat and thep
106#endif
Note: See TracBrowser for help on using the repository browser.