source: trunk/yat/statistics/Pearson.cc

Last change on this file was 2992, checked in by Peter, 9 years ago

set svndigest:ignore proprty to reflect when files were initially copied from other file (and early history should be ignored). closes #750

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svndigest:ignore set to 776
File size: 2.4 KB
Line 
1// $Id: Pearson.cc 2992 2013-03-03 05:03:44Z peter $
2
3/*
4  Copyright (C) 2004, 2005 Peter Johansson
5  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2012 Peter Johansson
7
8  This file is part of the yat library, http://dev.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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include <config.h>
25
26#include "Pearson.h"
27#include "AveragerPair.h"
28#include "AveragerPairWeighted.h"
29#include "yat/utility/VectorBase.h"
30#include "yat/classifier/DataLookupWeighted1D.h"
31#include "yat/classifier/Target.h"
32
33#include <cmath>
34
35namespace theplu {
36namespace yat {
37namespace statistics { 
38
39  Pearson::Pearson(bool b) 
40    : Score(b)
41  {
42  }
43
44  Pearson::~Pearson(void)
45  {
46  }
47
48  double Pearson::score(const classifier::Target& target, 
49                        const utility::VectorBase& value) const
50  {
51    AveragerPair ap;
52    for (size_t i=0; i<target.size(); i++){
53      if (target.binary(i))
54        ap.add(1, value(i));
55      else
56        ap.add(-1, value(i));
57    }
58    double r = ap.correlation();
59    if (r<0 && absolute_)
60      return -r;
61    return r;
62  } 
63   
64  double Pearson::score(const classifier::Target& target, 
65                        const classifier::DataLookupWeighted1D& value) const
66  {
67    AveragerPairWeighted ap;
68    for (size_t i=0; i<target.size(); i++){
69      if (target.binary(i))
70        ap.add(1, value.data(i),1,value.weight(i));
71      else
72        ap.add(-1, value.data(i),1,value.weight(i));
73    }
74    double r = ap.correlation();
75    if (r<0 && absolute_)
76      return -r;
77    return r;
78  }
79
80  double Pearson::score(const classifier::Target& target, 
81                        const utility::VectorBase& value,
82                        const utility::VectorBase& weight) const
83  {
84    AveragerPairWeighted ap;
85    for (size_t i=0; i<target.size(); i++){
86      if (target.binary(i))
87        ap.add(1, value(i),1,weight(i));
88      else
89        ap.add(-1, value(i),1,weight(i));
90    } 
91    double r = ap.correlation();
92    if (r<0 && absolute_)
93      return -r;
94    return r;
95  }
96
97}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.