-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKDEstimator.py
366 lines (321 loc) · 17.7 KB
/
KDEstimator.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import numbers
import numpy as np
from scipy.stats import gaussian_kde
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KernelDensity
from sklearn.neighbors import BallTree
from sklearn.neighbors import KDTree
from statsmodels.nonparametric.kde import KDEUnivariate
from statsmodels.nonparametric.kernel_density import KDEMultivariate
class KernelDensityEstimator(object):
__Likelyhood_Estimation_Functions__ = {}
__Cumulative_Estimation_Functions__ = {}
@staticmethod
def Init():
KernelDensityEstimator.__Likelyhood_Estimation_Functions__ = {0: KernelDensityEstimator.kde_scipy,
1: KernelDensityEstimator.kde_statsmodels_u,
2: KernelDensityEstimator.kde_statsmodels_m,
3: KernelDensityEstimator.kde_scikitlearn}
KernelDensityEstimator.__Cumulative_Estimation_Functions__ = {0: KernelDensityEstimator.kce_scipy,
1: KernelDensityEstimator.kce_statsmodels_u,
2: KernelDensityEstimator.kce_statsmodels_m,
3: KernelDensityEstimator.kce_scikitlearn}
KernelDensityEstimator.__Explore_Estimation_Functions = {0: KernelDensityEstimator.kde_explore_scipy,
1: KernelDensityEstimator.kde_explore_statsmodels_u,
2: KernelDensityEstimator.kde_explore_statsmodels_m,
3: KernelDensityEstimator.kde_explore_scikitlearn}
@staticmethod
def bandwidth_estimation(data, estimate_from, estimate_to, nspace, nfold):
grid = GridSearchCV(KernelDensity(), {'bandwidth': np.linspace(estimate_from, estimate_to, nspace)},
cv=nfold) # 20-fold cross-validation
grid.fit(np.array(data).reshape(-1, 1))
return grid.best_params_['bandwidth']
@staticmethod
def kde_explore_scipy(data, data_grid, bandwidth_from=0, bandwidth_to=100, nspace=10,
nfold=20, weigths=None, **kwargs):
"""Kernel Density Estimation with Scipy"""
# Note that scipy weights its bandwidth by the covariance of the
# input data. To make the results comparable to the other methods,
# we divide the bandwidth by the sample standard deviation here.
bw_methods = ["scott", "silverman"]
pdfs = []
bws = []
params = []
W = 5
gain = (bandwidth_to - bandwidth_from) / W
for i in range(W):
bandwidth = KernelDensityEstimator.bandwidth_estimation(data=data,
estimate_from=i * gain,
estimate_to=(i + 1) * gain,
nspace=nspace, nfold=nfold)
bw_methods.append([bandwidth])
for bw_method in bw_methods:
if isinstance(bw_method, int):
bw_method = bw_method / data.std(ddof=1)
elif isinstance(bw_method, str) or callable(bw_method):
bw_method = bw_method
else:
bw_method = None
try:
kde = gaussian_kde(data, bw_method=bw_method, weights=weigths, **kwargs)
pdfs.append(list(kde.evaluate(data_grid)))
except:
continue
bws.append(bandwidth)
param = {}
param['covariance'] = kde.covariance
param['inv_cov'] = kde.inv_cov
param['covariance_factor'] = kde.covariance_factor
param['factor'] = kde.factor
param['silverman_factor'] = kde.silverman_factor()
param['scotts_factor'] = kde.scotts_factor()
params.append(param)
return pdfs, bws, params
@staticmethod
def kde_scipy(data, data_grid, bandwidth, weigths=None, **kwargs):
"""Kernel Density Estimation with Scipy"""
# Note that scipy weights its bandwidth by the covariance of the
# input data. To make the results comparable to the other methods,
# we divide the bandwidth by the sample standard deviation here.
if isinstance(bandwidth, int):
bw_method = bandwidth / data.std(ddof=1)
elif isinstance(bandwidth, str):
bw_method = bandwidth
else:
bw_method = None
kde = gaussian_kde(data, bw_method=bw_method, weights=weigths, **kwargs)
return kde.evaluate(data_grid), bandwidth
@staticmethod
def kde_explore_statsmodels_u(data, data_grid, bandwidth_from=0, bandwidth_to=100,
nspace=10,
nfold=20, weigths=None, gridsize=None, adjust=1, cut=3, **kwargs):
"""Kernel Density Estimation with Scipy"""
# Note that scipy weights its bandwidth by the covariance of the
# input data. To make the results comparable to the other methods,
# we divide the bandwidth by the sample standard deviation here.
bw_methods = ["scott", "silverman", "normal_reference"]
kernels = ['biw', 'cos', 'epa', 'gau', 'tri', 'triw', 'uni']
pdfs = []
cdfs = []
bws = []
sfs = []
cumhazards = []
params = []
W = 5
gain = (bandwidth_to - bandwidth_from) / W
for i in range(W):
bandwidth = KernelDensityEstimator.bandwidth_estimation(data=data,
estimate_from=i * gain,
estimate_to=(i + 1) * gain,
nspace=nspace, nfold=nfold)
bw_methods.append([bandwidth])
for kernel in kernels:
for bw_method in bw_methods:
if isinstance(bw_method, int) or isinstance(bw_method, float):
bw_method = bw_method / np.std(data, ddof=1)
elif isinstance(bw_method, str) or callable(bw_method):
bw_method = bw_method
else:
bw_method = None
try:
kde = KDEUnivariate(data)
kde.fit(weights=weigths, fft=(kernel == "gau"), kernel=kernel,
bw=bw_method, gridsize=gridsize, adjust=adjust, cut=cut, **kwargs)
except:
continue
pdfs.append(kde.evaluate(data_grid))
cdfs.append(kde.cdf)
bws.append(kde.bw)
cumhazards.append(kde.cumhazard)
sfs.append(kde.sf)
param = {}
param['support'] = kde.support
param['endog'] = kde.endog
param['density'] = kde.density
param['kernel'] = kde.kernel
#param['entropy'] = kde.entropy
param['fft'] = kernel == "gau"
params.append(param)
return pdfs, cdfs, bws, cumhazards, sfs, params
@staticmethod
def kde_statsmodels_u(data, data_grid, bandwidth, gridsize=None, adjust=1, cut=3, weigths=None, fft=True,
kernel='gau', **kwargs):
"""Univariate Kernel Density Estimation with Statsmodels"""
kde = KDEUnivariate(data)
kde.fit(weights=weigths, fft=fft, kernel=kernel, bw=bandwidth, gridsize=gridsize, adjust=adjust, cut=cut,
**kwargs)
return kde.evaluate(data_grid), kde.bw
@staticmethod
def kde_explore_statsmodels_m(data, data_grid, bandwidth_from=0, bandwidth_to=100,
nspace=10,
nfold=20, defaults=0, **kwargs):
bw_methods = ["cv_ls", "cv_ml", "normal_reference"]
variable_types = ['c', 'u', 'o']
W = 5
gain = (bandwidth_to - bandwidth_from) / W
for i in range(W):
bandwidth = KernelDensityEstimator.bandwidth_estimation(data=data,
estimate_from=i * gain,
estimate_to=(i + 1) * gain,
nspace=nspace, nfold=nfold)
bw_methods.append([bandwidth])
pdfs = []
cdfs = []
loo_likelyhoods = []
params = []
bws = []
for variable_type in variable_types:
for bw_method in bw_methods:
try:
kde = KDEMultivariate(data=[data], bw=bw_method, var_type=variable_type, **kwargs)
pdfs.append(kde.pdf(data_grid))
cdfs.append(kde.cdf(data_grid))
loo_likelyhoods.append(kde.loo_likelihood(kde.bw))
except:
continue
bws.append(kde.bw)
param = {}
param['data_type'] = kde.data_type
param['efficient'] = kde.efficient
param['k_vars'] = kde.k_vars
param['nobs'] = kde.nobs
param['n_jobs'] = kde.n_jobs
param['n_sub'] = kde.n_sub
param['n_res'] = kde.n_res
param['var_type'] = kde.var_type
param['return_median'] = kde.return_median
param['imse'] = kde.imse(kde.bw)
params.append(param)
return pdfs, cdfs, bws, loo_likelyhoods, params
@staticmethod
def kde_statsmodels_m(data, data_grid, bandwidth, defaults=0, variable_type='c', **kwargs):
"""Multivariate Kernel Density Estimation with Statsmodels"""
if isinstance(bandwidth, int) or isinstance(bandwidth, float):
bandwidth = 'cv_ml'
kde = KDEMultivariate(data=[data], bw=bandwidth, var_type=variable_type, **kwargs)
return kde.pdf(data_grid), kde.bw
@staticmethod
def kde_explore_scikitlearn(data, data_grid, bandwidth_from=0, bandwidth_to=100,
nspace=10,
nfold=20, defaults=0, rtol=0, atol=0, **kwargs):
"""Kernel Density Estimation with Scipy"""
# Note that scipy weights its bandwidth by the covariance of the
# input data. To make the results comparable to the other methods,
# we divide the bandwidth by the sample standard deviation here.
W = 10
bandwidths = []
gain = (bandwidth_to - bandwidth_from) / W
for i in range(W):
bandwidths.append(KernelDensityEstimator.bandwidth_estimation(data=data,
estimate_from=i * gain,
estimate_to=(i + 1) * gain,
nspace=nspace, nfold=nfold))
bw_methods = list([b for b in bandwidths])
kernels = ['gaussian', 'tophat', 'epanechnikov', 'exponential', 'linear', 'cosine']
algorithms = ["ball_tree", "kd_tree", "auto"]
metrics = []
for metric in BallTree.valid_metrics:
metrics.append(metric)
for metric in KDTree.valid_metrics:
metrics.append(metric)
metrics = np.unique(metrics)
pdfs = []
bws = []
log_likelyhoods = []
params = []
for metric in metrics:
for algorithm in algorithms:
for kernel in kernels:
for bw_method in bw_methods:
if not isinstance(bw_method, numbers.Number):
bw_method = KernelDensityEstimator.bandwidth_estimation(data=data,
estimate_from=bandwidth_from + (i * gain),
estimate_to=bandwidth_to + ((i + 1) * gain),
nspace=nspace, nfold=nfold)
try:
kde = KernelDensity(rtol=rtol, atol=atol, bandwidth=bw_method, metric=metric,
algorithm=algorithm,
kernel=kernel, **kwargs)
kde.fit(data.reshape(-1,1))
# score_samples() returns the log-likelihood of the samples
log_pdf = kde.score_samples(data_grid.reshape(-1, 1))
except:
continue
pdfs.append(np.exp(log_pdf))
bws.append(kde.bandwidth)
log_likelyhoods.append(kde.score(data_grid.reshape(-1, 1)))
param = {}
param['algorithm'] = kde.algorithm
param['kernel'] = kde.kernel
param['atol'] = kde.atol
param['rtol'] = kde.rtol
param['breadth_first'] = kde.breadth_first
param['feature_names_in_'] = kde.n_features_in_
param['leaf_size'] = kde.leaf_size
param['tree_'] = kde.tree_
param['metric'] = kde.metric
params.append(param)
return pdfs, bws, log_likelyhoods, params
@staticmethod
def kde_scikitlearn(data, data_grid, bandwidth, rtol=0, atol=0, metric='euclidean', kernel='gaussian',
algorithm='auto', **kwargs):
"""Kernel Density Estimation with Scikit-learn"""
kde = KernelDensity(rtol=rtol, atol=atol, bandwidth=bandwidth, metric=metric, algorithm=algorithm,
kernel=kernel, **kwargs)
kde.fit(np.array(data).reshape(-1, 1))
# score_samples() returns the log-likelihood of the samples
log_pdf = kde.score_samples(data_grid[:, np.newaxis])
return np.exp(log_pdf), kde.bandwidth
@staticmethod
def kde_best_scikitlearn(data, data_grid, bandwidth, metric='euclidean', kernel='gaussian', algorithm='auto',
bandwidth_estimate_from=0, bandwidth_estimate_to=100, nspace=100):
"""Best Kernel Density Estimation with Scikit-learn"""
grid = GridSearchCV(KernelDensity(bandwidth=bandwidth, metric=metric, algorithm=algorithm, kernel=kernel),
{'bandwidth': np.logspace(bandwidth_estimate_from, bandwidth_estimate_to, nspace)})
grid.estimator.fit(np.array(data).reshape(-1, 1))
# score_samples() returns the log-likelihood of the samples
log_pdf = grid.estimator.score_samples(data_grid[:, np.newaxis])
return np.exp(log_pdf), grid.estimator.__getattribute__('bandwidth'), grid.estimator
@staticmethod
def kce_statsmodels_u(data, data_grid, bandwidth, weigths=None, fft=True, kernel='gau', **kwargs):
"""Univariate Kernel Cumulative Density Estimation with Statsmodels"""
kce = KDEUnivariate(data)
kce.fit(weights=weigths, fft=fft, kernel=kernel, bw=bandwidth, **kwargs)
return kce.cdf, kce.bw
@staticmethod
def kce_statsmodels_m(data, data_grid, bandwidth, variable_type='c', **kwargs):
"""Multivariate Kernel Cumulative Density Estimation with Statsmodels"""
if isinstance(bandwidth, int) or isinstance(bandwidth, float):
bandwidth = 'cv_ml'
kce = KDEMultivariate(data=[data], bw=bandwidth, var_type=variable_type, **kwargs)
return kce.cdf(data_grid), kce.bw
@staticmethod
def kce_scipy(data, data_grid, bandwidth, **kwargs):
return [], bandwidth
@staticmethod
def kce_scikitlearn(data, data_grid, bandwidth, **kwargs):
return [], bandwidth
@staticmethod
def pdf_estimation(data, data_grid, method, bandwidth, band_est_enable, bandwidth_estimate_from=0,
bandwidth_estimate_to=100, nspace=100,
nfold=20, **kwargs):
if isinstance(band_est_enable, bool) and band_est_enable == True:
bandwidth = KernelDensityEstimator.bandwidth_estimation(data, estimate_from=bandwidth_estimate_from,
estimate_to=bandwidth_estimate_to, nspace=nspace,
nfold=nfold)
return KernelDensityEstimator.__Likelyhood_Estimation_Functions__[method](data, data_grid, bandwidth, **kwargs)
@staticmethod
def cdf_estimation(data, data_grid, method, bandwidth, band_est_enable, bandwidth_estimate_from=0,
bandwidth_estimate_to=100, nspace=100,
nfold=20, **kwargs):
if isinstance(band_est_enable, bool) and band_est_enable == True:
bandwidth = KernelDensityEstimator.bandwidth_estimation(data, estimate_from=bandwidth_estimate_from,
estimate_to=bandwidth_estimate_to, nspace=nspace,
nfold=nfold)
return KernelDensityEstimator.__Cumulative_Estimation_Functions__[method](data, data_grid, bandwidth, **kwargs)
@staticmethod
def get_pdf_estimation_method(method):
return str(KernelDensityEstimator.__Likelyhood_Estimation_Functions__[method].__name__)
@staticmethod
def get_cdf_estimation_method(method):
return str(KernelDensityEstimator.__Cumulative_Estimation_Functions__[method].__name__)