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

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

Addresses #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.7 KB
Line 
1// $Id: AveragerPair.cc 718 2006-12-26 09:56:26Z jari $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/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 2 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 this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "AveragerPair.h"
25#include "Averager.h"
26
27#include <cassert>
28#include <utility>
29
30namespace theplu {
31namespace yat {
32namespace statistics {
33
34  AveragerPair::AveragerPair(void)
35    : x_(Averager()), y_(Averager()), xy_(0.0)
36  {
37  }
38
39  AveragerPair::AveragerPair(const AveragerPair& a)
40    : x_(a.x_averager()), y_(a.y_averager()), xy_(a.sum_xy())
41  {
42  }
43
44  void AveragerPair::add(const double x, const double y, const unsigned long n)
45  {
46    x_.add(x,n); y_.add(y,n); xy_ += n*x*y;
47  }
48
49  double AveragerPair::ccc(void) const
50  {
51    return ( (x_.variance() && y_.variance() && (x_.mean()-y_.mean()) ) ?
52             ((2*covariance()) /
53              ((x_.variance()+y_.variance()) +
54               (x_.mean()-y_.mean())*(x_.mean()-y_.mean()))) : 0);
55  }
56
57  double AveragerPair::correlation(void) const
58  { return ((x_.std()>0 && y_.std()>0) ?
59            (covariance() / (x_.std()*y_.std()) ) : 0);
60  }
61
62  double AveragerPair::covariance(void) const
63  {
64    return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / n(): 0;
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 ( x_averager().sum_xx()+y_averager().sum_xx()-2*sum_xy() )/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_xy(void) const
94  {
95    return xy_;
96  }
97
98  double AveragerPair::sum_xy_centered(void) const
99  {
100    return xy_-x_.sum_x()*y_.mean();
101  }
102
103  const Averager& AveragerPair::x_averager(void) const
104  {
105    return x_;
106  }
107
108  const Averager& AveragerPair::y_averager(void) const
109  {
110    return y_;
111  }
112
113  const AveragerPair& AveragerPair::operator+=(const AveragerPair& a)
114  {
115    x_+=a.x_averager();
116    y_+=a.y_averager();
117    xy_+=a.sum_xy();
118    return *this;
119  }
120
121}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.