An unexpected discrepancy between xcorr, xcov and autocorr

조회 수: 14 (최근 30일)
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2023년 1월 10일
답변: Askic V 2023년 3월 5일
Hello,
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:
data=csvread('Data_test.csv');
a1=xcorr(data);a2=xcov(data);a3=autocorr(data);
a1=a1(1:5);a2=a2(1:5);a3=a3(1:5);
[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:
data=csvread('Data_test.csv');
data=data-mean(data);
a1=xcorr(data);a2=xcov(data);a3=autocorr(data);
a1=a1(1:5);a2=a2(1:5);a3=a3(1:5);
[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!
Babak

답변 (1개)

Askic V
Askic V 2023년 3월 5일
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', 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1257910/Data_test.csv');
data = csvread(file);
data_n = normalize(data);
a1_n = xcorr(data_n);
a2_n = xcov(data_n);
a1_5=a1_n(1:5);a2_5=a2_n(1:5);
[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
norm(a1_n-a2_n)
ans = 6.4425e-12
Regarding the autororr function, execute this code:
file = websave('Data_test.csv', 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1257910/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.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by