source: trunk/yat/statistics/AveragerWeighted.cc @ 1122

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

refs #335 - Changed so Averager classes are consistently returning NaN when Averager is empty or for some other reason the estimation ends up with things like zero by zero division. Previously zero was returned from some functions and Nan from some functions. I did not change anythuing in NCC.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.0 KB
Line 
1// $Id: AveragerWeighted.cc 1122 2008-02-22 17:01:15Z peter $
2
3/*
4  Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
6
7  This file is part of the yat library, http://trac.thep.lu.se/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 "AveragerWeighted.h"
26#include "Averager.h"
27
28namespace theplu {
29namespace yat {
30namespace statistics {
31
32  AveragerWeighted::AveragerWeighted(void)
33    : w_(Averager()), wx_(Averager()), wwx_(0), wxx_(0)
34  {
35  }
36
37  AveragerWeighted::AveragerWeighted(const AveragerWeighted& a)
38    : w_(Averager(a.sum_w(),a.sum_ww(),1)),
39      wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)),
40      wwx_(a.sum_wwx()),
41      wxx_(a.sum_wxx())
42  {
43  }
44
45  void AveragerWeighted::add(const double d, const double w)
46  {
47    if (!w)
48      return;
49    w_.add(w); wx_.add(w*d); wwx_+=w*w*d; wxx_+=w*d*d;
50  }
51
52  double AveragerWeighted::mean(void) const
53  {
54    return sum_wx()/sum_w();
55  }
56
57  double AveragerWeighted::n(void) const
58  {
59    return sum_w()*sum_w()/sum_ww();
60  }
61
62  void AveragerWeighted::rescale(double a)
63  {
64    wx_.rescale(a);
65    wwx_*=a;
66    wxx_*=a*a;
67  }
68
69  void AveragerWeighted::reset(void)
70  {
71    wx_.reset(); w_.reset(); wwx_=0; wxx_=0;
72  }
73
74  double AveragerWeighted::std(void) const
75  {
76    return sqrt(variance());
77  }
78
79  double AveragerWeighted::standard_error(void) const
80  {
81    return sqrt(sum_ww()/(sum_w()*sum_w()*sum_w()) * sum_xx_centered());
82  }
83
84  double AveragerWeighted::sum_w(void)  const
85  {
86    return w_.sum_x();
87  }
88
89  double AveragerWeighted::sum_ww(void) const
90  {
91    return w_.sum_xx();
92  }
93
94  double AveragerWeighted::sum_wwx(void) const
95  {
96    return wwx_;
97  }
98
99  double AveragerWeighted::sum_wwxx(void) const
100  {
101    return wx_.sum_xx();
102  }
103
104  double AveragerWeighted::sum_wx(void) const
105  {
106    return wx_.sum_x();
107  }
108
109  double AveragerWeighted::sum_wxx(void) const
110  {
111    return wxx_;
112  }
113
114  double AveragerWeighted::sum_xx_centered(void) const
115  {
116    return sum_wxx() - mean()*mean()*sum_w();
117  }
118
119  double AveragerWeighted::variance(const double m) const
120  {
121    return (sum_wxx()-2*m*sum_wx())/sum_w()+m*m;
122  }
123
124  double AveragerWeighted::variance(void) const
125  {
126    return sum_xx_centered()/sum_w();
127  }
128
129  const Averager& AveragerWeighted::wx(void) const
130  {
131    return wx_;
132  }
133
134  const Averager& AveragerWeighted::w(void) const
135  {
136    return w_;
137  }
138
139  const AveragerWeighted& AveragerWeighted::operator+=(const AveragerWeighted& a)
140  {
141    wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); wxx_+=a.sum_wxx();
142    return *this;
143  }
144
145}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.