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:
Generation of gamma random variables without using the random function from Statistical toolbox

Subject: Generation of gamma random variables without using the random function from Statistical toolbox

From: jens

Date: 25 Mar, 2010 16:17:04

Message: 1 of 5

Hi everybody

I have to generate some random gamma variables. I know I can use the random function from the statistical toolbox, but I do not have the toolbox. Is there another way to generate random gamma variables? My though is that I can use rand and randn in some way. I have the gamma function:

function y = chi2pdf(x, v_degreesfreedom)

vhalf = v_degreesfreedom/2;
y = x.^(vhalf - 1) .* exp(-x / 2) ./ (2.^vhalf .* gamma(vhalf));

end

Best regards

Jens

Subject: Generation of gamma random variables without using the random function from Statistical toolbox

From: Roger Stafford

Date: 25 Mar, 2010 16:47:04

Message: 2 of 5

"jens " <storebonghoved@hotmail.com> wrote in message <hog2a0$c2n$1@fred.mathworks.com>...
> Hi everybody
>
> I have to generate some random gamma variables. I know I can use the random function from the statistical toolbox, but I do not have the toolbox. Is there another way to generate random gamma variables? My though is that I can use rand and randn in some way. I have the gamma function:
>
> function y = chi2pdf(x, v_degreesfreedom)
>
> vhalf = v_degreesfreedom/2;
> y = x.^(vhalf - 1) .* exp(-x / 2) ./ (2.^vhalf .* gamma(vhalf));
>
> end
>
> Best regards
>
> Jens

  The following Wikipedia article describes how you can do that in the section, "Generating gamma-distributed random variables". You would use rand for that.

 http://en.wikipedia.org/wiki/Gamma_distribution

Roger Staffod

Subject: Generation of gamma random variables without using the random function from Statistical toolbox

From: John D'Errico

Date: 25 Mar, 2010 16:59:06

Message: 3 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hog428$cej$1@fred.mathworks.com>...
> "jens " <storebonghoved@hotmail.com> wrote in message <hog2a0$c2n$1@fred.mathworks.com>...
> > Hi everybody
> >
> > I have to generate some random gamma variables. I know I can use the random function from the statistical toolbox, but I do not have the toolbox. Is there another way to generate random gamma variables? My though is that I can use rand and randn in some way. I have the gamma function:
> >
> > function y = chi2pdf(x, v_degreesfreedom)
> >
> > vhalf = v_degreesfreedom/2;
> > y = x.^(vhalf - 1) .* exp(-x / 2) ./ (2.^vhalf .* gamma(vhalf));
> >
> > end
> >
> > Best regards
> >
> > Jens
>
> The following Wikipedia article describes how you can do that in the section, "Generating gamma-distributed random variables". You would use rand for that.
>
> http://en.wikipedia.org/wiki/Gamma_distribution
>
> Roger Staffod

Let me just add that the trick is to use the inverse
incomplete gamma function for this, inverting
the results from rand through it. This works because
the incomplete gamma function is identical to the
gamma CDF, at least when you then scale the
incomplete gamma function by dividing by the
complete gamma (which you get from gamma.m)

HTH,
John

Subject: Generation of gamma random variables without using the random function from Statistical toolbox

From: Roger Stafford

Date: 25 Mar, 2010 17:15:19

Message: 4 of 5

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hog4oq$no4$1@fred.mathworks.com>...
> Let me just add that the trick is to use the inverse
> incomplete gamma function for this, inverting
> the results from rand through it. This works because
> the incomplete gamma function is identical to the
> gamma CDF, at least when you then scale the
> incomplete gamma function by dividing by the
> complete gamma (which you get from gamma.m)
>
> HTH,
> John

  Yes, you're right, John, that would be preferable.

Roger Stafford

Subject: Generation of gamma random variables without using the random function from Statistical toolbox

From: jens

Date: 25 Mar, 2010 18:48:04

Message: 5 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hog5n7$ac8$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <hog4oq$no4$1@fred.mathworks.com>...
> > Let me just add that the trick is to use the inverse
> > incomplete gamma function for this, inverting
> > the results from rand through it. This works because
> > the incomplete gamma function is identical to the
> > gamma CDF, at least when you then scale the
> > incomplete gamma function by dividing by the
> > complete gamma (which you get from gamma.m)
> >
> > HTH,
> > John
>
> Yes, you're right, John, that would be preferable.
>
> Roger Stafford

Dear John and Roger

Sorry for misleading both of you. I mean random chi number in the same way as:

random('Chi2',v,100,1); where v is degrees of freedom and (100,1) is the output column vector. Is there a way to get the same output without using the random function?

Best regards

Jens

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