STK++ 0.9.13
STK_Categorical_pjk.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_PJK_H
36#define STK_CATEGORICAL_PJK_H
37
38#include "../CategoricalModels/STK_CategoricalBase.h"
39
40namespace STK
41{
42
43//forward declaration
44template<class Array>class Categorical_pjk;
45
46namespace hidden
47{
50template<class Array_>
57
58} // namespace hidden
59
68template<class Array>
69class Categorical_pjk: public CategoricalBase<Categorical_pjk<Array> >
70{
71 public:
73 using Base::param_;
74 using Base::p_data;
76
87// Real lnComponentProbability(int i, int k) const;
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->nbModalities_-1).sum());}
95};
96
97/* Initialize randomly the parameters of the Categorical mixture. */
98template<class Array>
99void Categorical_pjk<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
100{
101 for (int k = p_tik->beginCols(); k < p_tik->endCols(); ++k)
102 {
103 param_.proba_[k].randUnif();
104 for (int j=param_.proba_[k].beginCols(); j< param_.proba_[k].endCols(); ++j)
105 { param_.proba_[k].col(j) /= param_.proba_[k].col(j).sum();}
106 }
107}
108
109
110/* Compute the modalities probabilities */
111template<class Array>
112bool Categorical_pjk<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
113{
114 for (int k = p_tik->beginCols(); k < p_tik->endCols(); ++k)
115 {
116 param_.proba_[k] = 0.;
117 for (int j = p_data()->beginCols(); j < p_data()->endCols(); ++j)
118 {
119 // count the number of modalities weighted by the tik
120 for (int i = p_data()->beginRows(); i < p_data()->endRows(); ++i)
121 { param_.proba_[k](p_data()->elt(i, j), j) += p_tik->elt(i, k);}
122 // normalize the probabilities
123 Real sum = param_.proba_[k].col(j).sum();
124 if (sum) { param_.proba_[k].col(j) /= sum;}
125 }
126 }
127 return true;
128}
129
130} // namespace STK
131
132#endif /* STK_Categorical_PJK_H */
Base class for the Categorical models.
Parameters param_
parameters of the derived mixture model.
PointXi nbModalities_
Array with the number of modalities of each columns of the data set.
The diagonal Categorical mixture model Categorical_pjk is the most general diagonal Categorical model...
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the weighted probabilities.
CategoricalBase< Categorical_pjk< Array > > Base
Categorical_pjk(Categorical_pjk const &model)
copy constructor
Categorical_pjk(int nbCluster)
default constructor
int computeNbFreeParameters() const
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Categorical mixture.
hidden::CSlice< Derived, sizeRows_, 1 >::Result col(int j) const
implement the col operator using a reference on the column of the allocator
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.
The namespace STK is the main domain space of the Statistical ToolKit project.
ModelParameters< Clust::Categorical_pjk_ > Parameters
Type of the structure storing the parameters of a Categorical_pjk model.
Main class for the mixtures traits policy.