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

Last change on this file since 749 was 749, checked in by Peter, 15 years ago

fixes #72

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