STK++ 0.9.13
STK_JointGaussianModel.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2015 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_JOINTGAUSSIANMODEL_H
38#define STK_JOINTGAUSSIANMODEL_H
39
40#include <cmath>
41
42#include "STK_IMultiStatModel.h"
45
46namespace STK
47{
48
52struct JointGaussianParameters: public IMultiParameters<JointGaussianParameters>
53{
54 public:
61 : mu_(param.mu_)
62 , sigma_(param.sigma_)
63 {}
67 inline Array2DPoint<Real> const& mu() const { return mu_;}
69 inline Array2DPoint<Real> const& sigma() const { return sigma_;}
71 inline Real const mu(int const& j) const { return mu_[j];}
73 inline Real const sigma(int const& j) const { return sigma_[j];}
75 inline void setMu(int const& j, Real const& mu) { mu_[j] = mu;}
77 inline void setSigma(int const& j, Real const& sigma) { sigma_[j] = sigma;}
79 inline void resizeImpl(Range const& range)
80 { mu_.resize(range); mu_ = 0.;
82 }
83
84 protected:
87};
88
101template <class Array, class WColVector = CVectorX>
102class JointGaussianModel: public IMultiStatModel<Array, WColVector, JointGaussianParameters >
103{
104 public:
106 typedef typename Array::Type Type;
113 using Base::p_data;
114 using Base::p_param;
118 JointGaussianModel(Array const& data): Base(data) {}
126 JointGaussianModel* clone() const { return new JointGaussianModel(*this);}
127
129 virtual int computeNbFreeParameters() const
130 { return 2*p_data()->sizeCols();}
133 {
134 Real sum =0.;
135 for (Integer j= rowData.begin(); j < rowData.end(); ++j)
136 { sum += Law::Normal::lpdf(rowData[j], p_param()->mu(j), p_param()->sigma(j));}
137 return sum;
138 }
139 protected:
141 virtual void computeParameters()
142 {
143 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
144 {
145 p_param()->setMu(j, p_data()->col(j).safe().mean());
146 p_param()->setSigma(j,std::sqrt(Stat::varianceWithFixedMean(p_data()->col(j), p_param()->mu(j), true)));
147 }
148 }
151 {
152 for (int j=p_data()->beginCols(); j < p_data()->endCols(); ++j)
153 {
154 p_param()->setMu(j, p_data()->col(j).safe().wmean(weights));
155 p_param()->setSigma(j,std::sqrt(Stat::varianceWithFixedMean(p_data()->col(j), weights, p_param()->mu(j), true)));
156 }
157 }
158};
159
160} // namespace STK
161
162#endif /* STK_JOINTGAUSSIANMODEL_H */
In this file we define the class IMultiStatModel.
In this file we define the Normal probability law class.
This file contain the functors computings statistics.
Derived & resize(Range const &I, Range const &J)
resize the array.
Interface base class for the parameters of a multivariate model.
Interface base class for all Multivariate Statistical Models.
hidden::StatModelTraits< Derived >::WColVector WColVector
Type of the vector with the weights.
Data const *const p_data() const
A joint Gaussian model is a statistical model of the form: following form.
hidden::Traits< Array >::Row RowVector
Type of the row vector of the container.
hidden::Traits< Array >::Col ColVector
Type of the column vector of the container.
virtual void computeParameters(WColVector const &weights)
compute the weighted parameters
JointGaussianModel(Array const &data)
Constructor with data set.
JointGaussianModel(Array const *p_data)
Constructor with a ptr on the data set.
Data const *const p_data() const
virtual int computeNbFreeParameters() const
compute the number of free parameters
JointGaussianModel(JointGaussianModel const &model)
Copy constructor.
JointGaussianModel()
default constructor.
IMultiStatModel< Array, WColVector, JointGaussianParameters > Base
Base class.
Array::Type Type
Type of the data contained in the container.
JointGaussianModel * clone() const
clone pattern.
virtual void computeParameters()
compute the parameters
virtual ~JointGaussianModel()
destructor
virtual Real computeLnLikelihood(RowVector const &rowData) const
compute the log Likelihood of an observation.
virtual Real lpdf(Real const &x) const
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...
hidden::FunctorTraits< Derived, VarianceWithFixedMeanOp >::Row varianceWithFixedMean(Derived const &A, MeanType const &mean, bool unbiased)
Compute the VarianceWithFixedMean(s) value(s) of A.
The namespace STK is the main domain space of the Statistical ToolKit project.
Structure encapsulating the parameters of a Joint Gaussian model.
Real const sigma(int const &j) const
void setMu(int const &j, Real const &mu)
set the mean of the jth law
Array2DPoint< Real > const & sigma() const
void setSigma(int const &j, Real const &sigma)
set the standard deviation of the jth law
JointGaussianParameters(Range const &range)
default constructor
void resizeImpl(Range const &range)
resize the set of parameter
JointGaussianParameters()
default constructor
JointGaussianParameters(JointGaussianParameters const &param)
copy constructor.
Array2DPoint< Real > const & mu() const
Real const mu(int const &j) const