1 | // $Id: roc_test.cc 865 2007-09-10 19:41:04Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007 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/DataLookupWeighted1D.h" |
---|
25 | #include "yat/classifier/Target.h" |
---|
26 | #include "yat/statistics/ROC.h" |
---|
27 | #include "yat/statistics/utility.h" |
---|
28 | #include "yat/utility/matrix.h" |
---|
29 | #include "yat/utility/vector.h" |
---|
30 | |
---|
31 | #include <cmath> |
---|
32 | #include <fstream> |
---|
33 | #include <iostream> |
---|
34 | |
---|
35 | |
---|
36 | using namespace theplu::yat; |
---|
37 | |
---|
38 | int main(const int argc,const char* argv[]) |
---|
39 | { |
---|
40 | std::ostream* error; |
---|
41 | if (argc>1 && argv[1]==std::string("-v")) |
---|
42 | error = &std::cerr; |
---|
43 | else { |
---|
44 | error = new std::ofstream("/dev/null"); |
---|
45 | if (argc>1) |
---|
46 | std::cout << "roc_test -v : for printing extra information\n"; |
---|
47 | } |
---|
48 | bool ok = true; |
---|
49 | |
---|
50 | *error << "testing ROC" << std::endl; |
---|
51 | utility::vector value(31); |
---|
52 | std::vector<std::string> label(31,"negative"); |
---|
53 | for (size_t i=0; i<16; i++) |
---|
54 | label[i] = "positive"; |
---|
55 | classifier::Target target(label); |
---|
56 | for (size_t i=0; i<value.size(); i++) |
---|
57 | value(i)=i; |
---|
58 | statistics::ROC roc; |
---|
59 | add(roc, value, target); |
---|
60 | double area = roc.area(); |
---|
61 | if (area!=0.0){ |
---|
62 | *error << "test_roc: area is " << area << " should be 0.0" |
---|
63 | << std::endl; |
---|
64 | ok = false; |
---|
65 | } |
---|
66 | target.set_binary(0,false); |
---|
67 | target.set_binary(1,true); |
---|
68 | roc.reset(); |
---|
69 | add(roc, value, target); |
---|
70 | area = roc.area(); |
---|
71 | if (area!=1.0){ |
---|
72 | *error << "test_roc: area is " << area << " should be 1.0" |
---|
73 | << std::endl; |
---|
74 | ok = false; |
---|
75 | } |
---|
76 | |
---|
77 | double p = roc.p_value_one_sided(); |
---|
78 | double p2 = 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 | *error << p << " expected " << p_matlab << std::endl; |
---|
83 | ok = false; |
---|
84 | } |
---|
85 | if (p2 != 2*p) { |
---|
86 | ok = false; |
---|
87 | *error << "Two-sided P-value should equal 2 * one-sided P-value.\n"; |
---|
88 | } |
---|
89 | roc.minimum_size() = 20; |
---|
90 | p = roc.p_value_one_sided(); |
---|
91 | p2 = roc.p_value(); |
---|
92 | if (p > pow(10, -8.0) | p < pow(10, -9.0)){ |
---|
93 | *error << "get_p_exact: p-value not correct" << std::endl; |
---|
94 | ok = false; |
---|
95 | } |
---|
96 | if (p2 != 2*p) { |
---|
97 | ok = false; |
---|
98 | *error << "Two-sided P-value should equal 2 * one-sided P-value.\n"; |
---|
99 | } |
---|
100 | |
---|
101 | classifier::DataLookupWeighted1D dlw(target.size(),1.3); |
---|
102 | add(roc, dlw, target); |
---|
103 | |
---|
104 | if (ok) |
---|
105 | return 0; |
---|
106 | return -1; |
---|
107 | } |
---|