yat/statistics/Pearson.cc

Code
Comments
Other
Rev Date Author Line
139 20 Aug 04 peter 1 // $Id$
139 20 Aug 04 peter 2
675 10 Oct 06 jari 3 /*
831 27 Mar 07 peter 4   Copyright (C) 2004, 2005 Peter Johansson
4359 23 Aug 23 peter 5   Copyright (C) 2006 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 6   Copyright (C) 2007 Peter Johansson
4359 23 Aug 23 peter 7   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 8   Copyright (C) 2012 Peter Johansson
295 29 Apr 05 peter 9
1437 25 Aug 08 peter 10   This file is part of the yat library, http://dev.thep.lu.se/yat
675 10 Oct 06 jari 11
675 10 Oct 06 jari 12   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 13   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 14   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 15   License, or (at your option) any later version.
675 10 Oct 06 jari 16
675 10 Oct 06 jari 17   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 18   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 20   General Public License for more details.
675 10 Oct 06 jari 21
675 10 Oct 06 jari 22   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 23   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 24 */
675 10 Oct 06 jari 25
2881 18 Nov 12 peter 26 #include <config.h>
2881 18 Nov 12 peter 27
680 11 Oct 06 jari 28 #include "Pearson.h"
680 11 Oct 06 jari 29 #include "AveragerPair.h"
680 11 Oct 06 jari 30 #include "AveragerPairWeighted.h"
1023 01 Feb 08 peter 31 #include "yat/utility/VectorBase.h"
675 10 Oct 06 jari 32 #include "yat/classifier/DataLookupWeighted1D.h"
675 10 Oct 06 jari 33 #include "yat/classifier/Target.h"
675 10 Oct 06 jari 34
139 20 Aug 04 peter 35 #include <cmath>
139 20 Aug 04 peter 36
139 20 Aug 04 peter 37 namespace theplu {
680 11 Oct 06 jari 38 namespace yat {
4200 19 Aug 22 peter 39 namespace statistics {
139 20 Aug 04 peter 40
4200 19 Aug 22 peter 41   Pearson::Pearson(bool b)
779 05 Mar 07 peter 42     : Score(b)
139 20 Aug 04 peter 43   {
139 20 Aug 04 peter 44   }
139 20 Aug 04 peter 45
703 18 Dec 06 jari 46   Pearson::~Pearson(void)
703 18 Dec 06 jari 47   {
703 18 Dec 06 jari 48   }
703 18 Dec 06 jari 49
4200 19 Aug 22 peter 50   double Pearson::score(const classifier::Target& target,
1023 01 Feb 08 peter 51                         const utility::VectorBase& value) const
139 20 Aug 04 peter 52   {
295 29 Apr 05 peter 53     AveragerPair ap;
475 22 Dec 05 peter 54     for (size_t i=0; i<target.size(); i++){
514 20 Feb 06 peter 55       if (target.binary(i))
475 22 Dec 05 peter 56         ap.add(1, value(i));
475 22 Dec 05 peter 57       else
475 22 Dec 05 peter 58         ap.add(-1, value(i));
139 20 Aug 04 peter 59     }
779 05 Mar 07 peter 60     double r = ap.correlation();
779 05 Mar 07 peter 61     if (r<0 && absolute_)
779 05 Mar 07 peter 62       return -r;
779 05 Mar 07 peter 63     return r;
4200 19 Aug 22 peter 64   }
4200 19 Aug 22 peter 65
4200 19 Aug 22 peter 66   double Pearson::score(const classifier::Target& target,
779 05 Mar 07 peter 67                         const classifier::DataLookupWeighted1D& value) const
623 05 Sep 06 peter 68   {
623 05 Sep 06 peter 69     AveragerPairWeighted ap;
623 05 Sep 06 peter 70     for (size_t i=0; i<target.size(); i++){
623 05 Sep 06 peter 71       if (target.binary(i))
623 05 Sep 06 peter 72         ap.add(1, value.data(i),1,value.weight(i));
623 05 Sep 06 peter 73       else
623 05 Sep 06 peter 74         ap.add(-1, value.data(i),1,value.weight(i));
623 05 Sep 06 peter 75     }
779 05 Mar 07 peter 76     double r = ap.correlation();
779 05 Mar 07 peter 77     if (r<0 && absolute_)
779 05 Mar 07 peter 78       return -r;
779 05 Mar 07 peter 79     return r;
623 05 Sep 06 peter 80   }
623 05 Sep 06 peter 81
4200 19 Aug 22 peter 82   double Pearson::score(const classifier::Target& target,
1023 01 Feb 08 peter 83                         const utility::VectorBase& value,
1023 01 Feb 08 peter 84                         const utility::VectorBase& weight) const
139 20 Aug 04 peter 85   {
475 22 Dec 05 peter 86     AveragerPairWeighted ap;
475 22 Dec 05 peter 87     for (size_t i=0; i<target.size(); i++){
514 20 Feb 06 peter 88       if (target.binary(i))
475 22 Dec 05 peter 89         ap.add(1, value(i),1,weight(i));
475 22 Dec 05 peter 90       else
475 22 Dec 05 peter 91         ap.add(-1, value(i),1,weight(i));
4200 19 Aug 22 peter 92     }
779 05 Mar 07 peter 93     double r = ap.correlation();
779 05 Mar 07 peter 94     if (r<0 && absolute_)
779 05 Mar 07 peter 95       return -r;
779 05 Mar 07 peter 96     return r;
139 20 Aug 04 peter 97   }
139 20 Aug 04 peter 98
683 11 Oct 06 jari 99 }}} // of namespace statistics, yat, and theplu