From 818b5865b2f89f4fa141c3a6b38c56e354ce54b1 Mon Sep 17 00:00:00 2001 From: Tobias Glaubach Date: Fri, 3 Aug 2018 13:01:53 +0200 Subject: [PATCH] changed the signatures to fit inputs with outputs, and fixed a bug in the Auto(IEnumerable x, int[] k) method I found along the way --- src/Numerics/Statistics/Correlation.cs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Numerics/Statistics/Correlation.cs b/src/Numerics/Statistics/Correlation.cs index a2b870b7..2de8517e 100644 --- a/src/Numerics/Statistics/Correlation.cs +++ b/src/Numerics/Statistics/Correlation.cs @@ -44,7 +44,6 @@ namespace MathNet.Numerics.Statistics { /// /// Autocorrelation function (ACF) based on FFT for all possible lags k. - /// The first element is hidden since ACF(k = 0) = 1. /// /// Data array to calculate auto correlation for. /// An array with the ACF as a function of the lags k. @@ -55,11 +54,10 @@ namespace MathNet.Numerics.Statistics /// /// Autocorrelation function (ACF) based on FFT for lags between kMin and kMax. - /// The first element is hidden since ACF(k = 0) = 1. /// /// The data array to calculate auto correlation for. - /// Max lag to calculate ACF for must be positive and smaller than x.Length-1. - /// Min lag to calculate ACF for (0 = no shift with acf=1) must be zero or positive and smaller than x.Length-1. + /// Max lag to calculate ACF for must be positive and smaller than x.Length. + /// Min lag to calculate ACF for (0 = no shift with acf=1) must be zero or positive and smaller than x.Length. /// An array with the ACF as a function of the lags k. public static double[] Auto(IEnumerable x, int kMax, int kMin = 0) { @@ -72,7 +70,6 @@ namespace MathNet.Numerics.Statistics /// /// Autocorrelation function based on FFT for lags k. - /// The first element is hidden since ACF(k = 0) = 1. /// /// The data array to calculate auto correlation for. /// Array with lags to calculate ACF for. @@ -89,14 +86,16 @@ namespace MathNet.Numerics.Statistics throw new ArgumentException("k"); } + var k_min = k.Min(); + var k_max = k.Max(); // get acf between full range - var acf = AutoCorrelationFft(x, k.Min(), k.Max()); + var acf = AutoCorrelationFft(x, k_min, k_max); // map output by indexing var result = new double[k.Length]; for (int i = 0; i < result.Length; i++) { - result[i] = acf[k[i]]; + result[i] = acf[k[i] - k_min]; } return result; @@ -106,8 +105,8 @@ namespace MathNet.Numerics.Statistics /// The internal core method for calculating the autocorrelation. /// /// The data array to calculate auto correlation for - /// Min lag to calculate ACF for (0 = no shift with acf=1) must be zero or positive and smaller than x.Length-1 - /// Max lag to calculate ACF for must be positive and smaller than x.Length-1 + /// Min lag to calculate ACF for (0 = no shift with acf=1) must be zero or positive and smaller than x.Length + /// Max lag (EXCLUSIVE) to calculate ACF for must be positive and smaller than x.Length /// An array with the ACF as a function of the lags k. private static double[] AutoCorrelationFft(IEnumerable x, int k_low, int k_high) { @@ -162,13 +161,12 @@ namespace MathNet.Numerics.Statistics double acf_Val1 = x_fft2[0].Real; - double[] acf_Vec = new double[k_high - k_low]; - double[] acf_Val = new double[k_high + 1]; + double[] acf_Vec = new double[k_high - k_low + 1]; - // normalize such that acf[0] would be 1.0 and drop the first element - for (int ii = 0; ii < (k_high - k_low); ii++) + // normalize such that acf[0] would be 1.0 + for (int ii = 0; ii < (k_high - k_low + 1); ii++) { - acf_Vec[ii] = x_fft2[k_low + ii + 1].Real / acf_Val1; + acf_Vec[ii] = x_fft2[k_low + ii].Real / acf_Val1; } return acf_Vec;