STK++ 0.9.13
STK_GaussianAAModel.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_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::AAModels
27 * Purpose: Interface base class for AA models.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
37#ifndef STK_GAUSSIANAAMODEL_H
38#define STK_GAUSSIANAAMODEL_H
39
40#include "STK_IAAModel.h"
42
46
47namespace STK
48{
49
50template<class> class GaussianAAModel;
51
52namespace hidden
53{
54
55template<class Data_>
57{
58 class Void {};
59 typedef Data_ Array;
60 typedef Data_ Data;
61 typedef Void ParamHandler;
62};
63
64} // namespace hidden
100template<class Array>
101class GaussianAAModel: public IAAModel<Array>
102 , public IStatModel< GaussianAAModel<Array> >
103{
104 public:
106 using IAAModel<Array>::p_regressor_;
107 using IAAModel<Array>::p_reducer_;
108 using IAAModel<Array>::p_reduced_;
109 using IAAModel<Array>::p_predicted_;
110 using IAAModel<Array>::p_residuals_;
111 using IAAModel<Array>::dim;
115 GaussianAAModel( Array* p_workData);
119 GaussianAAModel( Array& workData);
121 inline virtual ~GaussianAAModel() {}
123 inline Real const& projectedLnLikelihood() const { return projectedLnLikelihood_;}
125 inline Real const& residualLnLikelihood() const { return residualLnLikelihood_;}
129 inline ArraySquareX const& residualCovariance() const { return residualCovariance_;}
131 inline Real const& residualVariance() const { return residualVariance_;}
135 virtual void setWorkData(Array& workData);
143
144 protected:
163
164 private:
175};
176
177/* Constructor.
178 * @param p_workData a pointer on the data set to process
179 **/
180template<class Array>
182 : IAAModel<Array>(p_workData)
183 , Base(p_workData)
184 , projectedCovariance_()
185 , residualCovariance_()
186 , residualVariance_(0.)
187 , projectedLnLikelihood_(0.)
188 , residualLnLikelihood_(0.)
189{ }
190
191// constructor
192template<class Array>
194 : IAAModel<Array>(workData)
195 , Base(workData)
196 , projectedCovariance_()
197 , residualCovariance_()
198 , residualVariance_(0.)
199 , projectedLnLikelihood_(0.)
200 , residualLnLikelihood_(0.)
201{ }
202
203/* update the container when the data set is modified. **/
204template<class Array>
206{
207 // update data set and flags for the IAAModel part
209 // set dimensions to new size for the IStatModel part
210 Base::setData(workData);
211}
212
213/* compute the ln-likelihood of the model */
214template<class Array>
216{
217#ifdef STK_AAMODELS_VERBOSE
218 stk_cout << _T("GaussianAAModel::computeModelParameters().\n");
219#endif
220 // compute the number of free parameters
221 computeNbFreeParameters();
222 // compute the covariance matrix of the residual and the residual variance
223 computeResidualCovariance();
224 // compute projected lnLikelihood
225 computeProjectedLnLikelihood();
226 // compute projected lnLikelihood
227 computeResidualLnLikelihood();
228 // compute complete nLikelihood
229 this->setLnLikelihood(projectedLnLikelihood_ + residualLnLikelihood_);
230
231#ifdef STK_AAMODELS_VERBOSE
232 stk_cout << _T("GaussianAAModel::computeModelParameters() done.\n");
233#endif
234}
235
236
237/* @brief compute the number of free parameter of the model. **/
238template<class Array>
240{
241 this->setNbFreeParameter(p_regressor_->nbFreeParameter() + dim() * (dim()+1)/2 + 1);
242}
243
244/* compute the ln-likelihood of the model */
245template<class Array>
247{
248#ifdef STK_AAMODELS_VERBOSE
249 stk_cout << _T("GaussianAAModel::computeProjectedLnLikelihood().\n");
250#endif
251 // range of the column to use
252 Range cols = Range(p_reduced_->beginCols(), dim());
253 // create a reference with the first columns of the reduced data
254 Array reducedData(*p_reduced_, p_reduced_->rows(), cols);
255 // create a reference with the first columns of the reduced data
256 ArraySquareX reducedCovariance(projectedCovariance(), cols);
257 // compute first part of the ln-likelihood
258 PointX mean(cols, 0.);
259 MultiLaw::Normal<PointX> normalP(mean, reducedCovariance);
260 projectedLnLikelihood_ = normalP.lnLikelihood(reducedData);
261
262#ifdef STK_AAMODELS_VERBOSE
263 stk_cout << _T("GaussianAAModel::computeProjectedLnLikelihood() done.\n");
264 stk_cout << _T("projectedLnLikelihood_ = ") << projectedLnLikelihood_ << _T("\n");
265#endif
266}
267
268/* compute the ln-likelihood of the model */
269template<class Array>
271{
272#ifdef STK_AAMODELS_VERBOSE
273 stk_cout << _T("GaussianAAModel::computeResidualsLnLikelihood().\n");
274#endif
275 // compute constant part and determinant part of the log-likelihood
276 residualLnLikelihood_ = ( Const::_LNSQRT2PI_ + 0.5*std::log(residualVariance_ ))
277 * (dim() - this->nbVariable()) * this->nbSample();
278 // compute second part of the log-likelihood
279 for (int i=p_residuals_->beginRows(); i<p_residuals_->endRows(); i++)
280 { residualLnLikelihood_ -= p_residuals_->row(i).norm2()/(2.*residualVariance_);}
281
282#ifdef STK_AAMODELS_VERBOSE
283 stk_cout << _T("GaussianAAModel::computeResidualsLnLikelihood() done.\n");
284 stk_cout << _T("residualLnLikelihood_ = ") << residualLnLikelihood_ << _T("\n");
285#endif
286}
287
288/* compute the covariance matrix of the projected data set. */
289template<class Array>
291{
292#ifdef STK_AAMODELS_DEBUG
293 if (!p_reduced_)
295#endif
296#ifdef STK_AAMODELS_VERBOSE
297 stk_cout << _T("GaussianAAModel::computeProjectedCovariance().\n");
298#endif
299 projectedCovariance_ = Stat::covariance(*p_reduced_);
300#ifdef STK_AAMODELS_VERBOSE
301 stk_cout << _T("GaussianAAModel::computeProjectedCovariance() done.\n");
302#endif
303}
304/* compute the covariance matrix of the residuals. */
305template<class Array>
307{
308#ifdef STK_AAMODELS_DEBUG
309 if (!p_residuals_)
311#endif
312#ifdef STK_AAMODELS_VERBOSE
313 stk_cout << _T("in GaussianAAModel::computeResidualCovariance().\n");
314#endif
315 residualCovariance_ = Stat::covariance(*p_residuals_);
316 residualVariance_ = (residualCovariance_.trace())/Real(this->nbVariable()-dim());
317#ifdef STK_AAMODELS_VERBOSE
318 stk_cout << _T("GaussianAAModel::computeResidualCovariance() done.\n");
319 stk_cout << _T("residualVariance_ = ") << residualVariance_ << _T("\n");
320#endif
321}
322
323
324} // namespace STK
325
326#endif //STK_GAUSSIANAAMODEL_H
In this file, we define Array2DSquare class.
In this file we define the Interface Base class for all AutoAssociative Models.
In this file we define the class IStatModel.
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
In this file we define the constant and utilities methods used in the project Model.
In this file we define the multivariate Normal law.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Gaussian Auto-Associative models.
void computeModelParameters()
compute the ln-likelihood of the model
ArraySquareX projectedCovariance_
The covariance matrix of the projected data set.
Real const & projectedLnLikelihood() const
void computeNbFreeParameters()
compute the number of free parameter of the model.
Real residualLnLikelihood_
likelihood of the residuals.
IStatModel< GaussianAAModel< Array > > Base
void computeProjectedLnLikelihood()
compute the ln-likelihood of the projected data set The projected data set is assumed Gaussian with a...
virtual ~GaussianAAModel()
virtual destuctor.
void computeResidualCovariance()
compute the covariance matrix of the residuals.
void computeProjectedCovariance()
compute the covariance matrix of the projected data set.
ArraySquareX residualCovariance_
The covariance matrix of the residuals.
void computeResidualLnLikelihood()
compute the ln-likelihood of the projected data set.
ArraySquareX const & projectedCovariance() const
virtual void setWorkData(Array &workData)
Set a new working data set.
GaussianAAModel(Array *p_workData)
Constructor.
Real const & residualVariance() const
Real const & residualLnLikelihood() const
Real projectedLnLikelihood_
likelihood of the projected data set.
ArraySquareX const & residualCovariance() const
Real residualVariance_
The total variance of the residuals.
Array * p_residuals_
Array of the residuals: the data set is shared with p_regressor and set when the regression method is...
int dim() const
Reducer * p_reducer_
pointer on the reeducer.
Array * p_reduced_
Array of the reduced data set : the data set is shared with p_reducer and set when the regression met...
void setWorkData(Array &workData)
Regressor * p_regressor_
pointer on the regression method.
Array const & workData() const
Array * p_predicted_
Array of the predicted data set: the data set is shared with p_regressor and set when the regression ...
Base class for all Statistical Models [Deprecated], have been replaced by IStatModel.
Class for the multivariate Normal distribution.
Real lnLikelihood(Array const &data) const
compute the log likehood of a data set.
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
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...
Real covariance(ExprBase< XArray > const &X, ExprBase< YArray > const &Y, bool unbiased=false)
Compute the covariance of the variables X and Y.
The namespace STK is the main domain space of the Statistical ToolKit project.
TRange< UnknownSize > Range
Definition STK_Range.h:59