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