source: trunk/yat/classifier/Kernel.h @ 676

Last change on this file since 676 was 675, checked in by Jari Häkkinen, 15 years ago

References #83. Changing project name to yat. Compilation will fail in this revision.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 6.2 KB
Line 
1#ifndef _theplu_classifier_kernel_
2#define _theplu_classifier_kernel_
3
4// $Id$
5
6/*
7  Copyright (C) The authors contributing to this file.
8
9  This file is part of the yat library, http://lev.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 "yat/classifier/DataLookup2D.h"
28#include "yat/classifier/KernelFunction.h"
29#include "yat/classifier/MatrixLookupWeighted.h"
30
31#include <cctype>
32#include <vector>
33
34namespace theplu {
35namespace classifier {
36
37  class MatrixLookup;
38
39  ///
40  ///  @brief Abstract Base Class for Kernels.
41  ///
42  ///  Class taking care of the \f$ NxN \f$ kernel matrix, where \f$ N \f$
43  ///  is number of samples. Each element in the Kernel corresponds is
44  ///  the scalar product of the corresponding pair of samples. At the
45  ///  time being there are two kinds of kernels. Kernel_SEV that is
46  ///  optimized to be fast and Kernel_MEV that is preferable when
47  ///  dealing with many samples and memory might be a
48  ///  bottleneck. Also there are the corresponding weighted versions
49  ///  to deal with weights (including missing values). A
50  ///  KernelFunction defines what kind of scalar product the Kernel
51  ///  represents, e.g. a Polynomial Kernel of degree 1 means we are
52  ///  dealing with the ordinary linear scalar product.
53  ///
54  /// @note If the KernelFunction is destroyed, the Kernel is no
55  /// longer defined.
56  ///
57  class Kernel
58  {
59   
60  public:
61
62    ///
63    /// Constructor taking the @a data matrix and KernelFunction as
64    /// input. Each column in the data matrix corresponds to one
65    /// sample and the Kernel matrix is built applying the
66    /// KernelFunction on each pair of columns in the data matrix.
67    ///
68    /// @note Can not handle NaNs.
69    ///
70    Kernel(const MatrixLookup& data, const KernelFunction& kf, 
71           const bool own=false); 
72
73    ///
74    /// Constructor taking the @a data matrix (with weights) and
75    /// KernelFunction as
76    /// input. Each column in the data matrix corresponds to one
77    /// sample and the Kernel matrix is built applying the
78    /// KernelFunction on each pair of columns in the data matrix.
79    ///
80    /// @note Can not handle NaNs.
81    ///
82    Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, 
83           const bool own=false); 
84
85    ///
86    /// The new kernel is created using selected features @a
87    /// index. Kernel will own its underlying data and delete it in
88    /// destructor.
89    ///
90    Kernel(const Kernel& kernel, const std::vector<size_t>& index);
91
92    ///
93    ///   Destructor
94    ///
95    virtual ~Kernel(void);
96
97    ///
98    /// @return element at position (\a row, \a column) of the Kernel
99    /// matrix
100    ///
101    virtual double operator()(const size_t row, const size_t column) const=0;
102
103    ///
104    /// @return const reference to the underlying data.
105    ///
106    inline const DataLookup2D& data(void) const { return *data_; }
107
108    ///
109    /// @brief number of samples
110    ///
111    inline size_t size(void) const { return data_->columns(); } 
112
113    ///
114    /// Calculates the scalar product (using the KernelFunction)
115    /// between vector @a vec and the \f$ i \f$ th column in the data
116    /// matrix.
117    ///   
118    double element(const DataLookup1D& vec, const size_t i) const;
119
120    ///
121    /// Calculates the weighted scalar product (using the
122    /// KernelFunction) between vector @a vec and the \f$ i \f$ th column
123    /// in the data matrix. Using a weight vector with all elements
124    /// equal to unity yields same result as the non-weighted version
125    /// above.
126    ///
127    double element(const DataLookupWeighted1D& vec, const size_t i) const;
128
129    ///
130    /// An interface for making new classifier objects. This function
131    /// allows for specification at run-time of which kernel to
132    /// instatiate (see 'Prototype' in Design Patterns).
133    ///
134    /// @Note Returns a dynamically allocated Kernel, which has
135    /// to be deleted by the caller to avoid memory leaks.
136    ///
137    virtual const Kernel* make_kernel(const MatrixLookup&, const bool) const=0;
138
139
140    ///
141    /// An interface for making new classifier objects. This function
142    /// allows for specification at run-time of which kernel to
143    /// instatiate (see 'Prototype' in Design Patterns).
144    ///
145    /// @Note Returns a dynamically allocated Kernel, which has
146    /// to be deleted by the caller to avoid memory leaks.
147    ///
148    virtual const Kernel* make_kernel(const MatrixLookupWeighted&, 
149                                      const bool own=false) const=0;
150
151
152    ///
153    /// Created Kernel is built from selected features in data. The
154    /// @a index corresponds to which rows in data to use for the
155    /// calculation of the returned Kernel.
156    ///
157    /// @return Dynamically allocated Kernel based on selected features
158    ///
159    /// @Note Returns a dynamically allocated Kernel, which has
160    /// to be deleted by the caller to avoid memory leaks.
161    ///
162    /// @todo remove this function
163    virtual const Kernel* selected(const std::vector<size_t>& index) const=0;
164
165    ///
166    /// @return true if kernel is calculated using weights
167    ///
168    inline bool weighted(void) const { return data_w_; }
169
170  protected:
171    /// underlying data
172    const DataLookup2D* data_;
173    /// same as data_ if weifghted otherwise a NULL pointer
174    const MatrixLookupWeighted* data_w_;
175    /// type of Kernel Function e.g. Gaussian (aka RBF)
176    const KernelFunction* kf_;
177
178    ///
179    /// poiter telling how many owners to underlying data
180    /// (data_). NULL if this is not an owner.
181    ///
182    u_int* ref_count_;
183
184    ///
185    /// poiter telling how many owners to underlying weights
186    /// (data_w_). NULL if this is not an owner.
187    ///
188    u_int* ref_count_w_;
189
190  private:
191    ///
192    /// Copy constructor (not implemented)
193    ///
194    Kernel(const Kernel&);
195
196    const Kernel& operator=(const Kernel&);
197
198  }; // class Kernel
199
200}} // of namespace classifier and namespace theplu
201
202#endif
Note: See TracBrowser for help on using the repository browser.