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
33
34
36
37namespace STK
38{
39
40/* Constructor.
41 * @param nbCluster,nbSample number of clusters and samples
42 **/
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
CVectorXi ziPred_
The predicted class label.
IMixtureLearner(int nbSample, int nbCluster)
Constructor.
virtual void mapStep()
Compute ziPred using the Map estimate.
Clust::modelState state_
state of the model
virtual ~IMixtureLearner()
destructor
CArrayXX tik_
The tik probabilities.
virtual Real lnComponentProbability(int i, int k) const =0
IMixtureStatModel(int nbSample, int nbCluster)
Constructor.
CPointX pk_
The proportions of each mixtures.
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 Clust enclose all the enum and utilities functions needed by the Clustering project.
The namespace STK is the main domain space of the Statistical ToolKit project.
CArrayPoint< Real, UnknownSize, Arrays::by_col_ > CPointX