yat/classifier/Kernel.h

Code
Comments
Other
Rev Date Author Line
680 11 Oct 06 jari 1 #ifndef _theplu_yat_classifier_kernel_
680 11 Oct 06 jari 2 #define _theplu_yat_classifier_kernel_
330 01 Jun 05 peter 3
616 31 Aug 06 jari 4 // $Id$
616 31 Aug 06 jari 5
675 10 Oct 06 jari 6 /*
4359 23 Aug 23 peter 7   Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 8   Copyright (C) 2007 Peter Johansson
2119 12 Dec 09 peter 9   Copyright (C) 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
513 18 Feb 06 peter 10
1437 25 Aug 08 peter 11   This file is part of the yat library, http://dev.thep.lu.se/yat
675 10 Oct 06 jari 12
675 10 Oct 06 jari 13   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 14   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 15   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 16   License, or (at your option) any later version.
675 10 Oct 06 jari 17
675 10 Oct 06 jari 18   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 19   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 21   General Public License for more details.
675 10 Oct 06 jari 22
675 10 Oct 06 jari 23   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 24   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 25 */
675 10 Oct 06 jari 26
680 11 Oct 06 jari 27 #include "KernelFunction.h"
675 10 Oct 06 jari 28
1271 09 Apr 08 peter 29 #include <cstddef>
545 06 Mar 06 peter 30 #include <vector>
330 01 Jun 05 peter 31
330 01 Jun 05 peter 32 namespace theplu {
680 11 Oct 06 jari 33 namespace yat {
450 15 Dec 05 peter 34 namespace classifier {
330 01 Jun 05 peter 35
628 05 Sep 06 peter 36   class MatrixLookup;
747 11 Feb 07 peter 37   class MatrixLookupWeighted;
628 05 Sep 06 peter 38
749 11 Feb 07 peter 39   ///
4200 19 Aug 22 peter 40   ///  @brief Interface Class for Kernels.
330 01 Jun 05 peter 41   ///
597 28 Aug 06 markus 42   ///  Class taking care of the \f$ NxN \f$ kernel matrix, where \f$ N \f$
749 11 Feb 07 peter 43   ///  is number of samples. Each element in the Kernel corresponds to
592 24 Aug 06 peter 44   ///  the scalar product of the corresponding pair of samples. At the
592 24 Aug 06 peter 45   ///  time being there are two kinds of kernels. Kernel_SEV that is
592 24 Aug 06 peter 46   ///  optimized to be fast and Kernel_MEV that is preferable when
592 24 Aug 06 peter 47   ///  dealing with many samples and memory might be a
749 11 Feb 07 peter 48   ///  bottleneck. A
592 24 Aug 06 peter 49   ///  KernelFunction defines what kind of scalar product the Kernel
592 24 Aug 06 peter 50   ///  represents, e.g. a Polynomial Kernel of degree 1 means we are
592 24 Aug 06 peter 51   ///  dealing with the ordinary linear scalar product.
592 24 Aug 06 peter 52   ///
749 11 Feb 07 peter 53   /// @note If the KernelFunction is destroyed, the Kernel is no
749 11 Feb 07 peter 54   /// longer defined.
749 11 Feb 07 peter 55   ///
749 11 Feb 07 peter 56   class Kernel
749 11 Feb 07 peter 57   {
333 02 Jun 05 jari 58
749 11 Feb 07 peter 59   public:
749 11 Feb 07 peter 60
330 01 Jun 05 peter 61     ///
749 11 Feb 07 peter 62     /// Constructor taking the @a data matrix and KernelFunction as
749 11 Feb 07 peter 63     /// input. Each column in the data matrix corresponds to one
749 11 Feb 07 peter 64     /// sample and the Kernel matrix is built applying the
749 11 Feb 07 peter 65     /// KernelFunction on each pair of columns in the data matrix.
749 11 Feb 07 peter 66     /// If @a own is set to true, Kernel is owner of underlying data.
330 01 Jun 05 peter 67     ///
749 11 Feb 07 peter 68     /// @note Can not handle NaNs. To deal with missing values use
749 11 Feb 07 peter 69     /// constructor taking MatrixLookupWeighted.
513 18 Feb 06 peter 70     ///
4200 19 Aug 22 peter 71      Kernel(const MatrixLookup& data, const KernelFunction& kf,
4200 19 Aug 22 peter 72            const bool own=false);
545 06 Mar 06 peter 73
566 16 Mar 06 peter 74     ///
749 11 Feb 07 peter 75     /// Constructor taking the @a data matrix (with weights) and
749 11 Feb 07 peter 76     /// KernelFunction as
749 11 Feb 07 peter 77     /// input. Each column in the data matrix corresponds to one
749 11 Feb 07 peter 78     /// sample and the Kernel matrix is built applying the
749 11 Feb 07 peter 79     /// KernelFunction on each pair of columns in the data matrix.
749 11 Feb 07 peter 80     /// If @a own is set to true, Kernel is owner of underlying data.
566 16 Mar 06 peter 81     ///
4200 19 Aug 22 peter 82      Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf,
4200 19 Aug 22 peter 83            const bool own=false);
628 05 Sep 06 peter 84
545 06 Mar 06 peter 85     ///
628 05 Sep 06 peter 86     /// The new kernel is created using selected features @a
4200 19 Aug 22 peter 87     /// index. Kernel will own its underlying data
628 05 Sep 06 peter 88     ///
545 06 Mar 06 peter 89     Kernel(const Kernel& kernel, const std::vector<size_t>& index);
545 06 Mar 06 peter 90
749 11 Feb 07 peter 91     ///
749 11 Feb 07 peter 92     /// @brief Destructor
749 11 Feb 07 peter 93     ///
749 11 Feb 07 peter 94     /// If Kernel is owner of underlying data and Kernel is the last
749 11 Feb 07 peter 95     /// owner, underlying data is deleted.
749 11 Feb 07 peter 96     ///
545 06 Mar 06 peter 97     virtual ~Kernel(void);
330 01 Jun 05 peter 98
330 01 Jun 05 peter 99     ///
330 01 Jun 05 peter 100     /// @return element at position (\a row, \a column) of the Kernel
330 01 Jun 05 peter 101     /// matrix
749 11 Feb 07 peter 102     ///
527 01 Mar 06 peter 103     virtual double operator()(const size_t row, const size_t column) const=0;
330 01 Jun 05 peter 104
330 01 Jun 05 peter 105     ///
628 05 Sep 06 peter 106     /// @return const reference to the underlying data.
592 24 Aug 06 peter 107     ///
4200 19 Aug 22 peter 108     /// \throw if data is weighted
1165 26 Feb 08 peter 109     ///
1165 26 Feb 08 peter 110     const MatrixLookup& data(void) const;
545 06 Mar 06 peter 111
536 03 Mar 06 peter 112     ///
1165 26 Feb 08 peter 113     /// @return const reference to the underlying data.
1165 26 Feb 08 peter 114     ///
4200 19 Aug 22 peter 115     /// \throw if data is unweighted
1165 26 Feb 08 peter 116     ///
1165 26 Feb 08 peter 117     const MatrixLookupWeighted& data_weighted(void) const;
1165 26 Feb 08 peter 118
1165 26 Feb 08 peter 119     ///
592 24 Aug 06 peter 120     /// Calculates the scalar product (using the KernelFunction)
597 28 Aug 06 markus 121     /// between vector @a vec and the \f$ i \f$ th column in the data
592 24 Aug 06 peter 122     /// matrix.
4200 19 Aug 22 peter 123     ///
628 05 Sep 06 peter 124     double element(const DataLookup1D& vec, const size_t i) const;
592 24 Aug 06 peter 125
592 24 Aug 06 peter 126     ///
592 24 Aug 06 peter 127     /// Calculates the weighted scalar product (using the
597 28 Aug 06 markus 128     /// KernelFunction) between vector @a vec and the \f$ i \f$ th column
592 24 Aug 06 peter 129     /// in the data matrix. Using a weight vector with all elements
592 24 Aug 06 peter 130     /// equal to unity yields same result as the non-weighted version
592 24 Aug 06 peter 131     /// above.
592 24 Aug 06 peter 132     ///
628 05 Sep 06 peter 133     double element(const DataLookupWeighted1D& vec, const size_t i) const;
523 23 Feb 06 peter 134
545 06 Mar 06 peter 135     ///
658 25 Sep 06 peter 136     /// An interface for making new classifier objects. This function
658 25 Sep 06 peter 137     /// allows for specification at run-time of which kernel to
658 25 Sep 06 peter 138     /// instatiate (see 'Prototype' in Design Patterns).
658 25 Sep 06 peter 139     ///
1125 22 Feb 08 peter 140     /// @note Returns a dynamically allocated Kernel, which has
658 25 Sep 06 peter 141     /// to be deleted by the caller to avoid memory leaks.
658 25 Sep 06 peter 142     ///
658 25 Sep 06 peter 143     virtual const Kernel* make_kernel(const MatrixLookup&, const bool) const=0;
658 25 Sep 06 peter 144
658 25 Sep 06 peter 145
658 25 Sep 06 peter 146     ///
658 25 Sep 06 peter 147     /// An interface for making new classifier objects. This function
658 25 Sep 06 peter 148     /// allows for specification at run-time of which kernel to
658 25 Sep 06 peter 149     /// instatiate (see 'Prototype' in Design Patterns).
658 25 Sep 06 peter 150     ///
1125 22 Feb 08 peter 151     /// @note Returns a dynamically allocated Kernel, which has
658 25 Sep 06 peter 152     /// to be deleted by the caller to avoid memory leaks.
658 25 Sep 06 peter 153     ///
4200 19 Aug 22 peter 154     virtual const Kernel* make_kernel(const MatrixLookupWeighted&,
658 25 Sep 06 peter 155                                       const bool own=false) const=0;
658 25 Sep 06 peter 156
658 25 Sep 06 peter 157
720 26 Dec 06 jari 158     /**
4200 19 Aug 22 peter 159        \brief number of samples
720 26 Dec 06 jari 160     */
720 26 Dec 06 jari 161     size_t size(void) const;
720 26 Dec 06 jari 162
545 06 Mar 06 peter 163     ///
545 06 Mar 06 peter 164     /// @return true if kernel is calculated using weights
545 06 Mar 06 peter 165     ///
720 26 Dec 06 jari 166     bool weighted(void) const;
545 06 Mar 06 peter 167
749 11 Feb 07 peter 168   protected:
659 26 Sep 06 peter 169     /// underlying data
1163 26 Feb 08 peter 170     const MatrixLookup* ml_;
648 14 Sep 06 peter 171     /// same as data_ if weifghted otherwise a NULL pointer
1163 26 Feb 08 peter 172     const MatrixLookupWeighted* mlw_;
648 14 Sep 06 peter 173     /// type of Kernel Function e.g. Gaussian (aka RBF)
512 18 Feb 06 peter 174     const KernelFunction* kf_;
330 01 Jun 05 peter 175
659 26 Sep 06 peter 176     ///
749 11 Feb 07 peter 177     /// pointer telling how many owners to underlying data
659 26 Sep 06 peter 178     /// (data_). NULL if this is not an owner.
659 26 Sep 06 peter 179     ///
1271 09 Apr 08 peter 180     unsigned int* ref_count_;
659 26 Sep 06 peter 181
659 26 Sep 06 peter 182     ///
749 11 Feb 07 peter 183     /// pointer telling how many owners to underlying weights
659 26 Sep 06 peter 184     /// (data_w_). NULL if this is not an owner.
659 26 Sep 06 peter 185     ///
1271 09 Apr 08 peter 186     unsigned int* ref_count_w_;
659 26 Sep 06 peter 187
513 18 Feb 06 peter 188   private:
513 18 Feb 06 peter 189     ///
513 18 Feb 06 peter 190     /// Copy constructor (not implemented)
513 18 Feb 06 peter 191     ///
513 18 Feb 06 peter 192      Kernel(const Kernel&);
513 18 Feb 06 peter 193
555 08 Mar 06 peter 194     const Kernel& operator=(const Kernel&);
513 18 Feb 06 peter 195
749 11 Feb 07 peter 196   }; // class Kernel
330 01 Jun 05 peter 197
680 11 Oct 06 jari 198 }}} // of namespace classifier, yat, and theplu
330 01 Jun 05 peter 199
330 01 Jun 05 peter 200 #endif