generate a random number base on pdf function

6 ビュー (過去 30 日間)
hong chenyao
hong chenyao 2022 年 6 月 25 日
コメント済み: hong chenyao 2022 年 7 月 5 日
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 件のコメント
John D'Errico
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 件のコメント
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 件のコメント
Karan Kannoujiya
Karan Kannoujiya 2022 年 6 月 25 日
Ohh sorry, thanks for clearing

サインインしてコメントする。


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