STK++ 0.9.13
STK_Gamma_ak_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_AK_BK_H
36#define STK_GAMMA_AK_BK_H
37
39#include "../GammaModels/STK_GammaBase.h"
40
41
42namespace STK
43{
44template<class Array>class Gamma_ak_bk;
45
46namespace hidden
47{
51template<class Array_>
58
59} // namespace Clust
60
70template<class Array>
71class Gamma_ak_bk: public GammaBase< Gamma_ak_bk<Array> >
72{
73 public:
75 using Base::param_;
76 using Base::p_data;
77 using Base::meanjk;
78 using Base::variancejk;
79
95 void randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
97 bool run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
99 inline int computeNbFreeParameters() const { return 2*this->nbCluster();}
100};
101
102/* Initialize randomly the parameters of the Gaussian mixture. The centers
103 * will be selected randomly among the data set and the standard-deviation
104 * will be set to 1.
105 */
106template<class Array>
107void Gamma_ak_bk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
108{
109 // compute moments
110 this->moments(p_tik);
111 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
112 {
113 Real mean = this->meank(k), variance = this->variancek(k);
114 param_.shape_[k]= Law::Exponential::rand((mean*mean/variance));
115 param_.scale_[k] = Law::Exponential::rand((variance/mean));
116 }
117#ifdef STK_MIXTURE_VERY_VERBOSE
118 stk_cout << _T(" Gamma_ak_bk<Array>::randomInit done\n");
119#endif
120}
121
122/* Compute the weighted mean and the common variance. */
123template<class Array>
124bool Gamma_ak_bk<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
125{
126 if (!this->moments(p_tik)) { return false;}
127 // estimate a and b
128 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
129 {
130 // moment estimate and oldest value
131 Real x0 = this->meank(k)*this->meank(k)/this->variancek(k);
132 Real x1 = param_.shape_[k];
133 if ((x0 <=0.) || (isNA(x0))) return false;
134 // get shape
135 hidden::invPsiMLog f( (param_.meanLog_[k]-std::log( this->meank(k))).mean() );
136 Real a = Algo::findZero(f, x0, x1, 1e-08);
138 {
139#ifdef STK_MIXTURE_DEBUG
140 stk_cout << "ML estimation failed in Gamma_ak_bjk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) \n";
141 stk_cout << "x0 =" << x0 << _T("\n";);
142 stk_cout << "f(x0) =" << f(x0) << _T("\n";);
143 stk_cout << "x1 =" << x1 << _T("\n";);
144 stk_cout << "f(x1) =" << f(x1) << _T("\n";);
145#endif
146 a = x0; // use moment estimate
147 }
148 // set values
149 param_.shape_[k]= a;
150 param_.scale_[k] = this->meank(k)/a;
151 }
152 return true;
153}
154
155
156} // namespace STK
157
158#endif /* STK_Gamma_AK_BK_H */
In this file we implement the exponential law.
#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_ak_bk is a mixture model of the following form.
Gamma_ak_bk(int nbCluster)
default constructor
GammaBase< Gamma_ak_bk< Array > > Base
int computeNbFreeParameters() const
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gamma mixture.
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_ak_bk(Gamma_ak_bk const &model)
copy constructor
~Gamma_ak_bk()
destructor
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 derivative of the lnLikelihood of a gamma_ajk_bjk model.
Real findZero(IFunction< Function > const &f, Real const &x0, Real const &x1, Real tol)
find the zero of a function.
bool isNA(Type const &x)
utility method allowing to know if a value is a NA (Not Available) value
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_ak_bk_ > Parameters
Type of the structure storing the parameters of a Gamma_ak_bk model.
Main class for the mixtures traits policy.