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

Last change on this file since 1486 was 1486, checked in by Jari Häkkinen, 13 years ago

Addresses #436.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.4 KB
Line 
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 this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
28#include "DataLookup1D.h"
29#include "MatrixLookup.h"
30#include "Target.h"
31#include "yat/utility/Vector.h"
32#include "yat/utility/yat_assert.h"
33
34#include <cmath>
35#include <limits>
36#include <stdexcept>
37
38namespace theplu {
39namespace yat {
40namespace 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 and the target vector and
56    /// as input.
57    ///
58    IGP(const MatrixLookup&, const Target&);
59
60
61    ///
62    /// Constructor taking the training data, the target vector and
63    /// the distance measure as input.
64    ///
65    IGP(const MatrixLookup&, const Target&, const Distance&);
66
67    ///
68    /// Destrucutor
69    ///
70    virtual ~IGP();
71
72    ///
73    /// @return the IGP score for each class as elements in a vector.
74    ///
75    const utility::Vector& score(void) const;
76
77
78  private:
79    void calculate();
80
81    utility::Vector igp_;
82    Distance distance_;
83
84    const MatrixLookup& matrix_;
85    const Target& target_;
86  }; 
87
88 
89  // templates
90
91  template <typename Distance>
92  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target) 
93    : matrix_(data), target_(target)
94  {   
95    calculate();
96  }
97
98  template <typename Distance>
99  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target, const Distance& dist) 
100    : matrix_(data), target_(target), distance_(dist)
101  {   
102    calculate();
103  }
104
105 
106  template <typename Distance>
107  IGP<Distance>::~IGP()   
108  {
109  }
110
111  template <typename Distance>
112  void IGP<Distance>::calculate() 
113  {
114    utility::yat_assert<std::runtime_error>(target_.size()==matrix_.columns());
115   
116    // Calculate IGP for each class
117    igp_ = utility::Vector(target_.nof_classes());
118   
119    for(size_t i=0; i<target_.size(); i++) {
120      size_t neighbor=i;
121      double mindist=std::numeric_limits<double>::max();
122      const DataLookup1D a(matrix_,i,false);
123      for(size_t j=0; j<target_.size(); j++) {           
124        DataLookup1D b(matrix_,j,false);
125        double dist=distance_(a.begin, a.end(), b.begin());
126        if(j!=i && dist<mindist) {
127          mindist=dist;
128          neighbor=j;
129        }
130      }
131      if(target_(i)==target_(neighbor))
132        igp_(target_(i))++;
133     
134    }
135    for(size_t i=0; i<target_.nof_classes(); i++) {
136      igp_(i)/=static_cast<double>(target_.size(i));
137    }
138  }
139
140 
141  template <typename Distance>
142  const utility::Vector& IGP<Distance>::score(void) const 
143  {
144    return igp_;
145  }
146 
147}}} // of namespace classifier, yat, and theplu
148
149#endif
Note: See TracBrowser for help on using the repository browser.