STK++ 0.9.13
STK::MultiLaw::Normal< RowVector > Class Template Reference

Class for the multivariate Normal distribution. More...

#include <STK_MultiLaw_Normal.h>

Inheritance diagram for STK::MultiLaw::Normal< RowVector >:
Inheritance graph

Public Types

typedef MultiLaw::IMultiLaw< RowVector > Base
 

Public Member Functions

 Normal (RowVector const &mu, ArraySquareX const &sigma)
 Constructor.
 
virtual ~Normal ()
 destructor.
 
RowVector constmu () const
 @return the location parameter
 
ArraySquareX constsigma () const
 @return the variance-covariance matrix
 
ArraySquareX constsquareroot () const
 @return the square root of the variance-covariance matrix
 
SymEigen< ArraySquareX > constdecomp () const
 @return the eigenvalue decomposition
 
void setParameters (RowVector const &mu, ArraySquareX const &sigma)
 update the parameters specific to the law.
 
virtual Real pdf (RowVector const &x) const
 compute the probability distribution function (density) of the multivariate normal law
 
Real lpdf (RowVector const &x) const
 compute the log probability distribution function.
 
template<class Array >
Real lnLikelihood (Array const &data) const
 compute the log likehood of a data set.
 
virtual void rand (RowVector &x) const
 simulate a realization of the Multivariate Law and store the result in x.
 
template<class Array >
void rand (ArrayBase< Array > &x) const
 simulate a realization of the Multivariate Law and store the result in x (using a reference vector).
 
- Public Member Functions inherited from STK::MultiLaw::IMultiLaw< RowVector >
virtual ~IMultiLaw ()
 destructor.
 
- Public Member Functions inherited from STK::Law::ILawBase
String constname () const
 

Protected Attributes

RowVector mu_
 The position parameter.
 
ArraySquareX sigma_
 The covariance parameter.
 
SymEigen< ArraySquareXdecomp_
 the decomposition in eigenvalues of the covariance matrix
 
ArrayDiagonalX invEigenvalues_
 inverse of the eigenvalues of sigma_
 
ArraySquareX squareroot_
 The square root of the matrix Sigma_.
 
- Protected Attributes inherited from STK::Law::ILawBase
String name_
 Name of the Law.
 

Additional Inherited Members

- Protected Member Functions inherited from STK::MultiLaw::IMultiLaw< RowVector >
 IMultiLaw (String const &name)
 Constructor.
 
- Protected Member Functions inherited from STK::Law::ILawBase
 ILawBase (String const &name)
 Constructor.
 
 ~ILawBase ()
 destructor.
 

Detailed Description

template<class RowVector>
class STK::MultiLaw::Normal< RowVector >

Class for the multivariate Normal distribution.

In probability theory and statistics, the "multivariate normal distribution" or "multivariate Gaussian distribution", is a generalization of the one-dimensional (univariate) normal distribution to higher dimensions. A random vector is said to be multivariate normally distributed if every linear combination of its components has a univariate normal distribution. The multivariate normal distribution is often used to describe, at least approximately, any set of (possibly) correlated real-valued random variables each of which clusters around a mean value.

The multivariate normal distribution of a $ p$-dimensional random vector

\[
\mathbf{X} = \left(
 X_1, X_2, \ldots, X_p
             \right)'
\]

can be written in the following notation

\[
\mathbf{X}\ \sim\ \mathcal{N}(\mu,\ \Sigma).
\]

with $ p $-dimensional mean vector

\[
\mu = \left(
      \mathrm{E}[X_1], \mathrm{E}[X_2], \ldots, \mathrm{E}[X_k]
      \right)'
\]

and $ p \times p $ covariance matrix

\[
\Sigma = [\mathrm{Cov}(X_i, X_j)]_{i=1,2,\ldots,p;\ j=1,2,\ldots,p}
\]

Definition at line 100 of file STK_MultiLaw_Normal.h.

Member Typedef Documentation

◆ Base

template<class RowVector >
typedef MultiLaw::IMultiLaw<RowVector> STK::MultiLaw::Normal< RowVector >::Base

Definition at line 103 of file STK_MultiLaw_Normal.h.

Constructor & Destructor Documentation

◆ Normal()

template<class RowVector >
STK::MultiLaw::Normal< RowVector >::Normal ( RowVector const mu,
ArraySquareX const sigma 
)
inline

Constructor.

Parameters
mumean of the Normal distribution
sigmacovariance matrix of the Normal distribution

Definition at line 108 of file STK_MultiLaw_Normal.h.

109 : Base(_T("MultiLaw::Normal"))
110 , mu_()
111 , sigma_()
112 , decomp_(sigma)
#define _T(x)
Let x unmodified.
MultiLaw::IMultiLaw< RowVector > Base
ArraySquareX const & sigma() const
@return the variance-covariance matrix
SymEigen< ArraySquareX > decomp_
the decomposition in eigenvalues of the covariance matrix
RowVector mu_
The position parameter.
ArraySquareX sigma_
The covariance parameter.
RowVector const & mu() const
@return the location parameter
void setParameters(RowVector const &mu, ArraySquareX const &sigma)
update the parameters specific to the law.

References STK::MultiLaw::Normal< RowVector >::mu(), STK::MultiLaw::Normal< RowVector >::setParameters(), and STK::MultiLaw::Normal< RowVector >::sigma().

◆ ~Normal()

template<class RowVector >
virtual STK::MultiLaw::Normal< RowVector >::~Normal ( )
inlinevirtual

destructor.

Definition at line 116 of file STK_MultiLaw_Normal.h.

116{}

Member Function Documentation

◆ decomp()

template<class RowVector >
SymEigen< ArraySquareX > const & STK::MultiLaw::Normal< RowVector >::decomp ( ) const
inline

@return the eigenvalue decomposition

Definition at line 124 of file STK_MultiLaw_Normal.h.

124{ return decomp_;}

References STK::MultiLaw::Normal< RowVector >::decomp_.

◆ lnLikelihood()

template<class RowVector >
template<class Array >
Real STK::MultiLaw::Normal< RowVector >::lnLikelihood ( Array const data) const
inline

compute the log likehood of a data set.

sum the values of the log-pdf at the points stored in x.

Parameters
datathe multivariate values to compute the lpdf.
Returns
the value of the log-pdf

Definition at line 197 of file STK_MultiLaw_Normal.h.

198 {
199 // check ranges
200 if (data.cols() != mu_.range() )
201 { STKRUNTIME_ERROR_NO_ARG(MultiLaw::Normal::lnLikelihood(x),data.cols() != mu_.range());}
202 // get dimensions of the samples and sum over all ln-likelihood values
203 const int first = data.beginRows(), last = data.lastIdxRows();
204 Real sum = 0.0;
205 for (int i=first; i<= last; i++)
206 {
207 // compute lpdf using \sum_i ||(x_i-mu)'P||_{D^{-1}}^2
208 Real res = 0.5 * ((data.row(i) - mu_) * decomp_.rotation()).wnorm2(invEigenvalues_) ;
209 sum += res;
210 }
211 sum += data.sizeRows()*( invEigenvalues_.size() * Const::_LNSQRT2PI_
212 + 0.5 * log((double)decomp_.det())
213 );
214 return -sum;
215 }
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
ArrayDiagonalX invEigenvalues_
inverse of the eigenvalues of sigma_
Real lnLikelihood(Array const &data) const
compute the log likehood of a data set.
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.

References STK::MultiLaw::Normal< RowVector >::decomp_, STK::MultiLaw::Normal< RowVector >::invEigenvalues_, STK::MultiLaw::Normal< RowVector >::lnLikelihood(), STK::MultiLaw::Normal< RowVector >::mu_, STKRUNTIME_ERROR_NO_ARG, and STK::sum().

Referenced by STK::GaussianAAModel< Array >::computeProjectedLnLikelihood(), and STK::MultiLaw::Normal< RowVector >::lnLikelihood().

◆ lpdf()

template<class RowVector >
Real STK::MultiLaw::Normal< RowVector >::lpdf ( RowVector const x) const
inlinevirtual

compute the log probability distribution function.

Give the value of the log-pdf at the point x.

Parameters
xthe multivariate value to compute the lpdf.
Returns
the value of the log-pdf

Implements STK::MultiLaw::IMultiLaw< RowVector >.

Definition at line 179 of file STK_MultiLaw_Normal.h.

180 {
181 // check ranges
182 if (x.range() != mu_.range() )
183 { STKRUNTIME_ERROR_NO_ARG(MultiLaw::Normal::lpdf(x),x.range() != mu_.range());}
184 // compute pdf using ||(x-mu)'P||_{D^{-1}}^2
185 Real res = 0.5 * ((x - mu_) * decomp_.rotation()).wnorm2(invEigenvalues_)
186 + invEigenvalues_.size() * Const::_LNSQRT2PI_
187 + 0.5 * log((double)decomp_.det());
188 return -res;
189 }
Real lpdf(RowVector const &x) const
compute the log probability distribution function.

References STK::MultiLaw::Normal< RowVector >::decomp_, STK::MultiLaw::Normal< RowVector >::invEigenvalues_, STK::MultiLaw::Normal< RowVector >::lpdf(), STK::MultiLaw::Normal< RowVector >::mu_, and STKRUNTIME_ERROR_NO_ARG.

Referenced by STK::MultiLaw::Normal< RowVector >::lpdf(), and STK::MultiLaw::Normal< RowVector >::pdf().

◆ mu()

template<class RowVector >
RowVector const & STK::MultiLaw::Normal< RowVector >::mu ( ) const
inline

@return the location parameter

Definition at line 118 of file STK_MultiLaw_Normal.h.

118{ return mu_;}

References STK::MultiLaw::Normal< RowVector >::mu_.

Referenced by STK::MultiLaw::Normal< RowVector >::Normal(), and STK::MultiLaw::Normal< RowVector >::setParameters().

◆ pdf()

template<class RowVector >
virtual Real STK::MultiLaw::Normal< RowVector >::pdf ( RowVector const x) const
inlinevirtual

compute the probability distribution function (density) of the multivariate normal law

\[
 f(x) = \frac{1}{ (2\pi)^{k/2}|\Sigma|^{1/2} }
          \exp\!\left( {-\tfrac{1}{2}}(x-\mu)'\Sigma^{-1}(x-\mu) \right),
\]

Give the value of the pdf at the point x.

Parameters
xthe multivariate value to compute the pdf.
Returns
the value of the pdf

Implements STK::MultiLaw::IMultiLaw< RowVector >.

Definition at line 162 of file STK_MultiLaw_Normal.h.

163 {
164 // check determinant is not 0
165 if (decomp_.det() == 0.)
167 // check ranges
168 if (x.range() != mu_.range() )
169 { STKRUNTIME_ERROR_NO_ARG(MultiLaw::Normal::pdf(x),x.range() != mu_.range());}
170 // compute pdf in log-space
171 return std::exp((double)lpdf(x));
172 }
virtual Real pdf(RowVector const &x) const
compute the probability distribution function (density) of the multivariate normal law

References STK::MultiLaw::Normal< RowVector >::decomp_, STK::MultiLaw::Normal< RowVector >::lpdf(), STK::MultiLaw::Normal< RowVector >::mu_, STK::MultiLaw::Normal< RowVector >::pdf(), STK::MultiLaw::Normal< RowVector >::sigma(), and STKRUNTIME_ERROR_NO_ARG.

Referenced by STK::MultiLaw::Normal< RowVector >::pdf().

◆ rand() [1/2]

template<class RowVector >
template<class Array >
void STK::MultiLaw::Normal< RowVector >::rand ( ArrayBase< Array > &  x) const
inline

simulate a realization of the Multivariate Law and store the result in x (using a reference vector).

The class RowVector have to derive from IContainerRef.

Parameters
[out]xthe simulated value.

Definition at line 234 of file STK_MultiLaw_Normal.h.

235 {
236 // fill it with iid N(0,1) variates
237 x.randGauss();
238 // rotate with squareroot_ and translate with mu_
239 x.asDerived() = x.asDerived() * squareroot_ + Const::Vector<Real>(x.rows()) * mu_;
240 }
ArraySquareX squareroot_
The square root of the matrix Sigma_.

References STK::MultiLaw::Normal< RowVector >::mu_, and STK::MultiLaw::Normal< RowVector >::squareroot_.

◆ rand() [2/2]

template<class RowVector >
virtual void STK::MultiLaw::Normal< RowVector >::rand ( RowVector &  x) const
inlinevirtual

simulate a realization of the Multivariate Law and store the result in x.

Parameters
[out]xthe simulated value.

Implements STK::MultiLaw::IMultiLaw< RowVector >.

Definition at line 221 of file STK_MultiLaw_Normal.h.

222 {
223 // fill it with iid N(0,1) variates
224 x.randGauss();
225 // rotate with squareroot_ and translate with mu_
226 x = x * squareroot_ + mu_;
227 }

References STK::MultiLaw::Normal< RowVector >::mu_, and STK::MultiLaw::Normal< RowVector >::squareroot_.

◆ setParameters()

template<class RowVector >
void STK::MultiLaw::Normal< RowVector >::setParameters ( RowVector const mu,
ArraySquareX const sigma 
)
inline

update the parameters specific to the law.

Definition at line 126 of file STK_MultiLaw_Normal.h.

127 {
128 // check dimensions
129 if (mu.range() != sigma.range())
131 mu_ = mu;
132 sigma_ = sigma;
133 // decomposition of the covariance matrix
135 if (!decomp_.run())
136 {
137
138 throw runtime_error(decomp_.error());
139 }
140 // compute the inverse of the eigenvalues of sigma_ and the squareroot_
141 // matrix needed by rand
142 invEigenvalues_.resize(mu_.range());
143 squareroot_.resize(mu_.range());
144 // get dimension
145 int rank = decomp_.rank(), end = mu_.begin() + rank;
146 for (int j=mu_.begin(); j< end; j++)
147 { invEigenvalues_[j] = 1./decomp_.eigenValues()[j];}
148 for (int j=end; j< mu_.end(); j++) { invEigenvalues_[j] = 0.;}
149
150 squareroot_ = decomp_.rotation() * decomp_.eigenValues().sqrt().diagonalize() * decomp_.rotation().transpose();
151 }
Derived & resize(Range const &I, Range const &J)
resize the array.
virtual bool run()
run the computations.
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
virtual void setData(YArray_ const &y, XArray_ const &x)
set the data set.

References STK::MultiLaw::Normal< RowVector >::decomp_, STK::IRunnerBase::error(), STK::MultiLaw::Normal< RowVector >::invEigenvalues_, STK::MultiLaw::Normal< RowVector >::mu(), STK::MultiLaw::Normal< RowVector >::mu_, STK::IArray2D< Derived >::resize(), STK::IRegression< YArray, XArray, Weights >::run(), STK::IRunnerSupervised< YArray_, XArray_, Weights_ >::setData(), STK::MultiLaw::Normal< RowVector >::setParameters(), STK::MultiLaw::Normal< RowVector >::sigma(), STK::MultiLaw::Normal< RowVector >::sigma_, STK::MultiLaw::Normal< RowVector >::squareroot_, and STKRUNTIME_ERROR_NO_ARG.

Referenced by STK::MultiLaw::Normal< RowVector >::Normal(), and STK::MultiLaw::Normal< RowVector >::setParameters().

◆ sigma()

template<class RowVector >
ArraySquareX const & STK::MultiLaw::Normal< RowVector >::sigma ( ) const
inline

◆ squareroot()

template<class RowVector >
ArraySquareX const & STK::MultiLaw::Normal< RowVector >::squareroot ( ) const
inline

@return the square root of the variance-covariance matrix

Definition at line 122 of file STK_MultiLaw_Normal.h.

122{ return squareroot_;}

References STK::MultiLaw::Normal< RowVector >::squareroot_.

Member Data Documentation

◆ decomp_

◆ invEigenvalues_

template<class RowVector >
ArrayDiagonalX STK::MultiLaw::Normal< RowVector >::invEigenvalues_
protected

◆ mu_

◆ sigma_

template<class RowVector >
ArraySquareX STK::MultiLaw::Normal< RowVector >::sigma_
protected

The covariance parameter.

Definition at line 246 of file STK_MultiLaw_Normal.h.

Referenced by STK::MultiLaw::Normal< RowVector >::setParameters(), and STK::MultiLaw::Normal< RowVector >::sigma().

◆ squareroot_


The documentation for this class was generated from the following file: