STK++ 0.9.13
STK_Stat_Factor.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_FACTOR_H
36#define STK_STAT_FACTOR_H
37
38#include <Sdk.h>
39
40namespace STK
41{
42
43
44namespace Stat
45{
53template <class Array>
54class Factor: public IRunnerWithData<Array>
55{
56 public:
60 typedef typename Array::Type Type;
61
62 typedef std::map<Type, int> EncodingMap;
63 typedef std::map<int, Type> DecodingMap;
64
65 using Base::p_data_;
66
68 Factor();
72 Factor( Array const& data);
76 Factor( Array const* p_data);
80 Factor( Factor const& f);
82 inline virtual ~Factor() {}
83
85 inline virtual Factor* clone() const { return new Factor(*this);}
86
88 inline CVectorXi const& asInteger() const { return asInteger_;}
90 inline Array2DVector<Type> const& levels() const {return levels_;}
92 inline VectorXi const& counts() const {return counts_;}
94 inline int const& firstLevel() const { return firstLevel_;}
96 inline int const& nbLevels() const { return nbLevels_;}
98 inline EncodingMap const& encoder() const { return encoder_;}
100 inline DecodingMap const& decoder() const { return decoder_;}
101
104
106 virtual bool run();
107
108 protected:
123
125 virtual void update();
126};
127
128template <class Array>
129Factor<Array>::Factor(): Base(), asInteger_(), firstLevel_(baseIdx), nbLevels_()
130 , levels_(), counts_(), encoder_() {}
131
132template <class Array>
133Factor<Array>::Factor( Array const& data): Base(data)
134 , asInteger_(p_data_->range())
135 , firstLevel_(baseIdx)
136 , nbLevels_(0)
137 , levels_()
138 , counts_()
139 , encoder_()
140 , decoder_()
141{}
142
143/* Constructor.
144 * @param p_data a pointer on the data set
145 **/
146template <class Array>
147Factor<Array>::Factor( Array const* p_data): Base(p_data)
148 , asInteger_()
149 , firstLevel_(baseIdx)
150 , nbLevels_(0)
151 , levels_()
152 , counts_()
153 , encoder_()
154 , decoder_()
155{
156 if (p_data_)
157 {
158 asInteger_.resize(p_data_->range());
159 nbLevels_= 0;
160 }
161}
162
163template <class Array>
164Factor<Array>::Factor( Factor const& f): Base(f), asInteger_(f.asInteger_)
165 , firstLevel_(f.firstLevel_), nbLevels_(f.nbLevels_)
166 , levels_(f.levels_), counts_(f.counts_)
167 , encoder_(f.encoder_)
168 , decoder_(f.decoder_)
169{}
170
171template <class Array>
173{
174 // if there is no data there is nothing to update
175 if (p_data_)
176 {
177 asInteger_.resize(p_data_->rows());
178 firstLevel_ = baseIdx;
179 nbLevels_=0;
180 levels_.clear();
181 counts_.clear();
182 encoder_.clear();
183 decoder_.clear();
184 }
185}
186
187template <class Array>
189{
190 if (!p_data_)
191 { this->msg_error_ = STKERROR_NO_ARG(FactorArray::run,data is not set);
192 return false;
193 }
194 try
195 {
196 for (int i=p_data_->begin(); i< p_data_->end(); ++i)
197 {
198 // find coding
199 Type idData = p_data_->elt(i);
200 typename EncodingMap::const_iterator it = encoder_.find(idData);
201 if (it != encoder_.end()) // levels already exist, just update the levels array
202 { asInteger_[i] = it->second;
203 counts_[it->second]++; // add one to this level
204 }
205 else // find a new level to add
206 {
207 // create a new level and set it
208 int lev = firstLevel_ + nbLevels_;
209 asInteger_[i] = lev;
210 encoder_.insert(std::pair<Type, int>(idData, lev));
211 decoder_.insert(std::pair<int, Type>(lev, idData));
212 levels_.push_back(idData);
213 counts_.push_back(1); // start counting for this new level
214 nbLevels_++;
215 }
216 }
217 }
218 catch (Exception const& error)
219 {
220 this->msg_error_ += _T("Error in Factor::run():\nWhat: ");
221 this->msg_error_ += error.error();
222 return false;
223 }
224 // no error
225 return true;
226}
227
228} // namespace Stat
229
230} // namespace STK
231
232#endif /*STK_STAT_FACTOR_H */
#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 Factors of a 1D Container.
virtual Factor * clone() const
clone pattern
std::map< Type, int > EncodingMap
virtual bool run()
run the estimation of the Factor statistics.
DecodingMap const & decoder() const
int const & firstLevel() const
EncodingMap const & encoder() const
void setFirstLevel(int firstLevel)
set the value of the first level
virtual void update()
udpating method in case we set a new data set
int nbLevels_
Number of levels of each variables.
CVectorXi asInteger_
vector with the levels in an integer format
Factor()
Default Constructor.
VectorXi counts_
Array with the counts of each factor.
VectorXi const & counts() const
DecodingMap decoder_
decoder of the levels
IRunnerWithData< Array > Base
EncodingMap encoder_
encoder of the levels
Array const * p_data_
A pointer on the original data set.
int const & nbLevels() const
CVectorXi const & asInteger() const
Array2DVector< Type > levels_
vector with the levels
Array2DVector< Type > const & levels() const
int firstLevel_
first level
hidden::Traits< Array >::Col ColVector
std::map< int, Type > DecodingMap
hidden::Traits< Array >::Row RowVector
virtual ~Factor()
virtual destructor.
const int baseIdx
base index of the containers created in STK++.
The namespace STK is the main domain space of the Statistical ToolKit project.