STK++ 0.9.13
STK_DiagGaussian_sk.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, 2013
28 * Author: Serge Iovleff
29 **/
30
35#ifndef STK_DIAGGAUSSIAN_SK_H
36#define STK_DIAGGAUSSIAN_SK_H
37
38#include "../DiagGaussianModels/STK_DiagGaussianBase.h"
39
40namespace STK
41{
42
43//forward declaration, to allow for recursive template
44template<class Array>class DiagGaussian_sk;
45
46namespace hidden
47{
50template<class Array_>
57
58} // namespace hidden
59
68template<class Array>
69class DiagGaussian_sk: public DiagGaussianBase<DiagGaussian_sk<Array> >
70{
71 public:
73 using Base::param_;
74 using Base::p_data;
75
90 void randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
92 bool run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
94 inline int computeNbFreeParameters() const
95 { return this->nbCluster()*p_data()->sizeCols() + this->nbCluster();}
96};
97
98/* Initialize randomly the parameters of the Gaussian mixture. The centers
99 * will be selected randomly among the data set and the standard-deviation
100 * will be set to 1.
101 */
102template<class Array>
103void DiagGaussian_sk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
104{
105 this->randomMean(p_tik);
106 // compute the standard deviation
107 Real variance;
108 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
109 {
110 variance = sqrt( ( p_tik->col(k).transpose()
111 *(*p_data() - (Const::Vector<Real>(this->nbSample()) * param_.mean_[k])
112 ).square()
113 ).sum() / (p_data()->sizeCols()*p_tk->elt(k))
114 );
115 param_.sigma_[k] = ((variance<=0) || !Arithmetic<Real>::isFinite(variance))
116 ? 1.
117 : std::sqrt(variance/(this->nbSample()*p_data()->sizeCols()));
118 }
119#ifdef STK_MIXTURE_VERY_VERBOSE
120 stk_cout << _T("DiagGaussian_sk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
121#endif
122}
123
124/* Compute the weighted mean and the common standard deviation. */
125template<class Array>
126bool DiagGaussian_sk<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
127{
128 // compute the means
129 if (!this->updateMean(p_tik)) return false;
130 // compute the standard deviation
131 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
132 {
133 param_.sigma_[k]
134 = sqrt( ( p_tik->col(k).transpose()
135 *(*p_data() - (Const::Vector<Real>(this->nbSample()) * param_.mean_[k])
136 ).square()
137 ).sum()
138 /(p_data()->sizeCols()*p_tk->elt(k))
139 );
140// if (param(k).sigma_ <= 0.) return false;
141 }
142 return true;
143}
144
145} // namespace STK
146
147#endif /* STK_DiagGaussian_SK_H */
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Define the constant point.
Base class for the diagonal Gaussian models.
Parameters param_
parameters of the derived mixture model.
The diagonal Gaussian mixture model DiagGaussian_sk assumes an equal standard deviation in each clust...
int computeNbFreeParameters() const
DiagGaussianBase< DiagGaussian_sk< Array > > Base
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the weighted mean and the common standard deviation.
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gaussian mixture.
DiagGaussian_sk(int nbCluster)
default constructor
DiagGaussian_sk(DiagGaussian_sk const &model)
copy constructor
hidden::CSlice< Derived, sizeRows_, 1 >::Result col(int j) const
implement the col operator using a reference on the column of the allocator
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.
ModelParameters< Clust::Gaussian_sk_ > Parameters
Type of the structure storing the parameters of a MixturGaussian_sk model.
Main class for the mixtures traits policy.