.NET Matrix Library
CorrelationMatrix Method
See Also  Example Send Feedback



Returns a matrix containing the correlation coefficients between the columns of the current matrix.

Syntax

Visual Basic (Declaration) 
Public Function CorrelationMatrix() As Matrix
Visual Basic (Usage)Copy Code
Dim instance As Matrix
Dim value As Matrix
 
value = instance.CorrelationMatrix()
C# 
public Matrix CorrelationMatrix()
C++/CLI 
public:
Matrix^ CorrelationMatrix(); 

Return Value

A Matrix object having as elements the correlation coefficients 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 correlation between column i and j. The correlation matrix is symmetric and its diagonal elements are equal to 1.

Requirements

Target Platforms: Windows 2000, Windows XP, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

.NET Matrix Library