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

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

Made the project to compile on my Intel Mac running Leopard.

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