gamma-dev
implements a pseudorandom number generator for the
Gamma distribution [1].
The following distributions are either special cases or derived easily from the gamma distribution:
- ✓ Beta: Package
beta-dev
. - Exponential: is a special case of gamma. However, please use exp-dev as it is more efficient.
- ✖ Chi-square
- ✖ Erlang
- ✖ F-distribution
For the gamma distribution, you need to instantiate a number generator with the set of parameters you want to use. We use the following parameters:
a
(alpha in [2]): shape parameterb
(beta in [2]): scale parameter
make-gamma-gen a b => Instance of gamma-gen
(setf generator (make-gamma-gen a b))
Create a pseudorandom number generator for the Gamma distribution with shape parameter a and scale parameter b.
draw gamma-gen => Pseudorandom number
(draw generator)
Draw a pseudorandom number using the generator instance.
You can read back the parameters of the generator with:
(a generator)
(b generator)
In very much the same way:
(setf generator (beta-dev:make-beta-gen alpha beta))
(beta-dev:draw generator) ;=> pseudorandom number
(beta-dev:alpha generator) ;=> alpha
(beta-dev:beta generator) ;=> beta
We use the algorithm by Tanizaki [2]. It involves a rejection of
uniform variates. We use the in-built random
.
We generate histograms for the gamma distribution with some shape and scale parameters (see histograms).
This looks good enough when compared with the analytical PDFs plotted on Wikipedia [3].
- There are some specializations etc. to perform for performance if that is what you need.
- The algorithm by Tanizaki [2] is, according to its own author, not the best algorithm performance-wise for all choices of parameters. An efficient gamma pseudorandom generator would need to incorporate several of these algorithms and switch depending on the parameters.
Launch tests with:
(asdf:test-system "gamma-dev")
Includes a quick performance test.
gamma-dev
: None.gamma-dev/test
:
- https://en.wikipedia.org/wiki/Gamma_distribution
- H. Tanizaki, “A simple gamma random number generator for arbitrary shape parameters,” Economics Bulletin 3(7), 1–10 (2008).
- https://en.wikipedia.org/wiki/File:Gamma_distribution_pdf.svg