yat  0.21pre
AveragerWeighted.h
1 #ifndef _theplu_yat_statistics_averagerweighted_
2 #define _theplu_yat_statistics_averagerweighted_
3 
4 // $Id: AveragerWeighted.h 4245 2022-09-21 05:41:41Z 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 
78  void add(const double d,const double w=1);
79 
85  double mean(void) const;
86 
98  double n(void) const;
99 
105  void rescale(double a);
106 
110  void reset(void);
111 
118  double std(void) const;
119 
131  double standard_error(void) const;
132 
138  double sum_w(void) const;
139 
143  double sum_ww(void) const;
144 
150  double sum_wx(void) const;
151 
155  double sum_wxx(void) const;
156 
160  double sum_xx_centered(void) const;
161 
168  double variance(const double m) const;
169 
179  double variance(void) const;
180 
187 
188  private:
192  double sum_wwx(void) const;
193 
197  double sum_wwxx(void) const;
198 
199  double mean_; // wx/w
200  double m2_; // wxx - m*m*w
201  double w_;
202  double ww_;
203  double wwxx_;
204  double wxx_;
205 
206  // double wwx; // m2_ + m*m*w
207  // double wx; // w*mean_
208  };
209 
221  template <typename InputIterator>
222  void add(AveragerWeighted& a, InputIterator first, InputIterator last)
223  {
224  BOOST_CONCEPT_ASSERT((utility::DataIteratorConcept<InputIterator>));
225  BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator>));
227  for ( ; first != last; ++first)
228  a.add(traits.data(first), traits.weight(first));
229  }
230 
247  template <typename InputIterator1, typename InputIterator2>
248  void add(AveragerWeighted& a, InputIterator1 first1, InputIterator1 last1,
249  InputIterator2 first2)
250  {
251  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator1>));
252  BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator1>));
253  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator2>));
254  BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator2>));
255 
258  for ( ; first1 != last1; ++first1, ++first2)
259  a.add(*first1, *first2);
260  }
261 
262 }}} // of namespace statistics, yat, and theplu
263 
264 #endif
double n(void) const
Weighted version of number of data points.
Concept check for Data Iterator.
Definition: concept_check.h:240
data_reference data(Iter iter) const
Definition: iterator_traits.h:440
Definition: iterator_traits.h:412
The Department of Theoretical Physics namespace as we define it.
void add(AveragerWeighted &a, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
add values from two ranges to AveragerWeighted a
Definition: AveragerWeighted.h:248
Class to calulate averages with weights.
Definition: AveragerWeighted.h:66
double standard_error(void) const
Calculates standard deviation of the mean().
void add(const double d, const double w=1)
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 &)
double std(void) const
The standard deviation is defined as the square root of the variance().
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:222
double mean(void) const
Calculate the weighted mean.
void rescale(double a)
Rescale object.

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14