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

Last change on this file since 3319 was 3319, checked in by Peter, 9 years ago

avoid long line

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.7 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, 2010, 2014 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     IGP is defined to be the proportion of samples in a group whose
50     nearest neighbours are also in the same group.
51
52     \see <a HREF="
53     http://biostatistics.oxfordjournals.org/cgi/content/abstract/kxj029v1">
54     Kapp and Tibshirani, Biostatistics (2006)</a>.
55  */
56  template <typename Distance>
57  class IGP
58  {
59
60  public:
61    ///
62    /// Constructor taking the training data and the target vector and
63    /// as input.
64    ///
65    IGP(const MatrixLookup&, const Target&);
66
67
68    ///
69    /// Constructor taking the training data, the target vector and
70    /// the distance measure as input.
71    ///
72    IGP(const MatrixLookup&, const Target&, const Distance&);
73
74    ///
75    /// Destrucutor
76    ///
77    virtual ~IGP();
78
79    ///
80    /// @return the IGP score for each class as elements in a vector.
81    ///
82    const utility::Vector& score(void) const;
83
84
85  private:
86    void calculate();
87
88    utility::Vector igp_;
89    Distance distance_;
90
91    const MatrixLookup& matrix_;
92    const Target& target_;
93  };
94
95
96  // templates
97
98  template <typename Distance>
99  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target)
100    : matrix_(data), target_(target)
101  {
102    BOOST_CONCEPT_ASSERT((utility::DistanceConcept<Distance>));
103    calculate();
104  }
105
106  template <typename Distance>
107  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target,
108                     const Distance& dist)
109    : matrix_(data), target_(target), distance_(dist)
110  {
111    BOOST_CONCEPT_ASSERT((utility::DistanceConcept<Distance>));
112    calculate();
113  }
114
115
116  template <typename Distance>
117  IGP<Distance>::~IGP()
118  {
119  }
120
121
122  template <typename Distance>
123  void IGP<Distance>::calculate()
124  {
125    YAT_ASSERT(target_.size()==matrix_.columns());
126
127    // Calculate IGP for each class
128    igp_ = utility::Vector(target_.nof_classes());
129
130    for(size_t i=0; i<target_.size(); i++) {
131      size_t neighbor=i;
132      double mindist=std::numeric_limits<double>::max();
133      for(size_t j=0; j<target_.size(); j++) {
134        if (i==j) // avoid self-self comparison
135          continue;
136        double dist = distance_(matrix_.begin_column(i), matrix_.end_column(i),
137                                matrix_.begin_column(j));
138        if(dist<mindist) {
139          mindist=dist;
140          neighbor=j;
141        }
142      }
143      if(target_(i)==target_(neighbor))
144        igp_(target_(i))++;
145
146    }
147    for(size_t i=0; i<target_.nof_classes(); i++) {
148      igp_(i)/=static_cast<double>(target_.size(i));
149    }
150  }
151
152
153  template <typename Distance>
154  const utility::Vector& IGP<Distance>::score(void) const
155  {
156    return igp_;
157  }
158
159}}} // of namespace classifier, yat, and theplu
160
161#endif
Note: See TracBrowser for help on using the repository browser.