The exponential probability. More...
#include <RandomLib-2010-01/ExponentialProb.hpp>
Public Member Functions | |
ExponentialProb () | |
template<typename RealType , class Random > | |
bool | operator() (Random &r, RealType p) const throw (std::bad_alloc) |
Private Types | |
typedef unsigned | word |
Private Member Functions | |
template<typename RealType , class Random > | |
bool | ExpFraction (Random &r, RealType p) const throw (std::bad_alloc) |
Private Attributes | |
std::vector< word > | _v |
Static Private Attributes | |
static const unsigned | alloc_incr = 16 |
The exponential probability.
Return true with probability exp(-p). Basic method taken from:
J. von Neumann,
Various Techniques used in Connection with Random Digits,
J. Res. Nat. Bur. Stand., Appl. Math. Ser. 12, 36-38 (1951),
reprinted in Collected Works, Vol. 5, 768-770 (Pergammon, 1963).
See also the references given for the ExactExponential class.
Here the method is extended to be exact by generating sufficient bits in the random numbers in the algorithm to allow the unambiguous comparisons to be made.
Here's one way of sampling from a normal distribution with zero mean and unit variance in the interval [-1,1] with reasonable accuracy:
#include "RandomLib/Random.hpp" #include "RandomLib/ExponentialProb.hpp" double Normal(RandomLib::Random& r) { double x; RandomLib::ExponentialProb e; do x = r.FloatW(); while ( !e(r, - 0.5 * x * x) ); return x; }
typedef unsigned RandomLib::ExponentialProb::word [private] |
Definition at line 49 of file ExponentialProb.hpp.
RandomLib::ExponentialProb::ExponentialProb | ( | ) | [inline] |
Definition at line 52 of file ExponentialProb.hpp.
bool RandomLib::ExponentialProb::operator() | ( | Random & | r, |
RealType | p | ||
) | const throw (std::bad_alloc) [inline] |
Return true with probability exp(-p). Returns false if p <= 0. For in p (0,1], it requires about exp(p) random deviates. For p large, it requires about exp(1)/(1 - exp(-1)) random deviates.
Definition at line 78 of file ExponentialProb.hpp.
bool RandomLib::ExponentialProb::ExpFraction | ( | Random & | r, |
RealType | p | ||
) | const throw (std::bad_alloc) [inline, private] |
Return true with probability exp(-p) for p in [0,1].
Definition at line 89 of file ExponentialProb.hpp.
std::vector<word> RandomLib::ExponentialProb::_v [mutable, private] |
Holds as much of intermediate uniform deviates as needed.
Definition at line 70 of file ExponentialProb.hpp.
const unsigned RandomLib::ExponentialProb::alloc_incr = 16 [static, private] |
Increment on size of _v.
Definition at line 74 of file ExponentialProb.hpp.