STK++ 0.9.13
STK_HDMatrixGaussianModel.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_HDMATRIXGAUSSIANMODEL_H
36#define STK_HDMATRIXGAUSSIANMODEL_H
37
38#include "../STK_IMixtureDensity.h"
40
41namespace STK
42{
43
44//forward declaration, to allow for recursive template
45template<int IdRow_, int IdCol_, class Array>
46class HDMatrixGaussianModel;
47
48namespace hidden
49{
52template<int IdRow_, int IdCol_, class Array_>
59
60} // namespace hidden
61
62
66template<int IdRow_, int IdCol_, class Array_>
67class HDMatrixGaussianModel: public IMixtureDensity<HDMatrixGaussianModel<IdRow_, IdCol_, Array_> >
68{
69 public:
70
75
76 using Base::nbCluster;
77 using Base::nbSample;
78 using Base::param_;
79 using Base::p_data;
80
86 , isRowAj_(hidden::HDCovarianceChooser<IdRow_>::isAj_)
87 , isRowAk_(hidden::HDCovarianceChooser<IdRow_>::isAk_)
88 , isRowBk_(hidden::HDCovarianceChooser<IdRow_>::isBk_)
89 , isRowQk_(hidden::HDCovarianceChooser<IdRow_>::isQk_)
90 , isRowDk_(hidden::HDCovarianceChooser<IdRow_>::isDk_)
91 , isColAj_(hidden::HDCovarianceChooser<IdCol_>::isAj_)
92 , isColAk_(hidden::HDCovarianceChooser<IdCol_>::isAk_)
93 , isColBk_(hidden::HDCovarianceChooser<IdCol_>::isBk_)
94 , isColQk_(hidden::HDCovarianceChooser<IdCol_>::isQk_)
95 , isColDk_(hidden::HDCovarianceChooser<IdCol_>::isDk_)
96 {}
115
120 void randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk);
122 bool run( CArrayXX const* const& p_tik, CPointX const* const& p_tk);
124 int computeNbFreeParameters() const;
125
126 protected:
133
134 private:
136 bool runFreeOrientationByRow( CArrayXX const* const& p_tik, CPointX const* const& p_tk);
138 bool runCommonOrientationByRow( CArrayXX const* const& p_tik, CPointX const* const& p_tk);
140 bool runFreeOrientationByCol( CArrayXX const* const& p_tik, CPointX const* const& p_tk);
142 bool runCommonOrientationByCol( CArrayXX const* const& p_tik, CPointX const* const& p_tk);
143};
144
145template<int IdRow_, int IdCol_, class Array_>
147{
148 int sum = nbRow_*nbCol_;
149 int maxRowDk = param_.rowDk_.maxElt();
150 int maxColDk = param_.colDk_.maxElt();
151 // for Bk
152 sum += (isRowBk_)? nbCluster :1;
153 sum += (isColBk_)? nbCluster :1;
154 // for Ajk
155 sum += (isRowAk_) ? ((isRowAj_) ? param_.rowDk_.sum() : nbCluster_) : maxRowDk;
156 sum += (isColAk_) ? ((isColAj_) ? param_.colDk_.sum() : nbCluster_) : maxColDk;
157 // for Qk
158 if (isRowQk_)
159 {
160 int dk = param_.rowDk_[k];
161 sum += dk*nbRow_ - (dk*(dk+1))/2;
162 }
163 else
164 { sum += maxRowDk*nbRow - (maxRowDk*(maxRowDk+1))/2;}
165 if (isColQk_)
166 {
167 int dk = param_.colDk_[k];
168 sum += dk*nbCol_ - (dk*(dk+1))/2;
169 }
170 else
171 { sum += maxColDk*nbRow - (maxColDk*(maxColDk+1))/2;}
172 //
173 return sum;
174}
175/* Compute the weighted mean and the common standard deviation. */
176template<int IdRow_, int IdCol_, class Array_>
177bool HDMatrixGaussianModel<IdRow_, IdCol_, Array_>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
178{
179
180 return true;
181}
182
183template<int IdRow_, int IdCol_, class Array_>
185{
186 VectorXi indexes(p_data()->rows());
187 for(int i=indexes.begin(); i< indexes.end(); ++i) { indexes[i] = i;}
188 Range rind(p_data()->rows());
189 // sample mean between individuals without repetition
190 for (int k= p_tk->begin(); k < p_tk->end(); ++k)
191 {
192 // random number in [0, end-k[
193 int i = Law::UniformDiscrete::rand(rind.begin(), rind.end()-1);
194 // get ith individuals
195 param_.meank_[k] = p_data()->row(indexes[i]);
196 // exchange it with nth
197 indexes.swap(i, rind.lastIdx());
198 // decrease
199 rind.decLast(1);
200 }
201 //
206 //
207 for (int k= p_tk->begin(); k < p_tk->end(); ++k)
208 {
210 param_.rowQk_[k] = v * Const::Identity(nbRow_);
211 param_.colQk_[k] = w * Const::Identity(nbCol_);
212 param_.rowAjk_[k] = rowA;
213 param_.rowBk_[k] = rowB;
214 param_.colAjk_[k] = colA;
215 param_.colBk_[k] = colB;
216 }
217}
218
219/* Update parameters for models with free orientation */
220template<int IdRow_, int IdCol_, class Array_>
222{
223 CSquareX w;
225 for (int k=p_tk->begin(); k< p_tk->end(); ++k)
226 {
227
228 // compute the unbiased covariance function
229 Stat::Covariance(*p_data(), p_tik()->col(k), false);
230 }
231}
232
233} // namespace STK
234
235#endif /* STK_HDMATRIXGAUSSIANMODEL_H */
In this file we define the Parameters classes for High Dimensional Matrix Gaussian mixture models.
Define the constant identity matrix.
Main class for HD matrix valued matrix models.
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gaussian mixture.
IMixtureDensity< HDMatrixGaussianModel< IdRow_, IdCol_, Array_ > > Base
Base class.
bool runCommonOrientationByCol(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Update parameters for models with common orientation by columns.
bool runFreeOrientationByCol(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Update parameters for models with free orientation by columns.
HDMatrixGaussianModel(int nbCluster)
constructor
bool runCommonOrientationByRow(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Update parameters for models with common orientation by rows.
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the weighted mean and the common standard deviation.
bool isColAj_
Structure of the covariance matrices between clusters for the columns.
int nbRow_
number of rows and columns of the data
HDMatrixModelParameters< Array_ > Parameters
Type of the structure storing the parameters of a MixturGaussian_sjk model.
bool isRowAj_
Structure of the covariance matrices between clusters for the rows.
HDMatrixGaussianModel(HDMatrixGaussianModel const &model)
constructor
bool runFreeOrientationByRow(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Update parameters for models with free orientation by rows.
Base class for all Mixture densities.
virtual Real rand() const
Generate a pseudo Exponential random variate.
Real rand() const
Generate a pseudo Normal random variate.
virtual int rand() const
Generate a pseudo Uniform random variate.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
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.
HDMatrixModelParameters< Array_ > Parameters
Type of the structure storing the parameters of a MixturGaussian_sjk model.
Main class for the mixtures traits policy.