source: trunk/yat/statistics/AveragerPair.cc @ 1000

Last change on this file since 1000 was 1000, checked in by Jari Häkkinen, 14 years ago

trac moved to new location.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1// $Id: AveragerPair.cc 1000 2007-12-23 20:09:15Z jari $
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 ( (x_.variance() && y_.variance() && (x_.mean()-y_.mean()) ) ?
54             ((2*covariance()) /
55              ((x_.variance()+y_.variance()) +
56               (x_.mean()-y_.mean())*(x_.mean()-y_.mean()))) : 0);
57  }
58
59  double AveragerPair::correlation(void) const
60  { return ((x_.std()>0 && y_.std()>0) ?
61            (covariance() / (x_.std()*y_.std()) ) : 0);
62  }
63
64  double AveragerPair::covariance(void) const
65  {
66    return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / n(): 0;
67  }
68
69  double AveragerPair::mean_xy(void) const
70  {
71    return xy_/n();
72  }
73
74  double AveragerPair::msd(void) const
75  {
76    return sum_squared_deviation()/n();
77  }
78
79  unsigned long AveragerPair::n(void) const
80  {
81    return x_.n();
82  }
83
84  void AveragerPair::reset(void)
85  {
86    x_.reset(); y_.reset(); xy_=0.0;
87  }
88
89  const AveragerPair& AveragerPair::operator=(const AveragerPair& a)
90  {
91    x_=a.x_; y_=a.y_; xy_=a.xy_;
92    return *this;
93  }
94
95  double AveragerPair::sum_squared_deviation(void) const
96  {
97    return x_averager().sum_xx()+y_averager().sum_xx()-2*sum_xy() ;
98  }
99
100  double AveragerPair::sum_xy(void) const
101  {
102    return xy_;
103  }
104
105  double AveragerPair::sum_xy_centered(void) const
106  {
107    return xy_-x_.sum_x()*y_.mean();
108  }
109
110  const Averager& AveragerPair::x_averager(void) const
111  {
112    return x_;
113  }
114
115  const Averager& AveragerPair::y_averager(void) const
116  {
117    return y_;
118  }
119
120  const AveragerPair& AveragerPair::operator+=(const AveragerPair& a)
121  {
122    x_+=a.x_averager();
123    y_+=a.y_averager();
124    xy_+=a.sum_xy();
125    return *this;
126  }
127
128}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.