STK++ 0.9.13
STK_HDGaussianBase.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: Dec 4, 2013
28 * Authors: Serge Iovleff
29 **/
30
35#ifndef STK_HDGAUSSIANBASE_H
36#define STK_HDGAUSSIANBASE_H
37
38#include "../STK_IMixtureDensity.h"
47
48namespace STK
49{
50
54template<class Derived>
55class HDGaussianBase: public IMixtureDensity<Derived >
56{
57 public:
59 using Base::nbCluster;
60 using Base::param_;
61 using Base::p_data;
62
63 protected:
73 inline ~HDGaussianBase() {}
74
75 public:
77 inline Real const& mean(int k, int j) const { return param_.mean(k,j);}
79 inline int const& d(int k) const { return param_.d(k);}
81 inline Real const& b(int k) const { return param_.b(k);}
83 inline Real const& q(int k) const { return param_.q(k);}
85 inline Real const& a(int j, int k) const { return param_.a(k, j);}
86
88 inline void initializeModelImpl() { param_.resize(p_data()->cols());}
93 template<class Weights>
94 Real impute(int i, int j, Weights const& pk) const;
98 inline Real rand(int i, int j, int k) const
99 { return Law::Normal::rand(mean(k, j), sigma(k,j));}
104 template<class Array>
110 void writeParameters(CArrayXX const* p_tik, ostream& os) const;
111
112 protected:
116 void randomMean( CArrayXX const* p_tik);
118 bool updateMean( CArrayXX const* p_tik);
119};
120
121template<class Derived>
122template<class Weights>
124{
125 Real sum = 0.;
126 for (int k= pk.begin(); k < pk.end(); ++k)
127 { sum += pk[k] * mean(k,j);}
128 return sum;
129}
130
131template<class Derived>
133{
134 // indexes array
135 VectorXi indexes(this->nbSample());
136 for(int i=p_data()->beginRows(); i< p_data()->endRows(); ++i) { indexes[i] = i;}
137 Range rind = this->nbSample();
138 // sample without repetition
139 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
140 {
141 // random number in [0, end-k[
142 int i = Law::UniformDiscrete::rand(rind.begin(), rind.end()-1);
143 // get ith individuals
144 param_.mean_[k] = p_data()->row(indexes[i]);
145 // exchange it with nth
146 indexes.swap(i, rind.lastIdx());
147 // decrease
148 rind.decLast(1);
149 }
150}
151
152template<class Derived>
154{
155 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
156 {
157 for (int j=p_data()->beginCols(); j< p_data()->endCols(); ++j)
158 { param_.mean_[k][j] = p_data()->col(j).wmean(p_tik->col(k));}
159 }
160 return true;
161}
162
163/* This function is used in order to get the current values of the means
164 * and standard deviations.
165 * @param[out] params the array with the parameters of the mixture.
166 */
167template<class Derived>
168template<class Array>
170{
171 int nbClust = nbCluster();
172 params.resize(2*nbClust, p_data()->cols());
173 for (int k= 0; k < nbClust; ++k)
174 {
175 for (int j= params.beginCols(); j< params.endCols(); ++j)
176 {
177 params(baseIdx+2*k , j) = mean(baseIdx + k, j);
178 params(baseIdx+2*k+1, j) = sigma(baseIdx + k, j);
179 }
180 }
181}
182
183
184/* This function can be used to write summary of parameters to the output stream.
185 * @param p_tik a constant pointer on the posterior probabilities
186 * @param os Stream where you want to write the summary of parameters.
187 */
188template<class Derived>
190{
191 CPointX m(p_data()->cols()), s(p_data()->cols());
192 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
193 {
194 // store sigma values in an array for a nice output
195 for (int j= s.begin(); j < s.end(); ++j)
196 { m[j] = mean(k,j); s[j] = sigma(k,j);}
197 os << _T("---> Component ") << k << _T("\n");
198 os << _T("mean = ") << m;
199 os << _T("sigma = ")<< s;
200 }
201}
202
203} // namespace STK
204
205#endif /* STK_HDGaussianBASE_H */
In this file we implement the final class CArrayPoint.
In this file we implement the final class CArrayVector.
In this file we implement the final class CArray.
In this file we define the constant Arrays.
This file define methods for displaying Arrays and Expressions.
In this file we define the Normal probability law class.
In this file we implement the uniform (discrete) law.
This file contain the functors computings statistics.
#define _T(x)
Let x unmodified.
Base class for the diagonal Gaussian models.
Real const & b(int k) const
Real rand(int i, int j, int k) const
IMixtureDensity< Derived > Base
Array const *const & p_data() const
void writeParameters(CArrayXX const *p_tik, ostream &os) const
This function can be used to write summary of parameters to the output stream.
Parameters param_
parameters of the derived mixture model.
HDGaussianBase(int nbCluster)
default constructor
bool updateMean(CArrayXX const *p_tik)
compute the weighted mean of a Gaussian mixture.
void getParameters(Array &params) const
This function is used in order to get the current values of the means and standard deviations.
Real const & q(int k) const
HDGaussianBase(HDGaussianBase const &model)
copy constructor
Real const & a(int j, int k) const
void randomMean(CArrayXX const *p_tik)
sample randomly the mean of each component by sampling randomly a row of the data set.
Real const & mean(int k, int j) const
void initializeModelImpl()
Initialize the parameters of the model.
int const & d(int k) const
Real impute(int i, int j, Weights const &pk) const
hidden::CSlice< Derived, sizeRows_, 1 >::Result col(int j) const
implement the col operator using a reference on the column of the allocator
Base class for all Mixture densities.
Array const *const & p_data() const
Parameters param_
parameters of the derived mixture model.
hidden::MixtureTraits< Derived >::Array Array
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
const int baseIdx
base index of the containers created in STK++.
double Real
STK fundamental type of Real values.
hidden::SliceVisitorSelector< Derived, hidden::MeanVisitor, Arrays::by_col_ >::type_result mean(Derived const &A)
If A is a row-vector or a column-vector then the function will return the usual mean value of the vec...
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
The namespace STK is the main domain space of the Statistical ToolKit project.