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

Last change on this file since 2334 was 2334, checked in by Peter, 11 years ago

fixes #625

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.6 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/concept_check.h"
30#include "yat/utility/Vector.h"
31#include "yat/utility/yat_assert.h"
32
33#include <boost/concept_check.hpp>
34
35#include <cmath>
36#include <limits>
37#include <stdexcept>
38
39namespace theplu {
40namespace yat {
41namespace classifier { 
42
43  class Target;
44  class MatrixLookup;
45
46  ///
47  /// @brief Class for In Group Proportions (IGP)
48  ///
49  /// See <a HREF="http://biostatistics.oxfordjournals.org/cgi/content/abstract/kxj029v1">Kapp and Tibshirani, Biostatistics (2006)</a>.
50  ///
51  template <typename Distance>
52  class IGP
53  {
54 
55  public:
56    ///
57    /// Constructor taking the training data and the target vector and
58    /// as input.
59    ///
60    IGP(const MatrixLookup&, const Target&);
61
62
63    ///
64    /// Constructor taking the training data, the target vector and
65    /// the distance measure as input.
66    ///
67    IGP(const MatrixLookup&, const Target&, const Distance&);
68
69    ///
70    /// Destrucutor
71    ///
72    virtual ~IGP();
73
74    ///
75    /// @return the IGP score for each class as elements in a vector.
76    ///
77    const utility::Vector& score(void) const;
78
79
80  private:
81    void calculate();
82
83    utility::Vector igp_;
84    Distance distance_;
85
86    const MatrixLookup& matrix_;
87    const Target& target_;
88  }; 
89
90 
91  // templates
92
93  template <typename Distance>
94  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target) 
95    : matrix_(data), target_(target)
96  {   
97    BOOST_CONCEPT_ASSERT((utility::DistanceConcept<Distance>));
98    calculate();
99  }
100
101  template <typename Distance>
102  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target, const Distance& dist) 
103    : matrix_(data), target_(target), distance_(dist)
104  {   
105    BOOST_CONCEPT_ASSERT((utility::DistanceConcept<Distance>));
106    calculate();
107  }
108
109 
110  template <typename Distance>
111  IGP<Distance>::~IGP()   
112  {
113  }
114
115  template <typename Distance>
116  void IGP<Distance>::calculate() 
117  {
118    YAT_ASSERT(target_.size()==matrix_.columns());
119   
120    // Calculate IGP for each class
121    igp_ = utility::Vector(target_.nof_classes());
122   
123    for(size_t i=0; i<target_.size(); i++) {
124      size_t neighbor=i;
125      double mindist=std::numeric_limits<double>::max();
126      for(size_t j=0; j<target_.size(); j++) {           
127        if (i==j) // avoid self-self comparison
128          continue;
129        double dist = distance_(matrix_.begin_column(i), matrix_.end_column(i), 
130                                matrix_.begin_column(j));
131        if(dist<mindist) {
132          mindist=dist;
133          neighbor=j;
134        }
135      }
136      if(target_(i)==target_(neighbor))
137        igp_(target_(i))++;
138     
139    }
140    for(size_t i=0; i<target_.nof_classes(); i++) {
141      igp_(i)/=static_cast<double>(target_.size(i));
142    }
143  }
144
145 
146  template <typename Distance>
147  const utility::Vector& IGP<Distance>::score(void) const 
148  {
149    return igp_;
150  }
151 
152}}} // of namespace classifier, yat, and theplu
153
154#endif
Note: See TracBrowser for help on using the repository browser.