STK++ 0.9.13
STK_IAAModel.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 bas class for all AA Models.
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
36#ifndef STK_IAAMODEL_H
37#define STK_IAAMODEL_H
38
41
44
47
48#ifdef STK_AAMODELS_VERBOSE
50#endif
51
52
53namespace STK
54{
70template<class Array>
72{
73 private:
78
79 protected:
83 IAAModel( Array* p_workData);
87 IAAModel( Array& workData);
88 public:
90 ~IAAModel();
92 inline Array const& workData() const { return *p_workData_;}
94 inline Reducer* const& p_reducer() const { return p_reducer_;}
96 inline Regressor* const& p_regressor() const { return p_regressor_;}
98 inline Array* const& p_reduced() const { return p_reduced_;}
100 inline Array* const& p_predicted() const { return p_predicted_;}
102 inline Array* const& p_residuals() const { return p_residuals_;}
104 inline int dim() const { return dim_;}
106 inline bool isCentered() const { return isCentered_;}
108 inline bool isStandardized() const { return isStandardized_;}
110 inline PointX const& mean() const { return mean_;}
112 inline PointX const& std() const { return std_;}
114 void setDimension( int const& dim);
116 void setWorkData( Array& workData);
122 void freeReducer();
124 void freeRegressor();
126 void center();
130 void center( VectorX const& weights);
132 void standardize();
136 void standardize( VectorX const& weights);
140 void reductionStep();
144 void reductionStep( VectorX const& weights);
147 void regressionStep();
152 void regressionStep( VectorX const& weights);
153
156 void uncenterResults();
160
161 protected:
183
184 private:
186 int dim_;
197};
198
199/* Constructor.
200 * @param p_workData A pointer on the the working data set
201 **/
202template<class Array>
203IAAModel<Array>::IAAModel( Array* p_workData)
204 : p_regressor_(0)
205 , p_reducer_(0)
206 , p_workData_(p_workData)
207 , p_reduced_(0)
208 , p_predicted_(0)
209 , p_residuals_(0)
210 , dim_(0)
211 , mean_()
212 , std_()
213 , isCentered_(false)
214 , isStandardized_(false)
215{}
216
217template<class Array>
219 : p_regressor_(0)
220 , p_reducer_(0)
221 , p_workData_(&workData)
222 , p_reduced_(0)
223 , p_predicted_(0)
224 , p_residuals_(0)
225 , dim_(0)
226 , mean_()
227 , std_()
228 , isCentered_(false)
229 , isStandardized_(false)
230{ }
231
232/* destructor */
233template<class Array>
236
237/* set the dimension of the model */
238template<class Array>
239void IAAModel<Array>::setDimension( const int& dim)
240{ dim_ = dim;}
241
242/* set the working set with the Data to treat.
243 * @param p_reducer a pointer on the reduction dimension method to use
244 */
245template<class Array>
246void IAAModel<Array>::setWorkData( Array& workData)
247{
248 p_workData_ = &workData;
249 isCentered_ = false;
250 isStandardized_ = false;
251}
252/* set the reduction dimension method.
253 * @param p_reducer a pointer on the reduction dimension method to use
254 */
255template<class Array>
257{ p_reducer_ = p_reducer;}
258/* set the regression method.
259 * @param p_regressor a pointer on the regresssion method to use
260 */
261template<class Array>
263{ p_regressor_ = p_regressor;}
264
266template<class Array>
268{
269 if (p_reducer_) delete p_reducer_;
270 p_reducer_ = 0;
271}
273template<class Array>
275{
276 if (p_regressor_) delete p_regressor_;
277 p_regressor_ = 0;
278}
279
280/* standardize the local data set */
281template<class Array>
283{
284#ifdef STK_AAMODELS_DEBUG
285 if (!p_workData_)
286 STKRUNTIME_ERROR_NO_ARG(IAAModel<Array>::center,workData is not initialized);
287#endif
288 // we have to uncenter in case they have been centered with weights
289 if (isCentered_)
290 {
291 Stat::uncenter(*p_workData_, mean_);
292 isCentered_ = false;
293 }
294 Stat::center(*p_workData_, mean_);
295 isCentered_ = true;
296}
297
298/* center the local data set */
299template<class Array>
301{
302#ifdef STK_AAMODELS_DEBUG
303 if (!p_workData_)
304 STKRUNTIME_ERROR_NO_ARG(IAAModel<Array>::center,workData is not initialized);
305#endif
306 // we have to uncenter in case of this is not the same weights
307 if (isCentered_)
308 {
309 Stat::uncenter(*p_workData_, mean_);
310 isCentered_ = false;
311 }
312 // center
313 Stat::center(*p_workData_, weights, mean_);
314 isCentered_ = true;
315}
316
317/* standardize the local data set */
318template<class Array>
320{
321#ifdef STK_AAMODELS_DEBUG
322 if (!p_workData_)
323 STKRUNTIME_ERROR_NO_ARG(IAAModel<Array>::standardize,workData is not initialized);
324#endif
325 // we have to unstandardize in case they have been standardized with weights
326 if (isStandardized_)
327 {
328 Stat::unstandardize(*p_workData_, mean_, std_);
329 isStandardized_ = false;
330 }
331 Stat::standardize(*p_workData_, mean_, std_);
332 isStandardized_ = true;
333}
334
335/* standardize the local data set */
336template<class Array>
338{
339#ifdef STK_AAMODELS_DEBUG
340 if (!p_workData_)
341 STKRUNTIME_ERROR_NO_ARG(IAAModel<Array>::standardize,workData is not initialized);
342#endif
343 // we have to unstandardize in case of this is not the same weights
344 if (isStandardized_)
345 {
346 Stat::unstandardize(*p_workData_, mean_, std_);
347 isStandardized_ = false;
348 }
349 // standardize
350 Stat::standardize(*p_workData_, weights, mean_, std_);
351 isStandardized_ = true;
352}
353
354
355/* compute the dimension reduction **/
356template<class Array>
358{
359#ifdef STK_AAMODELS_DEBUG
360 if (!p_reducer_)
362#endif
363 // compute axis
364 p_reducer_->setDimension(dim_);
365 p_reducer_->run();
366 // compute matrix multiplication
367 p_reduced_= p_reducer_->p_reduced();
368}
369
370/* compute the weighted dimension reduction **/
371template<class Array>
373{
374#ifdef STK_AAMODELS_DEBUG
375 if (!p_reducer_)
377#endif
378 // compute axis
379 p_reducer_->setDimension(dim_);
380 p_reducer_->run(weights);
381 // get the reduced data set
382 p_reduced_= p_reducer_->p_reduced();
383}
384
385/* compute the regression **/
386template<class Array>
388{
389#ifdef STK_AAMODELS_DEBUG
390 if (!p_regressor_)
392#endif
393 // compute regression
394 p_regressor_->run();
395 // get results
396 p_predicted_ = p_regressor_->p_predicted();
397 p_residuals_ = p_regressor_->p_residuals();
398}
399/* compute the weighted regression **/
400template<class Array>
402{
403#ifdef STK_AAMODELS_DEBUG
404 if (!p_regressor_)
406#endif
407 p_regressor_->run(weights);
408 // get results
409 p_predicted_ = p_regressor_->p_predicted();
410 p_residuals_ = p_regressor_->p_residuals();
411}
412
413/* unstandardize the predicted result and residuals */
414template<class Array>
416{
417#ifdef STK_AAMODELS_DEBUG
418 if (!p_predicted_)
419 STKRUNTIME_ERROR_NO_ARG(IAAModel<Array>::uncenterResults,predictions are not computed);
420#endif
421 Stat::uncenter(*p_predicted_, mean_);
422}
423/* unstandardize the predicted result and residuals */
424template<class Array>
426{
427#ifdef STK_AAMODELS_DEBUG
428 if (!p_predicted_)
430 if (!p_residuals_)
432#endif
433 Stat::unstandardize(*p_predicted_, mean_, std_);
434 Stat::unstandardize(*p_residuals_, std_);
435}
436
437
438} // namespace STK
439
440#endif /* STK_IAAMODEL_H */
A Array2DPoint is a one dimensional horizontal container.
A Array2DVector is a one dimensional horizontal container.
This file define methods for displaying Arrays and Expressions.
In this file we define the interface base class IReducer.
In this file we define the Interface base class IRegression.
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
In this file we specialize the class Multivariate to Real type.
In this file we implement the main transformation on data set.
Array * p_residuals_
Array of the residuals: the data set is shared with p_regressor and set when the regression method is...
void uncenterResults()
uncenter the predicted data set.
PointX const & std() const
int dim_
The dimension of the AA Model.
~IAAModel()
destructor.
int dim() const
Reducer *const & p_reducer() const
bool isStandardized() const
IRegression< Array, Array, VectorX > Regressor
regression type
void freeRegressor()
delete the regressor set to this model by the method setRegressor.
Reducer * p_reducer_
pointer on the reeducer.
bool isStandardized_
a boolean true if the working data set is standardized, false otherwise
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.
void setDimension(int const &dim)
Array * p_reduced_
Array of the reduced data set : the data set is shared with p_reducer and set when the regression met...
bool isCentered_
a boolean true if the working data set is centered, false otherwise.
void setRegressor(Regressor *p_regressor)
void setWorkData(Array &workData)
void unstandardizeResults()
unstandardize the predicted data set and the residuals.
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.
PointX const & mean() const
Array *const & p_predicted() const
bool isCentered() const
IAAModel(Array *p_workData)
Constructor.
PointX mean_
vector of the means of the input data set.
Array *const & p_reduced() const
void standardize()
standardize the data set.
Regressor *const & p_regressor() const
Array const & workData() const
IReducer< Array, VectorX > Reducer
reducer type
void center()
center the data set workData_.
Array *const & p_residuals() const
Array * p_predicted_
Array of the predicted data set: the data set is shared with p_regressor and set when the regression ...
PointX std_
vector of the standard deviation of the input data set.
void setReducer(Reducer *p_reducer)
void freeReducer()
delete the reducer set to this model by the method setReducer.
Interface base class for reducing methods.
Interface base class for Regression methods.
void center(Array &m, RowVector &mean)
Compute the mean by column of the variables in the container m and center it.
void unstandardize(Array &m, RowVector const &std)
undo the standardization by columns of the standardized variable m.
void standardize(Array &m, RowVector &mean, RowVector &std, bool unbiased=false)
Compute the mean and the standard deviation by columns of the variable m and standardize it.
void uncenter(Array &m, RowVector const &mean)
Add the means to the columns of the container m.
The namespace STK is the main domain space of the Statistical ToolKit project.