An unexpected discrepancy between xcorr, xcov and autocorr

2 views (last 30 days)
I need to calculate the matrix of cross-correlations for a matrix data. However, I started to doubt about
the way matlab performs the calculations. So, for the purpose of testing I decided to use a univariate time
series data (attached). For this univariate dataset I expect all the commands xcorr, xcov and autocorr to give me
the same result (of course, for bivariate data I do not expect xcorr and xcov to behave the same for all columns). Let's do it now:
[a1.'; a2.'; a3.']
-0.0000 0.7698 0.3913 -0.1393 -0.3791
-0.0087 0.7569 0.3814 -0.1458 -0.3816
1.0000 0.3642 0.1493 0.0477 0.0208
I did not expect to see this. However, if I remove the mean of data both xcorr and xcov give me the same result (so, for me this is a sign of poor-organization. I think both should be consistent) but autocorr is still very different as in bellow:
[a1.'; a2.'; a3.']
-0.0087 0.7569 0.3814 -0.1458 -0.3816
-0.0087 0.7569 0.3814 -0.1458 -0.3816
1.0000 0.3642 0.1493 0.0477 0.0208
For me, only the outcomes of autocorr seem correct because autocorrelation should start with 1. So, I do not know why xcor and
Any explanation?
Thanks for your kind help!

Answers (1)

Askic V
Askic V on 5 Mar 2023
When in doubt always read the documentation.
doc xcov
Matlab functions are very good documented. For example, you can read:
xcov computes the mean of its inputs, subtracts the mean, and then calls xcorr.
So, if something is documented, then it cannot be poor organised.
for example, if you first normalize data, and then run xcov and xcorr for the univariate time series data, you will get the same result:
file = websave('Data_test.csv', '');
data = csvread(file);
data_n = normalize(data);
a1_n = xcorr(data_n);
a2_n = xcov(data_n);
[a1_5.'; a2_5.']
ans = 2×5
-0.0177 1.5322 0.7721 -0.2952 -0.7724 -0.0177 1.5322 0.7721 -0.2952 -0.7724
ans = 6.4425e-12
Regarding the autororr function, execute this code:
file = websave('Data_test.csv', '');
data = csvread(file);
a1 = xcorr(data, data);
a2 = xcov(data,data);
a3 = autocorr(data);
fprintf('Sizes a1 = %d, a2 = %d, a3 = %d\n', numel(a1), numel(a2), numel(a3));
Sizes a1 = 19999, a2 = 19999, a3 = 21
As you can see a3 has only 21 elements because it returns a sample ACF.
You need to study the documentation about the autocorr and in general all functions you intend to use to be sure how they work.


Find more on Descriptive Statistics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!