Click or drag to resize
Accord.NET (logo)

MeasuresExponentialWeightedCovariance Method (Double, Int32, Double, Boolean)

Calculates the exponentially weighted covariance matrix.

Namespace:  Accord.Statistics
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
public static double[,] ExponentialWeightedCovariance(
	this double[][] matrix,
	int window,
	double alpha = 0,
	bool unbiased = false
Request Example View Source


Type: SystemDouble
A matrix of observations whose EW covariance matrix will be calculated. It is assumed that the matrix is ordered with the most recent observations at the bottom of the matrix.
Type: SystemInt32
The number of rows to be used in the calculation.
alpha (Optional)
Type: SystemDouble
The weighting to be applied to the calculation. A higher alpha discounts older observations faster. Alpha must be between 0 and 1 (inclusive).
unbiased (Optional)
Type: SystemBoolean
Use a standard estimation bias correction.

Return Value

Type: Double
Returns a vector containing the exponentially weighted average of the columns of the given matrix.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type . When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

The following example shows how to compute the EW covariance matrix.

Suppose we have a time series of (possibly correlated) observations. Our 
sample has 17 observations in 2 variables (x and y). We wish to compute 
the covariance matrix for our series but would like to provide a heavier 
weighting to the more recent observations. First, arrange the observations 
in rows with the oldest data at the top and the newest data at the bottom. */
double[,] rawData =
    { 2, 2, 1, 3, 5, 6, 4, 2, 7, 8, 9, 2, 3, 4, 5, 6, 7 },
    { 1, 2, 5, 3, 8, 6, 4, 4, 3, 8, 9, 0, 9, 9, 1, 9, 2 }

// Transpose our raw data to get it into the required format.
double[][] timeSeries = rawData.ToJagged(transpose: true);

// The window size determines how many observations to include in the 
// calculation. If no window is specified, the entire dataset is used. 
int window = 15;

// We set alpha to 20% meaning each previous observation's contribution 
// carries 20% less weight (relative to its immediate successor).  
double alpha = 0.2;

// Now we calculate the covariance matrix. The result should be:
//  { 3.80, 0.55 }
//  { 0.55, 13.0 }
double[,] ewmCov = timeSeries.ExponentialWeightedCovariance(window, alpha);
See Also