autocorr, xcov, xcorr, which one should I choose to calculate auto-correlation function? And what's the difference among them?

36 visualizaciones (últimos 30 días)
I need to calculate an auto-correlation function. autocorr, xcov, xcorr, which one should I choose? The mean of the discrete data isn't 0. And I want to study of critical slowdown here.

Respuestas (1)

dpb
dpb el 30 de Abr. de 2023
Editada: dpb el 1 de Mayo de 2023
Doesn't really matter although from the doc, the autocorr function in the Econ TB uses the FFT to compute and then IFFT to return to the time domain for output. It thereby removes the mean and it also normalizes the 0-lag element to have magnitude 1.
xcov (see the doc notes) removes the mean and then calls xcorr so if you also set the options flag to 'coeff', it will return the same results to within roughly machine precision. If you use xcorr alone, it will not demean first and so will likely end up with a very slightly different result.
The Econ TB version also only returns zero and positive lags; the two builtin routines (actually boil down to just the one) return both negative/positive lags. To only return the positive, simply keep the lags vector and use logical indexing--
rng(1); y=randn(1000,1); % case from TB doc
ra=autocorr(y,10);
[rx,l]=xcorr(y,10,'coeff');
rx=rx(l>=0);
[rv,l]=xcov(y,10,'coeff');
rv=rv(l>=0);
[ra rx rv]
ans = 11×3
1.0000 1.0000 1.0000 -0.0180 -0.0179 -0.0180 0.0536 0.0538 0.0536 -0.0206 -0.0204 -0.0206 -0.0300 -0.0298 -0.0300 -0.0086 -0.0084 -0.0086 -0.0108 -0.0106 -0.0108 -0.0116 -0.0114 -0.0116 0.0309 0.0310 0.0309 0.0341 0.0343 0.0341
Which to use is all up to you; another case where TMW has introduced new and very slightly different syntax/functionality for a functionality that already existed with no hints as to why to use one over the other nor why it was needed to introduce a whole new function to compute the same thing. It's possible the FFT version is a little quicker, but if they wanted to boost performance if that is, indeed, the case, then why not rewrite the original core routine? All the TB'en seem to think they need to be totally self-contained and so are allowed to do stuff like this contributing to the code bloat that's been a topic in another conversation. If xcorr is good-enough for the masses/base product why wasn't xcov good enough for the TB? If they wanted/needed one to return only zero/positive lags, then the toolbox team should have the base TMW to approve/introduce the flag variable into the base product; it would be a worthwhile general enhancement anyway; I actually have built a custom utility function for the purpose that aliases the base routines as my uses virtually always wanted the same behavior.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by