STK++ 0.9.13
STK::IMultiStatModel< Derived > Class Template Reference

Interface base class for all Multivariate Statistical Models. More...

#include <STK_IMultiStatModel.h>

Inheritance diagram for STK::IMultiStatModel< Derived >:
Inheritance graph

Public Types

typedef hidden::StatModelTraits< Derived >::Data Data
 Type of the container with the data.
 
typedef hidden::StatModelTraits< Derived >::Type Type
 Type of the data in the container.
 
typedef hidden::StatModelTraits< Derived >::WColVector WColVector
 Type of the vector with the weights.
 
typedef hidden::StatModelTraits< Derived >::Parameters Parameters
 Type of the parameters of the Model.
 

Public Member Functions

Data const *const p_data () const
 
Parameters constparam () const
 
String consterror () const
 
void setData (Data const *p_data)
 Set the data set.
 
bool run ()
 Estimate the parameters of the model and update the model.
 
bool run (WColVector const &weights)
 compute the weighted empirical probability of success based on the observed variables.
 
void writeParameters (ostream &os)
 
void writeParametersImpl (ostream &os)
 default implementation of the writeParameters method.
 
- Public Member Functions inherited from STK::IStatModelBase
int nbSample () const
 
Real lnNbSample () const
 
int nbVariable () const
 
Real lnLikelihood () const
 
Real likelihood () const
 
int nbFreeParameter () const
 
Real computeBIC () const
 
Real computeAIC () const
 
Real computeML () const
 
- Public Member Functions inherited from STK::IRecursiveTemplate< Derived >
Derived & asDerived ()
 static cast : return a reference of this with a cast to the derived class.
 
Derived constasDerived () const
 static cast : return a const reference of this with a cast to the derived class.
 
Derived * asPtrDerived ()
 static cast : return a ptr on a Derived of this with a cast to the derived class.
 
Derived constasPtrDerived () const
 static cast : return a ptr on a constant Derived of this with a cast to the derived class.
 
Derived * clone () const
 create a leaf using the copy constructor of the Derived class.
 
Derived * clone (bool isRef) const
 create a leaf using the copy constructor of the Derived class and a flag determining if the clone is a reference or not.
 

Protected Member Functions

 IMultiStatModel ()
 default constructor.
 
 IMultiStatModel (Data const &data)
 Constructor with data set.
 
 IMultiStatModel (Data const *p_data)
 Constructor with a ptr on the data set.
 
 IMultiStatModel (IMultiStatModel const &model)
 Copy constructor.
 
 ~IMultiStatModel ()
 destructor
 
Parametersparam ()
 
Real computeLnLikelihood () const
 compute the log Likelihood of the statistical model.
 
void update ()
 update the model if a new data set is set
 
- Protected Member Functions inherited from STK::IStatModelBase
 IStatModelBase ()
 Default constructor.
 
 IStatModelBase (int nbSample)
 Constructor with specified dimension.
 
 IStatModelBase (int nbSample, int nbVariable)
 Constructor with specified dimension.
 
 IStatModelBase (IStatModelBase const &model)
 Copy constructor.
 
 ~IStatModelBase ()
 destructor
 
void setNbFreeParameter (int const &nbFreeParameter)
 set the number of free parameters of the model
 
void setNbSample (int const &nbSample)
 set the number of samples of the model
 
void setNbVariable (int const &nbVariable)
 set the number of variables 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
 
- Protected Member Functions inherited from STK::IRecursiveTemplate< Derived >
 IRecursiveTemplate ()
 constructor.
 
 ~IRecursiveTemplate ()
 destructor.
 

Protected Attributes

Data constp_data_
 Pointer on the parameters of the model.
 
Parameters param_
 Pointer on the parameters of the model.
 
String msg_error_
 String with the last error message.
 

Detailed Description

template<class Derived>
class STK::IMultiStatModel< Derived >

Interface base class for all Multivariate Statistical Models.

A Statistical model, $ \mathcal{P}$, is a collection of multivariate probability distribution functions or probability density functions (collectively referred to as ''distributions'' for brevity). A parametric model is a collection of distributions, each of which is indexed by a unique finite-dimensional parameter: $\mathcal{P}=\{\mathbb{P}_{\theta} : \theta \in \Theta\}$, where $\theta$ is a parameter and $\Theta \subseteq \mathbb{R}^d$ is the feasible region of parameters, which is a subset of d-dimensional Euclidean space.

A statistical model may be used to describe the set of distributions from which one assumes that a particular data set is sampled. For example, if one assumes that data arise from a multivariate Gaussian distribution, then one has assumed a Gaussian model: $
  \mathcal{P}=\{\mathbb{P}(x; \mu, \Sigma) = \frac{1}{\sqrt{2 \pi |\Sigma|} }
  \exp\left\{ -\frac{1}{2}(x-\mu)'\Sigma^{-1}(x-\mu)\right\} : \mu \in \mathbb{R}^p, \Sigma > 0\}
$.

From a computational point of view a statistical model is defined with the help of two elements

  • A data set where the number of samples is the number of rows and the number of variable is the number of columns. This data set is stored in a Container of type Data.
  • A set of parameters stored in a class of type Parameters. These parameters can be created using the createParameters method with the effect to call the default constructor of the Parameters class, or can be set to this class using the method setParameters.

The design for this class is the Curious Recursive Pattern. Derived implementations of this interface have to implement the following psudo virtual methods:

int computeNbFreeParameters() const;
Real computeLnLikelihood( RowVector const& rowData) const;
void computeParameters();
void computeParameters(ColVector const& weights);
Real computeLnLikelihood() const
compute the log Likelihood of the statistical model.
void writeParameters(ostream &os)
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
double Real
STK fundamental type of Real values.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
Template Parameters
Dervivedany kind of multidimensionnal model.
See also
IMixtureDensity.

Definition at line 112 of file STK_IMultiStatModel.h.

Member Typedef Documentation

◆ Data

template<class Derived >
typedef hidden::StatModelTraits<Derived>::Data STK::IMultiStatModel< Derived >::Data

Type of the container with the data.

Definition at line 116 of file STK_IMultiStatModel.h.

◆ Parameters

template<class Derived >
typedef hidden::StatModelTraits<Derived>::Parameters STK::IMultiStatModel< Derived >::Parameters

Type of the parameters of the Model.

Definition at line 122 of file STK_IMultiStatModel.h.

◆ Type

template<class Derived >
typedef hidden::StatModelTraits<Derived>::Type STK::IMultiStatModel< Derived >::Type

Type of the data in the container.

Definition at line 118 of file STK_IMultiStatModel.h.

◆ WColVector

template<class Derived >
typedef hidden::StatModelTraits<Derived>::WColVector STK::IMultiStatModel< Derived >::WColVector

Type of the vector with the weights.

Definition at line 120 of file STK_IMultiStatModel.h.

Constructor & Destructor Documentation

◆ IMultiStatModel() [1/4]

template<class Derived >
STK::IMultiStatModel< Derived >::IMultiStatModel ( )
inlineprotected

default constructor.

Definition at line 126 of file STK_IMultiStatModel.h.

126: IStatModelBase(), p_data_(0), param_() {}
Data const * p_data_
Pointer on the parameters of the model.
Parameters param_
Pointer on the parameters of the model.
IStatModelBase()
Default constructor.

◆ IMultiStatModel() [2/4]

template<class Derived >
STK::IMultiStatModel< Derived >::IMultiStatModel ( Data const data)
inlineprotected

Constructor with data set.

Definition at line 128 of file STK_IMultiStatModel.h.

128 : IStatModelBase(), p_data_(&data), param_(data.dataij().cols())
129 { this->initialize(data.dataij().sizeRows(), data.dataij().sizeCols());}
void initialize(int nbSample, int nbVariable)
set the dimensions of the parameters of the model

References STK::IStatModelBase::initialize().

◆ IMultiStatModel() [3/4]

template<class Derived >
STK::IMultiStatModel< Derived >::IMultiStatModel ( Data const p_data)
inlineprotected

Constructor with a ptr on the data set.

Definition at line 131 of file STK_IMultiStatModel.h.

132 {
133 if (p_data)
134 {
135 this->initialize(p_data->sizeRows(), p_data->sizeCols());
136 param_.resize(p_data_->cols());
137 }
138 }
Data const *const p_data() const

References STK::IStatModelBase::initialize(), STK::IMultiStatModel< Derived >::p_data(), STK::IMultiStatModel< Derived >::p_data_, and STK::IMultiStatModel< Derived >::param_.

◆ IMultiStatModel() [4/4]

template<class Derived >
STK::IMultiStatModel< Derived >::IMultiStatModel ( IMultiStatModel< Derived > const model)
inlineprotected

Copy constructor.

Parameters
modelthe model to copy

Definition at line 142 of file STK_IMultiStatModel.h.

143 : IStatModelBase(model)
144 , p_data_(model.p_data_)
145 , param_(param_)
146 {}

◆ ~IMultiStatModel()

template<class Derived >
STK::IMultiStatModel< Derived >::~IMultiStatModel ( )
inlineprotected

destructor

Definition at line 148 of file STK_IMultiStatModel.h.

148{}

Member Function Documentation

◆ computeLnLikelihood()

template<class Derived >
Real STK::IMultiStatModel< Derived >::computeLnLikelihood ( ) const
inlineprotected

compute the log Likelihood of the statistical model.

Definition at line 234 of file STK_IMultiStatModel.h.

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 }
Derived & asDerived()
static cast : return a reference of this with a cast to the derived class.
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays

References STK::IRecursiveTemplate< Derived >::asDerived(), STK::IMultiStatModel< Derived >::p_data(), and STK::sum().

Referenced by STK::IMultiStatModel< Derived >::run(), and STK::IMultiStatModel< Derived >::run().

◆ error()

template<class Derived >
String const & STK::IMultiStatModel< Derived >::error ( ) const
inline
Returns
the last error message

Definition at line 156 of file STK_IMultiStatModel.h.

156{ return msg_error_;}
String msg_error_
String with the last error message.

References STK::IMultiStatModel< Derived >::msg_error_.

◆ p_data()

◆ param() [1/2]

template<class Derived >
Parameters & STK::IMultiStatModel< Derived >::param ( )
inlineprotected
Returns
the reference on the parameters

Definition at line 232 of file STK_IMultiStatModel.h.

232{ return (param_);}

References STK::IMultiStatModel< Derived >::param_.

◆ param() [2/2]

template<class Derived >
Parameters const & STK::IMultiStatModel< Derived >::param ( ) const
inline
Returns
a reference on the parameters

Definition at line 154 of file STK_IMultiStatModel.h.

154{ return (param_);}

References STK::IMultiStatModel< Derived >::param_.

◆ run() [1/2]

template<class Derived >
bool STK::IMultiStatModel< Derived >::run ( )
inline

Estimate the parameters of the model and update the model.

Definition at line 167 of file STK_IMultiStatModel.h.

168 {
169#ifdef STK_DEBUG
170 if (!p_data())
171 { this->msg_error_ = STKERROR_NO_ARG(IMultiStatModel::run,data have not be set);
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 }
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
bool run()
Estimate the parameters of the model and update the model.
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

References STK::IRecursiveTemplate< Derived >::asDerived(), STK::IMultiStatModel< Derived >::computeLnLikelihood(), STK::Exception::error(), STK::IMultiStatModel< Derived >::msg_error_, STK::IMultiStatModel< Derived >::p_data(), STK::IMultiStatModel< Derived >::run(), STK::IStatModelBase::setLnLikelihood(), STK::IStatModelBase::setNbFreeParameter(), and STKERROR_NO_ARG.

Referenced by STK::IMultiStatModel< Derived >::run(), and STK::IMultiStatModel< Derived >::run().

◆ run() [2/2]

template<class Derived >
bool STK::IMultiStatModel< Derived >::run ( WColVector const weights)
inline

compute the weighted empirical probability of success based on the observed variables.

The NA values are discarded.

Parameters
weightsthe weights of the observations

Definition at line 192 of file STK_IMultiStatModel.h.

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 }

References STK::IRecursiveTemplate< Derived >::asDerived(), STK::IMultiStatModel< Derived >::computeLnLikelihood(), STK::Exception::error(), STK::IMultiStatModel< Derived >::msg_error_, STK::IMultiStatModel< Derived >::p_data(), STK::IMultiStatModel< Derived >::run(), STK::IStatModelBase::setLnLikelihood(), STK::IStatModelBase::setNbFreeParameter(), and STKERROR_NO_ARG.

◆ setData()

template<class Derived >
void STK::IMultiStatModel< Derived >::setData ( Data const p_data)
inline

Set the data set.

If the state of the derived runner change when a new data set is set the user have to overload the udpate() method.

Parameters
p_dataA pointer on the data set to run

Definition at line 161 of file STK_IMultiStatModel.h.

162 {
163 p_data_ = p_data;
164 update();
165 }
void update()
update the model if a new data set is set

References STK::IMultiStatModel< Derived >::p_data(), STK::IMultiStatModel< Derived >::p_data_, and STK::IMultiStatModel< Derived >::update().

◆ update()

template<class Derived >
void STK::IMultiStatModel< Derived >::update ( )
inlineprotected

update the model if a new data set is set

Definition at line 242 of file STK_IMultiStatModel.h.

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 }

References STK::IStatModelBase::initialize(), STK::IMultiStatModel< Derived >::p_data(), STK::IMultiStatModel< Derived >::p_data_, and STK::IMultiStatModel< Derived >::param_.

Referenced by STK::IMultiStatModel< Derived >::setData().

◆ writeParameters()

template<class Derived >
void STK::IMultiStatModel< Derived >::writeParameters ( ostream os)
inline
Parameters
osthe output stream for the parameters

Definition at line 214 of file STK_IMultiStatModel.h.

214{ this->asDerived().writeParametersImpl(os);}

References STK::IRecursiveTemplate< Derived >::asDerived().

◆ writeParametersImpl()

template<class Derived >
void STK::IMultiStatModel< Derived >::writeParametersImpl ( ostream os)
inline

default implementation of the writeParameters method.

Parameters
osthe output stream for the parameters

Definition at line 217 of file STK_IMultiStatModel.h.

218 {
219#ifdef STK_DEBUG
220 stk_cout << _T("You should implement this method in your derived class\n");
221#endif
222 }
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.

References _T, and stk_cout.

Member Data Documentation

◆ msg_error_

template<class Derived >
String STK::IMultiStatModel< Derived >::msg_error_
protected

◆ p_data_

◆ param_


The documentation for this class was generated from the following file: