source: trunk/yat/classifier/IGP.h @ 1874

Last change on this file since 1874 was 1874, checked in by Peter, 14 years ago

merge patch release 0.5.2 into trunk. Delta 0.5.2 - 0.5.1

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.5 KB
Line 
1#ifndef _theplu_yat_classifier_igp_
2#define _theplu_yat_classifier_igp_
3
4// $Id$
5
6/*
7  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér
8  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2009 Peter Johansson
10
11  This file is part of the yat library, http://dev.thep.lu.se/yat
12
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 3 of the
16  License, or (at your option) any later version.
17
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with yat. If not, see <http://www.gnu.org/licenses/>.
25*/
26
27#include "MatrixLookup.h"
28#include "Target.h"
29#include "yat/utility/Vector.h"
30#include "yat/utility/yat_assert.h"
31
32#include <cmath>
33#include <limits>
34#include <stdexcept>
35
36namespace theplu {
37namespace yat {
38namespace classifier { 
39
40  class Target;
41  class MatrixLookup;
42
43  ///
44  /// @brief Class for In Group Proportions (IGP)
45  ///
46  /// See <a HREF="http://biostatistics.oxfordjournals.org/cgi/content/abstract/kxj029v1">Kapp and Tibshirani, Biostatistics (2006)</a>.
47  ///
48  template <typename Distance>
49  class IGP
50  {
51 
52  public:
53    ///
54    /// Constructor taking the training data and the target vector and
55    /// as input.
56    ///
57    IGP(const MatrixLookup&, const Target&);
58
59
60    ///
61    /// Constructor taking the training data, the target vector and
62    /// the distance measure as input.
63    ///
64    IGP(const MatrixLookup&, const Target&, const Distance&);
65
66    ///
67    /// Destrucutor
68    ///
69    virtual ~IGP();
70
71    ///
72    /// @return the IGP score for each class as elements in a vector.
73    ///
74    const utility::Vector& score(void) const;
75
76
77  private:
78    void calculate();
79
80    utility::Vector igp_;
81    Distance distance_;
82
83    const MatrixLookup& matrix_;
84    const Target& target_;
85  }; 
86
87 
88  // templates
89
90  template <typename Distance>
91  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target) 
92    : matrix_(data), target_(target)
93  {   
94    calculate();
95  }
96
97  template <typename Distance>
98  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target, const Distance& dist) 
99    : matrix_(data), target_(target), distance_(dist)
100  {   
101    calculate();
102  }
103
104 
105  template <typename Distance>
106  IGP<Distance>::~IGP()   
107  {
108  }
109
110  template <typename Distance>
111  void IGP<Distance>::calculate() 
112  {
113    utility::yat_assert<std::runtime_error>(target_.size()==matrix_.columns());
114   
115    // Calculate IGP for each class
116    igp_ = utility::Vector(target_.nof_classes());
117   
118    for(size_t i=0; i<target_.size(); i++) {
119      size_t neighbor=i;
120      double mindist=std::numeric_limits<double>::max();
121      for(size_t j=0; j<target_.size(); j++) {           
122        if (i==j) // avoid self-self comparison
123          continue;
124        double dist = distance_(matrix_.begin_column(i), matrix_.end_column(i), 
125                                matrix_.begin_column(j));
126        if(dist<mindist) {
127          mindist=dist;
128          neighbor=j;
129        }
130      }
131      if(target_(i)==target_(neighbor))
132        igp_(target_(i))++;
133     
134    }
135    for(size_t i=0; i<target_.nof_classes(); i++) {
136      igp_(i)/=static_cast<double>(target_.size(i));
137    }
138  }
139
140 
141  template <typename Distance>
142  const utility::Vector& IGP<Distance>::score(void) const 
143  {
144    return igp_;
145  }
146 
147}}} // of namespace classifier, yat, and theplu
148
149#endif
Note: See TracBrowser for help on using the repository browser.