Google Answers Logo
View Question
 
Q: Random Number Sampling ( Answered 4 out of 5 stars,   4 Comments )
Question  
Subject: Random Number Sampling
Category: Science > Math
Asked by: simontrumpet-ga
List Price: $10.00
Posted: 13 Aug 2002 05:48 PDT
Expires: 12 Sep 2002 05:48 PDT
Question ID: 54001
what is the best algorithm to generate a random sample from a beta
probability distribution?

Request for Question Clarification by jeremymiles-ga on 13 Aug 2002 07:21 PDT
How do you want to put this to use?  Are you using a package/language
that has some random number generator features already, or do you need
to start from scratch?

Clarification of Question by simontrumpet-ga on 13 Aug 2002 07:45 PDT
I am developing some risk analysis software, which uses Monte Carlo
simulation.

I already have the random number generator (which uses Mersenne
Twister algorithm), so I can generate random numbers from
Uniform(0,1).  I want to know how to translate such a random number to
Beta(a,b).
Answer  
Subject: Re: Random Number Sampling
Answered By: maniac-ga on 14 Aug 2002 18:30 PDT
Rated:4 out of 5 stars
 
Hello Simontrumpet,

May I suggest a quick look at the documentation for the GNU Scientific
Library (GSL)? The Beta Distribution is described at...

http://sources.redhat.com/gsl/ref/gsl-ref_19.html#SEC300

which has the formula listed. You may be able to use the source code,
available as described at...

http://www.gnu.org/software/gsl/#downloading

The function is defined as gsl_ran_beta (const gsl_rng * r, double a,
double b), which allows you to pass the address of the random function
(your Mersenne Twister algorithm if it matches the function
prototype), as well as values for A and B.

If you can't comply with the license requirements of the GSL, you
could have someone implement the appropriate algorithm as defined in
the first link. If you need something with less restrictive licenses,
please ask as a clarifiction and I will do some more digging on this
problem.

--Maniac

Request for Answer Clarification by simontrumpet-ga on 15 Aug 2002 04:00 PDT
Dear maniac,

This is just what I was after, I think, but how do I open the tar
file?  (I use a PC with WinZip).  What is contained in here - is it C
code?

Also, you suggest I could have someone implement the algorithm in the
first link - but the first link doesn't define the algorithm, it only
declares the function.  It also lists the formula, but this is the
formula for the density function, not the inverse cumulative function.
 The inverse cumulative function does not exist in closed form,
apparently.

Clarification of Answer by maniac-ga on 15 Aug 2002 18:07 PDT
Hello Simontrumpet,

You should be able to read the tar file (compressed or not) with
winzip. From the winzip web site...

An explanation describing the general capabilities of winzip including
the ability to extract from tar files.
http://www.winzip.com/info.htm

An explanation of other formats used on the internet including tar.
http://www.winzip.com/internet.htm 

I haven't opened the tar file that you downloaded, but I expect it to
have C source code in it. From the documentation of GSL, there are a
number of C compilers (on Unix and Windows) that it should work with.
After winzip is done with it, you should get a directory with the
source code, README files, and other information.

Please let me know if you need any more help on this.
  --Maniac
simontrumpet-ga rated this answer:4 out of 5 stars
Thank you, the answer was fine ... I didn't use it in the end, as I
found a better way to do it myself, but I think I could have done!

Thanks agin, all the best,
simon.

Comments  
Subject: Re: Random Number Sampling
From: tne-ga on 14 Aug 2002 00:46 PDT
 
x * (b-a) + a, 0 < a < b, 0 <= x <= 1
Subject: Re: Random Number Sampling
From: simontrumpet-ga on 14 Aug 2002 03:10 PDT
 
Dear tne-ga

What is your x, is it my U(0,1)?

In which case, your anser just gives me U(a,b), not Beta(a,b)!!

Unless I misunderstood your comment?  Thanks anyway!
Subject: Re: Random Number Sampling
From: chinaski-ga on 14 Aug 2002 15:13 PDT
 
Simon,

This is certainly not the best way to get random deviates with a
beta distribution, but it works and is easy to code.
Let f(x) be your Beta pdf (0<x<1), and let m bet the maximum value
that your Beta pdf attains, which you can easily work out.

Step 1:
Pick the random number u from U(0,1).
Step 2:
Pick the random number z from U(0,m).
Step 3:
If z<f(u), print u.  Otherwise return to step 1.

The numbers u that you have printed out will have a Beta pdf.  It's not
optimally fast, because often you have to cycle through the steps
without printing anything, but it works.

chinaski
Subject: Re: Random Number Sampling
From: tne-ga on 15 Aug 2002 20:02 PDT
 
Whoops! I answered before I read the whole question
B(a, b) = integral (0, 1) (u^(a &#8722; 1) ) * ( (1 &#8722; u)^ (b&#8722;1) ) du


source
http://www.math.uah.edu/stat/special/special9.html

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy