what called this method of generation random sample
Show older comments
Hi all
is the code below represent inverse cdf method?
y is pdf of any distribution
cdf_y = cumsum(y);
sum_y = sum(y);
for j = 1:N
randx = sum_y*rand();
i = 1;
while cdf_y(i) < randx
i = i + 1;
end
f(j) = x(i); end
please give me explain
1 Comment
José-Luis
on 29 Aug 2014
Accepted Answer
More Answers (1)
Image Analyst
on 29 Aug 2014
0 votes
Not sure exactly what you mean and how specific your question is to that exact code, but in general the process of generating a bunch of random numbers and running an "experiment" N times (in a loop like you did) is called a "Monte Carlo Simulation". For example, check out the attached Monty Hall Problem that I coded up as a Monte Carlo simulation.
10 Comments
Image Analyst
on 29 Aug 2014
That would be inverse transform sampling, http://en.wikipedia.org/wiki/Inverse_transform_sampling. See my attached demo which works with a Rayleigh distribution.

mutah
on 29 Aug 2014
Image Analyst
on 29 Aug 2014
Edited: Image Analyst
on 29 Aug 2014
Well, sort of. I think that may be the intent of the author but they really messed up the code, and that's not even talking about the formatting. I did attach an example in my prior comment.
mutah
on 29 Aug 2014
Image Analyst
on 29 Aug 2014
Edited: Image Analyst
on 29 Aug 2014
It's a Monte Carlo simulation but the problem is that x is not defined. A skilled MATLAB programmer would also use find() rather than while.
mutah
on 29 Aug 2014
Edited: Image Analyst
on 29 Aug 2014
Image Analyst
on 29 Aug 2014
cumsum gives the cdf of y. For that x, the pdf if flat since it's a uniform distribution so the cdf will be a straight ramp. They use sum(y) to get the max value that the cdf could be. They just as well could have used cdf(end), because the cdf and pdf are not normalized and the sum of all y values is the number of x elements you have, which is 10,001.
Image Analyst
on 31 Aug 2014
Well, like I said, the while (and line before and two lines after) could be replaced:
i = find(cdf_y > randx, 1, 'first')
Categories
Find more on Inverse Gaussian Distribution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!