STK++ 0.9.13
STK_IMixtureDensity.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::Clustering
27 * created on: 16 oct. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 * Originally created by Parmeet Bhatia <b..._DOT_p..._AT_gmail_Dot_com>
30 **/
31
37#ifndef STK_IMIXTUREDENSITY_H
38#define STK_IMIXTUREDENSITY_H
39
40#include "STK_Clust_Util.h"
41
44
45#ifdef STK_MIXTURE_DEBUG
47#endif
48
49namespace STK
50{
98template<class Derived>
99class IMixtureDensity: public IRecursiveTemplate<Derived>
101 public:
105
106 protected:
118
119 public:
122
124 inline int nbCluster() const { return nbCluster_;}
126 inline int nbSample() const { return nbSample_;}
128 inline Real lnNbSample() const
129 { return (nbSample_ <= 0) ? -Arithmetic<Real>::infinity() : std::log((Real)nbSample_);}
131 inline Array const* const& p_data() const { return p_dataij_;}
133 inline Parameters const& param() const {return param_;}
134
139 void setData(Array const& data);
149 void setData(Array const& data, int nbRow, int nbCol, bool byRow = true);
155 inline bool initializeStep() { return this->asDerived().initializeStepImpl();}
158 inline void setParametersStep()
159 {
160 param_.setParametersStep();
161 this->asDerived().setParametersImpl();
162 }
165 inline void finalizeStep() { this->asDerived().finalizeStepImpl();}
166
171 template<class Weights>
172 inline Type sample(int i, int j, Weights const& tk) const
173 { return this->asDerived().rand(i, j, Law::Categorical::rand(tk));}
174
175 protected:
181
193 // default implementation of the pseudo-virtual methods
195 inline bool initializeStepImpl() { return true;/* do nothing*/}
197 inline void finalizeStepImpl() {/* do nothing*/}
198
203 inline void setNbSample( int nbSample) { nbSample_ = nbSample;}
204
205 private:
212};
213
214
215/* Default constructor.
216 * @param nbCluster the number of cluster
217 **/
218template<class Derived>
220 : param_(nbCluster)
221 , nbCluster_(nbCluster)
222 , nbSample_(0)
223 , p_dataij_(0)
224{}
225/* copy constructor.
226 * - The Parameter class is copied using the copy constructor.
227 * - The pointer on the data set is copied as-is. Check if you should not
228 * change it on the copied object.
229 * @param model the model to copy
230 **/
231template<class Derived>
233 : param_(model.param_)
234 , nbCluster_(model.nbCluster_)
235 , nbSample_(model.nbSample_)
236 , p_dataij_(model.p_dataij_)
237{}
238
239/* @brief Set the data set.
240 * Setting a (new) data set will trigger the initializeModel() method.
241 * @param data the data set to set
242 **/
243template<class Derived>
245{
246 p_dataij_ = &data;
247 initializeModel();
248}
249/* @brief Set the data set.
250 * Setting a (new) data set will trigger the initializeModel() method.
251 * @param data the data set to set
252 **/
253template<class Derived>
254void IMixtureDensity<Derived>::setData(Array const& data, int nbRow, int nbCol, bool byRow)
255{
256 p_dataij_ = &data;
257 initializeModel();
258}
259
260
261/* @brief Initialize the model before its first use.
262 * This function is triggered when data set is set.
263 * In this interface, the @c initializeModel() method
264 * - set the number of samples of the mixture model
265 * - call the derived class implemented method
266 * @code
267 * initializeModelImpl()
268 * @endcode
269 * for initialization of the specific model parameters if needed.
270 **/
271template<class Derived>
273{
274 // set dimensions
275 this->setNbSample(p_dataij_->sizeRows());
276 // call specific model initialization stuff
277 this->asDerived().initializeModelImpl();
278}
279
280} // namespace STK
281
282#endif /* STK_IMIXTUREDENSITY_H */
In this file we define the enum, constants and utilities functions of the Clustering project.
This file define methods for displaying Arrays and Expressions.
In this file we define the Categorical distribution.
In this file we define the constant and utilities methods used in the project Model.
Base class for all Mixture densities.
void setNbSample(int nbSample)
Set the number of sample of the model (needed by kernel models)
Array const *const & p_data() const
void initializeModel()
Initialize the model before its first use.
void finalizeStepImpl()
default implementation of finalizeStepImpl (do nothing)
hidden::Traits< Array >::Type Type
void setData(Array const &data)
Set the data set.
IMixtureDensity(IMixtureDensity const &model)
copy constructor.
Parameters param_
parameters of the derived mixture model.
Parameters const & param() const
bool initializeStep()
This function will be called at the beginning of the estimation process once the model is created and...
hidden::MixtureTraits< Derived >::Parameters Parameters
int nbCluster_
number of cluster.
hidden::MixtureTraits< Derived >::Array Array
IMixtureDensity(int nbCluster)
Default constructor.
int nbSample_
total available samples
void finalizeStep()
This function will be called once the model is estimated.
bool initializeStepImpl()
default implementation of initializeStepImpl (do nothing and return true)
Type sample(int i, int j, Weights const &tk) const
Array const * p_dataij_
pointer on the data set
void setParametersStep()
set the parameters obtained with the intermediate results and release the intermediate results.
void setData(Array const &data, int nbRow, int nbCol, bool byRow=true)
Set the data set and give dimensions.
Interface base class for all classes implementing the curious recursive template paradigm.
Derived & asDerived()
static cast : return a reference of this with a cast to the derived class.
virtual int rand() const
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
Main class for the mixtures traits policy.