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


col1
An integer specifying the first column.
col2
An integer specifying the second column.
Returns the covariance between two columns of the current matrix.

Syntax

Visual Basic (Declaration) 
Public Function ColCovariance( _
   ByVal col1 As Integer, _
   ByVal col2 As Integer _
) As Double
C# 
public double ColCovariance( 
   int col1,
   int col2
)
C++/CLI 
public:
double ColCovariance( 
   int col1,
   int col2
) 

Parameters

col1
An integer specifying the first column.
col2
An integer specifying the second column.

Return Value

A double that equals to the covariance between the two columns.

Exceptions

ExceptionDescription
IndexOutOfBoundsExceptionAny of col1, col2 parameters exceeds matrix size.

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();
    }

}
 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();
}

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