-
Notifications
You must be signed in to change notification settings - Fork 0
/
ks2d2s.py
82 lines (73 loc) · 1.85 KB
/
ks2d2s.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import numpy as np
from scipy import stats
def ks2d2s(x1, y1, x2, y2):
assert (len(x1) == len(y1)) & (len(x2) == len(y2))
n1 = len(x1)
n2 = len(x2)
d1 = maxdist(x1, y1, x2, y2)
d2 = maxdist(x2, y2, x1, y1)
d = np.mean([d1, d2])
sqen = np.sqrt(n1 * n2 / (n1+n2))
r1 = stats.pearsonr(x1, y1)[0]
r2 = stats.pearsonr(x2, y2)[0]
rr = np.sqrt(1 - 0.5 * (r1 * r1 + r2 *r2))
p = stats.kstwobign.sf(d * sqen / (1 + rr * (0.25 - 0.75/sqen)))
return p
def quadct(x, y, xx, yy):
nn = len(xx)
yyg = (yy > y)
xxg = (xx > x)
na = np.sum(yyg & xxg)
nb = np.sum(yyg & ~xxg)
nc = np.sum(~yyg & ~xxg)
nd = np.sum(~yyg & xxg)
ff = 1 / nn
fa = na * ff
fb = nb * ff
fc = nc * ff
fd = nd * ff
return fa, fb, fc, fd
def maxdist(x1, y1, x2, y2):
n1 = len(x1)
d1 = np.zeros((n1,4))
n1_inv = 1 / n1
for i in range(n1):
fa, fb, fc, fd = quadct(x1[i], y1[i], x1, y1)
ga, gb, gc, gd = quadct(x1[i], y1[i], x2, y2)
if fa > ga:
fa += n1_inv
if fb > gb:
fb += n1_inv
if fc > gc:
fc += n1_inv
if fd > gd:
fd += n1_inv
d1[i] = [fa - ga, fb - gb, fc - gc, fd - gd]
dmin, dmax = np.min(d1), np.max(d1)
return np.max([dmin, dmax])
# def quadct(x, y, xx, yy):
# assert len(xx) == len(yy)
# nn = len(xx)
# na = nb = nc = nd = 0
#
# for i in range(nn):
# if (yy[i] == y) & (xx[i] ==x):
# continue
# if yy[i] > y:
# if xx[i] > x:
# na += 1
# else:
# nb += 1
# else:
# if xx[k] > x:
# nd += 1
# else:
# nc += 1
#
# ff = 1 / nn
# fa = ff * na
# fb = ff * nb
# fc = ff * nc
# fd = ff * nd
#
# return fa, fb, fc, fd