Computation of the Multivariate Statistics of a Matrix. More...
#include <STK_Stat_MultivariateMatrix.h>
Inherits STK::Stat::Multivariate.
Public Member Functions | |
| MultivariateMatrix (Matrix const *p_data) | |
| virtual | ~MultivariateMatrix () |
| virtual void | run () |
| virtual void | run (Vector const *p_weights) |
| Vector const & | mean () const |
| Vector const & | variance () const |
| MatrixSquare const & | covariance () const |
| Vector * | p_mean () |
| Vector * | p_variance () |
| MatrixSquare * | p_covariance () |
Protected Attributes | |
| Vector * | mean_ |
| Vector * | var_ |
| MatrixSquare * | cov_ |
Computation of the Multivariate Statistics of a Matrix.
The class MultivariateMatrix is just a factory class for computing the mean, the variance and the covariance Matrix of a data set stored in a Matrix with n rows (the individuals) and p columns (the variables).
The data set can be weighted.
The base class Multivariate store additional information about the number of missing data.
The user can handle directly the statistics using the access method p_mean, p_variance and p_covariance and in this case is responsible of the memory allocated for these containers.
Definition at line 65 of file STK_Stat_MultivariateMatrix.h.
| STK::Stat::MultivariateMatrix::MultivariateMatrix | ( | Matrix const * | p_data | ) |
Constructor. Compute the Multivariate statistics of the Matrix data set.
| data | the data set |
Definition at line 49 of file STK_Stat_MultivariateMatrix.cpp.
: Multivariate(p_data) , mean_(0) , var_(0) , cov_(0) { ;}
| STK::Stat::MultivariateMatrix::~MultivariateMatrix | ( | ) | [virtual] |
| void STK::Stat::MultivariateMatrix::run | ( | ) | [virtual] |
run the estimation of the Multivariate statistics.
Implements STK::Stat::Multivariate.
Definition at line 61 of file STK_Stat_MultivariateMatrix.cpp.
References cov_, STK::IContainer2D::firstCol(), STK::IContainer2D::firstRow(), STK::IContainer2D::lastCol(), STK::IContainer2D::lastRow(), mean_, STK::Stat::Multivariate::nInd_, STK::Stat::Multivariate::nMiss_, STK::Stat::Multivariate::nObs_, STK::Stat::Multivariate::p_data_, STK::IContainer2D::rangeHo(), STK::sum(), and var_.
Referenced by STK::LocalVariance::computeCovarianceMatrices(), and STK::LocalVariance::computeWeightedCovarianceMatrices().
{
// delete memory
if (mean_) delete mean_;
if (var_) delete var_;
if (cov_) delete cov_;
// allocate memory
mean_ = new Vector(p_data_->rangeHo());
var_ = new Vector(p_data_->rangeHo());
cov_ = new MatrixSquare(p_data_->rangeHo());
// get dimensions
const Integer first_ind = p_data_->firstRow();
const Integer last_ind = p_data_->lastRow();
const Integer first_var = p_data_->firstCol();
const Integer last_var = p_data_->lastCol();
// for each variables
for (Integer j= first_var; j<= last_var; j++)
{
// sum of the observations
Real sum = 0.0;
// number of not missing observations
Integer nobs = nInd_;
// compute the mean
for (Integer i= first_ind; i<= last_ind; i++)
sum += (Arithmetic<Real>::isFinite((*p_data_)(i,j))) ? (*p_data_)(i,j) : nobs--;
Real mean_j = (nobs) ? sum/nobs : Arithmetic<Real>::NA();
// save results
(*mean_)[j] = mean_j;
nObs_[j] = nobs;
nMiss_[j] = nInd_ - nobs;
// there is nothing more to compute
if ((nobs<2))
{
(*var_)[j] = Arithmetic<Real>::NA();
(*cov_)[j] = Arithmetic<Real>::NA();
(*cov_)(j) = Arithmetic<Real>::NA();
continue;
}
// compute the variance
sum = 0.0;
Real dev, var = 0.0;
for (Integer i= first_ind; i<= last_ind; i++)
{
if (Arithmetic<Real>::isFinite((*p_data_)(i,j)))
{
sum += (dev = (*p_data_)(i,j) - mean_j); // deviation from the mean
var += (dev*dev); // squared values
}
}
// compute the variance
(*cov_)(j, j) = (*var_)[j] = (var - (sum*sum)/nobs)/(nobs-1);
// compute the covariances
for (Integer i= first_var; i<j; i++)
{
if (nObs_[i] < 2) continue;
Real mean_i = (*mean_)[i];
Real dev_i, dev_j, sum_i = 0.0, sum_j = 0.0;
Real cov = 0.0;
nobs = nInd_;
for (Integer k=first_ind; k<=last_ind; k++)
{
if ( (Arithmetic<Real>::isFinite((*p_data_)(k,i)))
&&(Arithmetic<Real>::isFinite((*p_data_)(k,j)))
)
{
sum_i += (dev_i = (*p_data_)(k, i)-mean_i); // dev from the mean
sum_j += (dev_j = (*p_data_)(k, j)-mean_j); // dev from the mean
cov += dev_i * dev_j; // co product value
}
else nobs--;
}
(*cov_)(j,i) = (*cov_)(i,j) = (cov - (sum_i*sum_j)/nobs)/(nobs-1);
}
}
}
| void STK::Stat::MultivariateMatrix::run | ( | Vector const * | p_weights | ) | [virtual] |
run the estimation of the weighted multivariate statistics.
| p_weights | the weights of the individuals |
Implements STK::Stat::Multivariate.
Definition at line 142 of file STK_Stat_MultivariateMatrix.cpp.
References cov_, STK::IContainer2D::firstCol(), STK::IContainer2D::firstRow(), STK::IContainer2D::lastCol(), STK::IContainer2D::lastRow(), mean_, STK::Stat::Multivariate::nInd_, STK::Stat::Multivariate::nMiss_, STK::Stat::Multivariate::nObs_, STK::Stat::Multivariate::p_data_, STK::Stat::Multivariate::p_weights_, STK::IContainer2D::rangeHo(), and var_.
{
// save pointer on weights
p_weights_ = p_weights;
// delete memory
if (mean_) delete mean_;
if (var_) delete var_;
if (cov_) delete cov_;
// allocate memory
mean_ = new Vector(p_data_->rangeHo());
var_ = new Vector(p_data_->rangeHo());
cov_ = new MatrixSquare(p_data_->rangeHo());
// get dimensions
const Integer first_ind = p_data_->firstRow();
const Integer last_ind = p_data_->lastRow();
const Integer first_var = p_data_->firstCol();
const Integer last_var = p_data_->lastCol();
// for each variables
for (Integer j= first_var; j<= last_var; j++)
{
// number of not missing observations
Integer nobs = nInd_;
// compute the mean
Real wsum = 0.0;
for (Integer i= first_ind; i<= last_ind; i++)
wsum += (Arithmetic<Real>::isFinite((*p_data_)(i,j))) ? (*p_weights_)[i]*(*p_data_)(i,j)
: nobs--;
Real mean_j = (nobs) ? wsum : Arithmetic<Real>::NA();
// save results
nObs_[j] = nobs;
nMiss_[j] = nInd_ - nobs;
(*mean_)[j] = mean_j;
// if there is no observations for variable j
if (!nobs)
{
(*var_)[j] = Arithmetic<Real>::NA();
(*cov_)[j] = Arithmetic<Real>::NA();
(*cov_)(j) = Arithmetic<Real>::NA();
continue;
}
// compute the variance
Real dev, var = 0.0;
for (Integer i= first_ind; i<= last_ind; i++)
{
if (Arithmetic<Real>::isFinite((*p_data_)(i,j)))
{
// deviation from the mean
dev = (*p_data_)(i,j) - mean_j;
// squared values
var += (*p_weights_)[i]*(dev*dev);
}
}
// compute the variance
(*cov_)(j, j) = ((*var_)[j] = var);
// compute the covariances
for (Integer k= first_var; k<j; k++)
{
// if there is no observation for variable k
if (!nObs_[k]) continue;
Real mean_k = (*mean_)[k];
Real cov = 0.0;
for (Integer i=first_ind; i<=last_ind; i++)
{
if ( (Arithmetic<Real>::isFinite((*p_data_)(i,k)))
&&(Arithmetic<Real>::isFinite((*p_data_)(i,j)))
)
{
// co-product value
cov += (*p_weights_)[i] * ((*p_data_)(i, k) - mean_k) * ((*p_data_)(i, j) - mean_j);
}
}
(*cov_)(j,k) = ((*cov_)(k,j) = cov);
}
}
}
| Vector const& STK::Stat::MultivariateMatrix::mean | ( | ) | const [inline] |
get the Vector of the mean
Definition at line 97 of file STK_Stat_MultivariateMatrix.h.
References mean_.
{ return *mean_;}
| Vector const& STK::Stat::MultivariateMatrix::variance | ( | ) | const [inline] |
get the vector of the variance of the Variables
Definition at line 102 of file STK_Stat_MultivariateMatrix.h.
References var_.
{ return *var_;}
| MatrixSquare const& STK::Stat::MultivariateMatrix::covariance | ( | ) | const [inline] |
Matrix of the covariance of the variables
Definition at line 107 of file STK_Stat_MultivariateMatrix.h.
References cov_.
{ return *cov_;}
| Vector* STK::Stat::MultivariateMatrix::p_mean | ( | ) | [inline] |
| Vector* STK::Stat::MultivariateMatrix::p_variance | ( | ) | [inline] |
| MatrixSquare* STK::Stat::MultivariateMatrix::p_covariance | ( | ) | [inline] |
Matrix of the covariance of the variables. The matrix cov_ will not be deleted by this.
Definition at line 127 of file STK_Stat_MultivariateMatrix.h.
Referenced by STK::LocalVariance::computeCovarianceMatrices(), and STK::LocalVariance::computeWeightedCovarianceMatrices().
Vector* STK::Stat::MultivariateMatrix::mean_ [protected] |
Vector of the mean of the Variables
Definition at line 69 of file STK_Stat_MultivariateMatrix.h.
Referenced by mean(), p_mean(), run(), and ~MultivariateMatrix().
Vector* STK::Stat::MultivariateMatrix::var_ [protected] |
Vector of the variance of the variables
Definition at line 71 of file STK_Stat_MultivariateMatrix.h.
Referenced by p_variance(), run(), variance(), and ~MultivariateMatrix().
MatrixSquare* STK::Stat::MultivariateMatrix::cov_ [protected] |
Matrix of the covariance of the variables
Definition at line 73 of file STK_Stat_MultivariateMatrix.h.
Referenced by covariance(), p_covariance(), run(), and ~MultivariateMatrix().
1.7.1