STK++ 0.9.13
STK_Kmm_sk.cpp
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, 2014
28 * Author: Serge Iovleff
29 **/
30
40
41#if STK_MIXTURE_DEBUG | STK_MIXTURE_VERBOSE | STK_MIXTURE_VERY_VERBOSE
43#endif
44
45namespace STK
46{
47/* default constructor
48 * @param nbCluster number of cluster in the model
49 **/
50Kmm_sk::Kmm_sk( int nbCluster): Base(nbCluster) {}
51/* copy constructor
52 * @param model The model to copy
53 **/
55/* destructor */
57/* @return the number of free parameters of the model */
59{ return param_.dim_.sum() + this->nbCluster();}
60
61/* @return the value of the probability of the i-th sample in the k-th component.
62 * @param i,k indexes of the sample and of the component
63 **/
65{
66 return(- dik_.elt(i,k)/(2.*param_.sigma2_[k])
67 - (std::log(param_.sigma2_[k]) +2.*Const::_LNSQRT2PI_)*param_.dim_[k]/2.);
68}
69
70/* Initialize randomly the parameters of the Gaussian mixture. */
71void Kmm_sk::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
72{
73#if STK_Kernel_DEBUG | STK_MIXTURE_VERBOSE
74 stk_cout << _T("Entering Kmm_sk::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk)\n");
75#endif
76 compute_dik(p_tik, p_tk);
77 param_.sigma2_ = sum( dik_.prod(*p_tik) )/ (*p_tk * param_.dim_)
78 + CPointX(p_tik->cols()).rand(Law::Normal(0, 0.05)).abs();
79#ifdef STK_MIXTURE_VERY_VERBOSE
80 stk_cout << _T("Kmm_sk::randomInit( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
81 stk_cout << param_.sigma2_ << "\n";
82#endif
83}
84
85/* Compute the weighted means and the weighted standard deviations. */
86bool Kmm_sk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)
87{
88#if STK_Kernel_DEBUG | STK_MIXTURE_VERBOSE
89 stk_cout << _T("Entering Kmm_sk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk)\n");
90#endif
91 compute_dik(p_tik, p_tk);
92#ifdef STK_Kernel_DEBUG
93 stk_cout<< _T("Stat::sumByCol( dik_.prod(*p_tik) ) =\n") << Stat::sumByCol( dik_.prod(*p_tik) ) << "\n";
94#endif
95 param_.sigma2_ = Stat::sumByCol( p_tik->prod(dik_) ) / (p_tk->prod(param_.dim_));
96#ifdef STK_MIXTURE_VERBOSE
97 stk_cout << _T("Kmm_sk::run( CArrayXX const* const& p_tik, CPointX const* const& p_tk) done\n");
98 stk_cout << _T("sigma2 = ") << param_.sigma2_ << "\n";
99#endif
100 return true;
101}
102
103} // namespace STK
In this file we give the main mathematical constants.
This file define methods for displaying Arrays and Expressions.
In this file we define the Kmm_sk class.
In this file we define the Normal probability law class.
This file contain the functors computings statistics.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
hidden::OperatorSelector< Derived, Rhs, Arrays::productOp_ >::Result const prod(ExprBase< Rhs > const &other) const
CArrayXX dik_
Array of the intermediate results dik.
Parameters param_
parameters of the derived mixture model.
void compute_dik(CArrayXX const *p_tik, CPointX const *p_tk)
compute the distance of the ith individual to the kth centroid
Real rand(int i, int j, int k) const
The Gaussian mixture model Kmm_sk is an isotrope Gaussian mixture model on a kernel space.
Definition STK_Kmm_sk.h:76
int computeNbFreeParameters() const
Real lnComponentProbability(int i, int k) const
~Kmm_sk()
destructor
Kmm_sk(int nbCluster)
default constructor
void randomInit(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
Initialize randomly the variances of the Gaussian kernel mixture.
bool run(CArrayXX const *const &p_tik, CPointX const *const &p_tk)
update the variances.
Normal distribution law.
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.
hidden::FunctorTraits< Derived, SumOp >::Row sumByCol(Derived const &A)
The namespace STK is the main domain space of the Statistical ToolKit project.
CArrayPoint< Real, UnknownSize, Arrays::by_col_ > CPointX