source: trunk/test/roc_test.cc @ 821

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

Modified ROC class to use AUC class in calculation of ROC area. Refs #101

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: roc_test.cc 821 2007-03-18 16:00:05Z 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
34using namespace theplu::yat;
35
36int 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  for (size_t i=0; i<value.size(); ++i){
58    roc.add(value(i), target.binary(i));
59  }
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  for (size_t i=0; i<value.size(); ++i)
70    roc.add(value(i), target.binary(i));
71  area = roc.area();
72  if (area!=1.0){
73    *error << "test_roc: area is " << area << " should be 1.0" 
74           << std::endl;
75    ok = false;
76  }
77 
78  double p = roc.p_value_one_sided();
79  double p2 = roc.p_value();
80  double p_matlab = 0.00000115;
81  if (p/p_matlab > 1.01 | p/p_matlab < 0.99){
82    *error << "get_p_approx: p-value not correct" << std::endl;
83    *error << p << " expected " << p_matlab << std::endl;
84    ok = false;
85  }
86  if (p2 != 2*p) {
87    ok = false;
88    *error << "Two-sided P-value should equal 2 * one-sided P-value.\n";
89  }
90  roc.minimum_size() = 20;
91  p = roc.p_value_one_sided();
92  p2 = roc.p_value();
93  if (p > pow(10, -8.0) | p < pow(10, -9.0)){
94    *error << "get_p_exact: p-value not correct" << std::endl;
95    ok = false;
96  }
97  if (p2 != 2*p) {
98    ok = false;
99    *error << "Two-sided P-value should equal 2 * one-sided P-value.\n";
100  }
101 
102  if (ok)
103    return 0;
104  return -1;
105}
Note: See TracBrowser for help on using the repository browser.