STK++ 0.9.13
STK_Gamma_a_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_A_BK_H
36#define STK_GAMMA_A_BK_H
37
39#include "../GammaModels/STK_GammaBase.h"
40
41namespace STK
42{
43template<class Array>class Gamma_a_bk;
44
45namespace hidden
46{
50template<class Array_>
57
58} // namespace Clust
59
69template<class Array>
70class Gamma_a_bk: public GammaBase< Gamma_a_bk<Array> >
71{
72 public:
74 using Base::param_;
75
76 using Base::p_data;
77 using Base::moments;
78 using Base::meanjk;
79 using Base::variancejk;
80 using Base::meank;
81 using Base::variancek;
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 { return this->nbCluster()+1;}
103};
104
105template<class Array>
106void Gamma_a_bk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
107{
108 // compute moments
109 this->moments(p_tik);
110 Real value = 0.0;
111 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
112 {
113 Real mean = meank(k), variance = variancek(k);
114 // generate scales
115 param_.scale_[k] = Law::Exponential::rand(variance/mean);
116 value += p_tk->elt(k) * (mean*mean/variance);
117 }
118 param_.shape_ = STK::Law::Exponential::rand(value/this->nbSample());
119#ifdef STK_MIXTURE_VERY_VERBOSE
120 stk_cout << _T(" Gamma_a_bk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
121#endif
122}
123
124
125/* Compute the weighted mean and the common variance. */
126template<class Array>
127bool Gamma_a_bk<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
128{
129 bool flag = true;
130 if (!moments(p_tik)) { flag = false;}
131 // estimate a
132 Real y =0.0, x0 = 0.0, x1 = param_.shape_;
133 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
134 {
135 Real mean = meank(k);
136 y += p_tk->elt(k) * (param_.meanLog_[k] - std::log(mean)).sum();
137 x0 += p_tk->elt(k) * (mean*mean/variancek(k));
138 }
139 y /= (this->nbSample()*p_data()->sizeCols());
140 x0 /= this->nbSample();
141 // moment estimate and oldest value
142 if ((x0 <=0.) || (isNA(x0)))
143 { x0 = 1; flag = false;}
144
145 // get shape
147 Real a = 1.;
148 try
149 {
150 a = Algo::findZero(f, x0, x1, 1e-08);
151 }
152 catch (...)
153 {
154#ifdef STK_MIXTURE_DEBUG
155 stk_cout << "ML estimation failed in Gamma_a_bjk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) \n";
156 stk_cout << "x0 =" << x0 << _T("\n";);
157 stk_cout << "f(x0) =" << f(x0) << _T("\n";);
158 stk_cout << "x1 =" << x1 << _T("\n";);
159 stk_cout << "f(x1) =" << f(x1) << _T("\n";);
160#endif
161 a = x0; // use moment estimate
162 flag = false;
163 }
164// Real a = Algo::findZero(f, x0, x1, 1e-08);
166 {
167#ifdef STK_MIXTURE_DEBUG
168 stk_cout << "ML estimation failed in Gamma_a_bjk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) \n";
169 stk_cout << "x0 =" << x0 << _T("\n";);
170 stk_cout << "f(x0) =" << f(x0) << _T("\n";);
171 stk_cout << "x1 =" << x1 << _T("\n";);
172 stk_cout << "f(x1) =" << f(x1) << _T("\n";);
173#endif
174 a = 1.; // use default value
175 flag = false;
176 }
177 // set values
178 param_.shape_ = a;
179 // estimate b
180 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
181 { param_.scale_[k] = meank(k)/a;}
182 return flag;
183}
184
185} // namespace STK
186
187
188#endif /* STK_Gamma_A_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.
Real meank(int k)
get the mean of the weighted means of the kth cluster.
Real variancek(int k)
get the mean of the weighted variances of the kth cluster.
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.
bool moments(CArrayXX const *p_tik)
compute the weighted moments of a gamma mixture.
Real variancejk(int j, int k)
get the weighted variance of the jth variable of the kth cluster.
Gamma_a_bk is a mixture model of the following form.
int computeNbFreeParameters() const
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gamma mixture.
~Gamma_a_bk()
destructor
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_a_bk(Gamma_a_bk const &model)
copy constructor
GammaBase< Gamma_a_bk< Array > > Base
Gamma_a_bk(int nbCluster)
default constructor
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
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::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_a_bk_ > Parameters
Type of the structure storing the parameters of a Mixture Gamma_a_bk model.
Main class for the mixtures traits policy.