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

Last change on this file since 1717 was 1717, checked in by Peter, 12 years ago

improved(?) docs. Moved some docs from operator() to class docs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1#ifndef _theplu_yat_normalizer_z_score_
2#define _theplu_yat_normalizer_z_score_
3
4// $Id: Zscore.h 1717 2009-01-13 22:32:00Z 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
30namespace theplu {
31namespace yat {
32namespace 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       \return result + (last-first)
58     */
59    template<class InputIterator, class OutputIterator>
60    OutputIterator operator()(InputIterator first, InputIterator last,
61                              OutputIterator result) const
62    {
63      typename utility::weighted_iterator_traits<InputIterator>::type tag;
64      return normalize(first, last, result, tag);
65    }
66
67  private:
68    template<class InputIterator, class OutputIterator>
69    OutputIterator normalize(InputIterator first, InputIterator last,
70                             OutputIterator result,
71                             utility::unweighted_iterator_tag tag) const
72    {
73      statistics::Averager a;
74      add(a, first, last);
75      double m = a.mean();
76      double std = a.std();
77      while (first!=last) {
78        *result = (*first - m) / std;
79        ++first;
80        ++result;
81      }
82      return result;
83    }
84
85    template<class InputIterator, class OutputIterator>
86    OutputIterator normalize(InputIterator first, InputIterator last,
87                             OutputIterator result,
88                             utility::weighted_iterator_tag tag) const
89    {
90      std::copy(utility::weight_iterator(first), 
91                utility::weight_iterator(last), 
92                utility::weight_iterator(result));
93      statistics::AveragerWeighted a;
94      add(a, first, last);
95      double m = a.mean();
96      double std = a.std();
97      utility::iterator_traits<InputIterator> trait;
98      while (first!=last) {
99        trait.data(result) = (trait.data(first) - m) / std;
100        ++first;
101        ++result;
102      }
103      return result;
104    }
105
106  };
107
108}}} // end of namespace normalizer, yat and thep
109#endif
Note: See TracBrowser for help on using the repository browser.