source: trunk/yat/statistics/AveragerPairWeighted.cc @ 1043

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

fixing #128 - iterators in Averger classes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: AveragerPairWeighted.cc 1043 2008-02-06 19:30:03Z peter $
2
3/*
4  Copyright (C) 2005 Markus Ringnér, 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 "AveragerPairWeighted.h"
27#include "AveragerPair.h"
28#include "Averager.h"
29#include "yat/classifier/DataLookup1D.h"
30#include "yat/classifier/DataLookupWeighted1D.h"
31
32#include <cassert>
33
34namespace theplu {
35namespace yat {
36namespace statistics {
37
38
39  AveragerPairWeighted::AveragerPairWeighted(void)
40    : wxy_(0), w_(0)
41  {
42  }
43
44
45  void  AveragerPairWeighted::add(const double x, const double y, 
46                                  const double wx, const double wy)
47  { 
48    if(wx==0.0 || wy==0.0) {
49      return;
50    }
51    assert(!std::isnan(x) && "x is nan");
52    assert(!std::isnan(y) && "y is nan");
53    assert(!std::isnan(wx) && "wx is nan");
54    assert(!std::isnan(wy) && "wy is nan");
55    double w=wx*wy;
56    x_.add(x,w);
57    y_.add(y,w); 
58    wxy_ += w*x*y;   
59    w_ +=w;
60  }
61
62
63  double AveragerPairWeighted::correlation(void) const
64  {
65    return ( x_.variance()>0 && y_.variance()>0 ? 
66             covariance() / sqrt(x_.variance()*y_.variance()) : 0 );
67  }
68
69
70  double AveragerPairWeighted::covariance(void) const
71  {
72    return sum_xy_centered()/sum_w();
73  }
74
75
76  double AveragerPairWeighted::msd(void) const
77  {
78    return (x_.sum_wxx()+y_.sum_wxx()-2*wxy_)/w_;
79  }
80
81
82  double AveragerPairWeighted::n(void) const
83  {
84    return x_.n();
85  }
86
87
88  void AveragerPairWeighted::reset(void)
89  {
90    x_.reset(); y_.reset(); wxy_=0; w_=0;
91  }
92
93
94  double AveragerPairWeighted::sum_w(void) const
95  {
96    return w_;
97  }
98
99
100  double AveragerPairWeighted::sum_xy(void) const
101  {
102    return wxy_;
103  }
104
105
106  double AveragerPairWeighted::sum_xy_centered(void) const
107  {
108    return sum_xy() - x_.sum_wx()*y_.mean();
109  }
110
111
112  const AveragerWeighted& AveragerPairWeighted::x_averager(void) const
113  {
114    return x_;
115  }
116
117
118  const AveragerWeighted& AveragerPairWeighted::y_averager(void) const
119  {
120    return y_;
121  }
122
123}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.