source: trunk/yat/statistics/AveragerPair.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: 2.8 KB
Line 
1// $Id: AveragerPair.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  Copyright (C) 2007 Peter Johansson
7
8  This file is part of the yat library, http://trac.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "AveragerPair.h"
27#include "Averager.h"
28
29#include <cassert>
30#include <utility>
31
32namespace theplu {
33namespace yat {
34namespace statistics {
35
36  AveragerPair::AveragerPair(void)
37    : x_(Averager()), y_(Averager()), xy_(0.0)
38  {
39  }
40
41  AveragerPair::AveragerPair(const AveragerPair& a)
42    : x_(a.x_averager()), y_(a.y_averager()), xy_(a.sum_xy())
43  {
44  }
45
46  void AveragerPair::add(const double x, const double y, const unsigned long n)
47  {
48    x_.add(x,n); y_.add(y,n); xy_ += n*x*y;
49  }
50
51  double AveragerPair::ccc(void) const
52  {
53    return ((2*covariance()) /
54            ((x_.variance()+y_.variance()) +
55             (x_.mean()-y_.mean())*(x_.mean()-y_.mean())));
56  }
57
58  double AveragerPair::correlation(void) const
59  { return covariance() / std::sqrt(x_.variance()*y_.variance());
60  }
61
62  double AveragerPair::covariance(void) const
63  {
64    return (xy_ - x_.sum_x()*y_.mean()) / n();
65  }
66
67  double AveragerPair::mean_xy(void) const
68  {
69    return xy_/n();
70  }
71
72  double AveragerPair::msd(void) const
73  {
74    return sum_squared_deviation()/n();
75  }
76
77  unsigned long AveragerPair::n(void) const
78  {
79    return x_.n();
80  }
81
82  void AveragerPair::reset(void)
83  {
84    x_.reset(); y_.reset(); xy_=0.0;
85  }
86
87  const AveragerPair& AveragerPair::operator=(const AveragerPair& a)
88  {
89    x_=a.x_; y_=a.y_; xy_=a.xy_;
90    return *this;
91  }
92
93  double AveragerPair::sum_squared_deviation(void) const
94  {
95    return x_averager().sum_xx()+y_averager().sum_xx()-2*sum_xy() ;
96  }
97
98  double AveragerPair::sum_xy(void) const
99  {
100    return xy_;
101  }
102
103  double AveragerPair::sum_xy_centered(void) const
104  {
105    return xy_-x_.sum_x()*y_.mean();
106  }
107
108  const Averager& AveragerPair::x_averager(void) const
109  {
110    return x_;
111  }
112
113  const Averager& AveragerPair::y_averager(void) const
114  {
115    return y_;
116  }
117
118  const AveragerPair& AveragerPair::operator+=(const AveragerPair& a)
119  {
120    x_+=a.x_averager();
121    y_+=a.y_averager();
122    xy_+=a.sum_xy();
123    return *this;
124  }
125
126}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.