STK++ 1.0

STK_GaussianStatModel.cpp

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2011  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::
00027  * created on: 13 août 2011
00028  * Purpose:  .
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  *
00031  **/
00032 
00037 #include "../include/STK_Gaussian2BlocksStatModel.h"
00038 
00039 #include "../../STatistiK/include/STK_Stat_UnivariateReal.h"
00040 #include "../../STatistiK/include/STK_Stat_BivariateRealReal.h"
00041 #include "../../STatistiK/include/STK_Law_MultivariateNormal.h"
00042 
00043 namespace STK
00044 {
00045 
00046 /* constructor */
00047 GaussianStatModel::GaussianStatModel( Matrix const* p_data)
00048                                     : RealStatModel(p_data)
00049                                     , mean_(p_data_->rangeHo())
00050                                     , cov_(p_data_->rangeHo())
00051 {
00052   nbFreeParameter_ = nbVar_ + (nbVar_* (nbVar_-1))/2;
00053 }
00054 
00055 /* destructor */
00056 GaussianStatModel::~GaussianStatModel() {}
00057 
00058 /* implementation of the Gaussian statistical model
00059  * @return @c true if no error occur and @c false otherwise.
00060  */
00061 bool GaussianStatModel::run()
00062 {
00063   // compute the mean
00064   compMean();
00065   // compute the covariance matrix
00066   compCovariance();
00067   // create p_law_ (will be deleted in base class)
00068   // update gaussian law (will be deleted in base class)
00069   if (!p_law_) p_law_ = new Law::MultivariateNormal<Point>(mean_, cov_);
00070   else         p_law_->update();
00071   // compute log likelihood of the gaussian law
00072   logLikelihood_ = static_cast<Law::MultivariateNormal<Point>* >(p_law_)->logLikelihood<Vector, Matrix>(*p_data_ );
00073   // everything ok
00074   return true;
00075 }
00076 
00077 /* implementation of the weighted Gaussian statistical model
00078  * @param weights the weights of the samples
00079  * @return @c true if no error occur and @c false otherwise.
00080  */
00081 bool GaussianStatModel::run(Vector const& weights)
00082 {
00083   // compute the mean
00084   compWeightedMean(weights);
00085   // compute the covariance matrix
00086   compWeightedCovariance(weights);
00087   // create p_law_ (will be deleted in base class)
00088   // update gaussian law (will be deleted in base class)
00089   if (!p_law_) p_law_ = new Law::MultivariateNormal<Point>(mean_, cov_);
00090   else         p_law_->update();
00091   // compute log likelihood of the gaussian law
00092   logLikelihood_ = static_cast<Law::MultivariateNormal<Point>* >(p_law_)->logLikelihood<Vector, Matrix>(*p_data_ );
00093   // everything ok
00094   return true;
00095 }
00096 
00097 /* compute the empirical mean */
00098 void GaussianStatModel::compMean()
00099 {
00100   // resize mean
00101   mean_.resize(p_data_->rangeHo());
00102   // get dimensions
00103   const Integer first = p_data_->firstCol(), last = p_data_->lastCol();
00104   for (Integer j= first; j <= last; ++j)
00105   {
00106     mean_[j] = Stat::mean<Vector>(p_data_->col(j));
00107   }
00108 }
00109 /* compute the empirical covariance matrix. */
00110 void GaussianStatModel::compCovariance()
00111 {
00112   // resize mean
00113   cov_.resize(p_data_->rangeHo());
00114   // get dimensions
00115   const Integer first = p_data_->firstCol(), last = p_data_->lastCol();
00116   for (Integer i= first; i <= last; ++i)
00117   {
00118     cov_(i, i) = Stat::varianceWithFixedMean<Vector>(p_data_->col(i), mean_[i]);
00119     for (Integer j= first; j < i; ++j)
00120     {
00121       cov_(i, j) = Stat::covarianceWithFixedMean<Vector>(p_data_->col(i), p_data_->col(j), mean_[i], mean_[j]);
00122       cov_(j, i) = cov_(i,j);
00123     }
00124   }
00125 }
00126 
00127 /* compute the weighted empirical mean */
00128 void GaussianStatModel::compWeightedMean(Vector const& weights)
00129 {
00130   // resize mean
00131   mean_.resize(p_data_->rangeHo());
00132   // get dimensions
00133   const Integer firstCol = p_data_->firstCol(), lastCol = p_data_->lastCol();
00134   for (Integer j= firstCol; j <= lastCol; ++j)
00135   {
00136     mean_[j] = Stat::mean<Vector>(p_data_->col(j), weights);
00137   }
00138 }
00139 /* compute the empirical covariance matrix. */
00140 void GaussianStatModel::compWeightedCovariance(Vector const& weights)
00141 {
00142   // resize mean
00143   cov_.resize(p_data_->rangeHo());
00144   // get dimensions
00145   const Integer first = p_data_->firstCol(), last = p_data_->lastCol();
00146   for (Integer i= first; i <= last; ++i)
00147   {
00148     cov_(i, i) = Stat::varianceWithFixedMean<Vector>(p_data_->col(i), weights, mean_[i]);
00149     for (Integer j= first; j < i; ++j)
00150     {
00151       cov_(i, j) = Stat::covarianceWithFixedMean<Vector>(p_data_->col(i), p_data_->col(j), weights, mean_[i], mean_[j]);
00152       cov_(j, i) = cov_(i,j);
00153     }
00154   }
00155 }
00156 
00157 } // namespace STK