MATLAB: Too high correlation value from xcorr and corrcoef for uncorelated sequences

corrcoefcorrelationcross correlationp-valuestatisticstime seriesxcorr

Hello
I have two time series both of length 5604. The data is in the attachment (data.csv). The first column is the first time series, the second column is the second time series.
Now I have calculated the correlation of the series in the following way:
in = csvread('data.csv')
a = in(:,1);
b = in(:,2);
[corr, pval] = corrcoef(a,b);
corr= min(corr(:));
pval = min(pval(:));
[corr2, ~] = xcorr(a,b,'coeff');
corr2 = max(corr2);
For corr and pval I'm getting 0.5 and 0, respectively, and for corr2 I'm getting 0.92. But when I'm looking at the plot of both time series there should be no correlation at all (see plot below).
Why do I getting such high correlation values (and such low p-value)?

Best Answer

  • Ah, but there is quite a lot of correlation; remember it's not the magnitude of the values that matters, it's only whether they tend to "move together". To see this, try
    figure, subplot(2,1,1)
    yyaxis left, plot(dat(:,1))
    yyaxis right,plot(dat(:,2))
    ylim([0.35 1.5])
    to overlay the two data series on top of each other at a scale factor that makes them roughly match each other in mean amplitude. What this shows is pretty much the same overall trend and that even some of the substructure is similar; particularly there being a drop towards the RH end around 5000. But, mostly what the correlation is measuring is the overall trend.
    To see this,
    subplot(2,1,2)
    dtr=detrend(dat);
    plot(dtr)
    corr=corrcoef(dtr(:,1),dtr(:,2))
    corr =
    1.0000 0.1071
    0.1071 1.0000
    which is probably much more like what you were expecting.