Random numbers from beta distribution, C ++

I wrote a C ++ simulation that generates (1,000,000) ^ 2 numbers from a specific probability distribution, and then does something with them. So far I have used exponential, normal, gamma, uniform and Poisson distributions. Here is the code for one of them:

#include <boost/random.hpp> ...main... srand(time(NULL)) ; seed = rand(); boost::random::mt19937 igen(seed) ; boost::random::variate_generator<boost::random::mt19937, boost::random::normal_distribution<> > norm_dist(igen, boost::random::normal_distribution<>(mu,sigma)) ; 

Now I need to run it to distribute the beta version. All the distributions that I have done so far have taken 10-15 hours. The beta distribution is not in the boost / random package, so I had to use the boost / math / distribution package. I found an https://stackoverflow.com/a/16626749/2127 which suggested a solution. Here it is (copied):

 #include <boost/math/distributions.hpp> using namespace boost::math; double alpha, beta, randFromUnif; //parameters and the random value on (0,1) you drew beta_distribution<> dist(alpha, beta); double randFromDist = quantile(dist, randFromUnif); 

I played it, and it worked. Estimates of the runtime of my simulation are linear and accurately predictable. They say it will work for 25 days. I see two possibilities: 1. the proposed method is inferior to the one I used earlier for other distributions 2. The beta distribution is much more difficult to generate random numbers from

Mention that I have below a minimal understanding of C ++ coding, so the questions I ask may be dumb. I cannot wait a month to complete this simulation, so what can I do to improve this? Perhaps use the initial method that I used and modified to work with the boost / math / distribution package? I don’t even know if this is possible.

Other useful information may be that the parameters are the same for all (1,000,000) ^ 2 numbers that I need to generate. I say this because the Beta distribution has an unpleasant PDF file, and perhaps knowing that the parameters are fixed can be used in some way to simplify the process? Just a random guess.

+8
c ++ boost boost-random beta-distribution
source share
3 answers

Beta distribution is related to gamma distribution. Let X be a random number taken from Gamma (α, 1) and Y from Gamma (β, 1), where the first argument to the gamma distribution is the shape parameter. Then Z = X / (X + Y) has a distribution Beta (α, β). With such a conversion, it only takes twice as long as your gamma distribution test.

Note: The above assumes the most common representation of gamma distribution, gamma (shape, scale). Keep in mind that various implementations of a random gamma distribution generator will vary with the value and order of the arguments.

+5
source share

If you want the distribution to be very beta, but have a very simple reverse CDF with a closed form, you should consider the Kumaraswamy distribution:

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

It is used as an alternative to beta distribution when a large number of random samples are required.

+2
source share

Try compiling with optimization. Using the -O3 flag usually speeds things up. See this post on optimization flags or this review for more details.

0
source share

All Articles