Reputation: 8461
I have the following two time series :
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!
Upvotes: 0
Views: 376
Reputation: 112749
I suggest two things to show the overall correlation, in Matlab. Let the x1
, x2
vectors denote your data.
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:
Upvotes: 1
Reputation: 26582
Here is an example of correlation in Python using numpy.corrcoef
which uses the following formula:
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()
Upvotes: 0