STK++ 0.9.13
STK_PoissonBase.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/* Project: stkpp::Clustering
26 * created on: Dec 9, 2014
27 * Authors: Serge Iovleff
28 **/
29
34#ifndef STK_POISSONBASE_H
35#define STK_POISSONBASE_H
36
37#include "../STK_IMixtureDensity.h"
38
40#include "../PoissonModels/STK_PoissonParameters.h"
41
42namespace STK
43{
47template<class Derived>
48class PoissonBase: public IMixtureDensity<Derived >
49{
50 public:
52 using Base::param_;
53 using Base::p_data;
54 using Base::nbCluster;
55
56 protected:
67
68 public:
70 inline Real lambda(int k, int j) const { return param_.lambda(k,j);}
72 void initializeModelImpl() { param_.resize(p_data()->cols());}
76 Real lnComponentProbability(int i, int k) const;
81 template<class Weights>
82 int impute(int i, int j, Weights const& pk) const;
84 Real impute(int i, int j, CArrayXX const* p_tik) const;
89 inline int rand(int i, int j, int k) const
90 { return Law::Poisson::rand(lambda(k,j));}
94 template<class Array>
100 void writeParameters(CArrayXX const* p_tik, ostream& os) const;
101};
102
103/* @return the value of the probability of the i-th sample in the k-th component.
104 * @param i,k indexes of the sample and of the component
105 **/
106template<class Derived>
108{
109 Real sum =0.;
110 for (int j=p_data()->beginCols(); j<p_data()->endCols(); ++j)
111 {
112 Real value = lambda(k,j);
113 if (value)
114 { sum += Law::Poisson::lpdf(p_data()->elt(i,j), value);}
115 }
116 return sum;
117}
118/* Implementation */
119template<class Derived>
120template<class Weights>
121int PoissonBase<Derived>::impute(int i, int j, Weights const& pk) const
122{
123 Real sum = 0.;
124 for (int k= pk.begin(); k < pk.end(); ++k)
125 { sum += pk[k] * lambda(k,j);}
126 return std::floor(sum+0.5);
127}
128
129/* @return a value to impute for the jth variable of the ith sample*/
130template<class Derived>
131Real PoissonBase<Derived>::impute(int i, int j, CArrayXX const* p_tik) const
132{
133 Real sum = 0.;
134 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
135 { sum += p_tik->elt(i,k) * lambda(k,j);}
136 return sum;
137}
138
139/* This function is used in order to get the current values of the lambdas.
140 * @param[out] params the array with the parameters of the mixture.
141 */
142template<class Derived>
143template<class Array>
145{
146 params.resize(nbCluster(), p_data()->cols());
147 for (int k= params.beginRows(); k < params.endRows(); ++k)
148 {
149 for (int j= p_data()->beginCols(); j < p_data()->endCols(); ++j)
150 { params(k, j) = lambda(k,j);}
151 }
152}
153
154
155/* This function can be used to write summary of parameters to the output stream.
156 * @param p_tik a constant pointer on the posterior probabilities
157 * @param os Stream where you want to write the summary of parameters.
158 */
159template<class Derived>
161{
162 CPointX params(p_data()->cols());
163 for (int k= p_tik->beginCols(); k < p_tik->endCols(); ++k)
164 {
165 for (int j= p_data()->beginCols(); j < p_data()->endCols(); ++j)
166 { params[j] = lambda(k,j);}
167 os << _T("---> Component ") << k << _T("\n");
168 os << _T("lambda = ") << params;
169 }
170}
171
172
173} // namespace STK
174
175#endif /* STK_PoissonBASE_H */
In this file we define the Poisson distribution.
#define _T(x)
Let x unmodified.
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
virtual int rand() const
virtual Real lpdf(int const &x) const
compute the log probability distribution function.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Base class for the Poisson models.
Array const *const & p_data() const
Real impute(int i, int j, CArrayXX const *p_tik) const
void writeParameters(CArrayXX const *p_tik, ostream &os) const
This function can be used to write summary of parameters to the output stream.
void initializeModelImpl()
Initialize the parameters of the model.
Parameters param_
parameters of the derived mixture model.
~PoissonBase()
destructor
void getParameters(Array &params) const
This function is used in order to get the current values of the lambdas.
Real lambda(int k, int j) const
Real lnComponentProbability(int i, int k) const
IMixtureDensity< Derived > Base
int impute(int i, int j, Weights const &pk) const
PoissonBase(PoissonBase const &model)
copy constructor
PoissonBase(int nbCluster)
default constructor
int rand(int i, int j, int k) const
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.
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.