# Generate random numbers from a probability density estimate

8 views (last 30 days)
Andreas Palmgren on 8 Feb 2021
Answered: Asvin Kumar on 15 Mar 2021
My problem is that I have data (2D coordinates) which I would like to generate more of. To solve my problem, I wanted to do a kernal density estimate and use it to both plot a heatmap and generate more data (which would generate data, which itself should create a similar heatmap).
I have a probability density estimate f which was returned from ksdensity(). The code I used to get f was
[f, xi, bw] = ksdensity(data, [X(:) Y(:)]);
where [X(:) Y(:)] was used to create my own grid covering my data. The data itself is a 2-column matrix, with each row representing a x- and y-coordinate.
f is a 20000x1 vector, which I could reshape and create a heatmap from. What I would like to do instead is using f somehow to generate new values from the probability density estimate which has been made over my data.
Do I need to use the bandwidth bw somehow?
Thank you for the help!

Asvin Kumar on 15 Mar 2021
By heatmap, if you're looking to plot the estimated density function, you can plot that using f and xi. Here's a good example that illustrates that: Plot kernel density estimate of bivariate data
If you want to sample the estimated density function at points of your interest, you can use the second input argument pts. pts is basically the list of locations at which you want to query the estimated function's value. Here's a detailed example on using the pts input: Estimate CDF at specific values.
The output xi will match the input pts when it is specified. When pts is not specified, xi will take default values as mentioned in its description here.