STK++ 0.9.13
STK_LinearAAModel.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_Dot_org (see copyright for ...) */
23
24/*
25 * Project: stkpp::AAModels
26 * Purpose: class for AA Linear models.
27 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
28 **/
29
34#ifndef STK_LINEARAAMODEL_H
35#define STK_LINEARAAMODEL_H
36
37#include "STK_GaussianAAModel.h"
42
43#ifdef STK_AAMODELS_VERBOSE
45#endif
46
47
48namespace STK
49{
50
51namespace Law
52{
53template<class TYPE>
54class IUnivLaw;
55}
56
94template<class Array>
95class LinearAAModel: public IRunnerUnsupervised<Array, VectorX>
96 , public GaussianAAModel<Array>
97{
98 public :
101 using Base::p_workData_;
102 using Base::p_regressor_;
103 using Base::p_reducer_;
104 using Base::p_reduced_;
105 using Base::p_predicted_;
106 using Base::p_residuals_;
107 using Base::dim;
114 LinearAAModel( Array const& data);
118 LinearAAModel( LinearAAModel const& model);
120 virtual ~LinearAAModel();
122 inline virtual LinearAAModel* clone() const { return new LinearAAModel(*this);}
133 bool run( int const& dim);
147 bool run( VectorX const& weights, int const& dim);
159 virtual bool run();
173 virtual bool run(VectorX const& weights);
188 static void simul( const Law::IUnivLaw<Real>& law
189 , VectorX const& mu
190 , Real const& std
191 , Array& proj
192 , Array& data
193 );
194 protected:
197};
198
199/* Constructors. */
200/* Default constructor : compute the Linear AA models of the matrix X
201 * using the local variance as criteria.
202**/
203template<class Array>
211
215template<class Array>
223
224
225
226/* Virtual destructor */
227template<class Array>
229{ if (p_regressor_) delete p_regressor_;}
230
231/* run the probabilistic AAModel */
232template<class Array>
233bool LinearAAModel<Array>::run( int const& dim)
234{
235 this->setDimension(dim);
236 bool res = run();
237#ifdef STK_DEBUG
238 if (!res)
239 { stk_cerr << _T("An error occur.\nWhat: ") << this->error();}
240#endif
241 return res;
242}
243
244
245/* run the probabilistic AAModel */
246template<class Array>
248{
249 // compute AAM
250 try
251 {
252 if (!p_reducer_)
253 {
255 return false;
256 }
257 if (!p_regressor_)
258 {
260 return false;
261 }
262 // set p_workData to the reducer
263 p_reducer_->setData(*p_workData_);
264 // compute the projected data set
265 reductionStep();
266 // compute the projected covariance
267 this->computeProjectedCovariance();
268#ifdef STK_AAMODELS_VERBOSE
269 stk_cout << _T("In LinearAAModel::run(), reduction done.\n");
270#endif
271 // set data
272 p_regressor_->setY(*p_workData_);
273 p_regressor_->setX(*p_reduced_);
274 // compute the regression function
276#ifdef STK_AAMODELS_VERBOSE
277 stk_cout << _T("In LinearAAModel::run(), regression done.\n");
278#endif
279 this->computeModelParameters();
280 // check if data have been standardized or centered
281 if (this->isStandardized()) { this->unstandardizeResults();}
282 else if (this->isCentered()){ this->uncenterResults();}
283 }
284 catch (Exception const& error)
285 {
286 this->msg_error_ = _T("Error in LinearAAModel::run():\nWhat: ");
287 this->msg_error_ += error.error();
288 return false;
289 }
290 return true;
291}
292
293/* run the probabilistic AAModel */
294template<class Array>
295bool LinearAAModel<Array>::run( VectorX const& weights, int const& dim)
296{
297 this->setDimension(dim);
298 return run(weights);
299}
300template<class Array>
302{
303 try
304 {
307 // set p_workData to the reducer
308 p_reducer_->setData(*p_workData_);
309 // compute the weighted reduced data set
310 reductionStep(weights);
311 // compute the projected covariance
312 this->computeProjectedCovariance();
313#ifdef STK_AAMODELS_VERBOSE
314 stk_cout << _T("In LinearAAModel::run(weights), reduction done.\n");
315#endif
316 // set data
317 p_regressor_->setY(*p_workData_);
318 p_regressor_->setX(*p_reduced_);
319 // compute the weighted regression vectors
321#ifdef STK_AAMODELS_VERBOSE
322 stk_cout << _T("In LinearAAModel::run(weights), regression done.\n");
323#endif
324 this->computeModelParameters();
325 // check if data have been standardized or centered
326 if (this->isStandardized()) { this->unstandardizeResults();}
327 else if (this->isCentered()){ this->uncenterResults();}
328 }
329 catch (Exception const& error)
330 {
331 this->msg_error_ = _T("Error in LinearAAModel::run(weights): ");
332 this->msg_error_ += error.error();
333 return false;
334 }
335 return true;
336}
337
338/* Simulate a centered auto-associative linear model of the form
339 * \f[
340 * X = X.P.P' + \epsilon
341 * \f]
342 * with \f$ P'P = I_d \f$ and d < p.
343 * @param law, the law to use in order to simulate the data.
344 * @param std the standard deviation of the gaussian noise
345 * @param data the data to simulate. The dimension of the container
346 * give the number of the samples and variables.
347 * @param proj the simulated projection matrix. The dimension of the
348 * container give the dimension of the AA model.
349 **/
350template<class Array>
352 , VectorX const& mu, Real const& std
353 , Array& proj, Array& data
354 )
355{
356 if (data.cols() != mu.range())
357 STKRUNTIME_ERROR_NO_ARG(LinearAAModel::simul,data.cols()!=mu.range());
358 // simul AA model
359 data.rand(law);
360#ifdef STK_AAMODELS_VERBOSE
361 stk_cout << _T("data simulated\n");
362#endif
363 // choose arbitrary coefficients for proj
364 proj.randGauss();
365#ifdef STK_AAMODELS_VERBOSE
366 stk_cout << _T("proj simulated\n");
367#endif
368 // orthonormalize proj
370#ifdef STK_AAMODELS_VERBOSE
371 stk_cout << _T("proj orthonormalized\n");
372#endif
373
374#ifdef STK_AAMODELS_DEBUG
375 stk_cout << _T("proj =") << proj;
376#endif
377 // compute data
378 data = data * (proj * proj.transpose());
379
380 // add noise to the model
381 for (int j= data.beginCols(); j< data.endCols(); j++)
382 {
383 Law::Normal noise(mu[j], std);
384 for (int i= data.beginRows(); i< data.endRows(); i++)
385 data(i, j) += noise.rand();
386 }
387}
388
389} // namespace STK
390
391#endif // STK_LINEARAAMODEL_H
This file define methods for displaying Arrays and Expressions.
In this file we declare the class GaussianAAModel for the auto-associative models.
In this file we define the GramSchmidt method.
In this file we define the interface base class ILinearReduct.
In this file we define the Normal probability law class.
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
In this file we define the MultidimRegression class.
#define stk_cerr
Standard stk error stream.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Sdk class for all library Exceptions.
virtual const String error() const
Returns a C-style character string describing the general cause of the current error.
Gaussian Auto-Associative models.
virtual void setWorkData(Array &workData)
Set a new working data set.
Array * p_residuals_
Array of the residuals: the data set is shared with p_regressor and set when the regression method is...
PointX const & std() const
int dim() const
Reducer * p_reducer_
pointer on the reeducer.
Array * p_workData_
Array of the local data set.
void reductionStep()
compute the reduction of the data set and store the result in the p_reduced_ container.
Array * p_reduced_
Array of the reduced data set : the data set is shared with p_reducer and set when the regression met...
void regressionStep()
compute the regression of the original data set and set the results in p_predicted and p_residuals.
Regressor * p_regressor_
pointer on the regression method.
Array * p_predicted_
Array of the predicted data set: the data set is shared with p_regressor and set when the regression ...
virtual bool run()
run the computations.
String msg_error_
String with the last error message.
Definition STK_IRunner.h:96
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
Abstract class for all classes making unsupervised learning.
virtual void setData(Array const *p_data)
Set the data set.
Interface base class for all the univariate distributions.
Normal distribution law.
A Linear AutoAssociative Model (LAAM) is a generalization of the PCA model for projecting variables o...
virtual bool run()
run the estimation of the AA model.
static void simul(const Law::IUnivLaw< Real > &law, VectorX const &mu, Real const &std, Array &proj, Array &data)
Simulate a centered auto-associative linear model in of the form.
virtual LinearAAModel * clone() const
clone pattern
GaussianAAModel< Array > Base
IRunnerUnsupervised< Array, VectorX > Runner
virtual ~LinearAAModel()
Virtual destructor.
Array workData_
working data set;
LinearAAModel(Array const &data)
constructor.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
virtual bool regressionStep()
compute the regression function.
void gramSchmidt(ArrayBase< TContainer2D > &A)
The gramSchmidt method perform the Gram Schmidt orthonormalization of an Array of Real.
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.