SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ROCEvaluation.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2011 Sergey Lisitsyn
8  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
9  */
10 
13 
14 using namespace shogun;
15 
17 {
18 }
19 
21 {
22  return evaluate_roc(predicted,ground_truth);
23 }
24 
26 {
27  ASSERT(predicted && ground_truth);
28  ASSERT(predicted->get_num_labels()==ground_truth->get_num_labels());
29  ASSERT(predicted->get_label_type()==LT_BINARY);
30  ASSERT(ground_truth->get_label_type()==LT_BINARY);
31  ground_truth->ensure_valid();
32 
33  // assume threshold as negative infinity
35  // false positive rate
36  float64_t fp = 0.0;
37  // true positive rate
38  float64_t tp=0.0;
39 
40  int32_t i;
41  // total number of positive labels in predicted
42  int32_t pos_count=0;
43  int32_t neg_count=0;
44 
45  // initialize number of labels and labels
46  SGVector<float64_t> orig_labels(predicted->get_num_labels());
47  int32_t length = orig_labels.vlen;
48  for (i=0; i<length; i++)
49  orig_labels[i] = predicted->get_confidence(i);
50  float64_t* labels = SGVector<float64_t>::clone_vector(orig_labels.vector, length);
51 
52  // get sorted indexes
53  int32_t* idxs = SG_MALLOC(int32_t, length);
54  for(i=0; i<length; i++)
55  idxs[i] = i;
56 
57  CMath::qsort_backward_index(labels,idxs,length);
58 
59  // number of different predicted labels
60  int32_t diff_count=1;
61 
62  // get number of different labels
63  for (i=0; i<length-1; i++)
64  {
65  if (labels[i] != labels[i+1])
66  diff_count++;
67  }
68 
69  SG_FREE(labels);
70 
71  // initialize graph and auROC
72  m_ROC_graph = SGMatrix<float64_t>(2,diff_count+1);
74  m_auROC = 0.0;
75 
76  // get total numbers of positive and negative labels
77  for(i=0; i<length; i++)
78  {
79  if (ground_truth->get_confidence(i) >= 0)
80  pos_count++;
81  else
82  neg_count++;
83  }
84 
85  // assure both number of positive and negative examples is >0
86  ASSERT(pos_count>0 && neg_count>0);
87 
88  int32_t j = 0;
89  float64_t label;
90 
91  // create ROC curve and calculate auROC
92  for(i=0; i<length; i++)
93  {
94  label = predicted->get_confidence(idxs[i]);
95 
96  if (label != threshold)
97  {
98  threshold = label;
99  m_ROC_graph[2*j] = fp/neg_count;
100  m_ROC_graph[2*j+1] = tp/pos_count;
101  j++;
102  }
103 
104  m_thresholds[i]=threshold;
105 
106  if (ground_truth->get_confidence(idxs[i]) > 0)
107  tp+=1.0;
108  else
109  fp+=1.0;
110  }
111 
112  // add (1,1) to ROC curve
113  m_ROC_graph[2*diff_count] = 1.0;
114  m_ROC_graph[2*diff_count+1] = 1.0;
115 
116  // calc auROC using area under curve
117  m_auROC = CMath::area_under_curve(m_ROC_graph.matrix,diff_count+1,false);
118 
119  m_computed = true;
120 
121  return m_auROC;
122 }
123 
125 {
126  if (!m_computed)
127  SG_ERROR("Uninitialized, please call evaluate first");
128 
129  return m_ROC_graph;
130 }
131 
133 {
134  if (!m_computed)
135  SG_ERROR("Uninitialized, please call evaluate first");
136 
137  return m_thresholds;
138 }
139 
141 {
142  if (!m_computed)
143  SG_ERROR("Uninitialized, please call evaluate first");
144 
145  return m_auROC;
146 }

SHOGUN Machine Learning Toolbox - Documentation