def _compute_smoothed_histogram(values, bandwidth, coord_range,
logtrans=False):
"""Approximate 1-D density estimation.
Estimate 1-D probability densities at evenly-spaced grid points,
for specified data. This method is based on creating a 1-D histogram of
data points quantised with respect to evenly-spaced grid points.
Probability densities are then estimated at the grid points by convolving
the obtained histogram with a Gaussian kernel.
Parameters
----------
values : np.array (N,)
A vector containing the data for which to perform density estimation.
Successive data points are indexed by the first axis in the array.
bandwidth : float
The desired KDE bandwidth. (When log-transformation
of data is desired, bandwidth should be specified in log-space.)
coord_range: (2,)
Minimum and maximum values of coordinate on which to evaluate the
smoothed histogram.
logtrans : boolean
Whether or not to log-transform the data before performing density
estimation.
Returns
-------
np.array (M-1,)
An array of estimated probability densities at specified grid points.
"""
if logtrans:
ber = [np.log10(extreme) for extreme in coord_range]
bin_edges = np.logspace(*ber, num=DENSITY_N + 1)
bin_edge_range = ber[1] - ber[0]
else:
bin_edges = np.linspace(*coord_range, num=DENSITY_N + 1)
bin_edge_range = coord_range[1] - coord_range[0]
if values.size < 2:
# Return zeros if there are too few points to do anything useful.
return bin_edges[:-1], np.zeros(bin_edges.shape[0] - 1)
# Bin the values
H = np.histogram(values, bin_edges)[0]
relative_bw = bandwidth / bin_edge_range
K = _compute_gaussian_kernel(H.shape, relative_bw)
pdf = signal.fftconvolve(H, K, mode='same')
# Return lower edges of bins and normalized pdf
return bin_edges[:-1], pdf / np.trapz(pdf, bin_edges[:-1])
评论列表
文章目录