Matlab: generate random numbers from custom made probability density function

孤人 提交于 2019-12-01 13:46:17

A histogram will give you an estimate of the PDF -- just divide the bin counts by the total number of samples. From there you can estimate the CDF by integrating. Finally, you can choose a uniformly distributed random number between 0 and 1 and estimate the argument of the CDF that would yield that number. That is, if y is the random number you choose, then you want to find x such that CDF(x) = y. The value of x will be a random number with the desired PDF.

If you have 'Statistics and Machine Learning Toolbox', you can evaluate the PDF of the data with 'Kernel Distribution' method:

Percip_pd = fitdist(Percip,'Kernel');

Then use it for generating N random numbers from the same distribution:

y = random(Percip_pd,N,1);

Quoting @AnonSubmitter85:

"estimate the CDF by integrating. Finally, you can choose a uniformly distributed random number between 0 and 1 and estimate the argument of the CDF that would yield that number. That is, if y is the random number you choose, then you want to find x such that CDF(x) = y. The value of x will be a random number with the desired PDF."

%random sampling
 N=10; %number of resamples

 pdf = normrnd(0, 1, 1,100); %your pdf
 s = cumsum(pdf); %its cumulative distribution

 r = rand(N,1); %random numbers between 0 and 1
 for ii=1:N
   inds = find(s>r(ii));
   indeces(ii)=inds(1); %find first value greater than the random number
 end
 resamples = pdf(indeces) %the resamples
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!