STK++ 0.9.13
STK_Stat_MultivariateReal.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::Stat
27 * Purpose: Compute multivariate elementary statistics for a Real 2D container.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_STAT_MULTIVARIATEREAL_H
36#define STK_STAT_MULTIVARIATEREAL_H
37
38//#include <STKernel.h>
39
42
43#include "STK_Stat_Covariance.h"
45
46namespace STK
47{
48namespace Stat
49{
58template <class Array>
59class Multivariate<Array, Real>: public IRunnerUnsupervised< Array, typename hidden::Traits<Array>::Col>
60{
65 typedef typename Array::Type Type_;
66 enum
67 {
69 };
70
71 public:
74 , nbSamples_(0), nbVar_(0)
75 , min_(), max_(), mean_(), var_(), cov_()
76 { STK_STATIC_ASSERT(value_, YOU_CANNOT_USED_THIS_TYPE_OF_DATA_WITH_THIS_OBJECT);}
80 Multivariate( Array const& data)
81 : Runner(&data)
82 , nbSamples_(data.sizeRows()), nbVar_(data.sizeCols())
83 , min_(), max_(), mean_(), var_(), cov_()
84 { STK_STATIC_ASSERT(value_, YOU_CANNOT_USED_THIS_TYPE_OF_DATA_WITH_THIS_OBJECT);}
88 Multivariate( Array const* p_data)
89 : Runner(p_data)
90 , nbSamples_((p_data) ? p_data->sizeRows() : 0)
91 , nbVar_((p_data) ? p_data->sizeCols() : 0)
92 , min_(), max_(), mean_(), var_(), cov_()
93 { STK_STATIC_ASSERT(value_, YOU_CANNOT_USED_THIS_TYPE_OF_DATA_WITH_THIS_OBJECT);}
98 : Runner(stat)
100 , min_(stat.min_), max_(stat.max_), mean_(stat.mean_)
101 , var_(stat.var_), cov_(stat.cov_)
102 {}
104 virtual ~Multivariate() { }
106 inline virtual Multivariate* clone() const { return new Multivariate(*this);}
108 inline int nbVariable() const {return nbVar_;}
110 inline int nbSamples() const {return nbSamples_;}
112 inline PointX const& min() const { return min_;}
114 inline PointX const& max() const { return max_;}
116 inline PointX const& mean() const { return mean_;}
118 inline PointX const& variance() const { return var_;}
120 inline ArraySquareX const& covariance() const { return cov_;}
121
123 virtual bool run()
124 {
125 if (!this->p_data_)
126 { this->msg_error_ = STKERROR_NO_ARG(MultivariateArray::run(),data is not set);
127 return false;
128 }
129 try
130 {
131 mean_.move(Stat::mean(*this->p_data_));
132 min_.move(Stat::min(*this->p_data_));
133 max_.move(Stat::max(*this->p_data_));
134 var_.move(varianceWithFixedMean(*this->p_data_, mean_, false));
135
136 cov_.resize(this->p_data_->cols());
137 for (int j=this->p_data_->beginCols(); j<this->p_data_->endCols(); j++)
138 {
139 cov_(j, j) = var_[j];
140 for (int i=this->p_data_->beginCols(); i<j; i++)
141 {
142 cov_(i, j) = covarianceWithFixedMean(this->p_data_->col(i), this->p_data_->col(j), mean_[i], mean_[j], false);
143 cov_(j, i) = cov_(i, j);
144 }
145 }
146 }
147 catch (Exception const& error)
148 {
149 this->msg_error_ += _T("Error in Multivariate::run():\nWhat: ");
150 this->msg_error_ += error.error();
151 return false;
152 }
153 // no error
154 return true;
155 }
159 virtual bool run( ColVector const& weights)
160 {
161 if (!this->p_data_)
162 { this->msg_error_ = STKERROR_NO_ARG(MultivariateArray::run(weights),data is not set);
163 return false;
164 }
165 if (this->p_data_->rows() != weights.range())
166 { this->msg_error_ = STKERROR_NO_ARG(MultivariateArray::run(weights),p_data_->rows() != weights.range());
167 return false;
168 }
169 try
170 {
171 mean_.move(Stat::mean(*this->p_data_, weights));
172 min_.move(Stat::min(*this->p_data_,weights));
173 max_.move(Stat::max(*this->p_data_, weights));
174 var_.move(varianceWithFixedMean(*this->p_data_, weights, mean_, false));
175
176 cov_.resize(this->p_data_->cols());
177 // for each variables
178 for (int j= this->p_data_->beginCols(); j< this->p_data_->endCols(); j++)
179 {
180 cov_(j, j) = var_[j];
181 // compute the covariances
182 for (int i= this->p_data_->beginCols(); i<j; i++)
183 {
184 cov_(i, j) = covarianceWithFixedMean(this->p_data_->col(i), this->p_data_->col(j), weights, mean_[i], mean_[j]);
185 cov_(j, i) = cov_(i, j);
186 }
187 }
188 }
189 catch (Exception const& error)
190 {
191 this->msg_error_ = _T("Error in Multivariate::run(weights): ");
192 this->msg_error_ += error.error();
193 return false;
194 }
195 // no error
196 return true;
197 }
198
199 protected:
204
215
217 virtual void update()
218 {
219 // if there is no data there is nothing to update
220 if (this->p_data_)
221 {
222 nbSamples_ = this->p_data_->sizeRows();
223 nbVar_ = this->p_data_->sizeCols();
224 }
225 }
226};
227
228
229} // namespace Stat
230
231} // namespace STK
232
233#endif /*STK_STAT_MULTIVARIATEREAL_H*/
A Array2DPoint is a one dimensional horizontal container.
In this file, we define Array2DSquare class.
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
This file contains the methods computing the covariance of an array.
This file contain the declaration of the base class Multivariate.
#define STK_STATIC_ASSERT(COND, MSG)
#define _T(x)
Let x unmodified.
Sdk class for all library Exceptions.
virtual const String error() const
Returns a C-style character string describing the general cause of the current error.
String msg_error_
String with the last error message.
Definition STK_IRunner.h:96
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
Abstract class for all classes making unsupervised learning.
Array const * p_data() const
get the data set
Array const * p_data_
A pointer on the original data set.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
virtual void update()
udpating method in case we set a new data set
Multivariate(Array const *p_data)
Constructor.
virtual Multivariate * clone() const
clone pattern
IRunnerUnsupervised< Array, ColVector > Runner
type of runner
virtual bool run(ColVector const &weights)
run the estimation of the weighted multivariate statistics.
PointX mean_
Vector of the mean of the Variables.
Multivariate(Multivariate const &stat)
copy constructor.
ArraySquareX cov_
Array of the covariance of the variables.
PointX min_
Vector of the mean of the Variables.
PointX var_
Vector of the variance of the variables.
virtual bool run()
run the estimation of the Multivariate statistics.
PointX max_
Vector of the mean of the Variables.
Computation of the multivariate statistics of a Variable.
int nbSamples_
number of samples
int nbVar_
Number of variables.
double Real
STK fundamental type of Real values.
hidden::FunctorTraits< Derived, VarianceWithFixedMeanOp >::Row varianceWithFixedMean(Derived const &A, MeanType const &mean, bool unbiased)
Compute the VarianceWithFixedMean(s) value(s) of A.
hidden::FunctorTraits< Derived, MaxOp >::Row max(Derived const &A)
Compute the maximal(s) value(s) of A.
hidden::FunctorTraits< Derived, MeanOp >::Row mean(Derived const &A)
Compute the mean(s) value(s) of A.
Real covarianceWithFixedMean(ExprBase< XArray > const &X, ExprBase< YArray > const &Y, typename hidden::Traits< XArray >::Type const &xMu, typename hidden::Traits< YArray >::Type const &yMu, bool unbiased=false)
Compute the covariance of the variables X and Y with fixed means.
hidden::FunctorTraits< Derived, MinOp >::Row min(Derived const &A)
Compute the minimal(s) value(s) of A.
The namespace STK is the main domain space of the Statistical ToolKit project.
check if T and U are of the same type.