STK++ 0.9.13
STK_KmmBase.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff
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.org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Clustering
27 * created on: Oct 24, 2014
28 * Author: Serge Iovleff
29 **/
30
35#ifndef STK_KMMBASE_H
36#define STK_KMMBASE_H
37
38#include "../STK_IMixtureDensity.h"
39
45
46namespace STK
47{
48
52template<class Derived>
53class KmmBase: public IMixtureDensity<Derived>
54{
55 public:
58
59 using Base::param_;
60 using Base::nbSample;
61 using Base::nbCluster;
62
63 protected:
72 : Base(model)
74 , dik_(model.dik_)
75 {}
78
79 public:
80 // getters
82 inline Real sigma2(int k) const { return param_.sigma2(k);}
84 inline Real dim(int k) const { return param_.dim(k);}
86 inline Kernel::IKernel const* const p_kernel() const { return p_kernel_;}
88 inline CArrayXX dik() const { return dik_;}
89
90 // setter
92 inline void setDim(Real const& dim) { param_.dim_ = dim;}
94 template<class Vector>
95 inline void setDim(ExprBase<Vector> const& dim)
96 {
98 param_.dim_ = dim.asDerived();
99 }
100
106 {
109 }
115 template<class Weights>
116 inline Real impute(int i, int j, Weights const& pk) const { return 0.;}
120 inline Real rand(int i, int j, int k) const { return 0.;}
121
126 template<class ArrayParam>
132 void writeParameters(CArrayXX const* p_tik, ostream& os) const;
133
134 protected:
139
155 void compute_dik(CArrayXX const* p_tik, CPointX const* p_tk);
157 inline void initializeModelImpl() {/* do nothing*/}
158
159 private:
164 void setData(Array const& data) { this->p_dataij_ = 0;}
165};
166
167
168/* compute the intermediate quantities \f$ d_{ik}^m = \|\phi(x_i)-m^m_k\| \f$
169 * using the kernel trick.
170 **/
171template<class Derived>
172void KmmBase<Derived>::compute_dik(CArrayXX const* p_tik, CPointX const* p_tk)
173{
174#ifdef STK_KERNELS_DEBUG
175 stk_cout << _T("Entering KmmBase::compute_dik\n");
176 stk_cout << _T("dik_.cols() =") << dik_.cols() << _T("\n");
177 stk_cout << _T("dik_.rows() =") << dik_.rows() << _T("\n");
178#endif
179 CVectorX wik(dik_.rows());
180 for (int k=dik_.beginCols(); k<dik_.endCols(); ++k)
181 {
182 // Compute wik=\sum_{j=1}^n k(x_i,x_j) t_{jk}/t_{.k}, for k=1,..,K
183 for (int i= wik.begin(); i < wik.end(); ++i)
184 {
185 wik[i] = 0.;
186 for (int j= wik.begin(); j < wik.end(); ++j)
187 { wik[i] += p_kernel_->comp(i, j) * p_tik->elt(j,k);}
188 }
189 wik /= p_tk->elt(k);
190 // compute dik_ = k(i,i) - 2 * wik + \sum_{i=1}^n t_{ik} w_{ik}/t_{.k}
191 Real dk = p_tik->col(k).dot(wik)/p_tk->elt(k);
192 for (int i= wik.begin(); i<wik.end(); ++i)
193 { dik_(i,k) = p_kernel_->diag(i) - 2. * wik[i] + dk ;}
194 }
195#ifdef STK_KERNELS_DEBUG
196 stk_cout << _T("KmmBase::compute_dik done\n");
197#endif
198}
199
200/* @brief Initialize the model before its first use.
201 * This function is triggered when data set is set.
202 * In this interface, the @c initializeModel() method
203 * - set the number of samples and variables of the mixture model
204 * - call the derived class implemented method
205 * @code
206 * initializeModelImpl()
207 * @endcode
208 * for initialization of the specific model parameters if needed.
209 **/
210template<class Derived>
212{
213 // set dimensions
214 this->setNbSample(p_kernel_->nbSample());
215 dik_.resize(p_kernel_->nbSample(), nbCluster());
216 // call specific model initialization stuff (not really necessary)
217 this->asDerived().initializeModelImpl();
218 //compute_dik();
219}
220
221/* This function is used in order to get the current values of the
222 * parameters in an array.
223 * @param[out] params the array with the parameters of the mixture.
224 */
225template<class Derived>
226template<class ArrayParam>
228{
229 param.resize(this->nbCluster(), 2);
230 for (int k= param.beginRows(); k < param.endRows(); ++k)
231 {
232 param(k, baseIdx ) = param_.sigma2(k);
233 param(k, baseIdx+1) = param_.dim(k);
234 }
235}
236
237/* This function can be used to write summary of parameters to the output stream.
238 * @param os Stream where you want to write the summary of parameters.
239 */
240template<class Derived>
241inline void KmmBase<Derived>::writeParameters(CArrayXX const* p_tik, ostream& os) const
242{
243 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
244 {
245 os << _T("---> Component ") << k << _T("\n");
246 os << _T("sigma2 = ") << param_.sigma2(k) << _T("\n");
247 os << _T("dim = ") << param_.dim(k) << _T("\n");
248 }
249}
250
251} // namespace STK
252
253#endif /* STK_KMMBASE_H */
In this file we implement the final class CArrayPoint.
In this file we implement the final class CArrayVector.
In this file we implement the final class CArray.
In this file we define the Parameters classes for kernel mixture models.
In this file we define the Interface base class for computing a Kernels.
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
hidden::CSlice< Derived, sizeRows_, 1 >::Result col(int j) const
implement the col operator using a reference on the column of the allocator
Base class for all Mixture densities.
Parameters param_
parameters of the derived mixture model.
Array const * p_dataij_
pointer on the data set
Interface class for the kernels classes.
Base class for the Gaussian Kernel Mixture Models.
Definition STK_KmmBase.h:54
CArrayXX dik() const
Definition STK_KmmBase.h:88
int nbCluster() const
void getParameters(ArrayParam &params) const
This function is used in order to get the current values of the parameters in an array.
Real impute(int i, int j, Weights const &pk) const
void initializeModelImpl()
default implementation of initializeModelImpl (do nothing)
Kernel::IKernel const *const p_kernel() const
Definition STK_KmmBase.h:86
void setData(Array const &data)
Set the data set.
void setDim(Real const &dim)
set the dimensions of the kernel mixture model using an unique value
Definition STK_KmmBase.h:92
~KmmBase()
destructor
Definition STK_KmmBase.h:77
CArrayXX dik_
Array of the intermediate results dik.
Parameters param_
parameters of the derived mixture model.
KmmBase(KmmBase const &model)
copy constructor
Definition STK_KmmBase.h:71
KmmBase(int nbCluster)
default constructor
Definition STK_KmmBase.h:67
void compute_dik(CArrayXX const *p_tik, CPointX const *p_tk)
compute the distance of the ith individual to the kth centroid
Real dim(int k) const
Definition STK_KmmBase.h:84
Kernel::IKernel const * p_kernel_
pointer on the kernel
Real rand(int i, int j, int k) const
IMixtureDensity< Derived > Base
Definition STK_KmmBase.h:56
hidden::MixtureTraits< Derived >::Array Array
Definition STK_KmmBase.h:57
void initializeModel()
Initialize the model before its first use.
void setKernel(Kernel::IKernel const *p_kernel)
set the dimensions of the kernel mixture model using an unique value.
Real sigma2(int k) const
Definition STK_KmmBase.h:82
void writeParameters(CArrayXX const *p_tik, ostream &os) const
This function can be used to write summary of parameters to the output stream.
void setDim(ExprBase< Vector > const &dim)
set the dimension of the kernel mixture model
Definition STK_KmmBase.h:95
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
const int baseIdx
base index of the containers created in STK++.
double Real
STK fundamental type of Real values.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
The namespace STK is the main domain space of the Statistical ToolKit project.
Main class for the mixtures traits policy.