-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKLDivergence_update.py
274 lines (221 loc) · 8.01 KB
/
KLDivergence_update.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
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import numpy as np
from sklearn.neighbors import KernelDensity
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import LeaveOneOut
from scipy.stats import entropy
from scipy import stats
# https://stackoverflow.com/questions/63812970/scipy-gaussian-kde-matrix-is-not-positive-definite
# class GaussianKde(stats.gaussian_kde):
# """
# Drop-in replacement for gaussian_kde that adds the class attribute EPSILON
# to the covmat eigenvalues, to prevent exceptions due to numerical error.
# """
# EPSILON = 1e-10 # adjust this at will
# def _compute_covariance(self):
# """Computes the covariance matrix for each Gaussian kernel using
# covariance_factor().
# """
# self.factor = self.covariance_factor()
# # Cache covariance and inverse covariance of the data
# if not hasattr(self, '_data_inv_cov'):
# self._data_covariance = np.atleast_2d(np.cov(self.dataset, rowvar=1,
# bias=False,
# aweights=self.weights))
# # we're going the easy way here
# self._data_covariance += self.EPSILON * np.eye(
# len(self._data_covariance))
# self._data_inv_cov = np.linalg.inv(self._data_covariance)
# print('05')
# print(self._data_covariance)
# self.covariance = self._data_covariance * self.factor**2
# self.inv_cov = self._data_inv_cov / self.factor**2
# L = np.linalg.cholesky(self.covariance * 2 * np.pi)
# self._norm_factor = 2*np.log(np.diag(L)).sum() # needed for scipy 1.5.2
# self.log_det = 2*np.log(np.diag(L)).sum() # changed var name on 1.6.2
def KDE3V(x, y, z, bw_type="grid", plot="T"):
xyz = np.vstack([x, y, z])
if bw_type == "grid":
bandwidths = 10 ** np.linspace(-1, 1, 100)
grid = GridSearchCV(
KernelDensity(kernel="gaussian"),
{"bandwidth": bandwidths},
cv=LeaveOneOut(),
)
grid.fit(xyz.T)
bw = grid.best_params_["bandwidth"]
# instantiate and fit the KDE model
kde = KernelDensity(bandwidth=bw, kernel="gaussian")
kde.fit(xyz.T)
xmin = 0
xmax = 3
ymin = 0
ymax = 1
zmin = 0
zmax = 1
X, Y, Z = np.mgrid[xmin:xmax:100j, ymin:ymax:100j, zmin:zmax:100j]
positions = np.vstack([X.ravel(), Y.ravel(), Z.ravel()])
gdens = np.exp(kde.score_samples(positions.T))
elif bw_type == "silverman":
xmin = 0
xmax = 3
ymin = 0
ymax = 1
zmin = 0
zmax = 1
X, Y, Z = np.mgrid[xmin:xmax:100j, ymin:ymax:100j, zmin:zmax:100j]
positions = np.vstack([X.ravel(), Y.ravel(), Z.ravel()])
kde = stats.gaussian_kde(xyz)
kde.set_bandwidth(bw_method="scott")
gdens = kde(positions).T
else:
print("Wrong bw_type")
return gdens
# def KDE3V(x, y, z, bw_type = 'grid', plot='T'):
# xyz = np.vstack([x,y,z])
# if bw_type == 'grid':
# bandwidths = 10 ** np.linspace(-1, 1, 100)
# grid = GridSearchCV(KernelDensity(kernel='gaussian'),
# {'bandwidth': bandwidths},
# cv=LeaveOneOut())
# grid.fit(xyz.T)
# bw = grid.best_params_['bandwidth']
# elif bw_type == 'silverman':
# d = xyz.shape[0]
# n = xyz.shape[1]
# bw = (n * (d + 2) / 4.)**(-1. / (d + 4))
# else:
# print('Wrong bw_type')
# # instantiate and fit the KDE model
# kde = KernelDensity(bandwidth=bw, kernel='gaussian')
# kde.fit(xyz.T)
# # xmin = x.min()
# # xmax = x.max()
# # ymin = y.min()
# # ymax = y.max()
# # zmin = z.min()
# # zmax = z.max()
# xmin = 0
# xmax = 3
# ymin = 0
# ymax = 1
# zmin = 0
# zmax = 1
# X, Y, Z = np.mgrid[xmin:xmax:100j, ymin:ymax:100j, zmin:zmax:100j]
# positions = np.vstack([X.ravel(), Y.ravel(), Z.ravel()])
# gdens = np.exp(kde.score_samples(positions.T))
# return gdens
# 2 variables KLD
def KLD3V(gdens1, gdens2):
return entropy(pk=gdens1, qk=gdens2, base=2)
def KDE2V(x, y, bw_type="grid", plot="T"):
xy = np.vstack([x, y])
if bw_type == "grid":
bandwidths = 10 ** np.linspace(-1, 1, 100)
grid = GridSearchCV(
KernelDensity(kernel="gaussian"),
{"bandwidth": bandwidths},
cv=LeaveOneOut(),
)
grid.fit(xy.T)
bw = grid.best_params_["bandwidth"]
elif bw_type == "silverman":
d = xy.shape[0]
n = xy.shape[1]
bw = (n * (d + 2) / 4.0) ** (-1.0 / (d + 4))
else:
print("Wrong bw_type")
# instantiate and fit the KDE model
kde = KernelDensity(bandwidth=bw, kernel="gaussian")
kde.fit(xy.T)
# xmin = x.min()
# xmax = x.max()
# ymin = y.min()
# ymax = y.max()
# xmin = 0
# xmax = 3
# ymin = 0
# ymax = y.max()+5
xmin = 0
xmax = 3
ymin = 0
ymax = 1
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
gdens = np.exp(kde.score_samples(positions.T))
Z = np.reshape(np.exp(kde.score_samples(positions.T)), X.shape)
if plot == "T":
fig = plt.figure(figsize=(12, 10))
ax = fig.add_subplot(111)
ax.imshow(
np.rot90(Z), cmap=plt.get_cmap("viridis"), extent=[xmin, xmax, ymin, ymax]
)
ax.scatter(x, y, c="red", s=20, edgecolor="red")
# ax.set_aspect('auto')
plt.show()
else:
pass
return gdens
# 2 variables KLD
def KLD2V(gdens1, gdens2):
return entropy(pk=gdens1, qk=gdens2, base=2)
def KDE1V(x, variable_name, bw_type="grid", plot="T"):
if bw_type == "grid":
bandwidths = 10 ** np.linspace(-1, 1, 100)
grid = GridSearchCV(
KernelDensity(kernel="gaussian"),
{"bandwidth": bandwidths},
cv=LeaveOneOut(),
)
grid.fit(x[:, None])
bw = grid.best_params_["bandwidth"]
# instantiate and fit the KDE model
kde = KernelDensity(bandwidth=bw, kernel="gaussian")
kde.fit(x[:, None])
if variable_name == "AvgDeg":
xmin = 0
xmax = 3
if variable_name == "Ng1/N":
xmin = 0
xmax = 1
if variable_name == "Ng2/N":
xmin = 0
xmax = 1
X = np.mgrid[xmin:xmax:100j]
positions = np.vstack([X.ravel()])
gdens = np.exp(kde.score_samples(positions.T))
elif bw_type == "silverman":
if variable_name == "AvgDeg":
xmin = 0
xmax = 3
if variable_name == "Ng1/N":
xmin = 0
xmax = 1
if variable_name == "Ng2/N":
xmin = 0
xmax = 1
X = np.mgrid[xmin:xmax:100j]
positions = np.vstack([X.ravel()])
print("=====")
print(x.std())
kde = stats.gaussian_kde(x)
kde.set_bandwidth(bw_method="silverman")
gdens = kde(positions).T
else:
print("Wrong bw_type")
# if plot == 'T':
# fig = plt.figure(figsize=(12,10))
# ax = fig.add_subplot(111)
# ax.imshow(np.rot90(Z), cmap=plt.get_cmap('viridis'),
# extent=[xmin, xmax, ymin, ymax])
# ax.scatter(x, y, c='red', s=20, edgecolor='red')
# #ax.set_aspect('auto')
# plt.show()
# else:
# pass
return gdens
# 1 variables KLD
def KLD1V(gdens1, gdens2):
return entropy(pk=gdens1, qk=gdens2, base=2)