1 | // $Id: score_test.cc 865 2007-09-10 19:41:04Z 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://trac.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/SNRScore.h" |
---|
30 | #include "yat/statistics/tScore.h" |
---|
31 | #include "yat/statistics/WilcoxonFoldChange.h" |
---|
32 | #include "yat/utility/matrix.h" |
---|
33 | #include "yat/utility/vector.h" |
---|
34 | |
---|
35 | #include <cmath> |
---|
36 | #include <fstream> |
---|
37 | #include <iostream> |
---|
38 | |
---|
39 | |
---|
40 | using namespace theplu::yat; |
---|
41 | |
---|
42 | int main(const int argc,const char* argv[]) |
---|
43 | { |
---|
44 | std::ostream* error; |
---|
45 | if (argc>1 && argv[1]==std::string("-v")) |
---|
46 | error = &std::cerr; |
---|
47 | else { |
---|
48 | error = new std::ofstream("/dev/null"); |
---|
49 | if (argc>1) |
---|
50 | std::cout << "score_test -v : for printing extra information\n"; |
---|
51 | } |
---|
52 | *error << "testing score classes" << std::endl; |
---|
53 | bool ok = true; |
---|
54 | |
---|
55 | *error << "testing AUC" << std::endl; |
---|
56 | utility::vector value(31); |
---|
57 | std::vector<std::string> label(31,"negative"); |
---|
58 | for (size_t i=0; i<16; i++) |
---|
59 | label[i] = "positive"; |
---|
60 | classifier::Target target(label); |
---|
61 | for (size_t i=0; i<value.size(); i++) |
---|
62 | value(i)=i; |
---|
63 | statistics::AUC auc(false); |
---|
64 | double area = auc.score(target, value); |
---|
65 | if (area!=0.0){ |
---|
66 | *error << "test_auc: area is " << area << " should be 0.0" |
---|
67 | << std::endl; |
---|
68 | ok = false; |
---|
69 | } |
---|
70 | target.set_binary(0,false); |
---|
71 | target.set_binary(1,true); |
---|
72 | area = auc.score(target, value); |
---|
73 | if (area!=1.0){ |
---|
74 | *error << "test_auc: area is " << area << " should be 1.0" |
---|
75 | << std::endl; |
---|
76 | ok = false; |
---|
77 | } |
---|
78 | |
---|
79 | std::ifstream is("data/rank_data.txt"); |
---|
80 | utility::matrix data(is); |
---|
81 | is.close(); |
---|
82 | |
---|
83 | is.open("data/rank_target.txt"); |
---|
84 | classifier::Target target2(is); |
---|
85 | is.close(); |
---|
86 | |
---|
87 | utility::vector correct_area(3); |
---|
88 | correct_area(0)=1.0/9.0; |
---|
89 | correct_area(1)=3.0/9.0; |
---|
90 | correct_area(2)=0.0; |
---|
91 | |
---|
92 | const double tol = 0.001; |
---|
93 | for (size_t i=0; i<data.rows(); i++){ |
---|
94 | utility::vector vec(data,i); |
---|
95 | if (vec.size()!=target2.size()){ |
---|
96 | *error << "vec.size() is " << vec.size() << " and target2.size() is " |
---|
97 | << target2.size() << ". Should be equal." << std::endl; |
---|
98 | ok=false; |
---|
99 | } |
---|
100 | area = auc.score(target2,vec); |
---|
101 | if (area<correct_area(i)-tol || area>correct_area(i)+tol){ |
---|
102 | *error << "test_roc: area is " << area << " should be " |
---|
103 | << correct_area(i) << std::endl; |
---|
104 | ok=false; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | utility::vector weight(target2.size(),1); |
---|
109 | for (size_t i=0; i<data.rows(); i++){ |
---|
110 | utility::vector vec(data,i); |
---|
111 | area = auc.score(target2, vec, weight); |
---|
112 | if (area<correct_area(i)-tol || area>correct_area(i)+tol){ |
---|
113 | *error << "test_roc: weighted area is " << area << " should be " |
---|
114 | << correct_area(i) << std::endl; |
---|
115 | ok=false; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | *error << "testing FoldChange" << std::endl; |
---|
120 | statistics::FoldChange fold_change(true); |
---|
121 | |
---|
122 | *error << "testing tScore" << std::endl; |
---|
123 | statistics::tScore t_score(true); |
---|
124 | |
---|
125 | *error << "testing Pearson" << std::endl; |
---|
126 | statistics::Pearson pearson(true); |
---|
127 | |
---|
128 | *error << "testing WilcoxonFoldChange" << std::endl; |
---|
129 | statistics::WilcoxonFoldChange wfc(true); |
---|
130 | |
---|
131 | *error << "testing SAMScore" << std::endl; |
---|
132 | statistics::SAMScore sam(1.0,true); |
---|
133 | |
---|
134 | *error << "testing SNRScore" << std::endl; |
---|
135 | statistics::SNRScore snr(true); |
---|
136 | |
---|
137 | |
---|
138 | if (ok) |
---|
139 | return 0; |
---|
140 | return -1; |
---|
141 | } |
---|