STK++ 0.9.13
STK_ModelDiagGaussian_muj_sj.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
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_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Model
27 * created on: 22 juil. 2011
28 * Purpose: define the class IUnivStatModel.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 *
31 **/
32
37#ifndef STK_GAUSSIAN_MUJ_SJMODEL_H
38#define STK_GAUSSIAN_MUJ_SJMODEL_H
39
40#include <cmath>
41
42#include "STK_Model_Util.h"
43#include "STK_IMultiStatModel.h"
46
47namespace STK
48{
49
50// forward declaration
51template <class Data_, class WColVector = CVectorX> class ModelDiagGaussian_muj_sj;
52struct DiagGaussian_muj_sjParameters;
53
54namespace hidden
55{
60template<class Data_, class WColVector_>
72
73} // hidden
74
79{
83 DiagGaussian_muj_sjParameters(Range const& range): mu_(range, 0.), sigma_(range, 1.) {}
91
93 inline CPointX const& mu() const { return mu_;}
95 inline CPointX const& sigma() const { return sigma_;}
96
98 inline Real const mu(int j) const { return mu_[j];}
100 inline Real const sigma(int j) const { return sigma_[j];}
101
106 inline void resize(Range const& range)
107 {
108 if (range != range_)
109 {
110 mu_.resize(range); sigma_.resize(range);
111 range_ = range;
112 }
113 }
117};
118
131template <class Data_, class WColVector_>
132class ModelDiagGaussian_muj_sj: public IMultiStatModel< ModelDiagGaussian_muj_sj<Data_, WColVector_> >
133{
134 public:
147 using Base::p_data;
148 using Base::param;
149
153 ModelDiagGaussian_muj_sj(Data const& data): Base(data) {}
160
162 inline CPointX const& mean() const {return param().mu();}
164 inline CPointX const& sigma() const {return param().sigma();}
165
167 inline int computeNbFreeParameters() const { return 2*p_data()->dataij().sizeCols();}
171 void computeParameters();
175 void writeParametersImpl(ostream& os) const;
176};
177
178/* compute the log Likelihood of an observation. */
179template<class Data_, class WColVector_>
181{
182 Real sum =0.;
183 for (Integer j= rowData.begin(); j < rowData.end(); ++j)
184 { sum += Law::Normal::lpdf(rowData[j], param().mu(j), param().sigma(j));}
185 return sum;
186}
187
188/* compute the parameters */
189template<class Data_, class WColVector_>
191{
192 for (int j=p_data()->dataij().beginCols(); j < p_data()->dataij().endCols(); ++j)
193 {
194 param().mu_[j] = p_data()->dataij().col(j).meanSafe();
195 param().sigma_[j] = std::sqrt(p_data()->dataij().col(j).varianceSafe(param().mu(j)));
196 }
197}
198/* compute the weighted parameters */
199template<class Data_, class WColVector_>
201{
202 for (int j=p_data()->dataij().beginCols(); j < p_data()->dataij().endCols(); ++j)
203 {
204 param().mu_[j] = p_data()->dataij().col(j).wmeanSafe(weights);
205 param().sigma_[j] = std::sqrt(p_data()->dataij().col(j).wvarianceSafe(param().mu(j), weights));
206 }
207}
208
209/* Write the parameters on the output stream os */
210template<class Data_, class WColVector_>
212{
213 os << _T("mean = ") << mean();
214 os << _T("sigma = ")<< sigma();
215}
216
217} // namespace STK
218
219#endif /* STK_GAUSSIAN_MUJ_SJMODEL_H */
In this file we define the class IMultiStatModel.
In this file we define the Normal probability law class.
In this file we define the constant and utilities methods used in the project Model.
This file contain the functors computings statistics.
#define _T(x)
Let x unmodified.
Derived & resize(Range const &I, Range const &J)
resize the Array.
Interface base class for all Multivariate Statistical Models.
Real computeLnLikelihood() const
compute the log Likelihood of the statistical model.
virtual Real lpdf(Real const &x) const
A DiagGaussian_muj_sj model is a statistical model of the form: following form.
ModelDiagGaussian_muj_sj(Data const &data)
Constructor with data set.
ModelDiagGaussian_muj_sj(Data const *p_data)
Constructor with a ptr on the data set.
ModelDiagGaussian_muj_sj(ModelDiagGaussian_muj_sj const &model)
Copy constructor.
int computeNbFreeParameters() const
compute the number of free parameters
CPointX const & sigma() const
vector of the mean log of the observations
DiagGaussian_muj_sjParameters Parameters
Type of the row vector of the container.
void computeParameters()
compute the parameters
DataBridge< Data_ > Data
Type of the container storing the data.
hidden::Traits< Data_ >::Type Type
Type of the data in the container.
void writeParametersImpl(ostream &os) const
Write the parameters on the output stream os.
WColVector_ WColVector
Type of the array storing the weights of the data.
IMultiStatModel< ModelDiagGaussian_muj_sj< Data_, WColVector_ > > Base
Base class.
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.
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.
Structure encapsulating the parameters of a DiagGaussian_muj_sj model.
void resize(Range const &range)
resize the parameters only if the range is modified, otherwise, stay with the current values.
DiagGaussian_muj_sjParameters(DiagGaussian_muj_sjParameters const &param)
copy constructor.
DiagGaussian_muj_sjParameters(Range const &range)
default constructor
DiagGaussian_muj_sjParameters Parameters
Type of the parameters of the ModelDiagGaussian_muj_sj.
WColVector_ WColVector
Type of the array storing the weights of the data.
Policy trait class for (Stat) Model classes.