STK++ 1.0
STK_Stat_MultivariateReal.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:  StatDesc
00027  * Purpose:  Compute multivariate elementary statistics for
00028  * a 2D container.
00029  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00030  **/
00031 
00036 #ifndef STK_STAT_MULTIVARIATEREAL_H
00037 #define STK_STAT_MULTIVARIATEREAL_H
00038 
00039 #include "../../STKernel/include/STK_Arithmetic.h"
00040 #include "../../STKernel/include/STK_Misc.h"
00041 
00042 #include "../../Arrays/include/STK_MatrixSquare.h"
00043 
00044 #include "STK_Stat_UnivariateReal.h"
00045 #include "STK_Stat_BivariateRealReal.h"
00046 #include "STK_Stat_Multivariate.h"
00047 
00048 namespace STK
00049 {
00050 namespace Stat
00051 {
00052 
00053 typedef Multivariate<Real, Matrix> MultivariateMatrix;
00054 
00068 template < class TContainer2D >
00069 class Multivariate<Real, TContainer2D > : public IRunnerPtr2D< Real, TContainer2D>
00070 {
00071   typedef typename TContainer2D::TContainerHo TContainerHo;
00072   typedef typename TContainer2D::TContainerVe TContainerVe;
00074   typedef IRunnerPtr2D< Real, TContainer2D> Runner2D;
00075 
00076   public:
00081     Multivariate( TContainer2D const* p_data)
00082                 : Runner2D(p_data)
00083                 , nbSamples_(0)
00084                 , nbVar_(0)
00085                 , mean_(0)
00086                 , var_(0)
00087                 , cov_(0)
00088     { update(); }
00089 
00091     virtual ~Multivariate() { }
00092 
00096     inline Integer const& nbVar() const {return nbVar_;}
00100     inline Integer const& nbSamples() const {return nbSamples_;}
00103     inline TContainerHo const& mean() const { return mean_;}
00107     inline TContainerHo const& variance() const { return var_;}
00111     inline MatrixSquare const& covariance() const { return cov_;}
00113     virtual bool run()
00114     {
00115       try
00116       {
00117         if (!this->p_data_)
00118           throw runtime_error("data have not be set.");
00119         // get dimensions
00120         const Integer first = this->p_data_->firstCol(), last  = this->p_data_->lastCol();
00121         // for each variables
00122         for (Integer j= first; j<= last; j++)
00123         {
00124           mean_[j] = Stat::mean(this->p_data_->col(j));
00125           // compute the variance
00126           var_[j]  = varianceWithFixedMean(this->p_data_->col(j), mean_[j]);
00127           cov_(j, j) = var_[j];
00128           // compute the covariances
00129           for (Integer i= first; i<j; i++)
00130           {
00131             cov_(i, j) = covarianceWithFixedMean(this->p_data_->col(i), this->p_data_->col(j), mean_[i], mean_[j]);
00132             cov_(j, i) = cov_(i, j);
00133           }
00134         }
00135       }
00136       catch (Exception error)
00137       {
00138         this->msg_error_  = _T("Error in MultivariateReal::run():\nWhat: ");
00139         this->msg_error_ += error.error();
00140         return false;
00141       }
00142       // no error
00143       return true;
00144     }
00145 
00149     virtual bool run( TContainerVe const& weights)
00150     {
00151       try
00152       {
00153         if (!this->p_data_)
00154           throw runtime_error("data have not be set.");
00155         // get dimensions
00156         const Integer first = this->p_data_->firstCol(), last  = this->p_data_->lastCol();
00157         // for each variables
00158         for (Integer j= first; j<= last; j++)
00159         {
00160           mean_[j] = Stat::mean(this->p_data_->col(j), weights);
00161           // compute the variance
00162           var_[j]  = varianceWithFixedMean(this->p_data_->col(j), weights, mean_[j]);
00163           cov_(j, j) = var_[j];
00164           // compute the covariances
00165           for (Integer i= first; i<j; i++)
00166           {
00167             cov_(i, j) = covarianceWithFixedMean(this->p_data_->col(i), this->p_data_->col(j), weights, mean_[i], mean_[j]);
00168             cov_(j, i) = cov_(i, j);
00169           }
00170         }
00171       }
00172       catch (Exception error)
00173       {
00174         this->msg_error_  = _T("Error in MultivariateReal::run(weights): ");
00175         this->msg_error_ += error.error();
00176         return false;
00177       }
00178       // no error
00179       return true;
00180     }
00181 
00182   protected:
00184     Integer nbSamples_;
00186     Integer nbVar_;
00187 
00189     TContainerHo mean_;
00191     TContainerHo var_;
00193     MatrixSquare cov_;
00194 
00196     virtual void update()
00197     {
00198       // if there is no data there is nothing to update
00199       if (this->p_data_)
00200       {
00201         nbSamples_ = this->p_data_->sizeVe();
00202         nbVar_ = this->p_data_->sizeHo();
00203         mean_.resize(this->p_data_->rangeHo());
00204         var_.resize(this->p_data_->rangeHo());
00205         cov_.resize(this->p_data_->rangeHo());
00206       }
00207     }
00208 };
00209 
00210 
00216 template < class TContainerHo, class TContainer2D >
00217 void mean( ITContainer2D< Real, TContainer2D> const&  V
00218          , ITContainer1D< Real, TContainerHo> & mean
00219          )
00220 {
00221   // get dimensions
00222   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00223   mean.resize(V.rangeHo());
00224   for (Integer j= firstVar; j<= lastVar; j++)
00225     mean[j] = Stat::mean(V.col(j));
00226 }
00227 
00238 template < class TContainerHo, class TContainerVe, class TContainer2D >
00239 void mean( ITContainer2D< Real, TContainer2D> const& V
00240          , ITContainer1D< Real, TContainerVe> const& W
00241          , ITContainer1D< Real, TContainerHo> & mean
00242          )
00243 {
00244   // get dimensions
00245   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00246   mean.resize(V.rangeHo());
00247   for (Integer j= firstVar; j<= lastVar; j++)
00248     mean[j] = Stat::mean(V.col(j), W);
00249 }
00250 
00255 template < class TContainerHo, class TContainer2D >
00256 TContainerHo mean( ITContainer2D< Real, TContainer2D> const&  V)
00257 {
00258   TContainerHo mean(V.rangeHo());
00259   // get dimensions
00260   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00261   for (Integer j= firstVar; j<= lastVar; j++)
00262     mean[j] = Stat::mean(V.col(j));
00263   return mean;
00264 }
00265 
00275 template < class TContainerHo, class TContainerVe, class TContainer2D >
00276 TContainerHo mean( ITContainer2D< Real, TContainer2D> const& V
00277                  , ITContainer1D< Real, TContainerVe> const& W
00278                  )
00279 {
00280   // create result
00281   TContainerHo mean(V.rangeHo());
00282   // get dimensions
00283   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00284   for (Integer j= firstVar; j<= lastVar; j++)
00285     mean[j] = Stat::mean(V.col(j), W);
00286   return mean;
00287 }
00288 
00298 template < class TContainer2D >
00299 typename TContainer2D::TContainerHo variance( ITContainer2D< Real, TContainer2D> const& V
00300                      , bool unbiased = false
00301                      )
00302 {
00303   // create result
00304   typename TContainer2D::TContainerHo var(V.rangeHo());
00305   // get dimensions
00306   const Integer firstVar = V.firstCol(), lastVar = V.lastCol();
00307   for (Integer j= firstVar; j<= lastVar; j++)
00308   {
00309     var[j] = Stat::variance(V.col(j), unbiased);
00310   }
00311   // return variance
00312   return var;
00313 }
00314 
00329 template < class TContainerHo, class TContainerVe, class TContainer2D >
00330 typename TContainer2D::TContainerHo variance( ITContainer2D< Real, TContainer2D> const& V
00331                      , TContainerVe const& W
00332                      , bool unbiased = false
00333                      )
00334 {
00335   // create result
00336     typename TContainer2D::TContainerHo var(V.rangeHo());
00337   // get dimensions
00338   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00339   // check if there exist a mean vector
00340   for (Integer j= firstVar; j<= lastVar; j++)
00341     var[j] = Stat::variance(V.col(j), W, unbiased);
00342   // return variance
00343   return var;
00344 }
00345 
00356 template < class TContainerHo, class TContainerVe, class TContainer2D >
00357 TContainerHo varianceWithFixedMean( ITContainer2D< Real, TContainer2D> const& V
00358                                   , TContainerHo const& mu
00359                                   , bool unbiased = false
00360                                   )
00361 {
00362   if (mu.range() != V.rangeHo())
00363     throw runtime_error("In varianceWithFixedMean(V, mu, unbiased) "
00364                              "mu.range() != V.rangeHo()");
00365   // create result
00366   TContainerHo var(V.rangeHo());
00367   // get dimensions
00368   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00369   for (Integer j= firstVar; j<= lastVar; j++)
00370     var[j] = Stat::varianceWithFixedMean(V.col(j), mu[j], unbiased);
00371   // return variance
00372   return var;
00373 }
00374 
00386 template < class TContainerHo, class TContainerVe, class TContainer2D >
00387 TContainerHo varianceWithFixedMean( ITContainer2D< Real, TContainer2D> const& V
00388                                   , TContainerVe const& W
00389                                   , TContainerHo const& mu
00390                                   , bool unbiased = false
00391                                   )
00392 {
00393 #ifdef STK_DEBUG
00394   if (mu.range() != V.rangeHo())
00395     throw runtime_error(_T("In varianceWithFixedMean(V, W, mu) "
00396                            "mu.range() != V.rangeHo()"));
00397 #endif
00398   // create result
00399   TContainerHo var(V.rangeHo());
00400   // get dimensions
00401   const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
00402   for (Integer j= firstVar; j<= lastVar; j++)
00403     var[j] = Stat::varianceWithFixedMean<TContainerVe>(V[j], W, mu[j], unbiased);
00404   // return variance
00405   return var;
00406 }
00407 
00418 template < class TContainer2D >
00419 void covariance( TContainer2D const& V
00420                , MatrixSquare & cov
00421                , bool unbiased = false
00422                )
00423 {
00424   typename ContainerTraits<Real, TContainer2D>::TContainerHo mean;
00425   // compute the
00426   Stat::mean(V, mean);
00427   // get dimensions
00428   const Integer firstVar = V.firstCol(), lastVar = V.lastCol();
00429   cov.resize(V.rangeHo());
00430   for (Integer j= firstVar; j<= lastVar; j++)
00431   {
00432     cov(j, j) = Stat::varianceWithFixedMean(V.col(j), mean[j], unbiased);
00433     for (Integer i= firstVar; i<j; i++)
00434     {
00435       cov(j,i) = ( cov(i, j) = Stat::covarianceWithFixedMean(V.col(i), V.col(j), mean[i], mean[j], unbiased));
00436     }
00437   }
00438 }
00439 
00451 template < class TContainerVe, class TContainer2D >
00452 void covariance( ITContainer2D< Real, TContainer2D> const& V
00453                , ITContainer1D< Real, TContainerVe> const& W
00454                , MatrixSquare & cov
00455                , bool unbiased = false
00456                )
00457 {
00458   typename ContainerTraits<Real, TContainer2D>::TContainerHo mean;
00459   // compute the
00460   Stat::mean(V, W, mean);
00461   // get dimensions
00462   const Integer firstVar = V.firstCol(), lastVar = V.lastCol();
00463   cov.resize(V.rangeHo(), V.rangeHo());
00464   for (Integer j= firstVar; j<= lastVar; j++)
00465   {
00466     cov(j, j) = Stat::varianceWithFixedMean(V.col(j), W, unbiased);
00467     for (Integer i= firstVar; i<j; i++)
00468     {
00469       cov(j,i) = ( cov(i, j) = Stat::covarianceWithFixedMean(V.col(i), V.col(j), W, mean[i], mean[j], unbiased));
00470     }
00471   }
00472 }
00473 
00474 
00475 }  // namespace Stat
00476 
00477 }  // namespace STK
00478 
00479 #endif /*STK_STAT_MULTIVARIATEREAL_H*/