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

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

Sorry this commit is a bit to big.

Adding a yat_assert. The yat assert are turned on by providing a
'-DYAT_DEBUG' flag to preprocessor if normal cassert is turned
on. This flag is activated for developers running configure with
--enable-debug. The motivation is that we can use these yat_asserts in
header files and the yat_asserts will be invisible to the normal user
also if he uses C-asserts.

added output operator in DataLookup2D and removed output operator in
MatrixLookup?

Removed template function add_values in Averager and weighted version

Added function to AveragerWeighted? taking iterator to four ranges.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: AveragerPairWeighted.cc 916 2007-09-30 00:50:10Z 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/trac/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  void AveragerPairWeighted::add(const classifier::DataLookup1D& x, 
64                                 const classifier::DataLookupWeighted1D& y)
65  {
66    assert(x.size()==y.size());
67    for (size_t i=0; i<x.size(); ++i)
68      add(x(i), y.data(i), 1.0, y.weight(i));
69  }
70
71
72  void AveragerPairWeighted::add(const classifier::DataLookupWeighted1D& x, 
73                                 const classifier::DataLookup1D& y)
74  {
75    add(y,x);
76  }
77
78
79  void AveragerPairWeighted::add(const classifier::DataLookupWeighted1D& x, 
80                                 const classifier::DataLookupWeighted1D& y)
81  {
82    assert(x.size()==y.size());
83    for (size_t i=0; i<x.size(); ++i)
84      add(x.data(i), y.data(i), x.weight(i), y.weight(i));
85  }
86
87
88  double AveragerPairWeighted::correlation(void) const
89  {
90    return ( x_.variance()>0 && y_.variance()>0 ? 
91             covariance() / sqrt(x_.variance()*y_.variance()) : 0 );
92  }
93
94
95  double AveragerPairWeighted::covariance(void) const
96  {
97    return sum_xy_centered()/sum_w();
98  }
99
100
101  double AveragerPairWeighted::msd(void) const
102  {
103    return (x_.sum_wxx()+y_.sum_wxx()-2*wxy_)/w_;
104  }
105
106
107  double AveragerPairWeighted::n(void) const
108  {
109    return x_.n();
110  }
111
112
113  void AveragerPairWeighted::reset(void)
114  {
115    x_.reset(); y_.reset(); wxy_=0; w_=0;
116  }
117
118
119  double AveragerPairWeighted::sum_w(void) const
120  {
121    return w_;
122  }
123
124
125  double AveragerPairWeighted::sum_xy(void) const
126  {
127    return wxy_;
128  }
129
130
131  double AveragerPairWeighted::sum_xy_centered(void) const
132  {
133    return sum_xy() - x_.sum_wx()*y_.mean();
134  }
135
136
137  const AveragerWeighted& AveragerPairWeighted::x_averager(void) const
138  {
139    return x_;
140  }
141
142
143  const AveragerWeighted& AveragerPairWeighted::y_averager(void) const
144  {
145    return y_;
146  }
147
148}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.