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

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

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 6.0 KB
Line 
1#ifndef _theplu_yat_classifier_kernel_
2#define _theplu_yat_classifier_kernel_
3
4// $Id$
5
6/*
7  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
11
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 3 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with yat. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28#include "KernelFunction.h"
29
30#include <cstddef>
31#include <vector>
32
33namespace theplu {
34namespace yat {
35namespace classifier {
36
37  class MatrixLookup;
38  class MatrixLookupWeighted;
39
40  ///
41  ///  @brief Interface 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    /// \throw if data is weighted
110    ///
111    const MatrixLookup& data(void) const;
112
113    ///
114    /// @return const reference to the underlying data.
115    ///
116    /// \throw if data is unweighted
117    ///
118    const MatrixLookupWeighted& data_weighted(void) const;
119
120    ///
121    /// Calculates the scalar product (using the KernelFunction)
122    /// between vector @a vec and the \f$ i \f$ th column in the data
123    /// matrix.
124    ///   
125    double element(const DataLookup1D& vec, const size_t i) const;
126
127    ///
128    /// Calculates the weighted scalar product (using the
129    /// KernelFunction) between vector @a vec and the \f$ i \f$ th column
130    /// in the data matrix. Using a weight vector with all elements
131    /// equal to unity yields same result as the non-weighted version
132    /// above.
133    ///
134    double element(const DataLookupWeighted1D& vec, const size_t i) const;
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 MatrixLookup&, const bool) const=0;
145
146
147    ///
148    /// An interface for making new classifier objects. This function
149    /// allows for specification at run-time of which kernel to
150    /// instatiate (see 'Prototype' in Design Patterns).
151    ///
152    /// @note Returns a dynamically allocated Kernel, which has
153    /// to be deleted by the caller to avoid memory leaks.
154    ///
155    virtual const Kernel* make_kernel(const MatrixLookupWeighted&, 
156                                      const bool own=false) const=0;
157
158
159    /**
160       \brief number of samples
161    */
162    size_t size(void) const;
163
164    ///
165    /// @return true if kernel is calculated using weights
166    ///
167    bool weighted(void) const;
168
169  protected:
170    /// underlying data
171    const MatrixLookup* ml_;
172    /// same as data_ if weifghted otherwise a NULL pointer
173    const MatrixLookupWeighted* mlw_;
174    /// type of Kernel Function e.g. Gaussian (aka RBF)
175    const KernelFunction* kf_;
176
177    ///
178    /// pointer telling how many owners to underlying data
179    /// (data_). NULL if this is not an owner.
180    ///
181    unsigned int* ref_count_;
182
183    ///
184    /// pointer telling how many owners to underlying weights
185    /// (data_w_). NULL if this is not an owner.
186    ///
187    unsigned int* ref_count_w_;
188
189  private:
190    ///
191    /// Copy constructor (not implemented)
192    ///
193    Kernel(const Kernel&);
194
195    const Kernel& operator=(const Kernel&);
196
197  }; // class Kernel
198
199}}} // of namespace classifier, yat, and theplu
200
201#endif
Note: See TracBrowser for help on using the repository browser.