STK++ 0.9.13
STK_Stat_Transform.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::StatDesc
27 * Purpose: Perform the usual transformation on data set.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_STAT_TRANSFORM_H
36#define STK_STAT_TRANSFORM_H
37
38//#include <STKernel.h>
40#include "STK_Stat_Functors.h"
41
42namespace STK
43{
44namespace Stat
45{
46
51template < class Array, class RowVector>
52void center( Array& m, RowVector& mean)
53{
54 typedef typename Array::Type Type;
55 enum
56 {
57 sizeRows_ = Array::sizeRows_,
58 sizeCols_ = Array::sizeCols_
59 };
61 * (mean = Stat::meanByCol(m));
62}
63template < class Array, class RowVector>
64inline void centerByCol( Array& m, RowVector& mean)
65{ center(m, mean);}
66
71template < class Array, class ColVector>
72void centerByRow( Array& m, ColVector& mean)
73{
74 typedef typename Array::Type Type;
75 enum
76 {
77 sizeRows_ = Array::sizeRows_,
78 sizeCols_ = Array::sizeCols_
79 };
80 m -= (mean = Stat::meanByRow(m))
82}
83
88template < class Array, class Weights, class RowVector>
89void center( Array& m, Weights const& W, RowVector& mean)
90{
91 typedef typename Array::Type Type;
92 enum
93 {
94 sizeRows_ = Array::sizeRows_,
95 sizeCols_ = Array::sizeCols_
96 };
98 * (mean = Stat::meanByCol(m, W));
99}
100template < class Array, class Weights, class RowVector>
101inline void centerByCol( Array& m, Weights const& W, RowVector& mean)
102{ center(m, W, mean);}
103
108template < class Array, class Weights, class ColVector>
109void centerByRow( Array& m, Weights const& W, ColVector& mean)
110{
111 typedef typename Array::Type Type;
112 enum
113 {
114 sizeRows_ = Array::sizeRows_,
115 sizeCols_ = Array::sizeCols_
116 };
117 m -= (mean = Stat::meanByRow(m, W))
119}
120
128template < class Array, class RowVector>
129void standardize( Array& m, RowVector& mean, RowVector& std, bool unbiased = false)
130{
131 typedef typename Array::Type Type;
132 enum
133 {
134 sizeRows_ = Array::sizeRows_,
135 sizeCols_ = Array::sizeCols_
136 };
137 // center V
139 // compute variance with mean 0 as V is centered
141 for (int j= m.beginCols(); j< m.endCols(); j++)
142 {
143 Real dev= std[j];
144 std[j] = (dev = Arithmetic<Real>::isFinite(dev) ? std::sqrt((double)dev) : 0.);
145 if (dev) { m.col(j) /= dev;}
146 }
147}
148template < class RowVector, class Array >
149inline void standardizeByCol( Array& m, RowVector& mean, RowVector& std, bool unbiased = false)
150{ standardize(m, mean, std, unbiased);}
151
159template < class Array, class ColVector>
160void standardizeByRow( Array& m, ColVector& mean, ColVector& std, bool unbiased = false)
161{
162 typedef typename Array::Type Type;
163 enum
164 {
165 sizeRows_ = Array::sizeRows_,
166 sizeCols_ = Array::sizeCols_
167 };
168 // center V
170 // compute variance with mean 0 as V is centered
172 for (int i= m.beginRows(); i< m.endRows(); i++)
173 {
174 Real dev = std[i];
175 std[i] = (dev = Arithmetic<Real>::isFinite(dev) ? std::sqrt((double)dev) : 0.);
176 if (dev) { m.row(i) /= dev;}
177 }
178}
179
187template < class Array, class Weights, class RowVector>
188void standardize( Array& m, Weights const& W, RowVector& mean, RowVector& std, bool unbiased = false)
189{
190 typedef typename Array::Type Type;
191 enum
192 {
193 sizeRows_ = Array::sizeRows_,
194 sizeCols_ = Array::sizeCols_
195 };
196 // center m
197 centerByCol(m, W, mean);
198 // compute variance with mean 0 as m is centered
200 // center
201 for (int j= m.beginCols(); j< m.endCols(); j++)
202 {
203 // compute standard deviation
204 Real dev = std[j];
205 // take square root and save result
206 std[j] = (dev = Arithmetic<Real>::isFinite(dev) ? std::sqrt((double)dev) : 0.);
207 // standardize data if necessary
208 if (dev) { m.col(j) /= std[j];}
209 }
210}
211template < class Array, class Weights, class RowVector>
212inline void standardizeByCol( Array& m, Weights const& W, RowVector& mean, RowVector& std, bool unbiased = false)
213{ standardize(m, W, mean, std, unbiased);}
214
222template < class Array, class Weights, class ColVector>
223void standardizeByRow( Array& m, Weights const& W, ColVector& mean, ColVector& std, bool unbiased = false)
224{
225 typedef typename Array::Type Type;
226 enum
227 {
228 sizeRows_ = Array::sizeRows_,
229 sizeCols_ = Array::sizeCols_
230 };
231 // center m
232 centerByRow(m, W, mean);
233 // compute variance with mean 0 as m is centered
235 // center
236 for (int i= m.beginRows(); i< m.endRows(); i++)
237 {
238 // compute standard deviation
239 Real dev = std[i];
240 // take square root and save result
241 std[i] = (dev = Arithmetic<Real>::isFinite(dev) ? std::sqrt((double)dev) : 0.);
242 // standardize data if necessary
243 if (dev) { m.row(i) /= std[i];}
244 }
245}
246
251template < class RowVector, class Array >
252void uncenter( Array& m, RowVector const& mean)
253{
254 typedef typename Array::Type Type;
255 enum
256 {
257 sizeRows_ = Array::sizeRows_,
258 sizeCols_ = Array::sizeCols_
259 };
260 if (m.cols() != mean.range())
262 // apply uncenter
264}
265template < class RowVector, class Array >
266inline void uncenterByCol( Array& m, RowVector const& mean)
267{ uncenter(m, mean);}
268
273template < class ColVector, class Array >
274void uncenterByRow( Array& m, ColVector const& mean)
275{
276 typedef typename Array::Type Type;
277 enum
278 {
279 sizeRows_ = Array::sizeRows_,
280 sizeCols_ = Array::sizeCols_
281 };
282 if (m.rows() != mean.range())
284 // apply uncenter
286}
291template < class Array, class RowVector>
292void unstandardize( Array& m, RowVector const& std)
293{
294 typedef typename Array::Type Type;
295 enum
296 {
297 sizeRows_ = Array::sizeRows_,
298 sizeCols_ = Array::sizeCols_
299 };
300 if (m.cols() != std.range())
302 m = m.prod(Const::Vector<Type, sizeRows_>(m.rows())*std);
303}
304template < class Array, class RowVector>
305inline void unstandardizeByCol( Array& m, RowVector const& std)
306{ unstandardize(m, std);}
307
312template < class Array, class ColVector>
313void unstandardizeByRow( Array& m, ColVector const& std)
314{
315 typedef typename Array::Type Type;
316 enum
317 {
318 sizeRows_ = Array::sizeRows_,
319 sizeCols_ = Array::sizeCols_
320 };
321 if (m.rows() != std.range())
323 m = m.prod(std * Const::Point<Type, sizeCols_>(m.cols()));
324}
325
330template < class Array, class RowVector>
331void unstandardize( Array& m, RowVector const& mean, RowVector const& std)
332{
333 unstandardizeByCol(m, std); // unstandardize
334 uncenterByCol(m, mean); // uncenter
335}
336template < class Array, class RowVector>
337inline void unstandardizeByCol( Array& m, RowVector const& mean, RowVector const& std)
338{ unstandardize(m, mean, std);}
339
344template < class Array, class ColVector>
345void unstandardizeByRow( Array& m, ColVector const& mean, ColVector const& std)
346{
347 unstandardizeByRow(m, std); // unstandardize
348 uncenterByRow(m, mean); // uncenter
349}
350
351} // namespace Stat
352
353} // namespace STK
354
355#endif /*STK_STAT_TRANSFORM_H*/
In this file we define the constant Arrays.
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
This file contain the functors computings statistics.
Define the constant point.
Define the constant point.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
double Real
STK fundamental type of Real values.
void centerByRow(Array &m, ColVector &mean)
Compute the mean by row of the variables in the container V and center V.
void center(Array &m, RowVector &mean)
Compute the mean by column of the variables in the container m and center it.
void unstandardizeByRow(Array &m, ColVector const &std)
undo the standardization by rows of the standardized variable m.
void unstandardize(Array &m, RowVector const &std)
undo the standardization by columns of the standardized variable m.
hidden::FunctorTraits< Derived, MeanOp >::Row mean(Derived const &A)
Compute the mean(s) value(s) of A.
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 standardizeByRow(Array &m, ColVector &mean, ColVector &std, bool unbiased=false)
Compute the mean and the standard deviation by rows of the variable m and standardize it.
hidden::FunctorTraits< Derived, MeanOp >::Row meanByCol(Derived const &A)
hidden::FunctorTraits< Derived, MeanOp >::Col meanByRow(Derived const &A)
void uncenter(Array &m, RowVector const &mean)
Add the means to the columns of the container m.
void uncenterByRow(Array &m, ColVector const &mean)
Add the means to the rows of the container m.
void unstandardizeByCol(Array &m, RowVector const &std)
hidden::FunctorTraits< Derived, VarianceWithFixedMeanOp >::Row varianceWithFixedMeanByCol(Derived const &A, MeanType const &mean, bool unbiased)
void standardizeByCol(Array &m, RowVector &mean, RowVector &std, bool unbiased=false)
void uncenterByCol(Array &m, RowVector const &mean)
hidden::FunctorTraits< Derived, VarianceWithFixedMeanOp >::Col varianceWithFixedMeanByRow(Derived const &A, MeanType const &mean, bool unbiased=false)
void centerByCol(Array &m, RowVector &mean)
The namespace STK is the main domain space of the Statistical ToolKit project.
static bool isFinite(Type const &x)