STK++ 0.9.13
STK_DiagGaussian_sjk.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_SJK_H
36#define STK_DIAGGAUSSIAN_SJK_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_sjk;
45
46namespace hidden
47{
50template<class Array_>
57
58} // namespace hidden
59
69template<class Array>
70class DiagGaussian_sjk: public DiagGaussianBase<DiagGaussian_sjk<Array> >
71{
72 public:
74 using Base::param_;
75 using Base::p_data;
76
91 void randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
93 bool run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
95 inline int computeNbFreeParameters() const
96 { return 2*this->nbCluster()*p_data()->sizeCols();}
97};
98
99/* Initialize randomly the parameters of the Gaussian mixture. The centers
100 * will be selected randomly among the data set and the standard-deviation
101 * will be set to 1.
102 */
103template<class Array>
104void DiagGaussian_sjk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
105{
106 this->randomMean(p_tik);
107 // compute the standard deviation
108 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
109 {
110 param_.sigma_[k] = Stat::varianceWithFixedMean(*p_data(), p_tik->col(k), param_.mean_[k], false).sqrt();
111 }
112#ifdef STK_MIXTURE_VERY_VERBOSE
113 stk_cout << _T("DiagGaussian_sjk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
114#endif
115}
116
117/* Compute the weighted means and the weighted standard deviations. */
118template<class Array>
119bool DiagGaussian_sjk<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
120{
121 // compute the means
122 if (!this->updateMean(p_tik)) return false;
123 // compute the standard deviation
124 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
125 {
126 param_.sigma_[k] = Stat::varianceWithFixedMean(*p_data(), p_tik->col(k), param_.mean_[k], false).sqrt();
127#ifdef STK_MIXTURE_DEBUG
128 if( (param_.sigma_[k] <= 0).any() )
129 {
130 stk_cout << _T("DiagGaussian_sjk::run() failed\n");
131 stk_cout << _T("p_tik->col(") << k << _T(") =\n") << p_tik->col(k).transpose() << _T("\n");
132 stk_cout << _T("param_.mean_[") << k << _T("] =") << param_.mean_[k];
133 stk_cout << _T("param_.sigma_[") << k << _T("] =") << param_.sigma_[k];
134 }
135#endif
136 }
137 return true;
138}
139
140} // namespace STK
141
142#endif /* STK_DiagGaussian_SJK_H */
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Base class for the diagonal Gaussian models.
Parameters param_
parameters of the derived mixture model.
The diagonal Gaussian mixture model DiagGaussian_sjk is the most general diagonal Gaussian model and ...
DiagGaussian_sjk(DiagGaussian_sjk const &model)
copy constructor
DiagGaussianBase< DiagGaussian_sjk< Array > > Base
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gaussian mixture.
DiagGaussian_sjk(int nbCluster)
default constructor
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the weighted mean and the common standard deviation.
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...
hidden::FunctorTraits< Derived, VarianceWithFixedMeanOp >::Row varianceWithFixedMean(Derived const &A, MeanType const &mean, bool unbiased)
Compute the VarianceWithFixedMean(s) value(s) of A.
The namespace STK is the main domain space of the Statistical ToolKit project.
ModelParameters< Clust::Gaussian_sjk_ > Parameters
Type of the structure storing the parameters of a MixturGaussian_sjk model.
Main class for the mixtures traits policy.