-
Notifications
You must be signed in to change notification settings - Fork 53
/
gamma.py
281 lines (248 loc) · 7.81 KB
/
gamma.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
Gamma index for spatial autocorrelation
"""
__author__ = "Luc Anselin <luc.anselin@asu.edu> Serge Rey <sjsrey@gmail.com>"
import warnings
import numpy as np
from libpysal.weights.spatial_lag import lag_spatial
from .crand import _prepare_univariate
from .crand import njit as _njit
from .tabular import _univariate_handler
__all__ = ["Gamma"]
PERMUTATIONS = 999
class Gamma(object):
"""Gamma index for spatial autocorrelation
Parameters
----------
y : array
variable measured across n spatial units
w : W
spatial weights instance
can be binary or row-standardized
operation : {'c', 's', 'a'}
attribute similarity function where,
'c' cross product
's' squared difference
'a' absolute difference
standardize : {False, True}
standardize variables first
False, keep as is
True, standardize to mean zero and variance one
permutations : int
number of random permutations for calculation of pseudo-p_values
Attributes
----------
y : array
original variable
w : W
original w object
op : {'c', 's', 'a'}
attribute similarity function, as per parameters
attribute similarity function
stand : {False, True}
standardization
permutations : int
number of permutations
gamma : float
value of Gamma index
sim_g : array
(if permutations>0)
vector of Gamma index values for permuted samples
p_sim_g : array
(if permutations>0)
p-value based on permutations (one-sided)
null: spatial randomness
alternative: the observed Gamma is more extreme than under randomness
implemented as a two-sided test
mean_g : float
average of permuted Gamma values
min_g : float
minimum of permuted Gamma values
max_g : float
maximum of permuted Gamma values
Examples
--------
use same example as for join counts to show similarity
>>> import libpysal, numpy as np
>>> from esda.gamma import Gamma
>>> w = libpysal.weights.lat2W(4,4)
>>> y=np.ones(16)
>>> y[0:8]=0
>>> np.random.seed(12345)
>>> g = Gamma(y,w)
>>> g.g
20.0
>>> round(g.g_z, 3)
3.188
>>> round(g.p_sim_g, 3)
0.003
>>> g.min_g
0.0
>>> g.max_g
20.0
>>> g.mean_g
11.093093093093094
>>> np.random.seed(12345)
>>> g1 = Gamma(y,w,operation='s')
>>> g1.g
8.0
>>> round(g1.g_z, 3)
-3.706
>>> g1.p_sim_g
0.001
>>> g1.min_g
14.0
>>> g1.max_g
48.0
>>> g1.mean_g
25.623623623623622
>>> np.random.seed(12345)
>>> g2 = Gamma(y,w,operation='a')
>>> g2.g
8.0
>>> round(g2.g_z, 3)
-3.706
>>> g2.p_sim_g
0.001
>>> g2.min_g
14.0
>>> g2.max_g
48.0
>>> g2.mean_g
25.623623623623622
>>> np.random.seed(12345)
>>> g3 = Gamma(y,w,standardize=True)
>>> g3.g
32.0
>>> round(g3.g_z, 3)
3.706
>>> g3.p_sim_g
0.001
>>> g3.min_g
-48.0
>>> g3.max_g
20.0
>>> g3.mean_g
-3.2472472472472473
>>> np.random.seed(12345)
>>> def func(z,i,j):
... q = z[i]*z[j]
... return q
...
>>> g4 = Gamma(y,w,operation=func)
>>> g4.g
20.0
>>> round(g4.g_z, 3)
3.188
>>> round(g4.p_sim_g, 3)
0.003
Notes
-----
For further technical details see :cite:`Hubert_1981`.
"""
def __init__(
self, y, w, operation="c", standardize=False, permutations=PERMUTATIONS
):
if isinstance(standardize, str):
standardize = standardize.lower() == "yes"
warnings.warn(
"Use True/False for standardize. The option to"
' provide "yes"/"no" will be deprecated in PySAL 2.2.'
)
y = np.asarray(y).flatten()
self.w = w
self.y = y
self.op = operation
self.stand = standardize
self.permutations = permutations
if self.stand:
ym = np.mean(self.y)
ysd = np.std(self.y)
ys = (self.y - ym) / ysd
self.y = ys
self.g = self.__calc(self.y, self.op)
if permutations:
sim = [
self.__calc(np.random.permutation(self.y), self.op)
for i in range(permutations)
]
self.sim_g = np.array(sim)
self.min_g = np.min(self.sim_g)
self.mean_g = np.mean(self.sim_g)
self.max_g = np.max(self.sim_g)
p_sim_g = self.__pseudop(self.sim_g, self.g)
self.p_sim_g = p_sim_g
self.g_z = (self.g - self.mean_g) / np.std(self.sim_g)
@property
def _statistic(self):
return self.g
@property
def p_sim(self):
"""new name to fit with Moran module"""
return self.p_sim_g
def __calc(self, z, op):
if op == "c": # cross-product
zl = lag_spatial(self.w, z)
g = (z * zl).sum()
elif op == "s": # squared difference
zs = np.zeros(z.shape)
z2 = z**2
for i, i0 in enumerate(self.w.id_order):
neighbors = self.w.neighbor_offsets[i0]
wijs = self.w.weights[i0]
zw = list(zip(neighbors, wijs))
zs[i] = sum(
[wij * (z2[i] - 2.0 * z[i] * z[j] + z2[j]) for j, wij in zw]
)
g = zs.sum()
elif op == "a": # absolute difference
zs = np.zeros(z.shape)
for i, i0 in enumerate(self.w.id_order):
neighbors = self.w.neighbor_offsets[i0]
wijs = self.w.weights[i0]
zw = list(zip(neighbors, wijs))
zs[i] = sum([wij * abs(z[i] - z[j]) for j, wij in zw])
g = zs.sum()
else: # any previously defined function op
zs = np.zeros(z.shape)
for i, i0 in enumerate(self.w.id_order):
neighbors = self.w.neighbor_offsets[i0]
wijs = self.w.weights[i0]
zw = list(zip(neighbors, wijs))
zs[i] = sum([wij * op(z, i, j) for j, wij in zw])
g = zs.sum()
return g
def __pseudop(self, sim, g):
above = sim >= g
larger = above.sum()
psim = (larger + 1.0) / (self.permutations + 1.0)
if psim > 0.5:
psim = (self.permutations - larger + 1.0) / (self.permutations + 1.0)
return psim
@classmethod
def by_col(
cls, df, cols, w=None, inplace=False, pvalue="sim", outvals=None, **stat_kws
):
msg = (
"The `.by_col()` methods are deprecated and will be "
"removed in a future version of `esda`."
)
warnings.warn(msg, FutureWarning)
return _univariate_handler(
df,
cols,
w=w,
inplace=inplace,
pvalue=pvalue,
outvals=outvals,
stat=cls,
swapname=cls.__name__.lower(),
**stat_kws
)
# --------------------------------------------------------------
# Conditional Randomization Function Implementations
# --------------------------------------------------------------
@_njit(fastmath=True)
def _local_gamma_crand(i, z, permuted_ids, weights_i, scaling):
zi, zrand = _prepare_univariate(i, z, permuted_ids, weights_i)
return (zi * zrand) @ weights_i * scaling