STK++ 0.9.13
STK_HDGaussian_AjkBkQkD.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_HDGAUSSIAN_AJKBKQKD_H
36#define STK_HDGAUSSIAN_AJKBKQKD_H
37
38#include "../HDGaussianModels/STK_HDGaussianBase.h"
39
40namespace STK
41{
42
43//forward declaration, to allow for recursive template
44template<class Array>class HDGaussian_AjkBkQkD;
45
46namespace hidden
47{
50template<class Array_>
51struct MixtureTraits< HDGaussian_AjkBkQkD_<Array_> >
52{
53 typedef Array_ Array;
56};
57
58} // namespace hidden
59
63template<>
64struct ModelParameters<Clust::HDGaussian_AjkBkQkD_>
65{
73 ModelParameters(int nbCluster): mean_(nbCluster), sigma_(nbCluster) {}
78 : mean_(param.mean_), sigma_(param.sigma_)
79 {}
83 inline Real const& mean(int k, int j) const { return mean_[k][j];}
85 inline Real const& sigma(int k, int j) const { return sigma_[k][j];}
87 inline void resize(Range const& range)
88 {
89 for (int k = mean_.begin(); k< mean_.end(); ++k)
90 { mean_[k].resize(range) =0;
91 sigma_[k].resize(range) = 1.;
92 }
93 }
94};
95
101template<class Array>
102class HDGaussian_AjkBkQkD: public HDGaussianBase<HDGaussian_AjkBkQkD<Array> >
103{
104 public:
106 using Base::param_;
107 using Base::p_data;
108
122 inline Real lnComponentProbability(int i, int k) const
123 {
124 Real sum =0.;
125 for (int j=p_data()->beginCols(); j<p_data()->endCols(); ++j)
126 {
127 if (param_.sigma_[k][j])
128 { sum += Law::Normal::lpdf(p_data()->elt(i,j), param_.mean_[k][j], param_.sigma_[k][j]);}
129 }
130 return sum;
131 }
136 void randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
138 bool run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
140 inline int computeNbFreeParameters() const
141 { return 2*this->nbCluster()*p_data()->sizeCols();}
142};
143
144/* Initialize randomly the parameters of the Gaussian mixture. The centers
145 * will be selected randomly among the data set and the standard-deviation
146 * will be set to 1.
147 */
148template<class Array>
149void HDGaussian_AjkBkQkD<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
150{
151 this->randomMean(p_tik);
152 // compute the standard deviation
153 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
154 {
155 param_.sigma_[k] = Stat::varianceWithFixedMean(*p_data(), p_tik->col(k), param_.mean_[k], false).sqrt();
156 }
157#ifdef STK_MIXTURE_VERY_VERBOSE
158 stk_cout << _T("MixtureHDGaussian_ajk_bk_qk_d<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
159#endif
160}
161
162/* Compute the weighted means and the weighted standard deviations. */
163template<class Array>
164bool HDGaussian_AjkBkQkD<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
165{
166 // compute the means
167 if (!this->updateMean(p_tik)) return false;
168 // compute the standard deviation
169 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
170 {
171 param_.sigma_[k] = Stat::varianceWithFixedMean(*p_data(), p_tik->col(k), param_.mean_[k], false).sqrt();
172#ifdef STK_MIXTURE_DEBUG
173 if( (param_.sigma_[k] <= 0).any() )
174 {
175 stk_cout << _T("MixtureHDGaussian_ajk_bk_qk_d::run() failed\n");
176 stk_cout << _T("p_tik->col(") << k << _T(") =\n") << p_tik->col(k).transpose() << _T("\n");
177 stk_cout << _T("param_.mean_[") << k << _T("] =") << param_.mean_[k];
178 stk_cout << _T("param_.sigma_[") << k << _T("] =") << param_.sigma_[k];
179 }
180#endif
181 }
182 return true;
183}
184
185} // namespace STK
186
187#endif /* STK_HDGAUSSIAN_AJKBKQKD_H */
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Base class for the diagonal Gaussian models.
Array const *const & p_data() const
Parameters param_
parameters of the derived mixture model.
Base class for the diagonal Gaussian models.
The diagonal Gaussian mixture model HDGaussian_AjkBkQkD is the most general HD Gaussian model and hav...
DiagGaussianBase< HDGaussian_AjkBkQkDk< Array > > Base
Array const *const & p_data() const
HDGaussian_AjkBkQkD(HDGaussian_AjkBkQkD const &model)
copy constructor
Parameters param_
parameters of the derived mixture model.
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the weighted mean and the common standard deviation.
Real lnComponentProbability(int i, int k) const
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gaussian mixture.
HDGaussian_AjkBkQkD(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
virtual Real lpdf(Real const &x) const
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
double Real
STK fundamental type of Real values.
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.
void resize(Range const &range)
resize the set of parameter
Array1D< CPointX > mean_
array of size nbCluster with the parameters mean of the variables
ModelParameters(ModelParameters const &param)
copy constructor.
Array1D< CPointX > sigma_
standard deviation of the variables
struct storing the parameters of the mixture.
ModelParameters< Clust::HDGaussian_AjkBkQkD_ > Parameters
Type of the structure storing the parameters of a MixturGaussian_sjk model.
Main class for the mixtures traits policy.