STK++ 0.9.13
STK_IMultiStatModel.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Model
27 * created on: 22 juil. 2011
28 * Purpose: define the class IMultiStatModel.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 *
31 **/
32
37#ifndef STK_IMULTISTATMODEL_H
38#define STK_IMULTISTATMODEL_H
39
40#include <cmath>
41#include <Sdk.h>
42
43#include "STK_IStatModelBase.h"
44
47
48
49namespace STK
50{
51
52namespace hidden
53{
55template<class Derived> struct StatModelTraits;
56
57} // namespace hidden
58
62template <int Id> struct ModelParameters;
63
111template <class Derived>
113{
114 public:
123
124 protected:
128 IMultiStatModel( Data const& data): IStatModelBase(), p_data_(&data), param_(data.dataij().cols())
129 { this->initialize(data.dataij().sizeRows(), data.dataij().sizeCols());}
132 {
133 if (p_data)
134 {
135 this->initialize(p_data->sizeRows(), p_data->sizeCols());
136 param_.resize(p_data_->cols());
137 }
138 }
149
150 public:
152 inline Data const* const p_data() const { return (p_data_);}
154 inline Parameters const& param() const { return (param_);}
156 inline String const& error() const { return msg_error_;}
161 inline void setData( Data const* p_data)
162 {
163 p_data_ = p_data;
164 update();
165 }
167 bool run()
168 {
169#ifdef STK_DEBUG
170 if (!p_data())
172 return false;
173 }
174#endif
175 try
176 {
177 // compute parameters
178 this->asDerived().computeParameters();
179 // compute log-likelihood
181 // set the number of free parameters
182 this->setNbFreeParameter(this->asDerived().computeNbFreeParameters());
183 }
184 catch (Exception const& e)
185 { this->msg_error_ = e.error(); return false;}
186 return true;
187 }
192 bool run(WColVector const& weights)
193 {
194#ifdef STK_DEBUG
195 if (!p_data())
196 { this->msg_error_ = STKERROR_NO_ARG(IMultiStatModel::run(weights),data have not be set);
197 return false;
198 }
199#endif
200 try
201 {
202 // compute weighted parameters
203 this->asDerived().computeParameters(weights);
204 // compute log-likelihood
206 // set the number of free parameters
207 this->setNbFreeParameter(this->asDerived().computeNbFreeParameters());
208 }
209 catch (Exception const& e)
210 { this->msg_error_ = e.error(); return false;}
211 return true;
212 }
214 void writeParameters(ostream &os) { this->asDerived().writeParametersImpl(os);}
217 void writeParametersImpl(ostream &os) // default implementation
218 {
219#ifdef STK_DEBUG
220 stk_cout << _T("You should implement this method in your derived class\n");
221#endif
222 }
223
224 protected:
226 Data const* p_data_;
232 inline Parameters& param() { return (param_);}
235 {
236 Real sum = 0.0;
237 for (int i= p_data()->dataij().beginRows(); i< p_data()->dataij().endRows(); i++)
238 { sum += this->asDerived().computeLnLikelihood(p_data()->dataij().row(i));}
239 return(sum);
240 }
242 void update()
243 {
244 if (p_data_)
245 {
246 this->initialize(p_data()->dataij().sizeRows(), p_data()->dataij().sizeCols());
247 param_.resize(p_data()->dataij().cols());
248 }
249 else
250 { this->initialize(0,0); }
251 }
252};
253
254} // namespace STK
255
256#endif /* STK_IMULTISTATMODEL_H */
In this file we define the data wrapper class.
In this file we define the interface base class IStatModelBase.
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
In this file we define the Interface base classes IMultiLaw and JointProbability.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
This file include all the other header files of the project Sdk.
Sdk class for all library Exceptions.
virtual const String error() const
Returns a C-style character string describing the general cause of the current error.
Interface base class for all Multivariate Statistical Models.
void setData(Data const *p_data)
Set the data set.
Real computeLnLikelihood() const
compute the log Likelihood of the statistical model.
String const & error() const
hidden::StatModelTraits< Derived >::Parameters Parameters
Type of the parameters of the Model.
hidden::StatModelTraits< Derived >::WColVector WColVector
Type of the vector with the weights.
IMultiStatModel(Data const &data)
Constructor with data set.
bool run(WColVector const &weights)
compute the weighted empirical probability of success based on the observed variables.
void writeParametersImpl(ostream &os)
default implementation of the writeParameters method.
Parameters const & param() const
hidden::StatModelTraits< Derived >::Type Type
Type of the data in the container.
IMultiStatModel(IMultiStatModel const &model)
Copy constructor.
hidden::StatModelTraits< Derived >::Data Data
Type of the container with the data.
void writeParameters(ostream &os)
Data const * p_data_
Pointer on the parameters of the model.
IMultiStatModel()
default constructor.
Data const *const p_data() const
String msg_error_
String with the last error message.
Parameters param_
Pointer on the parameters of the model.
IMultiStatModel(Data const *p_data)
Constructor with a ptr on the data set.
bool run()
Estimate the parameters of the model and update the model.
void update()
update the model if a new data set is set
Interface base class for all classes implementing the curious recursive template paradigm.
Derived & asDerived()
static cast : return a reference of this with a cast to the derived class.
Interface base class for all Statistical Models.
void setNbFreeParameter(int const &nbFreeParameter)
set the number of free parameters of the model
void setLnLikelihood(Real const &lnLikelihood)
set the log-likelihood of the model
void initialize(int nbSample, int nbVariable)
set the dimensions of the parameters of the model
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
std::basic_string< Char > String
STK fundamental type of a String.
double Real
STK fundamental type of Real values.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
The namespace STK is the main domain space of the Statistical ToolKit project.
struct storing the parameters of the mixture.
Policy trait class for (Stat) Model classes.