STK++ 0.9.13
STK_DiagGaussian_s.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_DIAGGAUSSIAN_S_H
36#define STK_DIAGGAUSSIAN_S_H
37
38#include "../DiagGaussianModels/STK_DiagGaussianBase.h"
39
40namespace STK
41{
42
43// forward declaration
44template<class Array>class DiagGaussian_s;
45
46namespace hidden
47{
50template<class Array_>
57
58} // namespace Clust
59
67template<class Array>
68class DiagGaussian_s: public DiagGaussianBase<DiagGaussian_s<Array> >
69{
70 public:
72 using Base::param_;
73 using Base::p_data;
74
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()*p_data()->sizeCols()+1;}
95};
96
97/* Initialize randomly the parameters of the Gaussian 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 DiagGaussian_s<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
103{
104 this->randomMean(p_tik);
105 // compute the standard deviation
106 Real variance = 0.0;
107 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
108 {
109 variance += ( p_tik->col(k).transpose()
110 * (*p_data() - (Const::Vector<Real>(this->nbSample()) * param_.mean_[k])
111 ).square()
112 ).sum();
113 }
114 param_.sigma_ = std::sqrt(variance/(this->nbSample()*p_data()->sizeCols()));
115#ifdef STK_MIXTURE_VERY_VERBOSE
116 stk_cout << _T("Gaussian_s<Array>::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
117#endif
118}
119
120/* Compute the weighted mean and the common standard deviation. */
121template<class Array>
122bool DiagGaussian_s<Array>::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
123{
124 // compute the means
125 if (!this->updateMean(p_tik)) return false;
126 // compute the standard deviation
127 Real variance = 0.0;
128 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
129 {
130 variance += ( p_tik->col(k).transpose()
131 * (*p_data() - (Const::Vector<Real>(this->nbSample()) * param_.mean_[k])
132 ).square()
133 ).sum();
134 }
135 param_.sigma_ = std::sqrt(variance/(this->nbSample()*p_data()->sizeCols()));
136#ifdef STK_MIXTURE_DEBUG
137 if( param_.sigma_ <= 0 )
138 {
139 stk_cout << _T("DiagGaussian_s::run() failed\n");
140 stk_cout << _T("param_.mean_ =") << param_.mean_;
141 stk_cout << _T("param_.sigma_=") << param_.sigma_ << _T("\n");
142 }
143#endif
144 //if ((variance<=0) || !Arithmetic<Real>::isFinite(variance)) return false;
145 return true;
146}
147
148} // namespace STK
149
150#endif /* STK_DiagGaussian_S_H */
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Define the constant point.
Base class for the diagonal Gaussian models.
Parameters param_
parameters of the derived mixture model.
The diagonal DiagGaussian_s mixture model hse a density function of the form.
DiagGaussianBase< DiagGaussian_s< Array > > Base
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Compute the weighted mean and the common standard deviation.
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the parameters of the Gaussian mixture.
DiagGaussian_s(DiagGaussian_s const &model)
copy constructor
DiagGaussian_s(int nbCluster)
default constructor
int computeNbFreeParameters() const
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::Gaussian_s_ > Parameters
Type of the structure storing the mixture parameters.
Main class for the mixtures traits policy.