Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have a simulation that produces a single floating point number as a result, and a different number is produced each time the simulation runs. These numbers are randomly distributed according to some unknown distribution. I would like to approximate the underlying density distribution function based on a finite sample of numbers.

I know that I could use a histogram, but the size of the bins, which is essentially arbitrary, will significantly affect the results. I would like a way to do this which has no "arbitrariness".

Any ideas?

share|improve this question
Try asking on – Spacedman Dec 7 '11 at 10:46
up vote 6 down vote accepted

Since a distribution function contains more information than the finite set of numbers you start with, you clearly have to add information in the process. This information comes in the form of a model that you assume, and whose parameters you adjust to make the model fit your finite sample. Unless the nature of your problem suggests some model, the choice will always be arbitrary to some extent.

With a histgram, your model is that of a piecewise constant function with the pieces (bins) having a fixed width. With Kernel Density Estimation, your model is the kernel. There is no way around choosing a model, the best you can do is to make an informed choice based on what you know (or can reasonably assume) about your data.

share|improve this answer
I think this answer really catches the essence of the problem, which is more important than any specific method one could suggest. Thank you! – Joe Dec 8 '11 at 11:13

Kernel density estimation is a good suggestion. Another option is to construct the empirical CDF and then seek the distribution that best fits. Depending on the form of your output this might be more appropriate than a KDE, which is typically used for multimodal distributions. More detail on the empirical CDF here:

Once you form the empirical CDF you can use a goodness of fit metric ( or maximum likelihood estimation ( to see which distribution type and parameter values best fit your data. The Pearson distribution is sometimes used in these situations if you have no good reason to assume the data follows one of the usual suspects due to its flexibility (

Lastly, as you noted, you only have finite data. It can be important to understand the uncertainty in the best fit given the limited data, which can be done via Bayesian inference ( Properly implemented, this can account for both your model selection and the best-fit parameters given that model.

share|improve this answer

What you are looking for is Kernel Density Estimation.

Algorithms to carry this out are built into many scientific software packages and libraries. There is still a degree of arbitrariness in the choice of kernel and bandwidth, but there are heuristics for these.

share|improve this answer

I think you also need to decide what you are going to do with this distribution when you've got it. If you think you can sample from it instead of running your simulation to get random variates from your process then you then have to ask yourself some more questions about how important accuracy is, and how variations in your approximation to the true density will affect the outcome of sampling from an approximated density versus getting samples via the process...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.