Question

I have the following two time series :

enter image description here

The x-axis is over 10000 values. Now, if I break them up into sliding windows, then I don't get a correlation since well, individually they aren't correlating. However, you can see that in the larger picture, they do correlate. I need to show this correlation. Can anyone please give me pointers on how to do this?

I am working in Matlab & Python, but I mainly need an overview really. Thanks!

Was it helpful?

Solution

I suggest two things to show the overall correlation, in Matlab. Let the x1, x2 vectors denote your data.

  • Compute c = corrcoef(x1,x2) and observe c(2,1). That's the correlation coefficient for the whole vectors. It measures correlation normalized between -1 and 1.
  • plot(x1,x2,'.','markersize',3). That draws a cloud of points, from which you can visually assess the correlation. For correlated x1 and x2, the points tend to form a more or less thin cloud along a straight line (see example shapes and its associated correlation coefficient)

If your vectors contain NaN's, you should first remove them:

ind = ~(isnan(x1)|isnan(x2));
x1 = x1(ind);
x2 = x2(ind);

For example: the following two example vectors give c=0.91, and the cloud shape makes it obvious that there is significant correlation:

Signals

Cloud

OTHER TIPS

Here is an example of correlation in Python using numpy.corrcoef which uses the following formula:

enter image description here

Where Cij is the covariance of variables xi and xj (being each of them random variables). The Pij variables tells use how similar are xi and xj, if both signals are similar they where close to 1 or -1, and if they are uncorrelated they will be close to 0.

>>> import numpy as np
>>> n = 100
>>> x = np.linspace(0, 10, n)
>>> y1 = np.sin(x) + np.random.randn(n) * 0.3 + 2
>>> y2 = np.sin(x) + np.random.randn(n) * 0.5
>>> np.corrcoef(y1,y2)
 [[ 1.          0.79680839]
  [ 0.79680839  1.        ]]

By the way, there are the two signals that we have correlated

>>> import matplotlib.pyplot as plt
>>> plt.plot(x,y1, x, y2)
>>> plt.show()

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top