STK++ 0.9.13
STK_Categorical_pk.h
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.org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Clustering
27 * created on: Oct 24, 2013
28 * Author: Serge Iovleff
29 **/
30
35#ifndef STK_CATEGORICAL_PK_H
36#define STK_CATEGORICAL_PK_H
37
38#include "../CategoricalModels/STK_CategoricalBase.h"
39
40namespace STK
41{
42
43//forward declaration, to allow for recursive template
44template<class Array>class Categorical_pk;
45
46namespace hidden
47{
50template<class Array_>
57
58} // namespace hidden
59
68template<class Array>
69class Categorical_pk: public CategoricalBase<Categorical_pk<Array> >
70{
71 public:
73 using Base::param_;
74 using Base::p_data;
89 void randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
91 bool run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) ;
93 inline int computeNbFreeParameters() const
94 { return this->nbCluster()*(this->modalities_.size()-1);}
95};
96
97/* Initialize randomly the parameters of the Categorical mixture. The centers
98 * will be selected randomly among the data set and the standard-deviation
99 * will be set to 1.
100 */
101template<class Array>
102void Categorical_pk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
103{
104 for (int k = p_tik->beginCols(); k < p_tik->endCols(); ++k)
105 {
106 param_.proba_[k].randUnif();
107 param_.proba_[k] /= param_.proba_[k].sum();
108 }
109}
110
111/* Compute the weighted mean and the common variance. */
112template<class Array>
113bool Categorical_pk<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
114{
115 for (int k = p_tik->beginCols(); k < p_tik->endCols(); ++k)
116 {
117 param_.proba_[k] = 0.;
118 for (int j = p_data()->beginCols(); j < p_data()->endCols(); ++j)
119 {
120 for (int i = p_tik->beginRows(); i < p_tik->endRows(); ++i)
121 { param_.proba_[k][(*p_data())(i, j)] += (*p_tik)(i, k);}
122 }
123 Real sum = param_.proba_[k].sum();
124 if (sum<=0.) return false;
125 param_.proba_[k] /= sum;
126 }
127 return true;
128}
129
130} // namespace STK
131
132#endif /* STK_CATEGORICAL_PK_H */
Base class for the Categorical models.
Parameters param_
parameters of the derived mixture model.
The diagonal Categorical mixture model Categorical_pk is a diagonal Categorical model and has a densi...
int computeNbFreeParameters() const
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Categorical mixture.
Categorical_pk(int nbCluster)
default constructor
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the weighted proportions of each class.
CategoricalBase< Categorical_pk< Array > > Base
Categorical_pk(Categorical_pk const &model)
copy constructor
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
int size() const
get the size of the TRange (the number of elements).
Definition STK_Range.h:303
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.
The namespace STK is the main domain space of the Statistical ToolKit project.
ModelParameters< Clust::Categorical_pk_ > Parameters
Type of the structure storing the parameters of a Categorical_pk model.
Main class for the mixtures traits policy.