source: branches/0.4-stable/yat/classifier/Kernel.h @ 1392

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

trac has moved

  • 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) 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 2 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 this program; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27  02111-1307, USA.
28*/
29
30#include "KernelFunction.h"
31
32#include <cstddef>
33#include <vector>
34
35namespace theplu {
36namespace yat {
37namespace classifier {
38
39  class MatrixLookup;
40  class MatrixLookupWeighted;
41
42  ///
43  ///  @brief Interface Class for Kernels.
44  ///
45  ///  Class taking care of the \f$ NxN \f$ kernel matrix, where \f$ N \f$
46  ///  is number of samples. Each element in the Kernel corresponds to
47  ///  the scalar product of the corresponding pair of samples. At the
48  ///  time being there are two kinds of kernels. Kernel_SEV that is
49  ///  optimized to be fast and Kernel_MEV that is preferable when
50  ///  dealing with many samples and memory might be a
51  ///  bottleneck. A
52  ///  KernelFunction defines what kind of scalar product the Kernel
53  ///  represents, e.g. a Polynomial Kernel of degree 1 means we are
54  ///  dealing with the ordinary linear scalar product.
55  ///
56  /// @note If the KernelFunction is destroyed, the Kernel is no
57  /// longer defined.
58  ///
59  class Kernel
60  {
61
62  public:
63
64    ///
65    /// Constructor taking the @a data matrix and KernelFunction as
66    /// input. Each column in the data matrix corresponds to one
67    /// sample and the Kernel matrix is built applying the
68    /// KernelFunction on each pair of columns in the data matrix.
69    /// If @a own is set to true, Kernel is owner of underlying data.
70    ///
71    /// @note Can not handle NaNs. To deal with missing values use
72    /// constructor taking MatrixLookupWeighted.
73    ///
74    Kernel(const MatrixLookup& data, const KernelFunction& kf, 
75           const bool own=false); 
76
77    ///
78    /// Constructor taking the @a data matrix (with weights) and
79    /// KernelFunction as
80    /// input. Each column in the data matrix corresponds to one
81    /// sample and the Kernel matrix is built applying the
82    /// KernelFunction on each pair of columns in the data matrix.
83    /// If @a own is set to true, Kernel is owner of underlying data.
84    ///
85    Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, 
86           const bool own=false); 
87
88    ///
89    /// The new kernel is created using selected features @a
90    /// index. Kernel will own its underlying data
91    ///
92    Kernel(const Kernel& kernel, const std::vector<size_t>& index);
93
94    ///
95    /// @brief Destructor
96    ///
97    /// If Kernel is owner of underlying data and Kernel is the last
98    /// owner, underlying data is deleted.
99    ///
100    virtual ~Kernel(void);
101
102    ///
103    /// @return element at position (\a row, \a column) of the Kernel
104    /// matrix
105    ///
106    virtual double operator()(const size_t row, const size_t column) const=0;
107
108    ///
109    /// @return const reference to the underlying data.
110    ///
111    /// \throw if data is weighted
112    ///
113    const MatrixLookup& data(void) const;
114
115    ///
116    /// @return const reference to the underlying data.
117    ///
118    /// \throw if data is unweighted
119    ///
120    const MatrixLookupWeighted& data_weighted(void) const;
121
122    ///
123    /// Calculates the scalar product (using the KernelFunction)
124    /// between vector @a vec and the \f$ i \f$ th column in the data
125    /// matrix.
126    ///   
127    double element(const DataLookup1D& vec, const size_t i) const;
128
129    ///
130    /// Calculates the weighted scalar product (using the
131    /// KernelFunction) between vector @a vec and the \f$ i \f$ th column
132    /// in the data matrix. Using a weight vector with all elements
133    /// equal to unity yields same result as the non-weighted version
134    /// above.
135    ///
136    double element(const DataLookupWeighted1D& vec, const size_t i) const;
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 MatrixLookup&, const bool) const=0;
147
148
149    ///
150    /// An interface for making new classifier objects. This function
151    /// allows for specification at run-time of which kernel to
152    /// instatiate (see 'Prototype' in Design Patterns).
153    ///
154    /// @note Returns a dynamically allocated Kernel, which has
155    /// to be deleted by the caller to avoid memory leaks.
156    ///
157    virtual const Kernel* make_kernel(const MatrixLookupWeighted&, 
158                                      const bool own=false) const=0;
159
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 MatrixLookup* ml_;
174    /// same as data_ if weifghted otherwise a NULL pointer
175    const MatrixLookupWeighted* mlw_;
176    /// type of Kernel Function e.g. Gaussian (aka RBF)
177    const KernelFunction* kf_;
178
179    ///
180    /// pointer telling how many owners to underlying data
181    /// (data_). NULL if this is not an owner.
182    ///
183    unsigned int* ref_count_;
184
185    ///
186    /// pointer telling how many owners to underlying weights
187    /// (data_w_). NULL if this is not an owner.
188    ///
189    unsigned 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.