An unexpected discrepancy between xcorr, xcov and autocorr
14 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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.']
norm(a1_n-a2_n)
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));
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!