I am trying to use Boost to generate random numbers according to the beta distribution using C++. I have seen many examples online for generating random numbers according to distributions in random.hpp (e.g. this book). However, I cannot seen to translate them to use the beta distribution found in beta.hpp.
Thanks.
You'll first want to draw a random number uniformly from the range (0,1). Given any distribution, you can then plug that number into the distribution's "quantile function," and the result is as if a random value was drawn from the distribution. From here:
A general method to generate random numbers from an arbitrary distribution which has a cdf without jumps is to use the inverse function to the cdf: G(y)=F^{-1}(y). If u(1), ..., u(n) are random numbers from the uniform on (0,1) distribution then G(u(1)), ..., G(u(n)) is a random sample from the distribution with cdf F(x).
So how do we get a quantile function for a beta distribution? The documentation for beta.hpp is here. You should be able to use something like this:
#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);
According to boost's demo for the random number library
Random_demo.cpp and Generating integers with different probabilities
What you should do is to use "variate_generator" class to bind your random number generator and distribution.
An example may look like
#include <iostream>
#include "boost/random.hpp"
int main(int argc, char *argv[])
{
int seed = 2018;
typedef boost::random::mt19937 RandomNumberGenerator;
typedef boost::random::beta_distribution<> BetaDistribution;
typedef boost::variate_generator<RandomNumberGenerator&, BetaDistribution>
Generator;
RandomNumberGenerator Rng(seed);
BetaDistribution distribution(2,5);
Generator getRandomNumber(Rng,distribution);
for (int idx = 0 ; idx < 1000 ; ++idx)
{
std::cout << getRandomNumber() << std::endl;
}
return 0;
}
However, in the more recent document enter link description here, it seems that boost recommends to directly passing the generator to the distribution obejct. The result from the code below is identical.
#include <iostream>
#include "boost/random.hpp"
int main(int argc, char *argv[])
{
int seed = 2018;
typedef boost::random::mt19937 RandomNumberGenerator;
typedef boost::random::beta_distribution<> BetaDistribution;
RandomNumberGenerator Rng(seed);
BetaDistribution distribution(2,5);
for (int idx = 0 ; idx < 1000 ; ++idx)
{
std::cout << distribution(Rng) << std::endl;
}
return 0;
}