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

Last change on this file since 2443 was 2443, checked in by Peter, 11 years ago

simplify code

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