yat
0.8.3pre
|
00001 #ifndef _theplu_yat_statistics_averagerweighted_ 00002 #define _theplu_yat_statistics_averagerweighted_ 00003 00004 // $Id: AveragerWeighted.h 2564 2011-09-25 20:03:41Z peter $ 00005 00006 /* 00007 Copyright (C) 2004 Jari Häkkinen, Peter Johansson, Cecilia Ritz 00008 Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér 00009 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson 00010 Copyright (C) 2009, 2010, 2011 Peter Johansson 00011 00012 This file is part of the yat library, http://dev.thep.lu.se/yat 00013 00014 The yat library is free software; you can redistribute it and/or 00015 modify it under the terms of the GNU General Public License as 00016 published by the Free Software Foundation; either version 3 of the 00017 License, or (at your option) any later version. 00018 00019 The yat library is distributed in the hope that it will be useful, 00020 but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00022 General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License 00025 along with yat. If not, see <http://www.gnu.org/licenses/>. 00026 */ 00027 00028 #include "yat/utility/iterator_traits.h" 00029 00030 #include <boost/concept_check.hpp> 00031 00032 #include <cmath> 00033 00034 namespace theplu{ 00035 namespace yat{ 00036 namespace statistics{ 00037 00065 class AveragerWeighted 00066 { 00067 public: 00068 00072 AveragerWeighted(void); 00073 00077 AveragerWeighted(const AveragerWeighted&); 00078 00082 void add(const double d,const double w=1); 00083 00089 double mean(void) const; 00090 00102 double n(void) const; 00103 00109 void rescale(double a); 00110 00114 void reset(void); 00115 00122 double std(void) const; 00123 00135 double standard_error(void) const; 00136 00142 double sum_w(void) const; 00143 00147 double sum_ww(void) const; 00148 00154 double sum_wx(void) const; 00155 00159 double sum_wxx(void) const; 00160 00164 double sum_xx_centered(void) const; 00165 00172 double variance(const double m) const; 00173 00183 double variance(void) const; 00184 00190 const AveragerWeighted& operator+=(const AveragerWeighted&); 00191 00192 private: 00196 double sum_wwx(void) const; 00197 00201 double sum_wwxx(void) const; 00202 00203 double mean_; // wx/w 00204 double m2_; // wxx - m*m*w 00205 double w_; 00206 double ww_; 00207 double wwxx_; 00208 double wxx_; 00209 00210 // double wwx; // m2_ + m*m*w 00211 // double wx; // w*mean_ 00212 }; 00213 00221 template <typename InputIterator> 00222 void add(AveragerWeighted& a, InputIterator first, InputIterator last) 00223 { 00224 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>)); 00225 utility::iterator_traits<InputIterator> traits; 00226 for ( ; first != last; ++first) 00227 a.add(traits.data(first), traits.weight(first)); 00228 } 00229 00241 template <typename InputIterator1, typename InputIterator2> 00242 void add(AveragerWeighted& a, InputIterator1 first1, InputIterator1 last1, 00243 InputIterator2 first2) 00244 { 00245 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator1>)); 00246 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator2>)); 00247 utility::check_iterator_is_unweighted(first1); 00248 utility::check_iterator_is_unweighted(first2); 00249 for ( ; first1 != last1; ++first1, ++first2) 00250 a.add(*first1, *first2); 00251 } 00252 00253 }}} // of namespace statistics, yat, and theplu 00254 00255 #endif