STK++ 1.0

STK_ITStatModel.h

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::StatModel
00027  * created on: 22 juil. 2011
00028  * Purpose: define the Interface base class ITStatModel (Statistical Model).
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  *
00031  **/
00032 
00037 #ifndef STK_ITSTATMODEL_H
00038 #define STK_ITSTATMODEL_H
00039 
00040 #include <cmath>
00041 
00042 #include "STK_IStatModelBase.h"
00043 #include "../../Sdk/include/STK_IRunnerPtr2D.h"
00044 #include "../../STatistiK/include/STK_Law_ITMultivariate.h"
00045 
00046 namespace STK
00047 {
00048 
00058 template < class TYPE
00059          , class TContainerHo
00060          , class TContainerVe
00061          , class TContainer2D
00062          >
00063 class ITStatModel : public IStatModelBase
00064                   , public IRunnerPtr2D< TYPE, TContainerHo, TContainerVe, TContainer2D>
00065 {
00067   typedef ITContainer2D<TYPE, TContainerHo, TContainerVe, TContainer2D> Container2D;
00069   typedef Law::ITMultivariate<TYPE, TContainerHo> MultiLaw;
00071   typedef IRunnerPtr2D< TYPE, TContainerHo, TContainerVe, TContainer2D> Runner2D;
00072 
00073   protected:
00075     ITStatModel( Container2D const* p_data)
00076                : IStatModelBase()
00077                , Runner2D(p_data)
00078                , p_law_(0)
00079     {
00080       if (this->p_data_)
00081       {
00082         nbSample_ = this->p_data_->sizeVe();
00083         nbVar_ = this->p_data_->sizeHo();
00084       }
00085     }
00086 
00087   public:
00089     virtual ~ITStatModel() { if (p_law_) delete p_law_;}
00090 
00094     inline MultiLaw const* p_law() const { return p_law_;}
00095 
00099     virtual void setLaw( MultiLaw* p_law) { p_law_ = p_law; }
00100 
00101   protected:
00103     MultiLaw* p_law_;
00104 
00106     virtual void compLogLikelihood()
00107     {
00108       // no data
00109       if (!this->p_data_) return;
00110       // check there exists a law
00111       if (!p_law_) throw std::runtime_error("In ITStatModel::compLogLikelihood() "
00112                                             "p_law_ is not initialized.");
00113       // get dimensions of the samples and sum over all log-likelihood values
00114       const Integer first = this->p_data_->firstRow(), last = this->p_data_->lastRow();
00115       Real sum = 0.0;
00116       for (Integer i=first; i<= last; i++)
00117       {
00118         sum += p_law_->lpdf(this->p_data_->row(i));
00119       }
00120       logLikelihood_ = sum;
00121     }
00122 };
00123 
00124 } // namespace STK
00125 
00126 #endif /* STK_ITStatModel */