comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » random numbers with gamma distribution
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
random numbers with gamma distribution [message #13427] Wed, 04 November 1998 00:00 Go to next message
f055 is currently offline  f055
Messages: 29
Registered: April 1995
Junior Member
Dear All

I'm trying to generate a random series that has a gamma distribution, using
the randomu() function with the gamma keyword. The online help says to set
gamma to an integer > 0. WHY DOES IT HAVE TO BE AN INTEGER? The gamma
distribution is in fact defined for all gamma > 0, not just integers.
I want to set gamma=0.5, which currently fails as it converts it to the
integer 0, which is not > 0, and so it fails.

Is this a bug, and is there any work-around?

Cheers

Tim



......................... Dr Tim Osborn . t.osborn@uea.ac.uk
.... ___/.. __ /.. /.. /. Senior Research Associate . phone:01603 592089
... /..... /. /.. /.. /.. Climatic Research Unit . fax: 01603 507784
.. /..... __/.. /.. /... School of Environmental Sciences.
. /..... /\ ... /.. /.... University of East Anglia .
____/.._/..\_..____/..... Norwich NR4 7TJ .
......................... UK .
Re: Random Numbers [message #81814 is a reply to message #13427] Thu, 25 October 2012 08:06 Go to previous message
John O'Neill is currently offline  John O'Neill
Messages: 2
Registered: October 2012
Junior Member
On Thursday, October 25, 2012 10:37:58 AM UTC-4, David wrote:
> Yngvar Larsen writes:
>
>
>
>>
>
>> On Wednesday, 24 October 2012 23:38:34 UTC+2, John O'Neill wrote:
>
>>> Hello Everyone,
>
>>>
>
>>> I am trying to create a set of random numbers using an Inverse Gaussian Distribution (Wald distribution) but randomu doesn't seem able to do this. Is there anything more general then randomu, or something where I can define what function I want to use to create random numbers?
>
>>
>
>>
>
>> Google and Wikipedia are your friends.
>
>>
>
>> http://en.wikipedia.org/wiki/Inverse_Gaussian_distribution#G enerating_random_variates_from_an_inverse-Gaussian_distribut ion
>
>>
>
>> IDL> N = 100
>
>> IDL> mu = 1d0 & lambda = 1d0
>
>> IDL> nu = randomn(seed, N)
>
>> IDL> z = randomu(seed, N)
>
>> IDL> igvariates = dblarr(N)
>
>> IDL> y = nu^2
>
>> IDL> x = mu + mu^2*y/(2*lambda) - mu/2/lambda*sqrt(4*mu*lambda*y + mu^2*y^2)
>
>> IDL> ind = where(z le mu/(mu+x), complement=cind)
>
>> IDL> igvariates[ind] = x[ind]
>
>> IDL> igvariates[cind] = mu^2/x[cind]
>
>>
>
>> Include checking for empty index arrays IND and/or CIND if you use IDL version < 8.0.
>
>
>
> This code is easy enough to implement that I just wrote
>
> a function, cgRandomWald to do it this morning. You can
>
> find it here:
>
>
>
> http://www.idlcoyote.com/programs/cgrandomwald.pro
>
>
>
> It works very much like the other RandomX functions in IDL.
>
>
>
> Cheers,
>
>
>
> David
>
>
>
> --
>
> David Fanning, Ph.D.
>
> Fanning Software Consulting, Inc.
>
> Coyote's Guide to IDL Programming: http://www.dfanning.com/
>
> Sepore ma de ni thue. ("Perhaps thos speakest truth.")

Thank you very much David, Yngvar, and Craig for your help. This is exactly what I was looking for.

John
Re: Random Numbers [message #81818 is a reply to message #13427] Thu, 25 October 2012 07:37 Go to previous message
David is currently offline  David
Messages: 24
Registered: April 1997
Junior Member
Yngvar Larsen writes:

>
> On Wednesday, 24 October 2012 23:38:34 UTC+2, John O'Neill wrote:
>> Hello Everyone,
>>
>> I am trying to create a set of random numbers using an Inverse Gaussian Distribution (Wald distribution) but randomu doesn't seem able to do this. Is there anything more general then randomu, or something where I can define what function I want to use to create random numbers?
>
>
> Google and Wikipedia are your friends.
>
> http://en.wikipedia.org/wiki/Inverse_Gaussian_distribution#G enerating_random_variates_from_an_inverse-Gaussian_distribut ion
>
> IDL> N = 100
> IDL> mu = 1d0 & lambda = 1d0
> IDL> nu = randomn(seed, N)
> IDL> z = randomu(seed, N)
> IDL> igvariates = dblarr(N)
> IDL> y = nu^2
> IDL> x = mu + mu^2*y/(2*lambda) - mu/2/lambda*sqrt(4*mu*lambda*y + mu^2*y^2)
> IDL> ind = where(z le mu/(mu+x), complement=cind)
> IDL> igvariates[ind] = x[ind]
> IDL> igvariates[cind] = mu^2/x[cind]
>
> Include checking for empty index arrays IND and/or CIND if you use IDL version < 8.0.

This code is easy enough to implement that I just wrote
a function, cgRandomWald to do it this morning. You can
find it here:

http://www.idlcoyote.com/programs/cgrandomwald.pro

It works very much like the other RandomX functions in IDL.

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thue. ("Perhaps thos speakest truth.")
Re: Random Numbers [message #81826 is a reply to message #13427] Thu, 25 October 2012 00:14 Go to previous message
Yngvar Larsen is currently offline  Yngvar Larsen
Messages: 134
Registered: January 2010
Senior Member
On Wednesday, 24 October 2012 23:38:34 UTC+2, John O'Neill wrote:
> Hello Everyone,
>
> I am trying to create a set of random numbers using an Inverse Gaussian Distribution (Wald distribution) but randomu doesn't seem able to do this. Is there anything more general then randomu, or something where I can define what function I want to use to create random numbers?


Google and Wikipedia are your friends.

http://en.wikipedia.org/wiki/Inverse_Gaussian_distribution#G enerating_random_variates_from_an_inverse-Gaussian_distribut ion

IDL> N = 100
IDL> mu = 1d0 & lambda = 1d0
IDL> nu = randomn(seed, N)
IDL> z = randomu(seed, N)
IDL> igvariates = dblarr(N)
IDL> y = nu^2
IDL> x = mu + mu^2*y/(2*lambda) - mu/2/lambda*sqrt(4*mu*lambda*y + mu^2*y^2)
IDL> ind = where(z le mu/(mu+x), complement=cind)
IDL> igvariates[ind] = x[ind]
IDL> igvariates[cind] = mu^2/x[cind]

Include checking for empty index arrays IND and/or CIND if you use IDL version < 8.0.

--
Yngvar
Re: Random Numbers [message #81832 is a reply to message #13427] Wed, 24 October 2012 16:34 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Wednesday, October 24, 2012 5:38:34 PM UTC-4, John O'Neill wrote:
> Hello Everyone,
>
>
>
> I am trying to create a set of random numbers using an Inverse Gaussian Distribution (Wald distribution) but randomu doesn't seem able to do this. Is there anything more general then randomu, or something where I can define what function I want to use to create random numbers?

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

I'm not aware of any IDL routines to do this though, but it's not hard with a little integrating and interpolating.

The difficulty of implementing the method depends on the distribution to some degree. For example, power law distributions can be hard to do because of the dynamic range required.

Craig
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: VALUE_LOCATE and NaNs
Next Topic: Re: VALUE_LOCATE and NaNs

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:58:07 PDT 2025

Total time taken to generate the page: 0.00618 seconds