STK++ 0.9.13
STK_Kernel_Hamming.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::STatisitK::Kernel
27 * created on: 2 feb. 2016
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_KERNEL_HAMMING_H
37#define STK_KERNEL_HAMMING_H
38
41
42namespace STK
43{
44
45namespace Kernel
46{
59template<class Array>
60class Hamming: public IKernelBase<Array>
61{
62 public:
64 typedef typename Array::Type Type;
65 using Base::p_data_;
66 using Base::gram_;
67 using Base::hasRun_;
68
85 Hamming( Array const& data, Real const& lambda= 1.)
86 : Base(data), lambda_(lambda), diagElt_(1.), factors_(data)
87 {
88 factors_.run();
90 }
95 template<class Derived>
96 Hamming( Array const* p_data, ExprBase<Derived> const& param)
97 : Base(p_data), lambda_(param.empty() ? 1. : param.front())
99 {
100 if (!p_data)
102 factors_.run();
104 }
109 template<class Derived>
110 Hamming( Array const& data, ExprBase<Derived> const& param)
111 : Base(data), lambda_(param.empty() ? 1. : param.front())
112 , diagElt_(1.), factors_(data)
113 {
114 factors_.run();
116 }
117
119 virtual ~Hamming() {}
120
122 Real const& lambda() const {return lambda_;}
126 void setLambda(Real const& lambda)
127 {
128 lambda_ = lambda;
130 }
134 template<class Derived>
135 void setParam( ExprBase<Derived> const& param)
136 {
137 lambda_ = (param.empty() ? 1. : param.front());
139 }
140
145 virtual Real diag(int i) const;
150 virtual Real comp(int i, int j) const;
151
152 private:
161};
162
163/* Compute the diagonal element of the kernel*/
164template<class Array>
166{
167 diagElt_ = 1.;
168 for(int j=factors_.nbLevels().begin(); j < factors_.nbLevels().end(); ++j)
169 { diagElt_ *= lambda_*lambda_*(factors_.nbLevels()[j]-1) + 1;}
170}
171
172template<class Array>
173inline Real Hamming<Array>::diag(int i) const { return diagElt_;}
174
175template<class Array>
177{
178 typedef typename hidden::Traits<Array>::Row RowVector;
179 if (hasRun_) return gram_(i,j);
180 // create references on row i and j
181 RowVector ind1(p_data_->row(i), true), ind2(p_data_->row(j), true);
182 Real value = 1.;
183 for(int j=factors_.nbLevels().begin(); j < factors_.nbLevels().end(); ++j)
184 {
185 value *= (ind1[j]==ind2[j]) ? lambda_*lambda_*(factors_.nbLevels()[j]-1) + 1.
186 : lambda_*(lambda_*(factors_.nbLevels()[j]-2) + 2.);
187 }
188 return value;
189}
190
191} // namespace Kernel
192
193} // namespace STK
194
195#endif /* STK_KERNEL_HAMMING_H */
In this file we define the Interface base class for computing a Kernels.
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
In this file we define and implement the MultiFactor class.
bool hasRun_
true if run has been used, false otherwise
Definition STK_IRunner.h:98
The Hamming Kernel is a kernel of the form.
Real diagElt_
diagonal element of the kernel
void setParam(ExprBase< Derived > const &param)
Set parameter using an array.
void setLambda(Real const &lambda)
set the lambda of the kernel
Hamming(Array const &data, Real const &lambda=1.)
constructor with a constant pointer on the data set
Hamming(Array const *p_data, ExprBase< Derived > const &param)
constructor with an array of parameter.
Hamming(Array const *p_data, Real const &lambda=1.)
constructor with a constant pointer on the data set
Real lambda_
lambda of the kernel
Stat::MultiFactor< Array > factors_
factors of the values
IKernelBase< Array > Base
void computeDiagonalElement()
Compute the diagonal element of the kernel.
virtual Real diag(int i) const
virtual method.
Hamming(Array const &data, ExprBase< Derived > const &param)
constructor with a constant pointer on the data set
Real const & lambda() const
virtual ~Hamming()
destructor
virtual Real comp(int i, int j) const
virtual method implementation.
Stat::MultiFactor< Array > const & factors() const
Interface Base class for the kernels classes.
Array const * p_data_
pointer on the data set
CSquareX gram_
the resulting gram_ matrix
virtual bool run()
compute Gram matrix
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Computation of the MultiFactors of a 2D Container.
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.