source: trunk/test/score_test.cc @ 747

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

replaced includes in header files with forward declarations when possible. Added some includes in cc files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1// $Id: score_test.cc 747 2007-02-11 13:26:41Z peter $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/classifier/Target.h"
25#include "yat/statistics/FoldChange.h"
26#include "yat/statistics/Pearson.h"
27#include "yat/statistics/ROC.h"
28#include "yat/statistics/SAM.h"
29#include "yat/statistics/tScore.h"
30#include "yat/statistics/WilcoxonFoldChange.h"
31#include "yat/utility/matrix.h"
32#include "yat/utility/vector.h"
33
34#include <cmath>
35#include <fstream>
36#include <iostream>
37
38
39using namespace theplu::yat;
40
41int main(const int argc,const char* argv[])
42{ 
43  std::ostream* error;
44  if (argc>1 && argv[1]==std::string("-v"))
45    error = &std::cerr;
46  else {
47    error = new std::ofstream("/dev/null");
48    if (argc>1)
49      std::cout << "score_test -v : for printing extra information\n";
50  }
51  *error << "testing score classes" << std::endl;
52  bool ok = true;
53
54  *error << "testing ROC" << std::endl;
55  utility::vector value(31);
56  std::vector<std::string> label(31,"negative");
57  for (size_t i=0; i<16; i++) 
58    label[i] = "positive";
59  classifier::Target target(label);
60  for (size_t i=0; i<value.size(); i++) 
61    value(i)=i;
62  statistics::ROC roc;
63  double area = roc.score(target, value);
64  if (area!=1.0){
65    *error << "test_roc: area is " << area << " should be 1.0" 
66           << std::endl;
67    ok = false;
68  }
69  target.set_binary(0,false);
70  target.set_binary(1,true);
71  area = roc.score(target, value);
72  if (area!=1.0){
73    *error << "test_roc: area is " << area << " should be 1.0" 
74           << std::endl;
75    ok = false;
76  }
77 
78  double p = roc.p_value();
79  double p_matlab = 0.00000115;
80  if (p/p_matlab > 1.01 | p/p_matlab < 0.99){
81    *error << "get_p_approx: p-value not correct" << std::endl;
82    ok = false;
83  }
84  roc.minimum_size() = 20;
85  p = roc.p_value();
86  if (p > pow(10, -8.0) | p < pow(10, -9.0)){
87    *error << "get_p_exact: p-value not correct" << std::endl;
88    ok = false;
89  }
90 
91  std::ifstream is("data/rank_data.txt");
92  utility::matrix data(is);
93  is.close();
94
95  is.open("data/rank_target.txt");
96  classifier::Target target2(is);
97  is.close();
98 
99  utility::vector correct_area(3);
100  correct_area(0)=8.0/9.0;
101  correct_area(1)=6.0/9.0;
102  correct_area(2)=1.0;
103
104  const double tol = 0.001;
105  for (size_t i=0; i<data.rows(); i++){
106    utility::vector vec(data,i);
107    if (vec.size()!=target2.size()){
108      *error << "vec.size() is " << vec.size() << " and target2.size() is " 
109             << target2.size() << ". Should be equal." << std::endl; 
110      ok=false;
111    }
112    area = roc.score(target2,vec);
113    if (area<correct_area(i)-tol || area>correct_area(i)+tol){
114      *error << "test_roc: area is " << area << " should be " 
115             << correct_area(i) << std::endl;
116      ok=false;
117    }
118  }
119
120  utility::vector weight(target2.size(),1);
121  for (size_t i=0; i<data.rows(); i++){
122    utility::vector vec(data,i);
123    area = roc.score(target2, vec, weight);
124    if (area<correct_area(i)-tol || area>correct_area(i)+tol){
125      *error << "test_roc: weighted area is " << area << " should be " 
126             << correct_area(i) << std::endl;
127      ok=false;
128    }
129  }
130
131  *error << "testing FoldChange" << std::endl;
132  statistics::FoldChange fold_change(true);
133
134  *error << "testing tScore" << std::endl;
135  statistics::tScore t_score(true);
136
137  *error << "testing Pearson" << std::endl;
138  statistics::Pearson pearson(true);
139
140  *error << "testing WilcoxonFoldChange" << std::endl;
141  statistics::WilcoxonFoldChange wfc(true);
142
143  *error << "testing SAM" << std::endl;
144  statistics::SAM sam(1.0,true);
145
146
147  if (ok)
148    return 0;
149  return -1;
150}
Note: See TracBrowser for help on using the repository browser.