STK++ 0.9.13
STK_Stat_MultiFactor.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::STatistiK
27 * Purpose: Compute factors of a set of variables.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_STAT_MULTIFACTOR_H
36#define STK_STAT_MULTIFACTOR_H
37
38#include <Sdk.h>
42
43namespace STK
44{
45
46
47namespace Stat
48{
56template <class Array>
57class MultiFactor: public IRunnerWithData<Array>
58{
59 public:
61 typedef typename Array::Type Type;
62
63 typedef std::map<Type, int> EncodingMap;
64 typedef std::map<int, Type> DecodingMap;
65
68
69 using Base::p_data_;
70
76 MultiFactor( Array const& data);
80 MultiFactor( Array const* p_data);
84 MultiFactor( MultiFactor const& f);
86 inline virtual ~MultiFactor() {}
87
89 inline virtual MultiFactor* clone() const { return new MultiFactor(*this);}
90
92 inline CArrayXXi const& asInteger() const { return asInteger_;}
94 inline CArrayPoint< Array2DVector<Type> > const& levels() const {return levels_;}
96 inline CArrayPoint< VectorXi > const& counts() const {return counts_;}
98 inline int const& firstLevel() const { return firstLevel_;}
100 inline CPointXi const& nbLevels() const { return nbLevels_;}
102 inline Encoder const& encoder() const { return encoder_;}
104 inline Decoder const& decoder() const { return decoder_;}
105
108
110 virtual bool run();
111
112 protected:
127
129 virtual void update();
130};
131
132template <class Array>
133MultiFactor<Array>::MultiFactor(): Base(), asInteger_(), firstLevel_(baseIdx)
134 , nbLevels_(), levels_(), counts_(), encoder_() {}
135
136template <class Array>
137MultiFactor<Array>::MultiFactor( Array const& data): Base(data)
138 , asInteger_(p_data_->rows(),p_data_->cols())
139 , firstLevel_(baseIdx)
140 , nbLevels_(p_data_->cols(), 0)
141 , levels_(p_data_->cols())
142 , counts_(p_data_->cols())
143 , encoder_(p_data_->cols())
144 , decoder_(p_data_->cols())
145{}
146
147/* Constructor.
148 * @param p_data a pointer on the data set
149 **/
150template <class Array>
151MultiFactor<Array>::MultiFactor( Array const* p_data): Base(p_data)
152 , asInteger_()
153 , firstLevel_(baseIdx)
154 , nbLevels_()
155 , levels_()
156 , encoder_()
157 , decoder_()
158{
159 if (p_data_)
160 {
161 asInteger_.resize(p_data_->rows(),p_data_->cols());
162 nbLevels_.resize(p_data_->cols()).setValue(0);
163 levels_.resize(p_data_->cols());
164 counts_.resize(p_data_->cols());
165 encoder_.resize(p_data_->cols());
166 decoder_.resize(p_data_->cols());
167 }
168}
169
170template <class Array>
171MultiFactor<Array>::MultiFactor( MultiFactor const& f): Base(f), asInteger_(f.asInteger_)
172 , firstLevel_(baseIdx), nbLevels_(f.nbLevels_)
173 , levels_(f.levels_), counts_(f.counts_)
174 , encoder_(f.encoder_)
175 , decoder_(f.decoder_)
176{}
177
178template <class Array>
180{
181 // if there is no data there is nothing to update
182 if (p_data_)
183 {
184 asInteger_.resize(p_data_->rows(),p_data_->cols());
185 firstLevel_ = baseIdx;
186 nbLevels_.resize(p_data_->cols()).setValue(0);
187 for(int j=encoder_.begin(); j<std::min(encoder_.end(), p_data_->cols().end()); ++j)
188 {
189 levels_[j].clear();
190 counts_[j].clear();
191 encoder_[j].clear();
192 decoder_[j].clear();
193 }
194 levels_.resize(p_data_->cols());
195 counts_.resize(p_data_->cols());
196 encoder_.resize(p_data_->cols());
197 decoder_.resize(p_data_->cols());
198 }
199}
200
201template <class Array>
203{
204 if (!p_data_)
205 { this->msg_error_ = STKERROR_NO_ARG(MultiFactorArray::run,data is not set);
206 return false;
207 }
208 try
209 {
210 for (int j=p_data_->beginCols(); j< p_data_->endCols(); ++j)
211 {
212 for (int i=p_data_->beginRows(); i< p_data_->endRows(); ++i)
213 {
214 // find coding
215 Type idData = p_data_->elt(i,j);
216 typename EncodingMap::const_iterator it = encoder_[j].find(idData);
217 if (it != encoder_[j].end())
218 { // levels already exist, just update the integer and counts array
219 asInteger_(i,j) = it->second;
220 counts_[j][it->second]++;
221 }
222 else
223 { // find a new level to add
224 // create a new level and set it
225 int lev = firstLevel_ + nbLevels_[j];
226 asInteger_(i,j) = lev;
227 encoder_[j].insert(std::pair<Type, int>(idData, lev));
228 decoder_[j].insert(std::pair<int, Type>(lev, idData));
229 levels_[j].push_back(idData);
230 counts_[j].push_back(1); // start counting for this new level
231 ++nbLevels_[j];
232 }
233 }
234 }
235 }
236 catch (Exception const& error)
237 {
238 this->msg_error_ += _T("Error in MultiFactor::run():\nWhat: ");
239 this->msg_error_ += error.error();
240 return false;
241 }
242 // no error
243 return true;
244}
245
246} // namespace Stat
247
248} // namespace STK
249
250#endif /*STK_STAT_MULTIFACTOR_H */
A Array2DVector is a one dimensional horizontal container.
In this file we implement the final class CArrayPoint.
In this file we implement the final class CArray.
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
#define _T(x)
Let x unmodified.
This file include all the other header files of the project Sdk.
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.
Derived & resize(Range const &I, Range const &J)
resize the Array.
Abstract class for all running class based on a data set.
Array const * p_data_
A pointer on the original data set.
Array const * p_data() const
get the data set
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Computation of the MultiFactors of a 2D Container.
CArrayPoint< VectorXi > counts_
Array with the counts of each factor.
CArrayPoint< EncodingMap > Encoder
Decoder decoder_
decoder of the levels
CArrayXXi const & asInteger() const
CArrayPoint< VectorXi > const & counts() const
virtual MultiFactor * clone() const
clone pattern
CArrayPoint< Array2DVector< Type > > levels_
Array with the levels of each variables.
virtual void update()
udpating method in case we set a new data set
CPointXi const & nbLevels() const
Decoder const & decoder() const
virtual ~MultiFactor()
virtual destructor.
IRunnerWithData< Array > Base
Encoder encoder_
encoder of the levels
int firstLevel_
first level number
virtual bool run()
run the estimation of the MultiFactor statistics.
MultiFactor()
Default Constructor.
std::map< Type, int > EncodingMap
CPointXi nbLevels_
Number of levels of each variables.
CArrayXXi asInteger_
Array of the data size with the levels of each variables in an integer format.
Array const * p_data_
A pointer on the original data set.
Encoder const & encoder() const
CArrayPoint< DecodingMap > Decoder
int const & firstLevel() const
CArrayPoint< Array2DVector< Type > > const & levels() const
std::map< int, Type > DecodingMap
void setFirstLevel(int firstLevel)
set the value of the first level
const int baseIdx
base index of the containers created in STK++.
The namespace STK is the main domain space of the Statistical ToolKit project.