5 Feb 16:06
Re: Exact sampling from the normal distribution
I've implemented MPFR algorithms for sampling from the unit exponential and unit normal distributions. The code is in http://randomlib.git.sourceforge.net/git/gitweb.cgi?p=randomlib/randomlib;a=blob;f=examples/RandomMPFR.hpp This is a header-only implementation via C++ classes. MPFR{Test,Check}.cpp exercise the implementation. The three files RandomMPFR.hpp and MPFR{Test,Check}.cpp are self-contained. You can download these and compile and link MPFR{Test,Check}.cpp against MPFR (version 3.x?). Converting this to C code should be relatively easy. RandomMPFR.hpp contains some implementation details at the top. Here I raise a few points/questions: * I explicitly handle rounding, overflow, and underflow. Partly this is because I am confused by the MPFR documentation on the rounding. It is claimed that the ternary value with MPFR_RNDU should always be 0 or 1. However, surely that's not the case if there's underflow (from a positive number to 0) or overflow (to -inf)? * The implementation of normal sampling is a *lot* faster than mpfr_grandom (MPFR version 3.1.0). Here's a table showing the times (microsec per sample) obtained on an Intel Xeon, x86_42, 2.66GHz computer with g++ version 4.6.1 (-O3). The columns are 1. RandomLib::NormalDistribution (using the ratio method) 2. RandomLib::ExactNormal (same algorithm as RandomMPFR.hpp) 3. RandomMPFR.hpp 4. mpfr_grandom (MPFR version 3.1.0)(Continue reading)
RSS Feed