00001 #ifndef _theplu_yat_normalizer_z_score_
00002 #define _theplu_yat_normalizer_z_score_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00077
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
00096
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 }}}
00115 #endif