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