STK++ 0.9.13
Tips: Centering an array

It's very easy to center a data set using high level template expressions and statistical functors.

Consider the following code and its output:

Example:Output:
#include "STKpp.h"
using namespace STK;
int main(int argc, char *argv[])
{
CArrayXX A(100, 5);
Law::Normal law(1,2);
A.rand(law);
// call column statistical functions
stk_cout << _T("min(A) =") << Stat::min(A);
stk_cout << _T("max(A) =") << Stat::max(A);
stk_cout << _T("max(A.abs()) =") << Stat::max(A.abs());
stk_cout << _T("mean(A) =") << Stat::mean(A);
// center the array
stk_cout << _T("\nCentering...\n\n");
A -= Const::VectorX(100) * Stat::mean(A);
// call column statistical functions with all the columns of A centered
stk_cout << _T("min(A) =") << Stat::min(A);
stk_cout << _T("max(A) =") << Stat::max(A);
stk_cout << _T("max(A.abs()) =") << Stat::max(A.abs());
stk_cout << _T("mean(A) =") << Stat::mean(A);
}
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
This file include all the header files of the STK++ project.
Define the constant point.
Normal distribution law.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
The namespace STK is the main domain space of the Statistical ToolKit project.
int main(int argc, char **argv)
min(A) =-4.66112 -3.1092 -2.76891 -4.65294 -5.47609
max(A) =4.17889 5.89107 5.08115 6.39462 5.17813
max(A.abs()) =4.66112 5.89107 5.08115 6.39462 5.47609
mean(A) =0.963606 1.26585 1.26198 1.05056 0.723175

Centering...

min(A) =-5.62472 -4.37505 -4.0309 -5.7035 -6.19926
max(A) =3.21529 4.62522 3.81916 5.34406 4.45495
max(A.abs()) =5.62472 4.62522 4.0309 5.7035 6.19926
mean(A) =2.57572e-16 -3.93019e-16 2.13163e-16 -3.4639e-16 -1.15463e-16

The expression

Const::Vector<Real>(100) * mean(A)

represents the matrix multiplication of a column vector of 1 with 100 rows and of row vector with the mean of each column of A.

Note
For each column of the array A we can get the maximal value in absolute value using max(A.abs()). It is possible to use functors mixed with unary or binary operators.