1 | // $Id: roc_test.cc 779 2007-03-05 18:58:30Z 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/ROC.h" |
---|
26 | #include "yat/utility/matrix.h" |
---|
27 | #include "yat/utility/vector.h" |
---|
28 | |
---|
29 | #include <cmath> |
---|
30 | #include <fstream> |
---|
31 | #include <iostream> |
---|
32 | |
---|
33 | |
---|
34 | using namespace theplu::yat; |
---|
35 | |
---|
36 | int main(const int argc,const char* argv[]) |
---|
37 | { |
---|
38 | std::ostream* error; |
---|
39 | if (argc>1 && argv[1]==std::string("-v")) |
---|
40 | error = &std::cerr; |
---|
41 | else { |
---|
42 | error = new std::ofstream("/dev/null"); |
---|
43 | if (argc>1) |
---|
44 | std::cout << "roc_test -v : for printing extra information\n"; |
---|
45 | } |
---|
46 | bool ok = true; |
---|
47 | |
---|
48 | *error << "testing ROC" << std::endl; |
---|
49 | utility::vector value(31); |
---|
50 | std::vector<std::string> label(31,"negative"); |
---|
51 | for (size_t i=0; i<16; i++) |
---|
52 | label[i] = "positive"; |
---|
53 | classifier::Target target(label); |
---|
54 | for (size_t i=0; i<value.size(); i++) |
---|
55 | value(i)=i; |
---|
56 | statistics::ROC roc; |
---|
57 | double area = roc.score(target, value); |
---|
58 | if (area!=0.0){ |
---|
59 | *error << "test_roc: area is " << area << " should be 0.0" |
---|
60 | << std::endl; |
---|
61 | ok = false; |
---|
62 | } |
---|
63 | target.set_binary(0,false); |
---|
64 | target.set_binary(1,true); |
---|
65 | area = roc.score(target, value); |
---|
66 | if (area!=1.0){ |
---|
67 | *error << "test_roc: area is " << area << " should be 1.0" |
---|
68 | << std::endl; |
---|
69 | ok = false; |
---|
70 | } |
---|
71 | |
---|
72 | double p = roc.p_value(); |
---|
73 | double p_matlab = 0.00000115; |
---|
74 | if (p/p_matlab > 1.01 | p/p_matlab < 0.99){ |
---|
75 | *error << "get_p_approx: p-value not correct" << std::endl; |
---|
76 | ok = false; |
---|
77 | } |
---|
78 | roc.minimum_size() = 20; |
---|
79 | p = roc.p_value(); |
---|
80 | if (p > pow(10, -8.0) | p < pow(10, -9.0)){ |
---|
81 | *error << "get_p_exact: p-value not correct" << std::endl; |
---|
82 | ok = false; |
---|
83 | } |
---|
84 | |
---|
85 | if (ok) |
---|
86 | return 0; |
---|
87 | return -1; |
---|
88 | } |
---|