STK++ 0.9.13
STK_Gamma_ajk_bk.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
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: 5 sept. 2013
28 * Author: iovleff, serge.iovleff@stkpp.org
29 **/
30
35#ifndef STK_GAMMA_AJK_BK_H
36#define STK_GAMMA_AJK_BK_H
37
38
40#include "../GammaModels/STK_GammaBase.h"
41
42#define MAXITER 400
43#define TOL 1e-8
44
45namespace STK
46{
47template<class Array>class Gamma_ajk_bk;
48
49namespace hidden
50{
54template<class Array_>
61
62} // namespace Clust
63
73template<class Array>
74class Gamma_ajk_bk: public GammaBase< Gamma_ajk_bk<Array> >
75{
76 public:
78 using Base::param_;
79 using Base::p_data;
80 using Base::meanjk;
81 using Base::variancejk;
82
98 void randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
100 bool run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
102 inline int computeNbFreeParameters() const
103 { return this->nbCluster()*p_data()->sizeCols()+this->nbCluster();}
104};
105
106/* Initialize randomly the parameters of the Gaussian mixture. The centers
107 * will be selected randomly among the data set and the standard-deviation
108 * will be set to 1.
109 */
110template<class Array>
111void Gamma_ajk_bk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
112{
113 // compute moments
114 this->moments(p_tik);
115 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
116 {
117 Real value =0;
118 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
119 {
120 Real mean = meanjk(j,k), variance = variancejk(j,k);
121 param_.shape_[k][j] = Law::Exponential::rand((mean*mean/variance));
122 value += variance/mean;
123 }
124 param_.scale_[k] = Law::Exponential::rand(value/(p_data()->sizeCols()));
125 }
126#ifdef STK_MIXTURE_VERY_VERBOSE
127 stk_cout << _T(" Gamma_ajk_bk<Array>::randomInit done\n");
128#endif
129}
130
131/* Compute the weighted mean and the common variance. */
132template<class Array>
133bool Gamma_ajk_bk<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
134{
135 if (!this->moments(p_tik)) { return false;}
136 // start estimations of the ajk and bj
137 Real qvalue = this->qValue(p_tik, p_tk);
138 // enter iterative algorithm
139 int iter;
140 for(iter = 0; iter<MAXITER; ++iter)
141 {
142 // compute ajk
143 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
144 {
145 for (int j=p_data()->beginCols(); j<p_data()->endCols(); ++j)
146 {
147 // moment estimate and oldest value
148 Real x0 = meanjk(j,k)*meanjk(j,k)/variancejk(j,k);
149 Real x1 = param_.shape_[k][j];
150 if ((x0 <=0.) || !Arithmetic<Real>::isFinite(x0)) return false;
151 // compute shape
152 hidden::invPsi f(param_.meanLog_[k][j] - std::log(param_.scale_[k]));
153 Real a = Algo::findZero(f, x0, x1, TOL);
154
156 {
157 param_.shape_[k][j] = x0; // use moment estimate
158#ifdef STK_MIXTURE_DEBUG
159 stk_cout << _T("ML estimation failed in Gamma_ajk_bj::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) \n");
160 stk_cout << "x0 =" << x0 << _T("\n";);
161 stk_cout << "f(x0) =" << f(x0) << _T("\n";);
162 stk_cout << "x1 =" << x1 << _T("\n";);
163 stk_cout << "f(x1) =" << f(x1) << _T("\n";);
164#endif
165 }
166 else { param_.shape_[k][j] = a;}
167 }
168 // compute bk
169 param_.scale_[k] = param_.mean_[k].sum()/ param_.shape_[k].sum();
170 } // end ajk
171 // check convergence
172 Real value = this->qValue(p_tik, p_tk);
173#ifdef STK_MIXTURE_VERBOSE
174 if (value < qvalue)
175 {
176 stk_cout << _T("In Gamma_ajk_bk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) : run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) diverge\n");
177 stk_cout << _T("New value =") << value << _T(", qvalue =") << qvalue << _T("\n");
178 }
179#endif
180 if ((value - qvalue) < TOL) break;
181 qvalue = value;
182 }
183#ifdef STK_MIXTURE_VERBOSE
184 if (iter == MAXITER)
185 {
186 stk_cout << _T("In Gamma_ajk_bk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) : run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) did not converge\n");
187 stk_cout << _T("qvalue =") << qvalue << _T("\n");
188 }
189#endif
190 return true;
191}
192
193} // namespace STK
194
195#undef MAXITER
196#undef TOL
197
198#endif /* STK_Gamma_AJK_BK_H */
#define TOL
In this file we implement the exponential law.
#define MAXITER
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Base class for the gamma models.
Parameters param_
parameters of the derived mixture model.
Real meanjk(int j, int k)
get the weighted mean of the jth variable of the kth cluster.
Real variancejk(int j, int k)
get the weighted variance of the jth variable of the kth cluster.
Gamma_ajk_bk is a mixture model of the following form.
~Gamma_ajk_bk()
destructor
GammaBase< Gamma_ajk_bk< Array > > Base
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) .
Gamma_ajk_bk(Gamma_ajk_bk const &model)
copy constructor
Gamma_ajk_bk(int nbCluster)
default constructor
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gamma mixture.
int computeNbFreeParameters() const
virtual Real rand() const
Generate a pseudo Exponential random variate.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Functor computing the difference between the psi function and a fixed value.
Real findZero(IFunction< Function > const &f, Real const &x0, Real const &x1, Real tol)
find the zero of a function.
double Real
STK fundamental type of Real values.
hidden::SliceVisitorSelector< Derived, hidden::MeanVisitor, Arrays::by_col_ >::type_result mean(Derived const &A)
If A is a row-vector or a column-vector then the function will return the usual mean value of the vec...
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
ModelParameters< Clust::Gamma_ajk_bk_ > Parameters
Type of the structure storing the parameters of a Gamma_ajk_bk model.
Main class for the mixtures traits policy.