STK++ 0.9.13
STK::MixtureSemiLearner Class Reference

Base class for Learning a Mixture mixed model when some classes are known. More...

#include <STK_MixtureSemiLearner.h>

Inheritance diagram for STK::MixtureSemiLearner:
Inheritance graph

Public Member Functions

 MixtureSemiLearner (int nbSample, int nbCluster)
 Constructor.
 
 MixtureSemiLearner (MixtureSemiLearner const &model)
 copy constructor.
 
virtual ~MixtureSemiLearner ()
 destructor
 
virtual MixtureSemiLearnercreate () const
 Create a composer, but reinitialize the mixtures parameters.
 
virtual MixtureSemiLearnerclone () const
 Create a clone of the current model, with mixtures parameters preserved.
 
std::vector< int > constknownLabels () const
 
std::vector< int > constunknownLabels () const
 
virtual int cStep ()
 Replace tik by zik.
 
virtual int sStep ()
 Simulate zi accordingly to tik and replace tik by zik by calling cStep().
 
virtual Real eStep ()
 compute the zi, the lnLikelihood of the current estimates and the next value of the tik.
 
virtual void mapStep ()
 Compute zi using the Map estimate.
 
template<class RowVector >
void setLabels (ExprBase< RowVector > const &zi)
 Set the known labels.
 
- Public Member Functions inherited from STK::MixtureComposer
 MixtureComposer (int nbSample, int nbCluster)
 Constructor.
 
 MixtureComposer (MixtureComposer const &composer)
 copy constructor.
 
virtual ~MixtureComposer ()
 The registered mixtures will be deleted there.
 
virtual Real lnComponentProbability (int i, int k) const
 
virtual void writeParameters (ostream &os) const
 write the parameters of the model in the stream os.
 
virtual void randomInit ()
 initialize randomly the parameters of the components of the model
 
virtual void paramUpdateStep ()
 Compute the proportions and the model parameters given the current tik mixture parameters.
 
virtual void imputationStep ()
 Impute the missing values.
 
virtual void samplingStep ()
 Simulation of all the latent variables and/or missing data excluding class labels.
 
virtual void storeIntermediateResults (int iteration)
 This step can be used to signal to the mixtures that they must store results.
 
virtual void releaseIntermediateResults ()
 This step can be used to signal to the mixtures that they must release the stored results.
 
virtual void setParametersStep ()
 Utility method allowing to signal to a mixture to set its parameters.
 
virtual void finalizeStep ()
 This step can be used by developer to finalize any thing.
 
- Public Member Functions inherited from STK::IMixtureComposer
virtual ~IMixtureComposer ()
 destructor
 
Clust::modelState state () const
 
void setState (Clust::modelState state)
 set the state of the model : should be used by any strategy
 
virtual void pStep ()
 Compute proportions using the ML estimates, default implementation.
 
virtual void initializeStep ()
 Initialize the model before its first use.
 
void randomClassInit ()
 Initialize randomly the labels zi of the model.
 
void randomFuzzyInit ()
 Initialize randomly the posterior probabilities tik of the model, then compute the zi values with mapStep, compute the initial parameter values using paramUpdateStep, and compute the tik using eStep().
 
- Public Member Functions inherited from STK::IMixtureStatModel
virtual ~IMixtureStatModel ()
 destructor
 
int nbCluster () const
 
CPointX constpk () const
 
CArrayXX consttik () const
 
CPointX consttk () const
 
CVectorXi constzi () const
 
std::vector< IMixture * > constv_mixtures () const
 
Real computeLnLikelihood (int i) const
 
Real computeLikelihood (int i) const
 
Real computeLnLikelihood () const
 
Real computeICL () const
 
IMixturegetMixture (String const &idData) const
 Utility lookup function allowing to find a Mixture from its idData.
 
void registerMixture (IMixture *p_mixture)
 register a mixture to the composer.
 
void releaseMixture (String const &idData)
 release a mixture from the composer.
 
int computeNbFreeParameters () const
 compute the number of free parameters of the model.
 
int computeNbMissingValues () const
 compute the missing values of the model.
 
template<class Array >
void setMixtureParameters (Array const &tik)
 set the mixture parameters using an array of posterior probabilities.
 
template<class Array , class RowVector >
void setMixtureParameters (Array const &tik, RowVector const &pk)
 set the mixture parameters giving the posterior probabilities and the proportions.
 
template<class RowVector >
void setProportions (RowVector const &pk)
 Set proportions of each classes.
 
template<class Manager , class Parameters >
void setParameters (IMixtureManager< Manager > const &manager, String const &idData, Parameters const &param)
 Utility method allowing to set the parameters to a specific mixture.
 
template<class Manager >
void createMixture (IMixtureManager< Manager > &manager)
 Utility method allowing to create all the mixtures registered in the data handler of a mixture manager and to register them.
 
template<class Manager >
IMixturecreateMixture (IMixtureManager< Manager > &manager, String const &idData)
 Utility method allowing to create a mixture with a given data set and register it.
 
template<class Manager >
void removeMixture (IMixtureManager< Manager > &manager, String const &idData)
 Utility method allowing to release completely a mixture with its data set.
 
template<class Manager , class Parameters >
void getParameters (IMixtureManager< Manager > const &manager, String const &idData, Parameters &param) const
 Utility method allowing to get the parameters of a specific mixture.
 
template<class Manager , class MissingValues >
void getMissingValues (IMixtureManager< Manager > const &manager, String const &idData, MissingValues &missing) const
 Utility method allowing to get the missing values of a specific mixture.
 
template<class DataHandler >
void createMixture (IMixtureManager< DataHandler > &manager)
 
- 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
 

Protected Member Functions

virtual void initializeMixtureParameters ()
 Create the mixture model parameters tik_ and pk_.
 
virtual int randomTik ()
 generate random tik_
 
virtual int randomZi ()
 generate random zi_
 
void createSemiLearner (MixtureSemiLearner const *const p_learner)
 Copy the ingredient of the semi-learner in the creation process.
 
- Protected Member Functions inherited from STK::MixtureComposer
void createComposer (std::vector< IMixture * > const &v_mixtures_)
 Create the composer using existing data handler and mixtures.
 
- Protected Member Functions inherited from STK::IMixtureComposer
 IMixtureComposer (int nbSample, int nbCluster)
 Constructor.
 
 IMixtureComposer (IMixtureComposer const &model)
 copy constructor.
 
void sStep (int i)
 Simulate zi accordingly to tik.
 
void cStep (int i)
 Replace tik by zik.
 
Real eStep (int i)
 compute one zi and the next value of the tik for i fixed
 
void mapStep (int i)
 Compute zi using the Map estimate for i fixed.
 
- Protected Member Functions inherited from STK::IMixtureStatModel
 IMixtureStatModel (int nbSample, int nbCluster)
 Constructor.
 
 IMixtureStatModel (IMixtureStatModel const &model)
 copy constructor.
 
void setNbCluster (int nbCluster)
 set the number of cluster of the model
 
- 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 Attributes

std::vector< intknownLabels_
 indexes of the known labels
 
std::vector< intunknownLabels_
 indexes of the unknown labels
 
- Protected Attributes inherited from STK::MixtureComposer
Real meanlnLikelihood_
 averaged lnLikelihood values.
 
- Protected Attributes inherited from STK::IMixtureStatModel
int nbCluster_
 number of cluster.
 
CPointX pk_
 The proportions of each mixtures.
 
CArrayXX tik_
 The tik probabilities.
 
CPointX tk_
 The sum of the columns of tik_.
 
CVectorXi zi_
 The zi class label.
 
std::vector< IMixture * > v_mixtures_
 vector of pointers to the mixtures components
 

Additional Inherited Members

- Public Types inherited from STK::MixtureComposer
typedef std::vector< IMixture * >::const_iterator ConstMixtIterator
 
typedef std::vector< IMixture * >::iterator MixtIterator
 
- Public Types inherited from STK::IMixtureStatModel
typedef std::vector< IMixture * >::const_iterator ConstMixtIterator
 
typedef std::vector< IMixture * >::iterator MixtIterator
 

Detailed Description

Base class for Learning a Mixture mixed model when some classes are known.

This class extend the MixtureComposer class and allow to handle the case where some labels are known (semi-supervised learning).

See also
MixtureComposer, MixtureLearner

Definition at line 52 of file STK_MixtureSemiLearner.h.

Constructor & Destructor Documentation

◆ MixtureSemiLearner() [1/2]

STK::MixtureSemiLearner::MixtureSemiLearner ( int  nbSample,
int  nbCluster 
)

Constructor.

Parameters
nbCluster,nbSamplenumber of clusters and samples

Definition at line 48 of file STK_MixtureSemiLearner.cpp.

50 , knownLabels_()
52{
53 int i = zi_.begin();
54 std::vector<int>::iterator it;
55 for (it = unknownLabels_.begin(); it != unknownLabels_.end(); ++it, ++i)
56 { *it = i;}
57}
CVectorXi zi_
The zi class label.
MixtureComposer(int nbSample, int nbCluster)
Constructor.
std::vector< int > unknownLabels_
indexes of the unknown labels
std::vector< int > knownLabels_
indexes of the known labels

References unknownLabels_, and STK::IMixtureStatModel::zi_.

◆ MixtureSemiLearner() [2/2]

STK::MixtureSemiLearner::MixtureSemiLearner ( MixtureSemiLearner const model)

copy constructor.

Parameters
modelthe model to clone

Definition at line 61 of file STK_MixtureSemiLearner.cpp.

62 : MixtureComposer(model)
63 , knownLabels_(model.knownLabels_)
64 , unknownLabels_(model.unknownLabels_)
65{}

◆ ~MixtureSemiLearner()

STK::MixtureSemiLearner::~MixtureSemiLearner ( )
virtual

destructor

Definition at line 67 of file STK_MixtureSemiLearner.cpp.

67{}

Member Function Documentation

◆ clone()

MixtureSemiLearner * STK::MixtureSemiLearner::clone ( ) const
virtual

Create a clone of the current model, with mixtures parameters preserved.

Reimplemented from STK::MixtureComposer.

Reimplemented in STK::MixtureSemiLearnerFixedProp.

Definition at line 70 of file STK_MixtureSemiLearner.cpp.

71{ return new MixtureSemiLearner(*this);}
MixtureSemiLearner(int nbSample, int nbCluster)
Constructor.

◆ create()

MixtureSemiLearner * STK::MixtureSemiLearner::create ( ) const
virtual

Create a composer, but reinitialize the mixtures parameters.

Reimplemented from STK::MixtureComposer.

Reimplemented in STK::MixtureSemiLearnerFixedProp.

Definition at line 73 of file STK_MixtureSemiLearner.cpp.

74{
75 // set dimensions
77 p_composer->createSemiLearner(this);
78 p_composer->createComposer(v_mixtures());
79 return p_composer;
80}
std::vector< IMixture * > const & v_mixtures() const

References STK::MixtureComposer::createComposer(), createSemiLearner(), STK::IMixtureStatModel::nbCluster(), STK::IStatModelBase::nbSample(), and STK::IMixtureStatModel::v_mixtures().

◆ createSemiLearner()

void STK::MixtureSemiLearner::createSemiLearner ( MixtureSemiLearner const *const  p_learner)
protected

Copy the ingredient of the semi-learner in the creation process.

Definition at line 178 of file STK_MixtureSemiLearner.cpp.

179{
180 knownLabels_ = p_learner->knownLabels();
181 unknownLabels_ = p_learner->unknownLabels();
182}

References knownLabels_, and unknownLabels_.

Referenced by create(), and STK::MixtureSemiLearnerFixedProp::create().

◆ cStep()

int STK::MixtureSemiLearner::cStep ( )
virtual

Replace tik by zik.

Returns
the minimal value of individuals in a class

Reimplemented from STK::IMixtureComposer.

Definition at line 84 of file STK_MixtureSemiLearner.cpp.

85{
86 std::vector<int>::const_iterator it;
87 for (it=unknownLabels_.begin(); it != unknownLabels_.end(); it++)
89 // count the number of individuals in each class
91 return tk_.minElt();
92}
Type const minElt(int &row, int &col) const
virtual int cStep()
Replace tik by zik.
CArrayXX tik_
The tik probabilities.
CPointX tk_
The sum of the columns of tik_.
hidden::FunctorTraits< Derived, SumOp >::Row sum(Derived const &A)
Compute the sum of A.

References STK::IMixtureComposer::cStep(), STK::ExprBase< Derived >::minElt(), STK::Stat::sum(), STK::IMixtureStatModel::tik_, STK::IMixtureStatModel::tk_, and unknownLabels_.

Referenced by randomZi(), and sStep().

◆ eStep()

Real STK::MixtureSemiLearner::eStep ( )
virtual

compute the zi, the lnLikelihood of the current estimates and the next value of the tik.

Returns
the minimal value of tk

Reimplemented from STK::IMixtureComposer.

Definition at line 105 of file STK_MixtureSemiLearner.cpp.

106{
107#ifdef STK_MIXTURE_DEBUG
108 stk_cout << _T("Entering MixtureSemiLearner::eStep()\n");
109#endif
110 Real sum = 0.; tk_ =0.;
111//#ifdef _OPENMP
112//#pragma omp parallel for reduction (+:sum)
113//#endif
114 std::vector<int>::const_iterator it;
115 for(it=unknownLabels_.begin(); it != unknownLabels_.end(); it++)
116 { sum += MixtureComposer::eStep(*it);}
117 for(it=knownLabels_.begin(); it != knownLabels_.end(); it++)
118 { sum += lnComponentProbability(*it, zi_[*it]) + std::log(pk_[zi_[*it]]);}
119 // update log-likelihood
121 // compute proportions
123#ifdef STK_MIXTURE_DEBUG
124 stk_cout << _T("MixtureSemiLearner::eStep() done\n");
125 stk_cout << _T("lnLikelihood =") << sum << _T("\n");
126#endif
127 return tk_.minElt();
128}
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
virtual Real eStep()
compute the zi, the lnLikelihood of the current estimates and the next value of the tik.
CPointX pk_
The proportions of each mixtures.
void setLnLikelihood(Real const &lnLikelihood)
set the log-likelihood of the model
virtual Real lnComponentProbability(int i, int k) const
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
double Real
STK fundamental type of Real values.
hidden::FunctorTraits< Derived, SumOp >::Row sumByCol(Derived const &A)

References _T, STK::IMixtureComposer::eStep(), knownLabels_, STK::MixtureComposer::lnComponentProbability(), STK::ExprBase< Derived >::minElt(), STK::IMixtureStatModel::pk_, STK::IStatModelBase::setLnLikelihood(), stk_cout, STK::sum(), STK::Stat::sumByCol(), STK::IMixtureStatModel::tik_, STK::IMixtureStatModel::tk_, unknownLabels_, and STK::IMixtureStatModel::zi_.

◆ initializeMixtureParameters()

void STK::MixtureSemiLearner::initializeMixtureParameters ( )
protectedvirtual

Create the mixture model parameters tik_ and pk_.

Reimplemented from STK::IMixtureComposer.

Definition at line 140 of file STK_MixtureSemiLearner.cpp.

141{
142 std::vector<int>::const_iterator it;
143 for (it=unknownLabels_.begin(); it != unknownLabels_.end(); it++)
144 { tik_.row(*it) = 1./nbCluster();}
145 for (it=knownLabels_.begin(); it != knownLabels_.end(); it++)
146 { tik_.row(*it) = 0.; tik_(*it, zi_[*it]) = 1.;}
147}
hidden::CSlice< Derived, 1, sizeCols_ >::Result row(int i) const
implement the row operator using a reference on the row of the allocator

References knownLabels_, STK::IMixtureStatModel::nbCluster(), STK::ICArray< Derived >::row(), STK::IMixtureStatModel::tik_, unknownLabels_, and STK::IMixtureStatModel::zi_.

◆ knownLabels()

std::vector< int > const & STK::MixtureSemiLearner::knownLabels ( ) const
inline
Returns
the vector of the known labels

Definition at line 70 of file STK_MixtureSemiLearner.h.

70{ return knownLabels_;}

References knownLabels_.

◆ mapStep()

void STK::MixtureSemiLearner::mapStep ( )
virtual

Compute zi using the Map estimate.

Reimplemented from STK::IMixtureComposer.

Definition at line 131 of file STK_MixtureSemiLearner.cpp.

132{
133 std::vector<int>::const_iterator it;
134 for (it=unknownLabels_.begin(); it != unknownLabels_.end(); it++)
136}
virtual void mapStep()
Compute zi using the Map estimate.

References STK::IMixtureComposer::mapStep(), and unknownLabels_.

◆ randomTik()

int STK::MixtureSemiLearner::randomTik ( )
protectedvirtual

generate random tik_

Reimplemented from STK::IMixtureComposer.

Definition at line 150 of file STK_MixtureSemiLearner.cpp.

151{
152 tk_ = 0.;
153 std::vector<int>::const_iterator it;
154 for (it = unknownLabels_.begin(); it != unknownLabels_.end(); ++it)
155 {
156 // create a reference on the i-th row
157 CPointX tikRowi(tik_.row(*it), true);
158 tikRowi.randUnif();
159 tikRowi = tikRowi * pk_;
160 tikRowi /= tikRowi.sum();
161 tk_ += tikRowi;
162 }
163 return tk_.minElt();
164}
Type const sum() const
CArrayPoint< Real, UnknownSize, Arrays::by_col_ > CPointX

References STK::ExprBase< Derived >::minElt(), STK::IMixtureStatModel::pk_, STK::ICArray< Derived >::row(), STK::IMixtureStatModel::tik_, STK::IMixtureStatModel::tk_, and unknownLabels_.

◆ randomZi()

int STK::MixtureSemiLearner::randomZi ( )
protectedvirtual

generate random zi_

Reimplemented from STK::IMixtureComposer.

Definition at line 167 of file STK_MixtureSemiLearner.cpp.

168{
169 Law::Categorical law(pk_);
170 std::vector<int>::const_iterator it;
171 for (it = unknownLabels_.begin(); it != unknownLabels_.end(); ++it)
172 { zi_[*it] = law.rand();}
173 // create tik and compute nk
174 return cStep();
175}
Derived & rand(Law::IUnivLaw< Type > const &law)
set random values to this using a distribution law given by the user.
virtual int cStep()
Replace tik by zik.

References cStep(), STK::IMixtureStatModel::pk_, STK::Law::Categorical::rand(), unknownLabels_, and STK::IMixtureStatModel::zi_.

◆ setLabels()

template<class RowVector >
void STK::MixtureSemiLearner::setLabels ( ExprBase< RowVector > const zi)

Set the known labels.

This method is expecting a vector of size nbSample with NA values for unknown labels.

Parameters
zithe vector with the labels

Definition at line 113 of file STK_MixtureSemiLearner.h.

114{
115 if (nbSample() != zi.size())
116 { STKRUNTIME_ERROR_NO_ARG(MixtureSemiLearner::setKnownLabels,zi must be of size nbSample);}
117 int nbUnknown = zi.isNA().count();
118#ifdef STK_MIXTURE_DEBUG
119 stk_cout << _T("Entering MixtureSemiLearner::setLabels()\n");
120 stk_cout << _T("nbUnknown=") << nbUnknown << _T("\n");
121#endif
122 // clear previous initialization
123 unknownLabels_.clear();
124 knownLabels_.clear();
125 zi_= baseIdx;
126 // reserve and set lables
127 unknownLabels_.reserve(nbUnknown);
128 knownLabels_.reserve(nbSample() - nbUnknown);
129 for (int i=zi.begin(); i < zi.end(); ++i)
130 {
131 if (isNA(zi[i]))
132 { unknownLabels_.push_back(i);}
133 else
134 {
135 knownLabels_.push_back(i); zi_[i] = zi[i];
136 tik_.row(i) = 0; tik_(i, zi_[i]) = 1.;
137 }
138 }
139#ifdef STK_MIXTURE_DEBUG
140 stk_cout << _T("Terminating MixtureSemiLearner::setLabels()\n");
141 stk_cout << _T("unknownLabels_.size()=") << unknownLabels_.size() << _T("\n");
142 stk_cout << _T("knownLabels_.size()=") << knownLabels_.size() << _T("\n");
143#endif
144}
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
UnaryOperator< IsNaOp< Type >, Derived > isNA() const
CVectorXi const & zi() const
bool isNA(Type const &x)
utility method allowing to know if a value is a NA (Not Available) value
const int baseIdx
base index of the containers created in STK++.

References _T, STK::baseIdx, STK::ExprBase< Derived >::isNA(), STK::isNA(), knownLabels_, STK::IStatModelBase::nbSample(), STK::ICArray< Derived >::row(), stk_cout, STKRUNTIME_ERROR_NO_ARG, STK::IMixtureStatModel::tik_, unknownLabels_, STK::IMixtureStatModel::zi(), and STK::IMixtureStatModel::zi_.

◆ sStep()

int STK::MixtureSemiLearner::sStep ( )
virtual

Simulate zi accordingly to tik and replace tik by zik by calling cStep().

Returns
the minimal value of individuals in a class

Reimplemented from STK::IMixtureComposer.

Definition at line 95 of file STK_MixtureSemiLearner.cpp.

96{
97 // simulate zi
98 std::vector<int>::const_iterator it;
99 for (it=unknownLabels_.begin(); it != unknownLabels_.end(); it++)
101 return cStep();
102}
virtual int sStep()
Simulate zi accordingly to tik and replace tik by zik by calling cStep().

References cStep(), STK::IMixtureComposer::sStep(), and unknownLabels_.

◆ unknownLabels()

std::vector< int > const & STK::MixtureSemiLearner::unknownLabels ( ) const
inline
Returns
the vector of the unknown labels

Definition at line 72 of file STK_MixtureSemiLearner.h.

72{ return unknownLabels_;}

References unknownLabels_.

Member Data Documentation

◆ knownLabels_

std::vector<int> STK::MixtureSemiLearner::knownLabels_
protected

indexes of the known labels

Definition at line 106 of file STK_MixtureSemiLearner.h.

Referenced by createSemiLearner(), eStep(), initializeMixtureParameters(), knownLabels(), and setLabels().

◆ unknownLabels_

std::vector<int> STK::MixtureSemiLearner::unknownLabels_
protected

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