STK++ 0.9.13
STK_IMixtureLearner.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff
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::Clustering
27 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
28 **/
29
36
37namespace STK
38{
39
40/* Constructor.
41 * @param nbCluster,nbSample number of clusters and samples
42 **/
43IMixtureLearner::IMixtureLearner( int nbSample, int nbCluster)
44 : IMixtureStatModel(nbSample, nbCluster)
45 , ziPred_(tik_.rows())
46 , state_(Clust::modelCreated_)
47{}
48/* copy constructor.
49 * @param model the model to clone
50 **/
54/* destructor */
56
57/* Compute ziPred using the Map estimate. */
59{
60 CPointX lnComp_(tik_.cols());
61 int kmax;
62 Real max;
63 for (int i=tik_.beginRows(); i < tik_.endRows(); i++)
64 {
65 // get maximal element of ln(x_i,\theta_k) + ln(p_k)
66 for (int k=tik_.beginCols(); k< tik_.endCols(); k++)
67 { lnComp_[k] = std::log(pk_[k])+lnComponentProbability(i,k);}
68 if (lnComp_.isInfinite().any()) { throw(Clust::mapStepFail_);}
69 max = lnComp_.maxElt(kmax);
70 // set ziPred_
71 ziPred_[i] = kmax;
72 // return max + sum_k p_k exp{lnCom_k - lnComp_kmax}
73 Real sum = (tik_.row(i) = (lnComp_ - max).exp()).sum();
74 tik_.row(i) /= sum;
75 }
76}
77
78} // namespace STK
In this file we define the interface base class for learners.
Type const maxElt(int &row, int &col) const
UnaryOperator< IsInfiniteOp< Type >, Derived > isInfinite() const
hidden::CSlice< Derived, 1, sizeCols_ >::Result row(int i) const
implement the row operator using a reference on the row of the allocator
Base class for Learner of a Mixture mixed model.
CVectorXi ziPred_
The predicted class label.
IMixtureLearner(int nbSample, int nbCluster)
Constructor.
virtual void mapStep()
Compute ziPred using the Map estimate.
virtual ~IMixtureLearner()
destructor
Interface base class for Mixture (composed) model.
CArrayXX tik_
The tik probabilities.
virtual Real lnComponentProbability(int i, int k) const =0
CPointX pk_
The proportions of each mixtures.
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
double Real
STK fundamental type of Real values.
hidden::SliceVisitorSelector< Derived, hidden::MaxVisitor, Arrays::by_col_ >::type_result max(Derived const &A)
If A is a row-vector or a column-vector then the function will return the usual maximal value of the ...
The namespace STK is the main domain space of the Statistical ToolKit project.