source: branches/0.7-stable/test/roc.cc @ 2548

Last change on this file since 2548 was 2548, checked in by Peter, 12 years ago

avoid crash. return nan when area is nan. fixes #669

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1// $Id: roc.cc 2548 2011-08-07 16:36:44Z peter $
2
3/*
4  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2011 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Suite.h"
24
25#include "yat/classifier/DataLookupWeighted1D.h"
26#include "yat/classifier/Target.h"
27#include "yat/statistics/ROC.h"
28#include "yat/statistics/utility.h"
29#include "yat/utility/Vector.h"
30
31#include <cmath>
32#include <fstream>
33#include <iostream>
34
35
36using namespace theplu::yat;
37
38void test_empty(test::Suite&);
39
40int main(int argc, char* argv[])
41{ 
42  test::Suite suite(argc, argv);
43
44  suite.err() << "testing ROC" << std::endl;
45  utility::Vector value(31);
46  std::vector<std::string> label(31,"negative");
47  for (size_t i=0; i<16; i++) 
48    label[i] = "positive";
49  classifier::Target target(label);
50  for (size_t i=0; i<value.size(); i++) 
51    value(i)=i;
52  statistics::ROC roc;
53  add(roc, value.begin(), value.end(), target);
54  double area = roc.area();
55  if (!suite.equal(area,0.0)){
56    suite.err() << "test_roc: area is " << area << " should be 0.0" 
57           << std::endl;
58    suite.add(false);
59  }
60  target.set_binary(0,false);
61  target.set_binary(1,true);
62  roc.reset();
63  add(roc, value.begin(), value.end(), target);
64  area = roc.area();
65  if (!suite.equal(area,1.0)){
66    suite.err() << "test_roc: area is " << area << " should be 1.0" 
67           << std::endl;
68    suite.add(false);
69  }
70 
71  double p = roc.p_value_one_sided();
72  double p2 = roc.p_value();
73  double p_matlab = 0.00000115;
74  if (!(p/p_matlab < 1.01 && p/p_matlab > 0.99)){
75    suite.err() << "get_p_approx: p-value not correct" << std::endl;
76    suite.err() << p << " expected " << p_matlab << std::endl;
77    suite.add(false);
78  }
79  if (!(p2==2*p)) {
80    suite.add(false);
81    suite.err() << "Two-sided P-value should equal 2 * one-sided P-value.\n";
82  }
83  roc.minimum_size() = 20;
84  p = roc.p_value_one_sided();
85  p2 = roc.p_value();
86  if (!( p < 1e-8 && p > 1e-9) ){
87    suite.err() << "get_p_exact: p-value not correct" << std::endl;
88    suite.add(false);
89  }
90  if (!( p2==2*p)) {
91    suite.add(false);
92    suite.err() << "Two-sided P-value should equal 2 * one-sided P-value.\n";
93  }
94 
95  classifier::DataLookupWeighted1D dlw(target.size(),1.3);
96  add(roc, dlw.begin(), dlw.end(), target);
97  test_empty(suite);
98
99  return suite.return_value();
100}
101
102void test_empty(test::Suite& suite)
103{
104  suite.out() << "test empty\n";
105  // testing buf #669
106  statistics::ROC roc;
107  roc.p_value();
108  roc.area();
109}
Note: See TracBrowser for help on using the repository browser.