Setting parameters for gamma_distribution using boost

629 Views Asked by At

I am trying to use the Gamma distribution from Boost 1.5. Now I want the value of k and theta to be 4 and .5 respectively. But I get a compile error whenever I set the value of theta < 1.

/usr/local/include/boost/random/gamma_distribution.hpp:118: boost::random::gamma_distribution<RealType>::gamma_distribution(const RealType&, const RealType&) [with RealType = double]: Assertion `_beta > result_type(0)' failed.

Is there any way to get around the same?

1

There are 1 best solutions below

0
mavam On

It looks like you do not pass the parameters correctly to the distribution function. Here is the C++11 version (Boost works equivalently):

#include <random>
#include <iostream>
int main()
{   
    std::random_device rd;
    std::mt19937 gen(rd());
    double alpha = 4.0;
    double theta = 0.5;
    std::gamma_distribution<> gamma(alpha, 1.0 / theta);
    auto value = gamma(gen);

    // May print: 6.94045.
    std::cout << value << std::endl;

    return 0;
}

Note the parametrization:

  • alpha is the same as k
  • beta the inverse scale parameter and is the same as 1 / theta.