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 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
9 | Copyright (C) 2008 Peter Johansson, Markus Ringnér |
---|
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 2 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 this program; if not, write to the Free Software |
---|
25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
26 | 02111-1307, USA. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "DataLookup1D.h" |
---|
30 | #include "MatrixLookup.h" |
---|
31 | #include "Target.h" |
---|
32 | #include "yat/utility/Vector.h" |
---|
33 | #include "yat/utility/yat_assert.h" |
---|
34 | |
---|
35 | #include <cmath> |
---|
36 | #include <limits> |
---|
37 | #include <stdexcept> |
---|
38 | |
---|
39 | namespace theplu { |
---|
40 | namespace yat { |
---|
41 | namespace classifier { |
---|
42 | |
---|
43 | class Target; |
---|
44 | class MatrixLookup; |
---|
45 | |
---|
46 | /// |
---|
47 | /// @brief Class for In Group Proportions (IGP) |
---|
48 | /// See Kapp and Tibshirani, Biostatistics (2006). |
---|
49 | /// |
---|
50 | template <typename Distance> |
---|
51 | class IGP |
---|
52 | { |
---|
53 | |
---|
54 | public: |
---|
55 | /// |
---|
56 | /// Constructor taking the training data and the target vector and |
---|
57 | /// as input. |
---|
58 | /// |
---|
59 | IGP(const MatrixLookup&, const Target&); |
---|
60 | |
---|
61 | |
---|
62 | /// |
---|
63 | /// Constructor taking the training data, the target vector and |
---|
64 | /// the distance measure as input. |
---|
65 | /// |
---|
66 | IGP(const MatrixLookup&, const Target&, const Distance&); |
---|
67 | |
---|
68 | /// |
---|
69 | /// Destrucutor |
---|
70 | /// |
---|
71 | virtual ~IGP(); |
---|
72 | |
---|
73 | /// |
---|
74 | /// @return the IGP score for each class as elements in a vector. |
---|
75 | /// |
---|
76 | const utility::Vector& score(void) const; |
---|
77 | |
---|
78 | |
---|
79 | private: |
---|
80 | void calculate(); |
---|
81 | |
---|
82 | utility::Vector igp_; |
---|
83 | Distance distance_; |
---|
84 | |
---|
85 | const MatrixLookup& matrix_; |
---|
86 | const Target& target_; |
---|
87 | }; |
---|
88 | |
---|
89 | |
---|
90 | // templates |
---|
91 | |
---|
92 | template <typename Distance> |
---|
93 | IGP<Distance>::IGP(const MatrixLookup& data, const Target& target) |
---|
94 | : matrix_(data), target_(target) |
---|
95 | { |
---|
96 | calculate(); |
---|
97 | } |
---|
98 | |
---|
99 | template <typename Distance> |
---|
100 | IGP<Distance>::IGP(const MatrixLookup& data, const Target& target, const Distance& dist) |
---|
101 | : matrix_(data), target_(target), distance_(dist) |
---|
102 | { |
---|
103 | calculate(); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | template <typename Distance> |
---|
108 | IGP<Distance>::~IGP() |
---|
109 | { |
---|
110 | } |
---|
111 | |
---|
112 | template <typename Distance> |
---|
113 | void IGP<Distance>::calculate() |
---|
114 | { |
---|
115 | utility::yat_assert<std::runtime_error>(target_.size()==matrix_.columns()); |
---|
116 | |
---|
117 | // Calculate IGP for each class |
---|
118 | igp_ = utility::Vector(target_.nof_classes()); |
---|
119 | |
---|
120 | for(size_t i=0; i<target_.size(); i++) { |
---|
121 | size_t neighbor=i; |
---|
122 | double mindist=std::numeric_limits<double>::max(); |
---|
123 | const DataLookup1D a(matrix_,i,false); |
---|
124 | for(size_t j=0; j<target_.size(); j++) { |
---|
125 | DataLookup1D b(matrix_,j,false); |
---|
126 | double dist=distance_(a.begin, a.end(), b.begin()); |
---|
127 | if(j!=i && dist<mindist) { |
---|
128 | mindist=dist; |
---|
129 | neighbor=j; |
---|
130 | } |
---|
131 | } |
---|
132 | if(target_(i)==target_(neighbor)) |
---|
133 | igp_(target_(i))++; |
---|
134 | |
---|
135 | } |
---|
136 | for(size_t i=0; i<target_.nof_classes(); i++) { |
---|
137 | igp_(i)/=static_cast<double>(target_.size(i)); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | template <typename Distance> |
---|
143 | const utility::Vector& IGP<Distance>::score(void) const |
---|
144 | { |
---|
145 | return igp_; |
---|
146 | } |
---|
147 | |
---|
148 | }}} // of namespace classifier, yat, and theplu |
---|
149 | |
---|
150 | #endif |
---|