Random samples from a doubly-truncated Normal distribution. In other words, a Normal distribution truncated to a finite interval.
I started with the C++ code of G. Dolle and V Mazet, which implements Chopin's algorithm for sampling from the doubly-truncated Normal distribution. My C translation of their code is available on github. I then did numerical experiments with various algorithms, and discovered that other algorithms work better in some regions of parameter space.
The code in this repository combines 4 algorithms:
-
Rejection sampling with a Gaussian proposal distribution
-
Rejection sampling with an exponential proposal distribution.
-
Chopin's algorithm, as implemented by Dolle and Mazet.
Each of these algorithms is used in a different region of parameter space.
The dtnorm function is in src/dtnorm.h, src/dtnorm_data.h, and src/dtnorm.c. For a usage example, see test/example.c. A unit test is in test/xdtnorm.c.
To compile and run the example:
cd test
make example
./example
To compile and run the unit test
cd test
make xdtnorm
./xdtnorm
The unit test takes a single optional argument, -v
, which will
provide verbose output.