I'm trying and failing to reproduce the results found in this paper, where the cross correlation between two distributions is calculated:
p(XY) = 0.5 * P(X = Y) + 1 * P(X > Y)
Here' the python code (x and y are taken from the first table on page 5):
import numpy as np
x = [-0.01, -0.05, -0.13, -0.02, -0.17, -0.09, 0.0, -0.04, -0.03, 0.06, -1.37, -0.03, 0.01, -0.57, 0.04, -0.09, -0.04, -5.56, -0.02, 0.0, 0.0, 0.0, 0.0, -0.03, -0.55, -2.6, -0.42, -1.35, 0.0, 0.43, -0.74, -0.47, 0.0, -10.25, -11.18, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
y = [0.0, -0.16, -0.11, -0.04, -0.1, 0.02, -0.04, -0.13, -0.03, -0.04, 0.0, -0.01, 0.01, -0.38, 0.13, -0.26, -0.11, -0.09, 0.0, 0.0, 0.18, 0.0, -0.02, 0.03, -0.17, 0.21, 0.17, -0.42, 0.0, 0.87, 0.15, 0.0, 0.47, 3.73, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
bins = np.arange(min(x + y), max(x + y), 0.01)
# x histogram (normalized to sum 1.0)
hx, dx = np.histogram(x, bins)
hx = 1.0*hx/np.sum(hx) # max(hx) = 0.3170731707317073
# y histogram (normalized to sum 1.0)
hy, dy = np.histogram(y, bins)
hy = 1.0*hy/np.sum(hy) # max(hy) = 0.34999999999999998
# Now comes the hard part, the result doesn't match with the paper (0.345)
# this gives 1.01219512
np.correlate(hx, hy)*0.5 + sum(np.correlate(hx, hy, 'same'))
# this gives 1.06097561
np.correlate(hx, hy)*0.5 + sum(np.correlate(hx, hy, 'full'))
What i'm doing wrong? Maybe i'm summing up all the lags? (positive and negative) Thanks for you help!
We can then make use of the well-known fact that the probability distribution of the difference between two random variables is the cross- correlation of their distributions
. And by looking at docs.scipy.org/doc/numpy/reference/generated/…, the formula looks the same. $\endgroup$ – Fernando Nov 24 '14 at 18:53