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

Last change on this file since 925 was 925, checked in by Markus Ringnér, 14 years ago

NCC and IGP have been changed to templates on Distance

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.0 KB
Line 
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/trac/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
37namespace theplu {
38namespace yat {
39namespace classifier { 
40
41  class Target;
42  class MatrixLookup;
43
44  ///
45  /// @brief Class for In Group Proportions (IGP)
46  /// See Kapp and Tibshirani, Biostatistics (2006).
47  ///
48  template <typename Distance>
49  class IGP
50  {
51 
52  public:
53    ///
54    /// Constructor taking the training data, the target vector and
55    /// the distance measure as input.
56    ///
57    IGP(const MatrixLookup&, const Target&);
58
59    ///
60    /// Destrucutor
61    ///
62    virtual ~IGP();
63
64    ///
65    /// @return the IGP score for each class as elements in a vector.
66    ///
67    const utility::vector& score(void) const;
68
69
70  private:
71    utility::vector igp_;
72
73    const MatrixLookup& matrix_;
74    const Target& target_;
75  }; 
76
77 
78  // templates
79
80  template <typename Distance>
81  IGP<Distance>::IGP(const MatrixLookup& data, const Target& target) 
82    : matrix_(data), target_(target)
83  {   
84    yat_assert(target_.size()==matrix_.columns());
85   
86    // Calculate IGP for each class
87    igp_.clone(utility::vector(target_.nof_classes()));
88   
89    for(u_int i=0; i<target_.size(); i++) {
90      u_int neighbor=i;
91      double mindist=std::numeric_limits<double>::max();
92      const DataLookup1D a(matrix_,i,false);
93      for(u_int j=0; j<target_.size(); j++) {           
94        DataLookup1D b(matrix_,j,false);
95        double dist=statistics::
96          vector_distance(a.begin,a.end(),b.begin(),
97                          statistics::vector_distance_traits<Distance>::distace());
98        if(j!=i && dist<mindist) {
99          mindist=dist;
100          neighbor=j;
101        }
102      }
103      if(target_(i)==target_(neighbor))
104        igp_(target_(i))++;
105     
106    }
107    for(u_int i=0; i<target_.nof_classes(); i++) {
108      igp_(i)/=static_cast<double>(target_.size(i));
109    }
110  }
111 
112  template <typename Distance>
113  IGP<Distance>::~IGP()   
114  {
115  }
116
117 
118  template <typename Distance>
119  const utility::vector& IGP<Distance>::score(void) const 
120  {
121    return igp_;
122  }
123 
124}}} // of namespace classifier, yat, and theplu
125
126#endif
Note: See TracBrowser for help on using the repository browser.