How to Generate random sampling from a probability distribution function generated by kernel density estimation?

Hi all,
I generate the PDF using ksdensity function matlab. Now I want to genrate random samples for the Monte Carlo simulation from that PDF, Anybody has an idea how to generate these random samples?
Many thanks in advance
MAnsour

Answers (1)

Either you do something like this (Untested-unverified-off-the-cuff^TM):
% Assuming pdfKD values at x:
cdfKD = cumtrapz(x,pdfKD);
cdfKD = (cdfKD - cdfKD(1))/(cdfKD(end)-cdfKD(1)); % normalizing the cdf to be between zero and 1 at the end of your intervall
Q = rand(n);
randVals = interp1(cdfKD,x,Q,'pchip-or-linear');
Or you take a look at the file exchange contributions that solves just this task:
HTH

Asked:

on 31 May 2019

Answered:

on 31 May 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!