generate a random number base on pdf function

I basically have the following PDF respectivley:
f(x)=1/2*x+0.5
range is -1 to 1
I would like to generate random numbers on matlab based on these equations.
thanks for your help.

3 Comments

Stop posting the same homework question miltiple times.
Note that having answered your question, IF you make an effort, (perhaps show what you tried as a comment on my answer) I might be willing to help you more if I see it as a serious effort.
This is a probability class's homework,not a matlab class' homework,i don't even use the matlab.I just try my best to get the answer.
It does not matter if it is MATLAB homework, or homework for some other class. IT IS HOMEWORK. It is YOUR HOMEWORK. It was assigned to you. We are not a homework solving service. You have made no attempt to do your homework.
How about this as an option. Ask your teacher to contact me directly. Have them give me their direct agreement that it is OK if I do your homework assignments for you. I'll send the completed homework directly to your teacher. Of course, then I'll be the one who gets credit. Can I get yet another degree? Maybe. Do I want one? Nope.

Sign in to comment.

 Accepted Answer

First, is that the PDF of a random variable? If it was, the integral would be 1.
syms x
P_x = x/2 + 1/2;
int(P_x,-1,1)
ans = 
1
And of course, P_x is always positive on that domain. So indeed, this has the necessary properties of a PDF.
Now, assuming this is not homework... Sadly, I wonder if it is homework. This has all the hallmarks of a homework probem. You are a new user, who has never asked a question here before. This is a fairly basic question, and the given PDF is such a nicely posed one. Essentially, it is too basic a question, with a perfectly posed question. Yep, I'd bet a lot this is just homework, with no effort shown.
Oh well, having started to write this, and since it MAY possibly not be homework, here is what you do:
First compute the CDF. That is just the integral of P_x, represented as a function of x. Hint:
int(P_x,[-1,x])
ans = 
I would test it. Does that have the desired properties as a CDF?
Next, you generate a random number in the interval [0,1]. Call it r.
Finally, compute the inverse of the CDF of the value r, thus solve for x, such that CDF(x) == r.
x as generated will have the desired triangular distribution.
You need to do the rest.

10 Comments

I don't understand what means to compute the inverse of the CDF of the value r, thus solve for x, such that CDF(x) == r. I put the pdf graph in attachment. please have a look.
Ah. I was pretty sure this was indeed a homework assignment. But then sorry, you need to make an effort. I gave you pretty clear directions, but you may want to read the link shown by Torsten.
i have calculated that the inverse of cdf is x(x+2)=y
You may have computed that, but you would be completely wrong. (Ok, I'll concede that you have actually computed the CDF on the left hand side of that expression. But you have not computed the inverse relation. You need to solve that for x, as a function of y.)
As a hint, I did give you the CDF. My computation would look something like this:
syms x
P_x = x/2 + 1/2;
CDF_x = int(P_x,-1,x)
syms r
solve(CDF_x == r,x)
though I have not finished the necessary steps. You still need to make some effort.
i find that x=√(4+1)-1,what's next step?
thanks for your help.
i have test my calculate,it still have a problem,i put the result in attachment,please give a help.
Is my well documented example (attached to my Answer below) so impenetrable that you can't figure out how to change this one line of code to use your formula?
% rayleighCDF = 1 - exp(-x.^2 / (2*sigma^2));
rayleighDistNumbers = sqrt(-log(1-uniformlyDistributedRandomNumbers)*(2*sigma^2));\
It has now been a few days. And I think you have probably turned in the assignment. So how would I solve the problem?
It is important to understand is the difference between a CDF and A PDF, but I am not teaching a class on probability and statistics here.
syms x
P_x = x/2 + 1/2;
CDF_x = int(P_x,-1,x)
CDF_x = 
syms r
solve(CDF_x == r,x)
ans = 
fplot(CDF_x,[-1,1])
grid on
xlabel X
ylabel 'P(x <= X)'
So here we see the x axis is labeled as X, and the CDF tells us the probability that if we sample a random variable with this distribution, what is the probability that X will be no larger than 0, for example? Reading off the plot, we see that probability is just about 0.25.
Anyway, we use the CDF by inverting random samples through that function. Thus, sample a random number, uniform on the interval [0,1]. Call that number r. Now solve for x, such that the CDF gave us exactly r. If we do that, this inverse transformation will result in a variable with the desired probability distributino. Lets try it!
n = 1e7; % a BIG sample size to make things look good.
r = rand(1,n);
x = 2*sqrt(r) - 1; % taken from the solve above...
histogram(x,250,'norm','pdf')
Do you see this has worked nicely, and given us a nearly perfect approximation to the desired triangular PDF? The large sample size helped in that respect of course.
So all we needed to do was to follow a few steps.
  1. Compute the CDF.
  2. Generate uniform samples on [0,1].
  3. Invert that uniform set through the CDF.
  4. Done.
I have asked my teather for the answer already,the methed is just like you said.thank you for your answering.

Sign in to comment.

More Answers (3)

Hi hong,
so your range is between -1 to 1 in that case your domain will be between -3 to 1
if you want to generate N random number between two number 'a' and 'b' then you can use the below syntax
In your case a=-3 and b=1
r = a + (b-a) .* rand(N,1)

2 Comments

NO NO NO. You told the @hong chenyao how to generate a UNIFORM random number on a different interval. That was not remotely the question asked.
Ohh sorry, thanks for clearing

Sign in to comment.

You need to do "inverse transform sampling" so you need the CDF, as the esteemed @John D'Errico said.
I'm attaching an example I worked up for drawing samples from a Rayleigh distribution. Adapt as needed.
In general, you basically compute the CDF of your PDF function and invert it. Go here for a generally applicable explanation of how to do it: http://en.wikipedia.org/wiki/Inverse_transform_sampling

Categories

Community Treasure Hunt

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

Start Hunting!