STK++ 1.0
STK_Stat_Transform.h
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project: stkpp::STatistiK::Statdesc
00027  * Purpose: Perform the usual transformation on data set.
00028  * Author:  Serge Iovleff, serge.iovleff@stkpp.org
00029  **/
00030 
00035 #ifndef STK_STAT_TRANSFORM_H
00036 #define STK_STAT_TRANSFORM_H
00037 
00038 #include "../../STKernel/include/STK_Arithmetic.h"
00039 #include "../../STKernel/include/STK_Misc.h"
00040 
00041 #include "../../Sdk/include/STK_ITContainer2D.h"
00042 
00043 #include "STK_Stat_UnivariateReal.h"
00044 
00045 namespace STK
00046 {
00047 namespace Stat
00048 {
00049 
00054 template < class TContainerHo, class TContainer2D >
00055 void center( ITContainer2D< Real, TContainer2D>&  V
00056            , ITContainer1D< Real, TContainerHo>& mean
00057            )
00058 {
00059   mean.resize(V.rangeHo());
00060   // get dimensions
00061   const Integer  firstRow = V.firstRow(), lastRow = V.lastRow();
00062   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00063   for (Integer j= firstVar; j<= lastVar; j++)
00064   {
00065     // compute mean
00066     Real mu = Stat::mean(V[j]);
00067     // save current mean
00068     mean[j] = (mu = Arithmetic<Real>::isFinite(mu) ?  mu : 0.);
00069     // translate data
00070     for (Integer i= firstRow; i<= lastRow; i++)
00071     { V(i,j) -= mu;}
00072   }
00073 }
00074 
00080 template < class TContainerHo, class TContainerVe, class TContainer2D >
00081 void center( ITContainer2D< Real, TContainer2D>& V
00082            , ITContainer1D< Real, TContainerVe> const& W
00083            , ITContainer1D< Real, TContainerHo>& mean
00084            )
00085 {
00086 #ifdef STK_DEBUG
00087   if (!V.rangeVe().isIn(W.range()))
00088     throw runtime_error("In center(V, W, mean) "
00089                              "V.range() not include in w.range()");
00090 #endif
00091   // create result
00092   mean.resize(V.rangeHo());
00093   // get dimensions
00094   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00095   const Integer  firstRow = V.firstRow(), lastRow = V.lastRow();
00096   for (Integer j= firstVar; j<= lastVar; j++)
00097   {
00098     // compute mean
00099     Real mu = Stat::mean<TContainerVe>(V[j], W);
00100     // save current mean
00101     mean[j] = (mu = Arithmetic<Real>::isFinite(mu) ?  mu : 0.);
00102     // translate data
00103     for (Integer i= firstRow; i<= lastRow; i++)
00104     { V(i,j) -= mu;}
00105   }
00106 }
00107 
00113 template < class TContainerHo, class TContainer2D >
00114 void standardize( ITContainer2D< Real, TContainer2D>& V
00115                 , ITContainer1D< Real, TContainerHo>& mean
00116                 , ITContainer1D< Real, TContainerHo>& std
00117                )
00118 {
00119     // create result
00120     mean.resize(V.rangeHo());
00121     std.resize(V.rangeHo());
00122     // center
00123     center(V, mean);
00124     // get dimensions
00125     const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00126     const Integer  firstRow = V.firstRow(), lastRow = V.lastRow();
00127     for (Integer j= firstVar; j<= lastVar; j++)
00128     {
00129       // compute standard deviation
00130       Real dev = Stat::variance(V[j]);
00131       // take square root and save result
00132       std[j] = (dev = Arithmetic<Real>::isFinite(dev) ?  sqrt((double)dev) : 0.);
00133       // standardize data
00134       if (dev)
00135       {
00136         for (Integer i= firstRow; i<= lastRow; i++)
00137         { V(i,j) /= dev;}
00138       }
00139     }
00140 }
00141 
00148 template < class TContainerHo, class TContainerVe, class TContainer2D >
00149 void standardize( ITContainer2D< Real, TContainer2D>& V
00150                 , ITContainer1D< Real, TContainerVe> const& W
00151                 , ITContainer1D< Real, TContainerHo>& mean
00152                 , ITContainer1D< Real, TContainerHo>& std
00153                )
00154 {
00155 #ifdef STK_DEBUG
00156   if (W.range() != V.rangeVe())
00157     throw runtime_error("In standardize(V, W, mean) "
00158                              "W.range() != V.rangeVe()");
00159 #endif
00160   // create result
00161   mean.resize(V.rangeHo());
00162   std.resize(V.rangeHo());
00163   // center
00164   center(V, W, mean);
00165   // get dimensions
00166   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00167   const Integer  firstRow = V.firstRow(), lastRow = V.lastRow();
00168   for (Integer j= firstVar; j<= lastVar; j++)
00169   {
00170     // compute standard deviation
00171     Real dev = Stat::variance(V[j], W);
00172     // take square root and save result
00173     std[j] = (dev = Arithmetic<Real>::isFinite(dev) ?  sqrt((double)dev) : 0.);
00174     // standardize data
00175     if (dev)
00176     {
00177       for (Integer i= firstRow; i<= lastRow; i++)
00178       { V(i,j) /= dev;}
00179     }
00180   }
00181 }
00182 
00187 template < class TContainerHo, class TContainer2D >
00188 void decenter( ITContainer2D< Real, TContainer2D>&  V
00189              , ITContainer1D< Real, TContainerHo> const& mean
00190              )
00191 {
00192   if (V.rangeHo() != mean.range())
00193     throw runtime_error(_T("Error in Stat::decenter(V, mean): "
00194                              "ranges are not the same."));
00195   // get dimensions
00196   const Integer  firstRow = V.firstRow(), lastRow = V.lastRow();
00197   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00198   for (Integer j= firstVar; j<= lastVar; j++)
00199   {
00200     // translate data
00201     Real mu = mean[j];
00202     for (Integer i= firstRow; i<= lastRow; i++)
00203     { V(i,j) += mu;}
00204   }
00205 }
00206 
00211 template < class TContainerHo, class TContainer2D >
00212 void destandardize( ITContainer2D< Real, TContainer2D>& V
00213                   , ITContainer1D< Real, TContainerHo> const& std
00214                   )
00215 {
00216     if (V.rangeHo() != std.range())
00217       throw runtime_error(_T("Error in Stat::decenter(V, std): "
00218                                "ranges are not the same."));
00219   // get dimensions
00220   const Integer  firstRow = V.firstRow(), lastRow = V.lastRow();
00221   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00222   for (Integer j= firstVar; j<= lastVar; j++)
00223   {
00224     // dilate
00225     if (std[j])
00226     {
00227       Real dev = std[j];
00228       // get dimensions
00229       for (Integer i= firstRow; i<= lastRow; i++)
00230       { V(i,j) *= dev;}
00231     }
00232   }
00233 }
00234 
00240 template < class TContainerHo, class TContainer2D >
00241 void destandardize( ITContainer2D< Real, TContainer2D>& V
00242                   , ITContainer1D< Real, TContainerHo> const& mean
00243                   , ITContainer1D< Real, TContainerHo> const& std
00244                )
00245 {
00246   // dilate
00247   destandardize(V, std);
00248   // decenter
00249   decenter(V, mean);
00250 }
00251 
00252 }  // namespace Stat
00253 
00254 }  // namespace STK
00255 
00256 #endif /*STK_STAT_TRANSFORM_H*/