source: trunk/yat/statistics/ROCscore.cc @ 778

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

added ROCscore class. refs #101

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1// $Id: ROCscore.cc 778 2007-03-04 13:20:01Z 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 "ROCscore.h"
25#include "yat/classifier/DataLookupWeighted1D.h"
26#include "yat/classifier/Target.h"
27#include "yat/utility/stl_utility.h"
28#include "yat/utility/vector.h"
29
30#include <cmath>
31#include <utility>
32#include <vector>
33
34namespace theplu {
35namespace yat {
36namespace statistics { 
37
38  double ROCscore::score(const classifier::Target& target, 
39                         const utility::vector& value)
40  {
41    assert(target.size()==value.size());
42    weighted_=false;
43
44    std::vector<std::pair<bool, double> > class_value; // class-value-pair
45    class_value.reserve(target.size());
46    for (size_t i=0; i<target.size(); i++)
47      class_value.push_back(std::make_pair(target.binary(i),value(i)));
48
49    std::sort(class_value.begin(),class_value.end(),
50              utility::pair_value_compare<bool, double>());
51    double area = 0;
52    u_int nof_pos =0;
53    for (size_t i=0; i<class_value.size(); i++){
54      if (class_value[i].first){
55        area+=i;
56        ++nof_pos;
57      }
58    }
59
60    // Normalizing the area to [0,1]
61    area = ( (area-nof_pos*(nof_pos-1)/2 ) /
62             (nof_pos*(class_value.size()-nof_pos)) );
63   
64    if (area<0.5 && absolute_)
65      return 1.0-area;
66    return area;
67  }
68
69
70  // Peter, should be possible to do this in NlogN
71  double ROCscore::score(const classifier::Target& target, 
72                    const classifier::DataLookupWeighted1D& value)
73  {
74
75    std::vector<std::pair<bool, double> > class_value; // class-value-pair
76    class_value.clear();
77    class_value.reserve(target.size());
78    for (unsigned int i=0; i<target.size(); i++)
79      if (value.weight(i))
80        class_value.push_back(std::make_pair(target.binary(i),value.data(i)));
81
82    std::sort(class_value.begin(),class_value.end(),
83              utility::pair_value_compare<int, double>());
84
85    double area=0;
86    double max_area=0;
87
88    for (size_t i=0; i<n(); i++)
89      if (target.binary(i))
90        for (size_t j=0; j<n(); j++)
91          if (!target.binary(j)){
92            if (value.data(i)>value.data(j))
93              area+=value.weight(i)*value.weight(j);
94            max_area+=value.weight(i)*value.weight(j);
95          }
96   
97    area/=max_area;
98   
99    if (area<0.5 && absolute_)
100      return 1.0-area;
101    return area;
102  }
103
104
105  // Peter, should be possible to do this in NlogN
106  double ROCscore::score(const classifier::Target& target, 
107                         const utility::vector& value, 
108                         const utility::vector& weight)
109  {
110    weighted_=true;
111
112
113    std::vector<std::pair<bool, double> > class_value; // class-value-pair
114    class_value.reserve(target.size());
115    for (unsigned int i=0; i<target.size(); i++)
116      if (weight(i))
117        class_value.push_back(std::make_pair(target.binary(i),value(i)));
118
119    std::sort(class_value.begin(),class_value.end(),
120              utility::pair_value_compare<int, double>());
121
122    double area=0;
123    double max_area=0;
124
125    for (size_t i=0; i<n(); i++)
126      if (target.binary(i))
127        for (size_t j=0; j<n(); j++)
128          if (!target.binary(j)){
129            if (value(i)>value(j))
130              area+=weight(i)*weight(j);
131            max_area+=weight(i)*weight(j);
132          }
133   
134    area/=max_area;
135   
136    if (area<0.5 && absolute_)
137      return 1.0-area;
138    return area;
139  }
140
141}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.