Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help with Random Variable Generation

Subject: Help with Random Variable Generation

From: Omega69 Omega69

Date: 16 Jan, 2009 17:44:01

Message: 1 of 7

Hi everybody,
I'm trying to generate a random variable which probability density function is:
f(x)=1-|x|, -1<=x<=1
To do that, I have calculated cumulative distribution function, which is (if I have not made any mistake...):
F(x)= 0 if x<-1
          x?/2+x+1/2 if -1<=x<=0
         -x?/2+x+1/2 if 0<=x<=-1
          1 if x>1

Now, I declare u=F(x) (where u is a uniform variable), and I try to isolate x to get F^-1(u), and then declare them in Matlab, but here is where I have problems:

1) I know how to isolate x when -1<=x<=0, and I get x=sqrt(2*u)-1, but I don't know how to isolate it when 0<=x<=-1, any idea for this?

2) And the other part is that I don't know how to write this into Matlab. I have try this:
N=1000;
u1=rand(N,1);
u2=rand(N,2)
X=(sqrt(2*u1)-1)*(u2(N,1)<0,5)+((the thing I don't know how to isolate))*(u2>0.5);

But the results I think that are wrong.

Anyone has any idea to get this correct?

Thank you very much in advance!!

Subject: Help with Random Variable Generation

From: Sadik

Date: 16 Jan, 2009 19:00:19

Message: 2 of 7

"Omega69 Omega69" <Omega69@gmail.com> wrote in message <gkqh11$osq$1@fred.mathworks.com>...
> Hi everybody,
> I'm trying to generate a random variable which probability density function is:
> f(x)=1-|x|, -1<=x<=1
> To do that, I have calculated cumulative distribution function, which is (if I have not made any mistake...):
> F(x)= 0 if x<-1
> x?/2+x+1/2 if -1<=x<=0
> -x?/2+x+1/2 if 0<=x<=-1
> 1 if x>1
>
> Now, I declare u=F(x) (where u is a uniform variable), and I try to isolate x to get F^-1(u), and then declare them in Matlab, but here is where I have problems:
>
> 1) I know how to isolate x when -1<=x<=0, and I get x=sqrt(2*u)-1, but I don't know how to isolate it when 0<=x<=-1, any idea for this?
>
> 2) And the other part is that I don't know how to write this into Matlab. I have try this:
> N=1000;
> u1=rand(N,1);
> u2=rand(N,2)
> X=(sqrt(2*u1)-1)*(u2(N,1)<0,5)+((the thing I don't know how to isolate))*(u2>0.5);
>
> But the results I think that are wrong.
>
> Anyone has any idea to get this correct?
>
> Thank you very much in advance!!

Hello Omega69,

1) You have already found the function so the inverse for the positive part will be

x = 1 - sqrt(2*(1-y))

This is because x-1 will be negative in the range 0 < x < 1. So you choose the negative square root.

2) Your inverse is one-to-one. So you don't need to generate a second uniform random variable. Your code should be

N=1000;
u1=rand(N,1);

X=(sqrt(2*u1)-1).*(u1<0.5)+(1 - sqrt(2*(1-u1))).*(u1>=0.5);

It seems to work. Hope this helps.

Subject: Help with Random Variable Generation

From: Roger Stafford

Date: 16 Jan, 2009 20:49:02

Message: 3 of 7

"Omega69 Omega69" <Omega69@gmail.com> wrote in message <gkqh11$osq$1@fred.mathworks.com>...
> I'm trying to generate a random variable which probability density function is:
> f(x)=1-|x|, -1<=x<=1
> ......

 x = sum(rand(1,2))-1;

Roger Stafford

Subject: Help with Random Variable Generation

From: Bruno Luong

Date: 16 Jan, 2009 21:09:02

Message: 4 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gkqrru$fht$1@fred.mathworks.com>...

>
> x = sum(rand(1,2))-1;
>

Clever!

Bruno

Subject: Help with Random Variable Generation

From: Bruno Luong

Date: 16 Jan, 2009 21:12:01

Message: 5 of 7

Inspired by Roger!

diff(rand(2,1));

Bruno

Subject: Help with Random Variable Generation

From: Roger Stafford

Date: 16 Jan, 2009 21:43:02

Message: 6 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gkqt71$jom$1@fred.mathworks.com>...
> Inspired by Roger!
>
> diff(rand(2,1));
>
> Bruno

That's even better, Bruno!

Roger Stafford

Subject: Help with Random Variable Generation

From: Bongo

Date: 17 Jan, 2009 13:00:04

Message: 7 of 7

Thanks everyone for your help! I've already solved my problem!!
Thanks!!! :)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us