STK++ 0.9.13
STK_DiagGaussian_sj.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_SJ_H
36#define STK_DIAGGAUSSIAN_SJ_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_sj;
45
46namespace hidden
47{
50template<class Array_>
57
58} // namespace hidden
59
68template<class Array>
69class DiagGaussian_sj: public DiagGaussianBase<DiagGaussian_sj<Array> >
70{
71 public:
73 using Base::param_;
74 using Base::p_data;
75
92 void randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
94 bool run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
96 inline int computeNbFreeParameters() const
97 { return this->nbCluster()*p_data()->sizeCols()+p_data()->sizeCols();}
98};
99
100/* Initialize randomly the parameters of the Gaussian mixture. The centers
101 * will be selected randomly among the data set and the standard-deviation
102 * will be set to 1.
103 */
104template<class Array>
105void DiagGaussian_sj<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
106{
107 // compute the initial mean
108 this->randomMean(p_tik);
109 // compute the standard deviation
110 Array2DPoint<Real> variance(p_data()->cols(), 0.);
111 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
112 {
113 variance += p_tik->col(k).transpose()
114 *(*p_data() - (Const::Vector<Real>(this->nbSample()) * param_.mean_[k])
115 ).square()
116 ;
117 }
118 // store the standard deviation
119 param_.sigma_ = (variance /= this->nbSample()).sqrt();
120#ifdef STK_MIXTURE_VERY_VERBOSE
121 stk_cout << _T("DiagGaussian_sj<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
122#endif
123}
124
125/* Compute the weighted mean and the common standard deviation. */
126template<class Array>
127bool DiagGaussian_sj<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
128{
129 // compute the means
130 if (!this->updateMean(p_tik)) return false;
131 // compute the standard deviation
132 Array2DPoint<Real> variance(p_data()->cols(), 0.);
133 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
134 {
135 variance += p_tik->col(k).transpose()
136 *(*p_data() - (Const::Vector<Real>(this->nbSample()) * param_.mean_[k])
137 ).square()
138 ;
139 }
140// if (variance.nbAvailableValues() != p_data()->sizeCols()) return false;
141// if ((variance > 0.).template cast<int>().sum() != p_data()->sizeCols()) return false;
142 // compute the standard deviation
143 param_.sigma_ = (variance /= this->nbSample()).sqrt();
144 return true;
145}
146
147} // namespace STK
148
149#endif /* STK_DiagGaussian_SJ_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_sj have a density function of the form.
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the weighted mean and the common standard deviation.
DiagGaussian_sj(int nbCluster)
default constructor
int computeNbFreeParameters() const
DiagGaussian_sj(DiagGaussian_sj const &model)
copy constructor
DiagGaussianBase< DiagGaussian_sj< Array > > Base
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gaussian mixture.
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...
The namespace STK is the main domain space of the Statistical ToolKit project.
ModelParameters< Clust::Gaussian_sj_ > Parameters
Type of the structure storing the parameters of a MixturGaussian_sj model.
Main class for the mixtures traits policy.