# Optimal approximation of nonlinear probability density function by piecewise constant density

Given a nonlinear probability density function F, the problem is to estimate F using histogram over a partition with N intervals.

I have tried to realise this with MATLAB function fmincon, but it seems to always generate local minimum. The optimisation function is the minimisation of the difference between F and the estimated result. As can be seen from the following figure, the estimation cannot be accepted. How can the optimal result be obtained?

## 1 Answer

There are histogram estimators in Matlab's "histogram" function, which do some automatic selection of bins (this is the recommended replacement for hist/histc): https://www.mathworks.com/help/matlab/creating_plots/replace-discouraged-instances-of-hist-and-histc.html

You should probably use these rather than rolling out your own histogram estimator.

Alternatively, if you just search for "Histogram bin selection" or something similar", you'll find papers you can follow like Wand, M. P. "Data-based choice of histogram bin width." The American Statistician 51.1 (1997): 59-64.

Most of these will try to be good with respect to mean integrated square error (i.e. the integrated square difference of the histogram pdf and the true pdf). Whether or not this matters rather than the usual (range of data)/(1+log_2 n) rule in practice being good enoguh is another matter.

You can also look in Wasserman's All of Statistics chapter on nonparametric curve estimation for another treatment of selection of bin widths.