-
Notifications
You must be signed in to change notification settings - Fork 2
/
lnn.py
341 lines (267 loc) · 9.88 KB
/
lnn.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#Written by Weihao Gao from UIUC
import scipy.spatial as ss
import scipy.stats as sst
import scipy.io as sio
from scipy.special import beta,digamma,gamma
from sklearn.neighbors import KernelDensity
from math import log,pi,exp
import numpy.random as nr
import numpy as np
import random
import time
import matplotlib.pyplot as plt
from cvxopt import matrix,solvers
#Usage Functions
#This is the main entropy estimator
def entropy(x,k=5,tr=30,bw=0):
return LNN_2_entropy(x,k,tr,bw)
def LNN_2_entropy(x,k=5,tr=30,bw=0):
'''
Estimate the entropy H(X) from samples {x_i}_{i=1}^N
Using Local Nearest Neighbor (LNN) estimator with order 2
Input: x: 2D list of size N*d_x
k: k-nearest neighbor parameter
tr: number of sample used for computation
bw: option for bandwidth choice, 0 = kNN bandwidth, otherwise you can specify the bandwidth
Output: one number of H(X)
'''
assert k <= len(x)-1, "Set k smaller than num. samples - 1"
assert tr <= len(x)-1, "Set tr smaller than num.samples - 1"
N = len(x)
d = len(x[0])
local_est = np.zeros(N)
S_0 = np.zeros(N)
S_1 = np.zeros(N)
S_2 = np.zeros(N)
tree = ss.cKDTree(x)
if (bw == 0):
bw = np.zeros(N)
for i in range(N):
lists = tree.query(x[i],tr+1,p=2)
knn_dis = lists[0][k]
list_knn = lists[1][1:tr+1]
if (bw[i] == 0):
bw[i] = knn_dis
S0 = 0
S1 = np.matrix(np.zeros(d))
S2 = np.matrix(np.zeros((d,d)))
for neighbor in list_knn:
dis = np.matrix(x[neighbor] - x[i])
S0 += exp(-dis*dis.transpose()/(2*bw[i]**2))
S1 += (dis/bw[i])*exp(-dis*dis.transpose()/(2*bw[i]**2))
S2 += (dis.transpose()*dis/(bw[i]**2))*exp(-dis*dis.transpose()/(2*bw[i]**2))
Sigma = S2/S0 - S1.transpose()*S1/(S0**2)
det_Sigma = np.linalg.det(Sigma)
if (det_Sigma < (1e-4)**d):
local_est[i] = 0
else:
offset = (S1/S0)*np.linalg.inv(Sigma)*(S1/S0).transpose()
local_est[i] = -log(S0) + log(N-1) + 0.5*d*log(2*pi) + d*log(bw[i]) + 0.5*log(det_Sigma) + 0.5*offset[0][0]
if (np.count_nonzero(local_est) == 0):
return 0
else:
return np.mean(local_est[np.nonzero(local_est)])
#These is main mutual information estimator
def mi(data,split,k=5,tr=30):
return _3LNN_2_mi(data,split,k,tr)
def _3LNN_2_mi(data,split,k=5,tr=30):
'''
Estimate the mutual information I(X;Y) from samples {x_i,y_i}_{i=1}^N
Using I(X;Y) = H_{LNN}(X) + H_{LNN}(Y) - H_{LNN}(X;Y)
where H_{LNN} is the LNN entropy estimator with order 2
Input: data: 2D list of size N*(d_x + d_y)
split: should be d_x, splitting the data into two parts, X and Y
k: k-nearest neighbor parameter
tr: number of sample used for computation
Output: one number of I(X;Y)
'''
assert split >=1, "x must have at least one dimension"
assert split <= len(data[0]) - 1, "y must have at least one dimension"
x = data[:,:split]
y = data[:,split:]
N = len(data)
return LNN_2_entropy(x,k,tr) + LNN_2_entropy(y,k,tr) - LNN_2_entropy(data,k,tr)
#Auxilary Functions
def vd(d,q):
# Return the volume of unit q-norm ball in d dimension space
if (q==float('inf')):
return d*log(2)
return d*log(2*gamma(1+1.0/q)) - log(gamma(1+d*1.0/q))
#These functions are entropy estimators used for comparison in the paper
def KDE_entropy(x):
'''
Estimate the entropy H(X) from samples {x_i}_{i=1}^N
Using Kernel Density Estimator (KDE) and resubstitution
Input: x: 2D list of size N*d_x
Output: one number of H(X)
'''
N = len(x)
d = len(x[0])
local_est = np.zeros(N)
for i in range(N):
kernel = sst.gaussian_kde(x.transpose())
local_est[i] = kernel.evaluate(x[i].transpose())
return -np.mean(map(log,local_est))
def KL_entropy(x,k=5):
'''
Estimate the entropy H(X) from samples {x_i}_{i=1}^N
Using Kozachenko-Leonenko (KL) estimator
Input: x: 2D list of size N*d_x
k: k-nearest neighbor parameter
Output: one number of H(X)
'''
assert k <= len(x)-1, "Set k smaller than num. samples - 1"
N = len(x)
d = len(x[0])
tree = ss.cKDTree(x)
knn_dis = [tree.query(point,k+1,p=2)[0][k] for point in x]
ans = -log(k) + log(N) + vd(d,2)
return ans + d*np.mean(map(log,knn_dis))
def LNN_1_entropy(x,k=5,tr=30,bw = 0):
'''
Estimate the entropy H(X) from samples {x_i}_{i=1}^N
Using Local Nearest Neighbor (LNN) estimator with order 1
Input: x: 2D list of size N*d_x
k: k-nearest neighbor parameter
tr: number of sample used for computation
bw: option for bandwidth choice, 0 = kNN bandwidth, otherwise you can specify the bandwidth
Output: one number of H(X)
'''
assert k <= len(x)-1, "Set k smaller than num. samples - 1"
assert tr <= len(x)-1, "Set tr smaller than num.samples - 1"
N = len(x)
d = len(x[0])
local_est = np.zeros(N)
S_0 = np.zeros(N)
S_1 = np.zeros(N)
S_2 = np.zeros(N)
tree = ss.cKDTree(x)
if (bw == 0):
bw = np.zeros(N)
for i in range(N):
lists = tree.query(x[i],tr+1,p=2)
knn_dis = lists[0][k]
list_knn = lists[1][1:tr+1]
if (bw[i] == 0):
bw[i] = knn_dis
#bw = 1.06*(N**(-1.0/(d+4)))
S0 = 0
S1 = np.matrix(np.zeros(d))
for neighbor in list_knn:
dis = np.matrix(x[neighbor] - x[i])
S0 += exp(-dis*dis.transpose()/(2*bw[i]**2))
S1 += (dis/bw[i])*exp(-dis*dis.transpose()/(2*bw[i]**2))
offset = (S1/S0)*(S1/S0).transpose()
local_est[i] = -log(S0) + log(N) + 0.5*d*log(2*pi) + d*log(bw[i]) + 0.5*offset[0][0]
if (abs(local_est[i]) > (1e+4)**d):
local_est[i] = 0
if (np.count_nonzero(local_est) == 0):
return 0
else:
return np.mean(local_est[np.nonzero(local_est)])
#These functions are mutual information estimators used for comparison in the paper
def _3KDE_mi(data,split):
'''
Estimate the mutual information I(X;Y) from samples {x_i,y_i}_{i=1}^N
Using I(X;Y) = H_{KDE}(X) + H_{KDE}(Y) - H_{KDE}(X;Y)
where H_{LNN} is the KDE entropy estimator
Input: data: 2D list of size N*(d_x + d_y)
split: should be d_x, splitting the data into two parts, X and Y
Output: one number of I(X;Y)
'''
assert split >=1, "x must have at least one dimension"
assert split <= len(data[0]) - 1, "y must have at least one dimension"
x = data[:,:split]
y = data[:,split:]
return KDE_entropy(x) + KDE_entropy(y) - KDE_entropy(data)
def _3KL_mi(data,split,k=5):
'''
Estimate the mutual information I(X;Y) from samples {x_i,y_i}_{i=1}^N
Using I(X;Y) = H_{KL}(X) + H_{KL}(Y) - H_{KL}(X;Y)
where H_{KL} is the KL entropy estimator
Input: data: 2D list of size N*(d_x + d_y)
split: should be d_x, splitting the data into two parts, X and Y
k: k-nearest neighbor parameter
Output: one number of I(X;Y)
'''
assert split >=1, "x must have at least one dimension"
assert split <= len(data[0]) - 1, "y must have at least one dimension"
x = data[:,:split]
y = data[:,split:]
return KL_entropy(x,k) + KL_entropy(y,k) - KL_entropy(data,k)
def _KSG_mi(data,split,k=5):
'''
Estimate the mutual information I(X;Y) from samples {x_i,y_i}_{i=1}^N
Using KSG mutual information estimator
Input: data: 2D list of size N*(d_x + d_y)
split: should be d_x, splitting the data into two parts, X and Y
k: k-nearest neighbor parameter
Output: one number of I(X;Y)
'''
assert split >=1, "x must have at least one dimension"
assert split <= len(data[0]) - 1, "y must have at least one dimension"
N = len(data)
x = data[:,:split]
y = data[:,split:]
dx = len(x[0])
dy = len(y[0])
tree_xy = ss.cKDTree(data)
tree_x = ss.cKDTree(x)
tree_y = ss.cKDTree(y)
knn_dis = [tree_xy.query(point,k+1,p=2)[0][k] for point in data]
ans = digamma(k) + log(N) + vd(dx,2) + vd(dy,2) - vd(dx+dy,2)
for i in range(N):
ans += -log(len(tree_y.query_ball_point(y[i],knn_dis[i],p=2))-1)/N - log(len(tree_x.query_ball_point(x[i],knn_dis[i],p=2))-1)/N
return ans
def _3LNN_1_mi(data,split,k=5,tr=30):
'''
Estimate the mutual information I(X;Y) from samples {x_i,y_i}_{i=1}^N
Using I(X;Y) = H_{LNN}(X) + H_{LNN}(Y) - H_{LNN}(X;Y)
where H_{LNN} is the LNN entropy estimator with order 1
Input: data: 2D list of size N*(d_x + d_y)
split: should be d_x, splitting the data into two parts, X and Y
k: k-nearest neighbor parameter
tr: number of sample used for computation
Output: one number of I(X;Y)
'''
assert split >=1, "x must have at least one dimension"
assert split <= len(data[0]) - 1, "y must have at least one dimension"
x = data[:,:split]
y = data[:,split:]
return LNN_1_entropy(x,k,tr) + LNN_1_entropy(y,k,tr) - LNN_1_entropy(data,k,tr)
def _3LNN_1_KSG_mi(data,split,k=5,tr=30):
'''
Estimate the mutual information I(X;Y) from samples {x_i,y_i}_{i=1}^N
Using I(X;Y) = H_{LNN}(X) + H_{LNN}(Y) - H_{LNN}(X;Y) with "KSG trick"
where H_{LNN} is the LNN entropy estimator with order 1
Input: data: 2D list of size N*(d_x + d_y)
split: should be d_x, splitting the data into two parts, X and Y
k: k-nearest neighbor parameter
tr: number of sample used for computation
Output: one number of I(X;Y)
'''
assert split >=1, "x must have at least one dimension"
assert split <= len(data[0]) - 1, "y must have at least one dimension"
x = data[:,:split]
y = data[:,split:]
tree_xy = ss.cKDTree(data)
knn_dis = [tree_xy.query(point,k+1,p=2)[0][k] for point in data]
return LNN_1_entropy(x,k,tr,bw=knn_dis) + LNN_1_entropy(y,k,tr,bw=knn_dis) - LNN_1_entropy(data,k,tr,bw=knn_dis)
def _3LNN_2_KSG_mi(data,split,k=5,tr=30):
'''
Estimate the mutual information I(X;Y) from samples {x_i,y_i}_{i=1}^N
Using I(X;Y) = H_{LNN}(X) + H_{LNN}(Y) - H_{LNN}(X;Y) with "KSG trick"
where H_{LNN} is the LNN entropy estimator
Input: data: 2D list of size N*(d_x + d_y)
split: should be d_x, splitting the data into two parts, X and Y
k: k-nearest neighbor parameter
tr: number of sample used for computation
Output: one number of I(X;Y)
'''
assert split >=1, "x must have at least one dimension"
assert split <= len(data[0]) - 1, "y must have at least one dimension"
x = data[:,:split]
y = data[:,split:]
tree_xy = ss.cKDTree(data)
knn_dis = [tree_xy.query(point,k+1,p=2)[0][k] for point in data]
return LNN_2_entropy(x,k,tr,bw=knn_dis) + LNN_2_entropy(y,k,tr,bw=knn_dis) - LNN_2_entropy(data,k,tr,bw=knn_dis)