source: trunk/lib/statistics/Pearson.cc @ 514

Last change on this file since 514 was 514, checked in by Peter, 17 years ago

generalised binary functionality in Target

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.6 KB
Line 
1// $Id: Pearson.cc 514 2006-02-20 09:45:34Z peter $
2
3
4#include <c++_tools/statistics/Pearson.h>
5#include <c++_tools/statistics/AveragerPair.h>
6#include <c++_tools/statistics/AveragerPairWeighted.h>
7#include <c++_tools/gslapi/vector.h>
8#include <c++_tools/classifier/Target.h>
9
10#include <cmath>
11#include <gsl/gsl_cdf.h>
12
13
14namespace theplu {
15namespace statistics { 
16
17  Pearson::Pearson(bool b) 
18    : Score(b), r_(0), nof_samples_(0)
19  {
20  }
21
22  double Pearson::p_value() const
23  {
24    if(weighted_)
25      return 1;
26    if(nof_samples_<2){
27      std::cerr << "Warning: Only " << nof_samples_ << "samples. " 
28                << "Need at lest 3.\n";
29      return 1;
30    }
31
32    double t = sqrt(nof_samples_ - 2)*fabs(r_) /sqrt(1-r_*r_);
33    double p = gsl_cdf_tdist_Q(t, nof_samples_ -2 );
34    if (absolute_)
35      return 2*p;
36    if (r_<0)
37      return 1-p;
38    return p;
39
40  }
41
42  double Pearson::score(const classifier::Target& target, 
43                        const gslapi::vector& value)
44  {
45    weighted_=false;
46    AveragerPair ap;
47    for (size_t i=0; i<target.size(); i++){
48      if (target.binary(i))
49        ap.add(1, value(i));
50      else
51        ap.add(-1, value(i));
52      nof_samples_ = target.size();
53    }
54    r_ = ap.correlation();
55    if (r_<0 && absolute_)
56      return -r_;
57     
58    return r_;
59  } 
60   
61  double Pearson::score(const classifier::Target& target, 
62                        const gslapi::vector& value,
63                        const gslapi::vector& weight)
64  {
65    weighted_=true;
66    AveragerPairWeighted ap;
67    for (size_t i=0; i<target.size(); i++){
68      if (target.binary(i))
69        ap.add(1, value(i),1,weight(i));
70      else
71        ap.add(-1, value(i),1,weight(i));
72      nof_samples_ = target.size();
73    }
74    r_ = ap.correlation();
75    if (r_<0 && absolute_)
76      return -r_;
77     
78    return r_;
79  }
80
81}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.