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

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

fix doc typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1#ifndef _theplu_yat_normalizer_z_score_
2#define _theplu_yat_normalizer_z_score_
3
4// $Id: Zscore.h 2284 2010-06-26 02:41:45Z peter $
5
6/*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010 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 "utility.h"
27
28#include "yat/statistics/Averager.h"
29#include "yat/statistics/AveragerWeighted.h"
30
31#include "yat/utility/iterator_traits.h"
32
33#include <boost/concept_check.hpp>
34
35namespace theplu {
36namespace yat {
37namespace normalizer {
38
39  /**
40     \brief Zero mean and unity variance
41
42     Shift and scale the values in a range as: \f$ y_i =
43     \frac{x_i-m}{s} \f$ where \a m is the mean and \a s is the
44     standard deviation. After normalization, the range will have zero
45     mean and unity variance.
46
47     \since New in yat 0.5
48   */
49  class Zscore
50  {
51  public:
52    /**
53       The element in range [result, result + (last-first)) is
54       calculated as result[i] = (first[i] - m) / s where m and std
55       are the mean and standard deviation, respectively, of the range
56       [first, last).
57
58       It is possible to centralize a range "in place"; it is
59       permissible for the iterators \a first and \a result to be the
60       same. \see std::transform
61     */
62    template<class ForwardIter1, class ForwardIter2>
63    void operator()(ForwardIter1 first, ForwardIter1 last,
64                    ForwardIter2 result) const
65    {
66      typename utility::weighted_if_any2<ForwardIter1, ForwardIter2>::type tag;
67      normalize(first, last, result, tag);
68    }
69
70  private:
71    template<class ForwardIterator, class OutputIterator>
72    void normalize(ForwardIterator first, ForwardIterator last,
73                   OutputIterator result,
74                   utility::unweighted_iterator_tag tag) const
75    {
76      // we require forward iterator since we iterate through the range
77      // multiple times.
78      BOOST_CONCEPT_ASSERT((boost::ForwardIterator<ForwardIterator>));
79      BOOST_CONCEPT_ASSERT((boost::OutputIterator<OutputIterator, double>));
80      statistics::Averager a;
81      add(a, first, last);
82      double m = a.mean();
83      double std = a.std();
84      while (first!=last) {
85        *result = (*first - m) / std;
86        ++first;
87        ++result;
88      }
89    }
90
91    template<class ForwardIter1, class ForwardIter2>
92    void normalize(ForwardIter1 first, ForwardIter1 last, ForwardIter2 result,
93                   utility::weighted_iterator_tag tag) const
94    {
95      // we require forward iterator since we iterate through the range
96      // multiple times.
97      BOOST_CONCEPT_ASSERT((boost::ForwardIterator<ForwardIter1>));
98      BOOST_CONCEPT_ASSERT((boost::Mutable_ForwardIterator<ForwardIter2>));
99      detail::copy_weight_if_weighted(first, last, result);
100      statistics::AveragerWeighted a;
101      add(a, first, last);
102      double m = a.mean();
103      double std = a.std();
104      utility::iterator_traits<ForwardIter1> in_trait;
105      utility::iterator_traits<ForwardIter2> out_trait;
106      while (first!=last) {
107        out_trait.data(result) = (in_trait.data(first) - m) / std;
108        ++first;
109        ++result;
110      }
111    }
112  };
113
114}}} // end of namespace normalizer, yat and thep
115#endif
Note: See TracBrowser for help on using the repository browser.