Click or drag to resize

Correlation and Covariance

Correlation and covariance methods provide measures of statistical relation between series data based on linear Pearson’s product-moment correlation coefficient.

Hereinafter the following convention is used:

X, Y denote data container, i.e. a vector or an array of doubles.

DSxi, DSYi denote data container element, i = 0..n-1. For vector, array of doubles and array of nullable doubles it is an array element or vector element.

Covariance is a measure of how much two variables change together.

Correlation coefficient is ranged from -1 to 1 and measures the extent to which values of two variables change "proportionally" to each other, i.e. as one variable increases, how much another variable tends to increase or decrease. The value of 1 represents a perfect positive correlation when both variables fluctuate around their means coherently, the value of -1 indicates exactly opposite tendency and the value of 0 represents a lack of correlation, i.e. linear statistical independence. Although Correlation Coefficient is sensitive only to a linear relationship between two variables, it can also (not necessarily) point out a nonlinear relationship between the variables. The same formula applied to one variable X and describing the correlation between values of the variable at different time points (i.e. the correlation of the variable against a time-shifted version of itself) is called autocorrelation coefficient or cross-correlation coefficient.

The correlation and autocorrelation coefficients are commonly used for checking randomness in data series, to visualize correlations often correlogram and autocorellogram are used, that are spectrum-like plots of correlation coefficients at varying time lags.

Methods

Operation

Description

Performance

covariance

Returns covariance between values in containers. Warning: containers must have the same size.

DSCov

StaticCovariance(Vector, Vector, Boolean)

StaticCovariance(IEnumerableDouble, IEnumerableDouble, Boolean)

StaticCovariance(IEnumerableNullableDouble, IEnumerableNullableDouble, Boolean)

population covariance

Returns population covariance between values in containers. It is identical to covariance. Warning: containers must have the same size.

DSPop Cov

StaticCovariancePopulation(Vector, Vector, Boolean)

StaticCovariancePopulation(IEnumerableDouble, IEnumerableDouble, Boolean)

StaticCovariancePopulation(IEnumerableNullableDouble, IEnumerableNullableDouble, Boolean)

sample covariance

Returns sample covariance between values in containers. Warning: containers must have the same size.

DSSample Cov

StaticCovarianceSample(Vector, Vector, Boolean)

StaticCovarianceSample(IEnumerableDouble, IEnumerableDouble, Boolean)

StaticCovarianceSample(IEnumerableNullableDouble, IEnumerableNullableDouble, Boolean)

correlation

Returns correlation between values in containers. Warning: containers must have the same size.

DSCorrelation

StaticCorrelation(Vector, Vector, Boolean)

StaticCorrelation(IEnumerableDouble, IEnumerableDouble, Boolean)

StaticCorrelation(IEnumerableNullableDouble, IEnumerableNullableDouble, Boolean)

autocorrelogram

Builds autocorrelogram of specified size of container's values. Method will enum all lags between zero and specified one inclusive.

DSACorrelogram

Void methods:

StaticAutocorrelogram(IEnumerableDouble, Int32, Double, Boolean)

StaticAutocorrelogram(IEnumerableNullableDouble, Int32, Double, Boolean)

Methods that return an array of double values containing autocorrelogram:

StaticAutocorrelogram(Vector, Int32, Boolean)

StaticAutocorrelogram(IEnumerableDouble, Int32, Boolean)

StaticAutocorrelogram(IEnumerableNullableDouble, Int32, Boolean)

correlogram

Builds correlogram between values from containers. Method will enumerates all lags between -lag and +lag. So be careful with result array size it must be at least lag * 2 + 1. Warning: containers must have the same size.

DSCorrelogram

Void methods:

StaticCorrelogram(IEnumerableDouble, IEnumerableDouble, Int32, Double, Boolean)

StaticCorrelogram(IEnumerableNullableDouble, IEnumerableNullableDouble, Int32, Double, Boolean)

Methods that return an array of double values containing correlogram:

StaticCorrelogram(Vector, Vector, Int32, Boolean)

StaticCorrelogram(IEnumerableDouble, IEnumerableDouble, Int32, Boolean)

StaticCorrelogram(IEnumerableNullableDouble, IEnumerableNullableDouble, Int32, Boolean)

Code Sample

The example of covariance and correlation computation:

C#
 1using System;
 2using FinMath.LinearAlgebra;
 3using FinMath.Statistics;
 4
 5namespace FinMath.Samples
 6{
 7    class VectorCorrelationSample
 8    {
 9        static void Main()
10        {
11            // Input paramters.
12            const Int32 observationsCount = 10;
13            const Int32 correlogramLag = 2;
14
15            // Genereating input vectors.
16            Vector vectorX = Vector.Random(observationsCount);
17            Vector vectorY = Vector.Random(observationsCount);
18            Console.WriteLine("Input vector X:");
19            Console.WriteLine("  " + vectorX.ToString("0.000"));
20            Console.WriteLine("Input vector Y:");
21            Console.WriteLine("  " + vectorY.ToString("0.000"));
22
23            // Compute covariance and correlation and output results.
24            Console.WriteLine();
25            Console.WriteLine("Covariance of X and Y: " + vectorX.Covariance(vectorY).ToString("0.000"));
26            Console.WriteLine("Correlation of X and Y: " + vectorX.Correlation(vectorY).ToString("0.000"));
27            // Allocate memory for correlogram, beware, correlogram size is (2 x Lag + 1),
28            // Because it computes correlation for lags: [-Lag, -Lag + 1, ..., 0, ... Lag - 1, Lag].
29            Vector correlogram = new Vector(correlogramLag * 2 + 1);
30            // Compute correlogram and output results.
31            vectorX.Correlogram(vectorY, correlogramLag, (Double[])correlogram);
32            Console.WriteLine("Correlogram of X and Y:");
33            Console.WriteLine("  " + correlogram.ToString("0.000"));
34        }
35    }
36}

See Also