STK++ 0.9.13
STK_GaussianModel.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::StatModel
27 * created on: 13 août 2011
28 * Purpose: Create a gaussian statistical model.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 *
31 **/
32
37#ifndef STK_GAUSSIANMODEL_H
38#define STK_GAUSSIANMODEL_H
39
40#include "STK_IGaussianModel.h"
43
44namespace STK
45{
46
51template <class Array>
52class GaussianModel: public IGaussianModel<Array>
53{
54 public:
56 using Base::p_dataij_;
57
58 using Base::mean_;
59
65 GaussianModel( Array const* p_data);
69 GaussianModel( Array const& data);
73 MultiLaw::Normal<RowVector>* const& p_law() const { return p_law_;}
77 bool run();
82 bool run(ColVector const& weights);
86 inline ArraySquareX const& covariance() const { return cov_;}
87
88 protected:
92 void compCovariance();
99};
100
101/* constructor */
102template <class Array>
104 : Base(p_dataij)
105 , cov_(p_dataij_->cols())
106 , p_law_(0)
107{
108 this->setNbFreeParameter(p_dataij_->sizeCols() + (p_dataij_->sizeCols()* (p_dataij_->sizeCols()-1))/2);
109}
110
111/* constructor */
112template <class Array>
114 : Base(data)
115 , cov_(data.cols())
116 , p_law_(0)
117{ setNbFreeParameter(p_dataij_->sizeCols() + (p_dataij_->sizeCols()* (p_dataij_->sizeCols()-1))/2);}
118
119/* destructor */
120template <class Array>
122{ if (p_law_) delete p_law_;}
123
124/* implementation of the Gaussian statistical model
125 * @return @c true if no error occur and @c false otherwise.
126 */
127template <class Array>
129{
130 // compute the mean
131 this->compMean();
132 // compute the covariance matrix
133 compCovariance();
134 // create p_law_ (will be deleted in base class)
135 // update gaussian law (will be deleted in base class)
136 if (!p_law_) p_law_ = new MultiLaw::Normal<RowVector>(mean_, cov_);
137 else p_law_->setParameters(mean_, cov_);
138 // compute log likelihood of the gaussian law
139 this->setLnLikelihood(p_law_->lnLikelihood(*p_dataij_ ));
140 // everything ok
141 return true;
142}
143
144/* implementation of the weighted Gaussian statistical model
145 * @param weights the weights of the samples
146 * @return @c true if no error occur and @c false otherwise.
147 */
148template <class Array>
150{
151 // compute the mean
152 this->compWeightedMean(weights);
153 // compute the covariance matrix
154 compWeightedCovariance(weights);
155 // create p_law_ (will be deleted in base class)
156 // update gaussian law (will be deleted in base class)
157 if (!p_law_) p_law_ = new MultiLaw::Normal<RowVector>(mean_, cov_);
158 else p_law_->setParameters(mean_, cov_);
159 // compute log likelihood of the gaussian law
160 this->setLnLikelihood(p_law_->lnLikelihood(*p_dataij_ ));
161 // everything ok
162 return true;
163}
164
166template <class Array>
172template <class Array>
175
176} // namespace STK
177
178#endif /* STK_GAUSSIANMODEL_H */
In this file we define the Interface class IGaussianModel.
In this file we define the multivariate Normal law.
In this file we specialize the class Multivariate to Real type.
Compute the maximum likelihood estimates of a complete Gaussian statistical model.
ArraySquareX const & covariance() const
get the empirical covariance
hidden::Traits< Array >::Col ColVector
void compCovariance()
compute the empirical covariance matrix.
GaussianModel(Array const *p_data)
constructor.
hidden::Traits< Array >::Row RowVector
MultiLaw::Normal< RowVector > *const & p_law() const
Accessor to the normal law.
void compWeightedCovariance(ColVector const &weights)
compute the empirical weighted covariance matrix.
IGaussianModel< Array > Base
ArraySquareX cov_
ArrayXX of the empirical covaiance.
MultiLaw::Normal< RowVector > * p_law_
pointer on the normal law
bool run()
implementation of the Gaussian statistical model
~GaussianModel()
destructor.
Compute the the maximum likelihood estimates of a complete Gaussian statistical model.
RowVector mean_
Vector of the empirical means.
void setNbFreeParameter(int const &nbFreeParameter)
set the number of free parameters of the model
Data const * p_dataij_
A pointer on the original data set.
Class for the multivariate Normal distribution.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Real covariance(ExprBase< XArray > const &X, ExprBase< YArray > const &Y, bool unbiased=false)
Compute the covariance of the variables X and Y.
The namespace STK is the main domain space of the Statistical ToolKit project.