def maximize(samples):
'''
Use Kernel Density Estimation to find a continuous PDF
from the discrete sampling. Maximize that distribution.
Parameters
----------
samples : list (shape = (nsamples, ndim))
Observations drawn from the distribution which is
going to be fit.
Returns
-------
maximum : list(ndim)
Maxima of the probability distributions along each axis.
'''
from scipy.optimize import minimize
from scipy.stats import gaussian_kde as kde
# Give the samples array the proper shape.
samples = np.transpose(samples)
# Estimate the continous PDF.
estimate = kde(samples)
# Take the minimum of the estimate.
def PDF(x):
return -estimate(x)
# Initial guess on maximum is made from 50th percentile of the sample.
p0 = [np.percentile(samples[i], 50) for i in range(samples.shape[0])]
# Calculate the maximum of the distribution.
maximum = minimize(PDF, p0).x
return maximum
评论列表
文章目录