STK++ 0.9.13
STK_DiagGaussian_sjsk.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_SJSK_H
36#define STK_DIAGGAUSSIAN_SJSK_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_sjsk;
45
46namespace hidden
47{
50template<class Array_>
57
58} // namespace hidden
59
68template<class Array>
69class DiagGaussian_sjsk: public DiagGaussianBase<DiagGaussian_sjsk<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 2*this->nbCluster()*p_data()->sizeCols();}
96};
97
101template<class Array>
103/* copy constructor
104 * @param model The model to copy
105 **/
106template<class Array>
108/* Initialize randomly the parameters of the Gaussian mixture. The centers
109 * will be selected randomly among the data set and the standard-deviation
110 * will be set to 1.
111 */
112template<class Array>
113void DiagGaussian_sjsk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
114{
115 this->randomMean(p_tik);
116 // compute the standard deviation
117 //Real sd;
118 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
119 {
120 param_.sigma_[k] = Stat::varianceWithFixedMean(*p_data(), p_tik->col(k), param_.mean_[k], false).sqrt();
121 }
122 param_.sigmaj_ = 1.;
123#ifdef STK_MIXTURE_VERY_VERBOSE
124 stk_cout << _T("DiagGaussian_sjsk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
125#endif
126}
127
128/* Compute the weighted means and the weighted standard deviations. */
129template<class Array>
130bool DiagGaussian_sjsk<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
131{
132 // compute the means
133 if (!this->updateMean(p_tik)) return false;
134 // compute the standard deviation
135 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
136 {
137 param_.sigma_[k] = Stat::varianceWithFixedMean(*p_data(), p_tik->col(k), param_.mean_[k], false).sqrt();
138#ifdef STK_MIXTURE_DEBUG
139 if( (param_.sigma_[k] <= 0).any() )
140 {
141 stk_cout << _T("DiagGaussian_sjsk::run() failed\n");
142 stk_cout << _T("p_tik->col(") << k << _T(") =\n") << p_tik->col(k).transpose() << _T("\n");
143 stk_cout << _T("param_.mean_[") << k << _T("] =") << param_.mean_[k];
144 stk_cout << _T("param_.sigma_[") << k << _T("] =") << param_.sigma_[k];
145 }
146#endif
147 }
148 return true;
149}
150
151} // namespace STK
152
153#endif /* STK_DIAGGAUSSIAN_SJSK_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_sjsk is an intermediate diagonal Gaussian model with...
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gaussian mixture.
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the weighted mean and the common standard deviation.
DiagGaussianBase< DiagGaussian_sjsk< Array > > Base
DiagGaussian_sjsk(int nbCluster)
default 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...
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_sjsk_ > Parameters
Type of the structure storing the parameters of a MixturGaussian_sjk model.
Main class for the mixtures traits policy.