yat  0.21pre
IGP.h
1 #ifndef _theplu_yat_classifier_igp_
2 #define _theplu_yat_classifier_igp_
3 
4 // $Id: IGP.h 3562 2017-01-04 01:16:07Z peter $
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, 2017 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/Matrix.h"
31 #include "yat/utility/Vector.h"
32 #include "yat/utility/yat_assert.h"
33 
34 #include <boost/concept_check.hpp>
35 
36 #include <cmath>
37 #include <limits>
38 #include <stdexcept>
39 
40 namespace theplu {
41 namespace yat {
42 namespace classifier {
43 
44  class Target;
45  class MatrixLookup;
46 
62  template <typename Distance>
63  class IGP
64  {
65 
66  public:
71  IGP(const MatrixLookup&, const Target&);
72 
73 
78  IGP(const MatrixLookup&, const Target&, const Distance&);
79 
83  virtual ~IGP();
84 
88  const utility::Vector& score(void) const;
89 
90 
91  private:
92  void calculate();
93 
94  utility::Vector igp_;
95  Distance distance_;
96 
97  const MatrixLookup& matrix_;
98  const Target& target_;
99  };
100 
101 
102  // templates
103 
104  template <typename Distance>
105  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target)
106  : matrix_(data), target_(target)
107  {
108  BOOST_CONCEPT_ASSERT((utility::DistanceConcept<Distance>));
109  calculate();
110  }
111 
112  template <typename Distance>
113  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target,
114  const Distance& dist)
115  : matrix_(data), target_(target), distance_(dist)
116  {
117  BOOST_CONCEPT_ASSERT((utility::DistanceConcept<Distance>));
118  calculate();
119  }
120 
121 
122  template <typename Distance>
124  {
125  }
126 
127 
128  template <typename Distance>
130  {
131  YAT_ASSERT(target_.size()==matrix_.columns());
132 
133  // Calculate IGP for each class
134  igp_.resize(target_.nof_classes(), 0.0);
135 
136  // calculate distances
137  utility::Matrix dist(matrix_.columns(), matrix_.columns());
138  for (size_t i=0; i<dist.rows(); ++i)
139  for (size_t j=i+1; j<dist.rows(); ++j) {
140  dist(i,j) = dist(j,i) = distance_(matrix_.begin_column(i),
141  matrix_.end_column(i),
142  matrix_.begin_column(j));
143  }
144 
145  // find nearest neigbour for each sample
146  for(size_t i=0; i<target_.size(); i++) {
147  size_t neighbor=i;
148  double mindist=std::numeric_limits<double>::max();
149  for(size_t j=0; j<target_.size(); j++) {
150  if (i==j) // avoid self-self comparison
151  continue;
152  if(dist(i,j)<mindist) {
153  mindist=dist(i,j);
154  neighbor=j;
155  }
156  }
157  if(target_(i)==target_(neighbor))
158  igp_(target_(i))++;
159 
160  }
161  for(size_t i=0; i<target_.nof_classes(); i++) {
162  igp_(i)/=static_cast<double>(target_.size(i));
163  }
164  }
165 
166 
167  template <typename Distance>
169  {
170  return igp_;
171  }
172 
173 }}} // of namespace classifier, yat, and theplu
174 
175 #endif
General view into utility::Matrix.
Definition: MatrixLookup.h:70
Class for containing sample labels.
Definition: Target.h:47
The Department of Theoretical Physics namespace as we define it.
IGP(const MatrixLookup &, const Target &)
Definition: IGP.h:105
Class for In Group Proportions (IGP)
Definition: IGP.h:63
virtual ~IGP()
Definition: IGP.h:123
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:699
This is the yat interface to GSL vector.
Definition: Vector.h:59
Concept check for a Distance.
Definition: concept_check.h:290
Interface to GSL matrix.
Definition: Matrix.h:104
const utility::Vector & score(void) const
Definition: IGP.h:168

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14