source: trunk/yat/statistics/Averager.cc @ 916

Last change on this file since 916 was 916, checked in by Peter, 14 years ago

Sorry this commit is a bit to big.

Adding a yat_assert. The yat assert are turned on by providing a
'-DYAT_DEBUG' flag to preprocessor if normal cassert is turned
on. This flag is activated for developers running configure with
--enable-debug. The motivation is that we can use these yat_asserts in
header files and the yat_asserts will be invisible to the normal user
also if he uses C-asserts.

added output operator in DataLookup2D and removed output operator in
MatrixLookup?

Removed template function add_values in Averager and weighted version

Added function to AveragerWeighted? taking iterator to four ranges.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 KB
Line 
1// $Id: Averager.cc 916 2007-09-30 00:50:10Z peter $
2
3/*
4  Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér
6
7  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "Averager.h"
26
27#include <cassert>
28
29namespace theplu {
30namespace yat {
31namespace statistics {
32
33  Averager::Averager(void)
34    : n_(0), x_(0), xx_(0) 
35  {
36  }
37
38  Averager::Averager(double x, double xx, u_long n)
39    : n_(n), x_(x), xx_(xx) 
40  {
41  }
42
43  Averager::Averager(const Averager& a)
44    : n_(a.n_), x_(a.x_), xx_(a.xx_)
45  {
46  }
47
48  void Averager::add(double d, u_long n)
49  {
50    assert(!std::isnan(d));
51    n_  += n;
52    x_  += n*d;
53    xx_ += n*d*d;
54  }
55
56  double Averager::cv(void) const
57  {
58    return x_ ? std()/mean() : 0;
59  }
60
61  double Averager::mean(void) const
62  {
63    return n_ ? x_/n_ : 0;
64  }
65
66  u_long Averager::n(void) const
67  {
68    return n_;
69  }
70
71  void Averager::rescale(double a)
72  {
73    x_  *= a;
74    xx_ *= a*a;
75  }
76
77  void Averager::reset(void)
78  {
79    n_=0;
80    x_=xx_=0.0;
81  }
82
83  double Averager::standard_error(void) const
84  {
85    return sqrt(variance()/n_);
86  }
87
88  double Averager::std(void) const
89  {
90    return sqrt(variance());
91  }
92
93  double Averager::std(double m) const
94  {
95    return sqrt(variance(m));
96  }
97
98  double Averager::sum_x(void)  const
99  {
100    return x_;
101  }
102
103  double Averager::sum_xx(void) const
104  {
105    return xx_;
106  }
107
108  double Averager::sum_xx_centered(void)  const
109  {
110    return xx_-x_*x_/n_;
111  }
112
113  double Averager::variance(double m) const
114  {
115    return n_ ? (xx_ - 2*m*x_ + m*m*n()) /n_ : 0;
116  }
117
118  double Averager::variance(void) const
119  {
120    return n_>1 ? sum_xx_centered()/n_ : 0;
121  }
122
123  double Averager::variance_unbiased(void) const
124  {
125    return (n_>1) ? sum_xx_centered()/(n_-1) : 0;
126  }
127
128  const Averager& Averager::operator=(const Averager& a)
129  {
130    n_  = a.n_;
131    x_  = a.x_;
132    xx_ = a.xx_;
133    return *this;
134  }
135
136  const Averager& Averager::operator+=(const Averager& a)
137  {
138    n_+=a.n_;
139    x_+=a.x_;
140    xx_+=a.xx_;
141    return *this;
142  }
143
144}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.