CVR-Lib last update 20 Sep 2009

cvr::secondOrderStatistics< T, U > Class Template Reference
[Statistics]

Computes mean, variance, and/or covariance matrix of data matrices and vectors. More...

#include <cvrSecondOrderStatistics.h>

Inheritance diagram for cvr::secondOrderStatistics< T, U >:

Inheritance graph
[legend]
Collaboration diagram for cvr::secondOrderStatistics< T, U >:

Collaboration graph
[legend]

List of all members.

Classes

class  parameters
 the parameters for the class secondOrderStatistics More...

Public Types

enum  eVarianceType { Empirical = 0, Maxlikely = 1 }

Public Member Functions

 secondOrderStatistics (eDataRepresentationType dataRep=Rows)
 secondOrderStatistics (const secondOrderStatistics &other)
 secondOrderStatistics (const parameters &param)
virtual ~secondOrderStatistics ()
secondOrderStatisticscopy (const secondOrderStatistics &other)
secondOrderStatisticsoperator= (const secondOrderStatistics &other)
const std::string & name () const
virtual secondOrderStatisticsclone () const
virtual secondOrderStatisticsnewInstance () const
const parametersgetParameters () const
bool apply (const vector< T > &src, U &mean, U &variance) const
bool apply (const matrix< T > &src, U &mean, U &variance) const
bool apply (const matrix< T > &src, vector< U > &mean, vector< U > &variance) const
bool apply (const matrix< T > &src, vector< U > &mean, matrix< U > &cov) const
bool mean (const vector< T > &src, U &mean) const
bool mean (const matrix< T > &src, vector< U > &mean, eDataRepresentationType dataRep=Rows) const
bool variance (const vector< T > &src, U &variance) const
bool variance (const matrix< T > &src, vector< U > &variance, eDataRepresentationType dataRep=Rows) const
bool covarianceMatrix (const matrix< T > &src, matrix< U > &covMat, eDataRepresentationType dataRep=Rows) const

Protected Member Functions

bool applyRows (const matrix< T > &src, vector< U > &mu, vector< U > &var) const
bool applyColumns (const matrix< T > &src, vector< U > &mu, vector< U > &var) const
bool applyRows (const matrix< T > &src, vector< U > &mu, matrix< U > &cov) const
bool applyColumns (const matrix< T > &src, vector< U > &mu, matrix< U > &cov) const
void covar2corrcoef (matrix< U > &cv) const


Detailed Description

template<typename T, typename U = T>
class cvr::secondOrderStatistics< T, U >

Computes mean, variance, and/or covariance matrix of data matrices and vectors.

Correlations can be obtained as well.

The first template parameter (T) represents the type of the input data, and the second (U) the type of the output data, which defaults to T if not specified. The user should ensure that the result values fit into U, that is, T*T should be assignable to a U without loss of precision.

Row or column representations

If the data samples are given in form of a matrix data samples (points) can be in the rows or columns of the matrix. The CVR-Lib usually assumes rows, but here you can choose via parameters::dataRepresentationType.

The following example clarifies the usage of eDataRepresentationType. Given the data matrix

 |  5  6  10  2 |
 |  1 -2   4  4 |
 |  3  2   1 -3 |

With the default parameters (Rows, Empirical, no correlation) calling variance() yields (4 8 21 13). Switching to Maxlikely yields (2.667 5.333 14 8.667).

When the data samples are set to be in Columns the empirical variance is (8.667 4.667 2)^T. The Maxlikely variance is (6.5 3.5 1.5)^T.

Variance type

Variances and covariances can either be calculated Empirical (unbiased) or as maximum likelihood (Maxlikely, second moments). With N the number of samples, the former normalizes by dividing by N-1 the latter by N.

To obtain a correlation matrix instead of a covariance matrix set parameters::useCorrelation to true.

Besides the mandatory apply-methods, this functor also offers a number of shortcuts to calculate only first or second order statistics. However, if both are needed it is always faster to call an apply() method that serves your purpose.

Complex numbers

This class works with matrices and vectors of complex numbers too. The definition of mean and variance is kept as with real numbers.

Complex numbers

You can also compute the mean, variance and covariance of vectors/scalars of complex values (i.e. cvr::complex<float> or cvr::complex<double>). The variace is defined through conjugation as

\[ \operatorname{var}(x) = E((x-\mu)^*(x-\mu)) \]


Member Enumeration Documentation

template<typename T , typename U = T>
enum cvr::secondOrderStatistics::eVarianceType

Type of the variance.

It can either be empirical or maximum likelihood.

Enumerator:
Empirical  Empirical Variance:

\[ \frac{1}{n-1} \sum_i^n (x_i - \mu)^2 \]

.

Maxlikely  Maximum Likelihood Variance:

\[ \frac{1}{n} \sum_i^n (x_i - \mu)^2 \]

.


Constructor & Destructor Documentation

template<typename T , typename U = T>
cvr::secondOrderStatistics< T, U >::secondOrderStatistics ( eDataRepresentationType  dataRep = Rows  ) 

Default constructor.

template<typename T , typename U = T>
cvr::secondOrderStatistics< T, U >::secondOrderStatistics ( const secondOrderStatistics< T, U > &  other  ) 

Copy constructor.

Parameters:
other the object to be copied

template<typename T , typename U = T>
cvr::secondOrderStatistics< T, U >::secondOrderStatistics ( const parameters param  ) 

Constructor that set the parameters to param.

Parameters:
param uses these parameters

template<typename T , typename U = T>
virtual cvr::secondOrderStatistics< T, U >::~secondOrderStatistics (  )  [virtual]

Destructor.


Member Function Documentation

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::apply ( const matrix< T > &  src,
vector< U > &  mean,
matrix< U > &  cov 
) const

Computes the mean vector and the covariance matrix for the data samples given in src.

The representation of data sample (point) as Rows or Columns of src can be chosen via parameters::dataRepresentationType but is usually the default: Rows.

In this case each dimension of mean corresponds to the mean value of the corresponding column of src. The covMatrix has dimension src.columns() x src.columns().

Depending on the value of parameters::varianceType the Empirical of Maxlikely variances are calculated.

If parameters::useCorrelation is true the correlation coefficients are calculated instead of the covariances.

Parameters:
src matrix<T> with the source data.
mean mean vector of src rows or columns
cov covariance matrix
Returns:
true if successful, false otherwise.

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::apply ( const matrix< T > &  src,
vector< U > &  mean,
vector< U > &  variance 
) const

Computes the mean vector and each dimension's variance for the data samples given in src.

The representation of data sample (point) as Rows or Columns of src can be chosen via parameters::dataRepresentationType but is usually the default: Rows.

In this case each dimension of mean and variance corresponds to the mean value and variance of the corresponding column of src.

Depending on the value of parameters::varianceType the Empirical of Maxlikely variances are calculated.

Parameters:
src matrix<T> with the source data.
mean mean vector of src rows or columns
variance variances of each sample dimension
Returns:
true if successful, false otherwise.

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::apply ( const matrix< T > &  src,
U &  mean,
U &  variance 
) const

Computes the mean and variance of the elements in the given matrix.

Parameters:
src matrix<T> with the source data.
mean mean value of src elements
variance variance of the elements of src
Returns:
true if successful, false otherwise.

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::apply ( const vector< T > &  src,
U &  mean,
U &  variance 
) const

Computes the mean and variance of the given vector.

Parameters:
src vector<T> with the source data.
mean mean value of src elements
variance variance of the elements of src
Returns:
true if successful, false otherwise.

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::applyColumns ( const matrix< T > &  src,
vector< U > &  mu,
matrix< U > &  cov 
) const [protected]

This function computes the mean and covariance matrix of the row vectors of the matrix.

I.e. each row is a data point in n-dimensional space).

Parameters:
src matrix<T> with the source data.
mu mean row vector;
cov covariance matrix of row vectors

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::applyColumns ( const matrix< T > &  src,
vector< U > &  mu,
vector< U > &  var 
) const [protected]

This function computes the mean and variance of each row of the matrix.

I.e. each column is a data point in n-dimensional space).

Parameters:
src matrix<T> with the source data.
mu mean column vector;
var variance of column vectors

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::applyRows ( const matrix< T > &  src,
vector< U > &  mu,
matrix< U > &  cov 
) const [protected]

This function computes the mean and covariance matrix of the row vectors of the matrix.

I.e. each row is a data point in n-dimensional space).

Parameters:
src matrix<T> with the source data.
mu mean row vector;
cov covariance matrix of row vectors

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::applyRows ( const matrix< T > &  src,
vector< U > &  mu,
vector< U > &  var 
) const [protected]

This function computes the mean and variance of each column of the matrix.

I.e. each row is a data point in n-dimensional space).

Parameters:
src matrix<T> with the source data.
mu mean row vector;
var variance of row vectors

template<typename T , typename U = T>
virtual secondOrderStatistics* cvr::secondOrderStatistics< T, U >::clone (  )  const [virtual]

Returns a pointer to a clone of this functor.

Implements cvr::functor.

template<typename T , typename U = T>
secondOrderStatistics& cvr::secondOrderStatistics< T, U >::copy ( const secondOrderStatistics< T, U > &  other  ) 

Copy data of "other" functor.

Parameters:
other the functor to be copied
Returns:
a reference to this functor object

template<typename T , typename U = T>
void cvr::secondOrderStatistics< T, U >::covar2corrcoef ( matrix< U > &  cv  )  const [protected]

Converts a covariance matrix into a correlation coefficient matrix.

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::covarianceMatrix ( const matrix< T > &  src,
matrix< U > &  covMat,
eDataRepresentationType  dataRep = Rows 
) const

Computes only the covariance matrix covMat of the given src data.

Do not use this function if you also need the mean vector, as the combined calculation via apply() is much faster.

Data samples in src can either be in Rows (default) or Columns. This can be set in dataRep. Other parameters are used as set in this functor's parameters (see apply()).

Parameters:
src the source data.
covMat covariance matrix of src
dataRep data representation, Rows or Columns
Returns:
true if successful, false otherwise.

template<typename T , typename U = T>
const parameters& cvr::secondOrderStatistics< T, U >::getParameters (  )  const

Returns used parameters.

Reimplemented from cvr::parametersManager.

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::mean ( const matrix< T > &  src,
vector< U > &  mean,
eDataRepresentationType  dataRep = Rows 
) const

Computes only the mean vector of the given src data.

Do not use this function if you also need the variances, as the combined calculation via apply() is much faster.

Data samples in src can either be in Rows (default) or Columns. This can be set in dataRep.

Parameters:
src the source data.
mean mean vector of src
dataRep data representation, Rows or Columns
Returns:
true if successful, false otherwise.

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::mean ( const vector< T > &  src,
U &  mean 
) const

Computes only the mean value of the given src vector.

Do not use this function if you also need the variances, as the combined calculation via apply() is much faster.

Parameters:
src vector<T> with the source data.
mean mean value of src elements
Returns:
true if successful, false otherwise.

template<typename T , typename U = T>
const std::string& cvr::secondOrderStatistics< T, U >::name (  )  const [virtual]

Returns the name of this class.

Implements cvr::functor.

template<typename T , typename U = T>
virtual secondOrderStatistics* cvr::secondOrderStatistics< T, U >::newInstance (  )  const [virtual]

Returns a pointer to a new instance of this functor.

Implements cvr::functor.

template<typename T , typename U = T>
secondOrderStatistics& cvr::secondOrderStatistics< T, U >::operator= ( const secondOrderStatistics< T, U > &  other  ) 

Alias for copy member.

Parameters:
other the functor to be copied
Returns:
a reference to this functor object

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::variance ( const matrix< T > &  src,
vector< U > &  variance,
eDataRepresentationType  dataRep = Rows 
) const

Computes only the variance vector of the given src data.

Do not use this function if you also need the mean vector, as the combined calculation via apply() is much faster.

Data samples in src can either be in Rows (default) or Columns. This can be set in dataRep. Other parameters are used as set in this functor's parameters (see apply()).

Parameters:
src the source data.
variance variance of each dimension of src
dataRep data representation, Rows or Columns
Returns:
true if successful, false otherwise.

template<typename T , typename U = T>
bool cvr::secondOrderStatistics< T, U >::variance ( const vector< T > &  src,
U &  variance 
) const

Computes only the variance of the given src vector.

Do not use this function if you also need the mean value, as the combined calculation via apply() is much faster.

Parameters:
src vector<T> with the source data.
variance variance of the elements of src
Returns:
true if successful, false otherwise.


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

Generated on Sun Sep 20 22:09:04 2009 for CVR-Lib by Doxygen 1.5.8