Custom Uniform Random Distribution

2 ビュー (過去 30 日間)
matlabkid602
matlabkid602 2018 年 2 月 23 日
回答済み: Roger Stafford 2018 年 2 月 24 日
Say I have a uniform random distribution in matlab using the rand() function.
How can I change the distribution for a given function such as 1/sqrt(x), so the distribution follows this curve?
I've tried to reject values but had no luck.
  1 件のコメント
John BG
John BG 2018 年 2 月 23 日
Hi Matlabkid602
1.
the main constraint is that whatever probability density function you choose, it must have area 1, integrating from -Inf to +Inf.
2.
Since you choose 1/sqrt(x), you have to truncate, let's say with a continuous interval [a b], a<b.
The new pdf should have the shape
pdf2(x<a)=0
pdf2(x>b)=0
pfg2(0<x<b)=1/sqrt(x)
3. make sure that for instance 0<b<a, otherwise and undesired discontinuity will be included in the function intended to behave as pdf and it will not work.
4.
The values of the interval [a b] are related in the following way:
sqrt(b)-sqrt(a)=0.5

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

回答 (2 件)

Image Analyst
Image Analyst 2018 年 2 月 23 日

Roger Stafford
Roger Stafford 2018 年 2 月 24 日
If you want to obtain a density distribution proportional to 1/sqrt(x) for x in some finite interval [a,b], you can proceed as follows. The cumulative probability function must be:
cdf(x) = (sqrt(x)-sqrt(a))/(sqrt(b)-sqrt(a))
which you get by integrating 1/sqrt(x) from a to x and adjusting the proportionality constant to get a cdf(b)=1 for the entire interval from a to b.
To generate this using rand, set the above cdf(x) to r = rand and solve for x:
r = rand;
%Solve for x in (sqrt(x)-sqrt(a))/(sqrt(b)-sqrt(a)) = r:
x = (sqrt(a)+(sqrt(b)-sqrt(a))*r).^2;
This illustrates how one would proceed to use rand for generating any given probability density function. It depends on being able to solve for x in an equation cdf(x) = r.

Community Treasure Hunt

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

Start Hunting!

Translated by