Given a histogram obtained using given data points, how do I randomly sample from the distribution predicted by the histogram?
Any conceptual comment / R code would be welcome.
density functionhistogramrsampling
Given a histogram obtained using given data points, how do I randomly sample from the distribution predicted by the histogram?
Any conceptual comment / R code would be welcome.
Best Answer
Since the sampling from a kernel density estimate is solved once or twice already, I'll focus on sampling from a histogram-as-population-pdf.
The idea is simply
For example in R:
Just for completeness, (since sampling from the kernel density estimate* is very simple):
* note that some kernels - like fourth order kernels - are not densities and this assumes that the kernel is a density
In R, for a Gaussian kernel and bandwidth h, with data in x: