# How to generate random variable x for specific cdf Fx=1 - exp(- x.^2 / (2*sigma.^2)). for sigma.^2 = 1 and 3.

15 views (last 30 days)
Cengizhan on 31 Mar 2020
Commented: John D'Errico on 1 Apr 2020
I tried with randn but it comes to me meaningless. So it's not gaussian(normal) distribution beacuse question gives me cdf ,not pdf. for using icdf, again i have to determine what type distribution of it. i created the line using linspace but it's not what i want because it's not random variable. What can i do plot cdf for random vairable X, after that how can i find pdf analytically??

David Goodmanson on 1 Apr 2020
Hi Cenigzhan.
I assume that x is resticted to x>= 0. Take a look at 'inverse transform sampling' on wikipedia, where one of the examples is close to yours.
John D'Errico on 1 Apr 2020
Exactly. You don't need to know what type of random variable this is, because it does not appear to be of any standard type. You don't care.
The problem as stated is meaningless for negative x, but if we assume x lives in [0,inf), so x>= 0, then assume this is given as the CDF. That is, it has the desired properties:
1. At x==0, the CDF is 0.
2. As x--> inf, the CDF approaches 1 as a limit.
3. The supplied CDF is monotone increasing between those points.
Since the supplied function has a clear (and almost trivial) functional inverse for any value of the CDF between 0 and 1, your problem is simple. You have no need at all to know the PDF, because all the necessary magic lies in the CDF anyway. (Could you compute the PDF? Yes. It is just the derivative of the function shown as the CDF in your subject title. It still won't help you, since you already have exactly what you need.)
David gave you the right place to look. Try it. Make an effort - you might be surprised at how easy this one is.
Cengizhan on 1 Apr 2020
Thanks for help. Actually no need to know what type of X, i see. I made this question difficult in my brain :))) i handled

Ameer Hamza on 1 Apr 2020
Edited: Ameer Hamza on 1 Apr 2020
An interesting approach is suggested by David, this turn out to be quite easy.
sigma = 1;
F = @(x) 1 - exp(-x.^2/(2*sigma^2)); % your CDF
F_inv = @(u) sqrt(-2*sigma.^2.*log(1-u)); % inverse of CDF
x = rand(10000,1); % uniform random numbers
x_F = F_inv(x); % random numbers according to your CDF
Now to check if this is correct.
[count, bins] = histcounts(x_F);
locs = (bins(1:end-1) + bins(2:end))/2;
simulated_cdf = normalize(cumsum(count), 'range');
actual_cdf = F(locs);
plot(locs, simulated_cdf, 'r', locs, actual_cdf, 'b--')
legend({'Simulated', 'Actual'}, 'FontSize', 14) Cengizhan on 1 Apr 2020
Thanks but i wonder why you choose uniformly random numbers? And sigma is not determined correctly i think.
Ameer Hamza on 1 Apr 2020
Sorry, I misread that 2*sigma^2=1. Now I have corrected it. I used the inverse transform sampling method which converts a uniformly distributed random numbers to any distribution with a given CDF.
John D'Errico on 1 Apr 2020
What are you talking about? Sigma has no need to be determined. The question clearly stated to do this twice, once for sigma^2=1, and once for sigma^2=3. The case where sigma^2=1 just happens to be the same as sigma=1, So, yes, sigma is determined correctly.
As far as the use of uniformly distributed numbers, do some reading. You will learn that this is how the inverse CDF method works. David gave you the place to look.