yat  0.8.3pre
Zscore.h
00001 #ifndef _theplu_yat_normalizer_z_score_
00002 #define _theplu_yat_normalizer_z_score_
00003 
00004 // $Id: Zscore.h 2284 2010-06-26 02:41:45Z peter $
00005 
00006 /*
00007   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
00008   Copyright (C) 2009, 2010 Peter Johansson
00009 
00010   This file is part of the yat library, http://dev.thep.lu.se/yat
00011 
00012   The yat library is free software; you can redistribute it and/or
00013   modify it under the terms of the GNU General Public License as
00014   published by the Free Software Foundation; either version 3 of the
00015   License, or (at your option) any later version.
00016 
00017   The yat library is distributed in the hope that it will be useful,
00018   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00020   General Public License for more details.
00021 
00022   You should have received a copy of the GNU General Public License
00023   along with yat. If not, see <http://www.gnu.org/licenses/>.
00024 */
00025 
00026 #include "utility.h"
00027 
00028 #include "yat/statistics/Averager.h"
00029 #include "yat/statistics/AveragerWeighted.h"
00030 
00031 #include "yat/utility/iterator_traits.h"
00032 
00033 #include <boost/concept_check.hpp>
00034 
00035 namespace theplu {
00036 namespace yat {
00037 namespace normalizer {
00038 
00049   class Zscore
00050   {
00051   public:
00062     template<class ForwardIter1, class ForwardIter2>
00063     void operator()(ForwardIter1 first, ForwardIter1 last,
00064                     ForwardIter2 result) const
00065     {
00066       typename utility::weighted_if_any2<ForwardIter1, ForwardIter2>::type tag;
00067       normalize(first, last, result, tag);
00068     }
00069 
00070   private:
00071     template<class ForwardIterator, class OutputIterator>
00072     void normalize(ForwardIterator first, ForwardIterator last,
00073                    OutputIterator result,
00074                    utility::unweighted_iterator_tag tag) const
00075     {
00076       // we require forward iterator since we iterate through the range
00077       // multiple times.
00078       BOOST_CONCEPT_ASSERT((boost::ForwardIterator<ForwardIterator>));
00079       BOOST_CONCEPT_ASSERT((boost::OutputIterator<OutputIterator, double>));
00080       statistics::Averager a;
00081       add(a, first, last);
00082       double m = a.mean();
00083       double std = a.std();
00084       while (first!=last) {
00085         *result = (*first - m) / std;
00086         ++first;
00087         ++result;
00088       }
00089     }
00090 
00091     template<class ForwardIter1, class ForwardIter2>
00092     void normalize(ForwardIter1 first, ForwardIter1 last, ForwardIter2 result,
00093                    utility::weighted_iterator_tag tag) const
00094     {
00095       // we require forward iterator since we iterate through the range
00096       // multiple times.
00097       BOOST_CONCEPT_ASSERT((boost::ForwardIterator<ForwardIter1>));
00098       BOOST_CONCEPT_ASSERT((boost::Mutable_ForwardIterator<ForwardIter2>));
00099       detail::copy_weight_if_weighted(first, last, result);
00100       statistics::AveragerWeighted a;
00101       add(a, first, last);
00102       double m = a.mean();
00103       double std = a.std();
00104       utility::iterator_traits<ForwardIter1> in_trait;
00105       utility::iterator_traits<ForwardIter2> out_trait;
00106       while (first!=last) {
00107         out_trait.data(result) = (in_trait.data(first) - m) / std;
00108         ++first;
00109         ++result;
00110       }
00111     }
00112   };
00113 
00114 }}} // end of namespace normalizer, yat and thep
00115 #endif

Generated on Thu Dec 20 2012 03:12:57 for yat by  doxygen 1.8.0-20120409