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, Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 2 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "DataLookup1D.h" |
---|
28 | #include "MatrixLookup.h" |
---|
29 | #include "Target.h" |
---|
30 | #include "yat/utility/vector.h" |
---|
31 | #include "yat/utility/yat_assert.h" |
---|
32 | #include "yat/statistics/vector_distance.h" |
---|
33 | |
---|
34 | #include <cmath> |
---|
35 | #include <limits> |
---|
36 | #include <stdexcept> |
---|
37 | |
---|
38 | namespace theplu { |
---|
39 | namespace yat { |
---|
40 | namespace classifier { |
---|
41 | |
---|
42 | class Target; |
---|
43 | class MatrixLookup; |
---|
44 | |
---|
45 | /// |
---|
46 | /// @brief Class for In Group Proportions (IGP) |
---|
47 | /// See Kapp and Tibshirani, Biostatistics (2006). |
---|
48 | /// |
---|
49 | template <typename Distance> |
---|
50 | class IGP |
---|
51 | { |
---|
52 | |
---|
53 | public: |
---|
54 | /// |
---|
55 | /// Constructor taking the training data, the target vector and |
---|
56 | /// the distance measure as input. |
---|
57 | /// |
---|
58 | IGP(const MatrixLookup&, const Target&); |
---|
59 | |
---|
60 | /// |
---|
61 | /// Destrucutor |
---|
62 | /// |
---|
63 | virtual ~IGP(); |
---|
64 | |
---|
65 | /// |
---|
66 | /// @return the IGP score for each class as elements in a vector. |
---|
67 | /// |
---|
68 | const utility::vector& score(void) const; |
---|
69 | |
---|
70 | |
---|
71 | private: |
---|
72 | utility::vector igp_; |
---|
73 | |
---|
74 | const MatrixLookup& matrix_; |
---|
75 | const Target& target_; |
---|
76 | }; |
---|
77 | |
---|
78 | |
---|
79 | // templates |
---|
80 | |
---|
81 | template <typename Distance> |
---|
82 | IGP<Distance>::IGP(const MatrixLookup& data, const Target& target) |
---|
83 | : matrix_(data), target_(target) |
---|
84 | { |
---|
85 | utility::yat_assert<std::runtime_error>(target_.size()==matrix_.columns()); |
---|
86 | |
---|
87 | // Calculate IGP for each class |
---|
88 | igp_ = utility::vector(target_.nof_classes()); |
---|
89 | |
---|
90 | for(u_int i=0; i<target_.size(); i++) { |
---|
91 | u_int neighbor=i; |
---|
92 | double mindist=std::numeric_limits<double>::max(); |
---|
93 | const DataLookup1D a(matrix_,i,false); |
---|
94 | for(u_int j=0; j<target_.size(); j++) { |
---|
95 | DataLookup1D b(matrix_,j,false); |
---|
96 | double dist=statistics:: |
---|
97 | vector_distance(a.begin,a.end(),b.begin(), |
---|
98 | statistics::vector_distance_traits<Distance>::distace()); |
---|
99 | if(j!=i && dist<mindist) { |
---|
100 | mindist=dist; |
---|
101 | neighbor=j; |
---|
102 | } |
---|
103 | } |
---|
104 | if(target_(i)==target_(neighbor)) |
---|
105 | igp_(target_(i))++; |
---|
106 | |
---|
107 | } |
---|
108 | for(u_int i=0; i<target_.nof_classes(); i++) { |
---|
109 | igp_(i)/=static_cast<double>(target_.size(i)); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | template <typename Distance> |
---|
114 | IGP<Distance>::~IGP() |
---|
115 | { |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | template <typename Distance> |
---|
120 | const utility::vector& IGP<Distance>::score(void) const |
---|
121 | { |
---|
122 | return igp_; |
---|
123 | } |
---|
124 | |
---|
125 | }}} // of namespace classifier, yat, and theplu |
---|
126 | |
---|
127 | #endif |
---|