Statistics.Covariance Method

Overload List

#SignatureDescription
1void Covariance(TDenseMtxVec X, TDenseMtxVec Y, TMtx aResult, Boolean NormN)Calculate the variance-covariance matrix (Result), assuming vectors X and Y are two variable and their elements are the observations.
2void Covariance(TMtx X, TMtx aResult, Boolean NormN)Calculate the covariance matrix (Result), assuming matrix X columns are variables and its rows are observations.
3void Covariance(TVec X, ref Double aResult, Boolean NormN)Covariance/variance.

Overload 1: void Covariance(TDenseMtxVec X, TDenseMtxVec Y, TMtx aResult, Boolean NormN)

Calculate the variance-covariance matrix (Result), assuming vectors X and Y are two variable and their elements are the observations.

#NameTypeDescription
1XTDenseMtxVecsource TVec or TMtx
2YTDenseMtxVecsource TVec or TMtx
3aResultTMtxsource TMtx
4NormNBoolean

Result: stored in self (calling object)

Remarks:

For column-vector valued random variables X and Y with respective expected values mu and nu, and respective scalar components m and n, the covariance is defined to be the m-by-n matrix called the covariance matrix:

Cov(X,Y)=E((Xμ)(Yν)T).\text{Cov}(X,Y)= E\left( (X-\mu) (Y-\nu)^T \right) .
Examples
using Dew.Math;
using Dew.Stats.Units;
namespace Dew.Examples
{
    private void Example()
    {
        Vector data1 = new Vector(0);
        Vector data2 = new Vector(0);
        Matrix cov = new Matrix(0,0);
        data1.SetIt(false,new double[] {1.2,3});
        data2.SetIt(false,new double[] {5,5.5});
        Statistics.Covariance(data1,data2,cov,false);
        // cov = [1.62, 0.45,
        //        0.45, 0.125]
    }
}

Overload 2: void Covariance(TMtx X, TMtx aResult, Boolean NormN)

Calculate the covariance matrix (Result), assuming matrix X columns are variables and its rows are observations.

#NameTypeDescription
1XTMtxsource TMtx
2aResultTMtxsource TMtx
3NormNBoolean

Result: stored in self (calling object)

Remarks:

By definition the covariance matrix is a matrix of covariances between elements of a vector. It is the natural generalization to higher dimensions of the concept of the variance of a scalar-valued random variable.

If X columns represent observation samples (variables), it's rows sample(s) values (observables), muj Xj j-th column average value, then the covariance matrix is defined as:

Σi,j=E((Xiμi)(Xjμj)).\Sigma_{i,j} = E\left((X_i-\mu_i)(X_j-\mu_j)\right) \qquad.

or in matrix form:

Σ=E((XIμ)T(XIμ)).\Sigma = E \left((X-I\cdot\mu)^T (X-I\cdot\mu)\right) \qquad.

where E is the expected value. The inverse of this matrix, is called the inverse covariance matrix or the precision matrix.

Note
This version does all necessary calculations to calculate covariance matrix.

Overload 3: void Covariance(TVec X, ref Double aResult, Boolean NormN)

Covariance/variance.

#NameDescription
1XDefines sample (variable) values (observables). In this case X is treated as row and not (as normally) column vector.
2aResultReturns the covariance (in this case equal to variance) for X vector elements. Because in this case X is represented as row vectro, the the result is simply scalar value E(X(T)*X)-E(X(T))E(X) = Var(X).
3NormNIf true (default value), the result will be normalized with number of observations (N), otherwise it will be normalized with N-1.

Result: stored in self (calling object)

Remarks:

The covariance between two real-valued random variables x and y,with expected values E(x)=mu and E(y)=nu is defined as:

Cov(x,y)=E((xμ)(xν))=E(xy)E(x)E(y).\text{Cov}(x,y)= E\left( (x-\mu) (x-\nu) \right) = E \left( x\cdot y \right) - E(x)E(y)\qquad .

where E(x), E(y) are x and y expected values.

For more info about covariance definition and properties check thd following links:

1. http://mathworld.wolfram.com/Covariance.html

2. http://en.wikipedia.org/wiki/Covariance