generate a random number base on pdf function

42 次查看(过去 30 天)
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 个评论
hong chenyao
hong chenyao 2022-6-25
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.
John D'Errico
John D'Errico 2022-6-25
编辑:John D'Errico 2022-6-25
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.

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2022-6-25
编辑:John D'Errico 2022-6-25
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 个评论
John D'Errico
John D'Errico 2022-7-4
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.
hong chenyao
hong chenyao 2022-7-5
I have asked my teather for the answer already,the methed is just like you said.thank you for your answering.

请先登录,再进行评论。

更多回答(3 个)

Karan Kannoujiya
Karan Kannoujiya 2022-6-25
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 个评论
John D'Errico
John D'Errico 2022-6-25
编辑:John D'Errico 2022-6-25
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.

请先登录,再进行评论。


Image Analyst
Image Analyst 2022-6-25
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.

Shivam Lahoti
Shivam Lahoti 2022-7-3
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

Community Treasure Hunt

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

Start Hunting!

Translated by