Opened 13 years ago

Closed 12 years ago

#3888 closed Bugs (fixed)

Generation of two-parameter Gamma variates

Reported by: Marco Guazzone <marco.guazzone@…> Owned by: No-Maintainer
Milestone: Boost 1.42.0 Component: random
Version: Boost 1.41.0 Severity: Problem
Keywords: Cc:

Description

Actually, the code used to generate random Gamma variates cannot be used to generate variates for two-parameters Gamma distributions, that is for Gamma distributions with both shape and scale parameters (e.g., see http://en.wikipedia.org/wiki/Gamma_distribution or http://www.itl.nist.gov/div898/handbook/eda/section3/eda366b.htm).

In principle this should be possible by means of the scaling property: Gamma(shape,scale) ~ scale*Gamma(shape,1)

This would simply translate into

boost::mt19937 rng;
boost::gamma_distribution<> gamma(shape); // 1-parameter Gamma distribution
boost::variate_generator< boost::mt19937&, boost::gamma_distribution<> > rvg(rng, gamma);
double r = scale*rvg();

However, when shape == 1 the implementation uses the fact that Gamma(1) ~ Exp(1). This is rigth when scale==1 but not when scale != 1 since the exact relation is Gamma(1,scale) ~ Exp(1/scale)

Change History (3)

comment:1 by Marco Guazzone <marco.guazzone@…>, 13 years ago

R provides a possible implementation:

https://svn.r-project.org/R/trunk/src/nmath/rgamma.c

comment:2 by Steven Watanabe, 12 years ago

Might I point out that Exp(1/scale) ~ scale*Exp(1). In fact this is how Exp is implemented anyway. We probably lose slightly less accuracy by multiplying.

comment:3 by Steven Watanabe, 12 years ago

Resolution: fixed
Status: newclosed

Fixed in [63247].

Note: See TracTickets for help on using tickets.