MeasuresExponentialWeightedMean Method (Double, Double) |
Namespace: Accord.Statistics
The following example shows how to compute the EW mean.
/* 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 mean of 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; double[] ewm = timeSeries.ExponentialWeightedMean(window, alpha); // (5.47, 5.20)