yat  0.14.5pre
AveragerWeighted.h
1 #ifndef _theplu_yat_statistics_averagerweighted_
2 #define _theplu_yat_statistics_averagerweighted_
3 
4 // $Id: AveragerWeighted.h 3550 2017-01-03 05:41:02Z peter $
5 
6 /*
7  Copyright (C) 2004 Jari Häkkinen, Peter Johansson, Cecilia Ritz
8  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2009, 2010, 2011, 2016 Peter Johansson
11 
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13 
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 3 of the
17  License, or (at your option) any later version.
18 
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with yat. If not, see <http://www.gnu.org/licenses/>.
26 */
27 
28 #include "yat/utility/concept_check.h"
29 #include "yat/utility/iterator_traits.h"
30 
31 #include <boost/concept_check.hpp>
32 
33 #include <cmath>
34 
35 namespace theplu{
36 namespace yat{
37 namespace statistics{
38 
67  {
68  public:
69 
73  AveragerWeighted(void);
74 
79 
83  void add(const double d,const double w=1);
84 
90  double mean(void) const;
91 
103  double n(void) const;
104 
110  void rescale(double a);
111 
115  void reset(void);
116 
123  double std(void) const;
124 
136  double standard_error(void) const;
137 
143  double sum_w(void) const;
144 
148  double sum_ww(void) const;
149 
155  double sum_wx(void) const;
156 
160  double sum_wxx(void) const;
161 
165  double sum_xx_centered(void) const;
166 
173  double variance(const double m) const;
174 
184  double variance(void) const;
185 
192 
193  private:
197  double sum_wwx(void) const;
198 
202  double sum_wwxx(void) const;
203 
204  double mean_; // wx/w
205  double m2_; // wxx - m*m*w
206  double w_;
207  double ww_;
208  double wwxx_;
209  double wxx_;
210 
211  // double wwx; // m2_ + m*m*w
212  // double wx; // w*mean_
213  };
214 
226  template <typename InputIterator>
227  void add(AveragerWeighted& a, InputIterator first, InputIterator last)
228  {
229  BOOST_CONCEPT_ASSERT((utility::DataIteratorConcept<InputIterator>));
230  BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator>));
232  for ( ; first != last; ++first)
233  a.add(traits.data(first), traits.weight(first));
234  }
235 
252  template <typename InputIterator1, typename InputIterator2>
253  void add(AveragerWeighted& a, InputIterator1 first1, InputIterator1 last1,
254  InputIterator2 first2)
255  {
256  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator1>));
257  BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator1>));
258  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator2>));
259  BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator2>));
260 
263  for ( ; first1 != last1; ++first1, ++first2)
264  a.add(*first1, *first2);
265  }
266 
267 }}} // of namespace statistics, yat, and theplu
268 
269 #endif
data_reference data(Iter iter) const
Definition: iterator_traits.h:440
Concept check for Data Iterator.
Definition: concept_check.h:228
Definition: iterator_traits.h:412
double mean(void) const
Calculate the weighted mean.
double standard_error(void) const
Calculates standard deviation of the mean().
void add(AveragerWeighted &a, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
add values from two ranges to AveragerWeighted a
Definition: AveragerWeighted.h:253
Class to calulate averages with weights.
Definition: AveragerWeighted.h:66
void add(const double d, const double w=1)
double std(void) const
The standard deviation is defined as the square root of the variance().
double n(void) const
Weighted version of number of data points.
void check_iterator_is_unweighted(Iter iter)
check (at compile time) that iterator is unweighted.
Definition: iterator_traits.h:200
void reset(void)
Reset everything to zero.
const AveragerWeighted & operator+=(const AveragerWeighted &)
weight_reference weight(Iter iter) const
Definition: iterator_traits.h:446
AveragerWeighted(void)
The default constructor.
void add(AveragerWeighted &a, InputIterator first, InputIterator last)
adding a range of values to AveragerWeighted a
Definition: AveragerWeighted.h:227
void rescale(double a)
Rescale object.

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5