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

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

Fixes #170. Almost all inlines removed, some classes have no cc file.

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