.NET Matrix Library
CovarianceMatrix Method
See Also  Example Send comments on this topic


Returns a matrix containing the covariances between the columns of the current matrix.

Syntax

Visual Basic (Declaration) 
Public Function CovarianceMatrix() As Matrix
C# 
public Matrix CovarianceMatrix()
C++/CLI 
public:
Matrix^ CovarianceMatrix(); 

Return Value

A Matrix object having as elements the covariances between the columns of the current matrix.

Example

The following examples demonstrate the use of the ColCorrelation, ColCovariance and CorrelationMatrix, CovarianceMatrix methods.
Visual BasicCopy Code
Imports System
Imports Bluebit.MatrixLibrary

Class Test

    Public Shared Sub Main()

        Dim A As New Matrix(New Double(,) _
        {{1, 1, 1, 5}, _
         {2, 4, 3, 3}, _
         {3, 6, 3, 3}, _
         {4, 7, 5, 2}, _
         {5, 9, 5, 1}})

        Dim ACov As Matrix = A.CovarianceMatrix
        Dim ACor As Matrix = A.CorrelationMatrix

        Console.WriteLine("Matrix A = ")
        Console.WriteLine(A)

        Console.WriteLine("Covariance matrix = ")
        Console.WriteLine(ACov)
        Console.WriteLine("Correlation matrix =")
        Console.WriteLine(ACor)

        Dim Cov, Cor As Double

        'Finding covariance between columns 0 and 1
        'this should be the same as element 0,1 of matrix ACov
        Cov = A.ColCovariance(0, 1)
        Console.WriteLine(String.Format("Covariance between columns 0 and 1 of matrix A = {0:F3}", Cov))

        'Finding correlation between columns 0 and 1
        'this should be the same as element 0,1 of matrix ACor
        Cor = A.ColCorrelation(0, 1)
        Console.WriteLine(String.Format("Correlation between columns 0 and 1 of matrix A = {0:F3}", Cor))

        Console.Read()

    End Sub


End Class
C#Copy Code
using System;
using Bluebit.MatrixLibrary;

class Test
{
    static void Main(string[] args)
    {
        Matrix A = new Matrix(new double[,]
        {{1, 1, 1, 5}, 
         {2, 4, 3, 3}, 
         {3, 6, 3, 3}, 
         {4, 7, 5, 2}, 
         {5, 9, 5, 1}});

        Matrix ACov = A.CovarianceMatrix();
        Matrix ACor = A.CorrelationMatrix();

        Console.WriteLine("Matrix A = ");
        Console.WriteLine(A);

        Console.WriteLine("Covariance matrix = ");
        Console.WriteLine(ACov);
        Console.WriteLine("Correlation matrix =");
        Console.WriteLine(ACor);

        double Cov, Cor;

        //Finding covariance between columns 0 and 1
        //this should be the same as element 0,1 of matrix ACov
        Cov = A.ColCovariance(0, 1);
        Console.WriteLine(String.Format("Covariance between columns 0 and 1 of matrix A = {0:F3}", Cov));

        //Finding correlation between columns 0 and 1
        //this should be the same as element 0,1 of matrix ACor
        Cor = A.ColCorrelation(0, 1);
        Console.WriteLine(String.Format("Correlation between columns 0 and 1 of matrix A = {0:F3}", Cor));

        Console.Read();
    }

}
C++Copy Code
#include "stdafx.h"

using namespace System;
using namespace Bluebit::MatrixLibrary;

int main(array<System::String ^> ^args)
{
        array<double> ^darray = 
        {1, 1, 1, 5, 
         2, 4, 3, 3, 
         3, 6, 3, 3, 
         4, 7, 5, 2, 
         5, 9, 5, 1};
         
        Matrix^ A  = gcnew Matrix(darray, 5,4);

        Matrix^ ACov = A->CovarianceMatrix();
        Matrix^ ACor = A->CorrelationMatrix();

        Console::WriteLine("Matrix A = ");
        Console::WriteLine(A);

        Console::WriteLine("Covariance matrix = ");
        Console::WriteLine(ACov);
        Console::WriteLine("Correlation matrix =");
        Console::WriteLine(ACor);

        double Cov, Cor;

        //Finding covariance between columns 0 and 1
        //this should be the same as element 0,1 of matrix ACov
        Cov = A->ColCovariance(0, 1);
        Console::WriteLine(String::Format("Covariance between columns 0 and 1 of matrix A = {0:F3}", Cov ));

        //Finding correlation between columns 0 and 1
        //this should be the same as element 0,1 of matrix ACor
        Cor = A->ColCorrelation(0, 1);
        Console::WriteLine(String::Format("Correlation between columns 0 and 1 of matrix A = {0:F3}", Cor ));
        
        Console::Read();
}

Remarks

The element (i, j) of the correlation matrix is the covariance between column i and j. The covariance matrix is symmetric.

Requirements

Platforms: Windows 7, Windows Vista, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Windows 2000

See Also

.NET Matrix Library Documentation