source: trunk/test/score_test.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: 3.2 KB
Line 
1// $Id: score_test.cc 514 2006-02-20 09:45:34Z peter $
2
3#include <c++_tools/gslapi/matrix.h>
4#include <c++_tools/statistics/ROC.h>
5#include <c++_tools/statistics/tScore.h>
6#include <c++_tools/statistics/Pearson.h>
7#include <c++_tools/statistics/FoldChange.h>
8#include <c++_tools/gslapi/vector.h>
9#include <c++_tools/statistics/WilcoxonFoldChange.h>
10
11#include <gsl/gsl_cdf.h>
12#include <cmath>
13#include <fstream>
14#include <iostream>
15
16
17using namespace theplu;
18
19int main(const int argc,const char* argv[])
20{ 
21  std::ostream* error;
22  if (argc>1 && argv[1]==std::string("-v"))
23    error = &std::cerr;
24  else {
25    error = new std::ofstream("/dev/null");
26    if (argc>1)
27      std::cout << "score_test -v : for printing extra information\n";
28  }
29  *error << "testing score classes" << std::endl;
30  bool ok = true;
31
32  *error << "testing ROC" << std::endl;
33  gslapi::vector value(31);
34  std::vector<std::string> label(31,"negative");
35  for (size_t i=0; i<16; i++) 
36    label[i] = "positive";
37  classifier::Target target(label);
38  for (size_t i=0; i<value.size(); i++) 
39    value(i)=i;
40  statistics::ROC roc;
41  double area = roc.score(target, value);
42  if (area!=1.0){
43    *error << "test_roc: area is " << area << " should be 1.0" 
44           << std::endl;
45    ok = false;
46  }
47  target.set_binary(0,false);
48  target.set_binary(1,true);
49  area = roc.score(target, value);
50  if (area!=1.0){
51    *error << "test_roc: area is " << area << " should be 1.0" 
52           << std::endl;
53    ok = false;
54  }
55 
56  double p = roc.p_value();
57  double p_matlab = 0.00000115;
58  if (p/p_matlab > 1.01 | p/p_matlab < 0.99){
59    *error << "get_p_approx: p-value not correct" << std::endl;
60    ok = false;
61  }
62  roc.minimum_size() = 20;
63  p = roc.p_value();
64  if (p > pow(10, -8.0) | p < pow(10, -9.0)){
65    *error << "get_p_exact: p-value not correct" << std::endl;
66    ok = false;
67  }
68 
69  std::ifstream is("data/rank_data.txt");
70  gslapi::matrix data(is);
71  is.close();
72
73  is.open("data/rank_target.txt");
74  classifier::Target target2(is);
75  is.close();
76 
77  gslapi::vector correct_area(3);
78  correct_area(0)=8.0/9.0;
79  correct_area(1)=6.0/9.0;
80  correct_area(2)=1.0;
81
82  const double tol = 0.001;
83  for (size_t i=0; i<data.rows(); i++){
84    gslapi::vector vec(data,i);
85    if (vec.size()!=target2.size()){
86      *error << "vec.size() is " << vec.size() << " and target2.size() is " 
87             << target2.size() << ". Should be equal." << std::endl; 
88      ok=false;
89    }
90    area = roc.score(target2,vec);
91    if (area<correct_area(i)-tol || area>correct_area(i)+tol){
92      *error << "test_roc: area is " << area << " should be " 
93             << correct_area(i) << std::endl;
94      ok=false;
95    }
96  }
97
98  gslapi::vector weight(target2.size(),1);
99  for (size_t i=0; i<data.rows(); i++){
100    gslapi::vector vec(data,i);
101    area = roc.score(target2, vec, weight);
102    if (area<correct_area(i)-tol || area>correct_area(i)+tol){
103      *error << "test_roc: weighted area is " << area << " should be " 
104             << correct_area(i) << std::endl;
105      ok=false;
106    }
107  }
108
109  *error << "testing FoldChange" << std::endl;
110  statistics::FoldChange fold_change(true);
111
112  *error << "testing tScore" << std::endl;
113  statistics::tScore t_score(true);
114
115  *error << "testing Pearson" << std::endl;
116  statistics::Pearson pearson(true);
117
118  *error << "testing WilcoxonFoldChange" << std::endl;
119  statistics::WilcoxonFoldChange wfc(true);
120
121
122  if (ok)
123    return 0;
124  return -1;
125}
Note: See TracBrowser for help on using the repository browser.