source: trunk/test/score_test.cc @ 831

Last change on this file since 831 was 831, checked in by Peter, 15 years ago

Refs #185.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1// $Id: score_test.cc 831 2007-03-27 13:22:09Z peter $
2
3/*
4  Copyright (C) 2004, 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
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/AUC.h"
28#include "yat/statistics/SAMScore.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 AUC" << 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::AUC auc(false);
63  double area = auc.score(target, value);
64  if (area!=0.0){
65    *error << "test_auc: area is " << area << " should be 0.0" 
66           << std::endl;
67    ok = false;
68  }
69  target.set_binary(0,false);
70  target.set_binary(1,true);
71  area = auc.score(target, value);
72  if (area!=1.0){
73    *error << "test_auc: area is " << area << " should be 1.0" 
74           << std::endl;
75    ok = false;
76  }
77 
78  std::ifstream is("data/rank_data.txt");
79  utility::matrix data(is);
80  is.close();
81
82  is.open("data/rank_target.txt");
83  classifier::Target target2(is);
84  is.close();
85 
86  utility::vector correct_area(3);
87  correct_area(0)=1.0/9.0;
88  correct_area(1)=3.0/9.0;
89  correct_area(2)=0.0;
90
91  const double tol = 0.001;
92  for (size_t i=0; i<data.rows(); i++){
93    utility::vector vec(data,i);
94    if (vec.size()!=target2.size()){
95      *error << "vec.size() is " << vec.size() << " and target2.size() is " 
96             << target2.size() << ". Should be equal." << std::endl; 
97      ok=false;
98    }
99    area = auc.score(target2,vec);
100    if (area<correct_area(i)-tol || area>correct_area(i)+tol){
101      *error << "test_roc: area is " << area << " should be " 
102             << correct_area(i) << std::endl;
103      ok=false;
104    }
105  }
106
107  utility::vector weight(target2.size(),1);
108  for (size_t i=0; i<data.rows(); i++){
109    utility::vector vec(data,i);
110    area = auc.score(target2, vec, weight);
111    if (area<correct_area(i)-tol || area>correct_area(i)+tol){
112      *error << "test_roc: weighted area is " << area << " should be " 
113             << correct_area(i) << std::endl;
114      ok=false;
115    }
116  }
117
118  *error << "testing FoldChange" << std::endl;
119  statistics::FoldChange fold_change(true);
120
121  *error << "testing tScore" << std::endl;
122  statistics::tScore t_score(true);
123
124  *error << "testing Pearson" << std::endl;
125  statistics::Pearson pearson(true);
126
127  *error << "testing WilcoxonFoldChange" << std::endl;
128  statistics::WilcoxonFoldChange wfc(true);
129
130  *error << "testing SAMScore" << std::endl;
131  statistics::SAMScore sam(1.0,true);
132
133
134  if (ok)
135    return 0;
136  return -1;
137}
Note: See TracBrowser for help on using the repository browser.