bpp-core
2.2.0
|
Utilitary function dealing with random numbers. More...
#include <Bpp/Numeric/Random/RandomTools.h>
Public Member Functions | |
RandomTools () | |
virtual | ~RandomTools () |
Static Public Member Functions | |
static double | giveRandomNumberBetweenZeroAndEntry (double entry, const RandomFactory &generator= *DEFAULT_GENERATOR) |
Get a double random value (between 0 and specified range). More... | |
static bool | flipCoin (const RandomFactory &generator= *DEFAULT_GENERATOR) |
Get a boolean random value. More... | |
template<class intType > | |
static intType | giveIntRandomNumberBetweenZeroAndEntry (intType entry, const RandomFactory &generator= *DEFAULT_GENERATOR) |
Get an integer random value (between 0 and specified range). More... | |
static void | setSeed (long seed) |
Set the default generator seed. More... | |
static double | randGaussian (double mean, double variance, const RandomFactory &generator= *DEFAULT_GENERATOR) |
static double | randGamma (double dblAlpha, const RandomFactory &generator= *DEFAULT_GENERATOR) |
static double | randGamma (double alpha, double beta, const RandomFactory &generator= *DEFAULT_GENERATOR) |
static double | randBeta (double alpha, double beta, const RandomFactory &generator= *DEFAULT_GENERATOR) |
static double | randExponential (double mean, const RandomFactory &generator= *DEFAULT_GENERATOR) |
template<class T > | |
static T | pickOne (std::vector< T > &v, bool replace=false) throw (EmptyVectorException<T>) |
Pick one element in a vector. More... | |
template<class T > | |
static T | pickOne (const std::vector< T > &v) throw (EmptyVectorException<T>) |
template<class T > | |
static void | getSample (const std::vector< T > &vin, std::vector< T > &vout, bool replace=false) throw (EmptyVectorException<T>, IndexOutOfBoundsException) |
Sample a vector. More... | |
template<class T > | |
static T | pickOne (std::vector< T > &v, std::vector< double > &w, bool replace=false) throw (EmptyVectorException<T>) |
Pick one element in a vector, with associated probability weights. More... | |
template<class T > | |
static void | getSample (const std::vector< T > &vin, const std::vector< double > &w, std::vector< T > &vout, bool replace=false) throw (EmptyVectorException<T>, IndexOutOfBoundsException) |
Sample a vector, with associated probability weights. More... | |
static std::vector< size_t > | randMultinomial (size_t n, const std::vector< double > &probs) |
Get a random state from a set of probabilities/scores. More... | |
Probability functions. | |
Adapted from Yang's PAML package. | |
static double | qNorm (double prob) |
Normal quantile function. More... | |
static double | qNorm (double prob, double mu, double sigma) |
Normal quantile function. More... | |
static double | lnGamma (double alpha) |
Computes ![]() ![]() | |
static double | incompleteGamma (double x, double alpha, double ln_gamma_alpha) |
Returns the incomplete gamma ratio I(x,alpha). More... | |
static double | qChisq (double prob, double v) |
![]() | |
static double | pChisq (double x, double v) |
![]() | |
static double | qGamma (double prob, double alpha, double beta) |
The Gamma quantile function. More... | |
static double | pGamma (double x, double alpha, double beta) throw (Exception) |
![]() | |
Other probability functions. | |
Adapted from C routines for R programming langague Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka Copyright (C) 1998 Ross Ihaka Copyright (C) 2000-2002 The R Development Core Team Copyright (C) 2003 The R Foundation | |
static double | pNorm (double z) |
Normal cumulative function. More... | |
static double | pNorm (double z, double mu, double sigma) |
static double | lnBeta (double alpha, double beta) |
Computes ![]() ![]() ![]() | |
static double | incompleteBeta (double x, double alpha, double beta) |
Returns the regularized incomplete beta function ![]() | |
static double | pBeta (double x, double alpha, double beta) |
static double | qBeta (double prob, double alpha, double beta) |
The Beta quantile function. More... | |
Static Public Attributes | |
static RandomFactory * | DEFAULT_GENERATOR = new Uniform01K(time(NULL)) |
Static Private Member Functions | |
static double | incompletebetafe (double a, double b, double x, double big, double biginv) |
functions for the computation of incompleteBeta More... | |
static double | incompletebetafe2 (double a, double b, double x, double big, double biginv) |
static double | incompletebetaps (double a, double b, double x, double maxgam) |
static double | DblGammaGreaterThanOne (double dblAlpha, const RandomFactory &generator) |
static double | DblGammaLessThanOne (double dblAlpha, const RandomFactory &generator) |
Utilitary function dealing with random numbers.
This class uses Uniform01K generator by default. It is possible to change this by setting the DEFAULT_GENERATOR variable.
This class is adapted from Pupko's SEMPHY library. It also borrow some code from Yang's PAML package.
Definition at line 69 of file RandomTools.h.
|
inline |
Definition at line 72 of file RandomTools.h.
|
inlinevirtual |
Definition at line 73 of file RandomTools.h.
|
staticprivate |
Definition at line 131 of file RandomTools.cpp.
References giveRandomNumberBetweenZeroAndEntry().
Referenced by randGamma().
|
staticprivate |
Definition at line 175 of file RandomTools.cpp.
Referenced by randGamma().
|
static |
Get a boolean random value.
generator | Random number generator to use. |
Definition at line 70 of file RandomTools.cpp.
References giveRandomNumberBetweenZeroAndEntry().
|
inlinestatic |
Sample a vector.
The sample is a new vector of the specified size. If the size of the sample is identical to the original vector, the result is a shuffle of the original vector.
vin | The vector to sample. |
vout | [out] The output vector to fill, with the appropriate size. |
replace | Should sampling be with replacement? |
IndexOutOfBoundException | if the sample size exceeds the original size when sampling without replacement. |
EmptyVectorException | if the vector is empty. |
Definition at line 223 of file RandomTools.h.
References pickOne().
|
inlinestatic |
Sample a vector, with associated probability weights.
The sample is a new vector of the specified size. If the size of the sample is identical to the original vector, the result is a shuffle of the original vector.
It has to be noted that in case of sampling without replacement, the effect of the weighting scheme will be weaker as the sample size becomes close to the population size. In case the two are equal (pure permutations), the weigths have no effect at all.
vin | The vector to sample. |
w | The vector of weights. |
vout | [out] The output vector to fill, with the appropriate size. |
replace | Should sampling be with replacement? |
IndexOutOfBoundException | if the sample size exceeds the original size when sampling without replacement. |
EmptyVectorException | if the vector is empty. |
Definition at line 303 of file RandomTools.h.
References pickOne().
|
inlinestatic |
Get an integer random value (between 0 and specified range).
Note : the number you get is between 0 and entry not including entry !
entry | Max number to reach. |
generator | Random number generator to use. |
Definition at line 121 of file RandomTools.h.
References giveRandomNumberBetweenZeroAndEntry().
|
static |
Get a double random value (between 0 and specified range).
Note : the number you get is between 0 and entry not including entry !
entry | Max number to reach. |
generator | Random number generator to use. |
Definition at line 62 of file RandomTools.cpp.
References bpp::RandomFactory::drawNumber().
Referenced by DblGammaGreaterThanOne(), flipCoin(), giveIntRandomNumberBetweenZeroAndEntry(), pickOne(), bpp::AbstractDiscreteDistribution::rand(), bpp::UniformDiscreteDistribution::randC(), randExponential(), randGamma(), and randMultinomial().
|
static |
Returns the regularized incomplete beta function .
alpha and beta are the parameters of the function.
Adapted From Cephes Math Library Release 2.8: June, 2000 Copyright by Stephen L. Moshier Under GPL License
x | the upper limit of the integration. |
alpha,beta | the shape parameters. |
Definition at line 659 of file RandomTools.cpp.
References bpp::NumConstants::VERY_BIG(), and bpp::NumConstants::VERY_TINY().
Referenced by pBeta().
|
staticprivate |
functions for the computation of incompleteBeta
Definition at line 770 of file RandomTools.cpp.
References bpp::NumConstants::VERY_TINY().
|
staticprivate |
Definition at line 884 of file RandomTools.cpp.
References bpp::NumConstants::VERY_TINY().
|
staticprivate |
Definition at line 1000 of file RandomTools.cpp.
References bpp::NumConstants::VERY_BIG(), and bpp::NumConstants::VERY_TINY().
|
static |
Returns the incomplete gamma ratio I(x,alpha).
X is the upper limit of the integration and alpha is the shape parameter. returns (-1) if in error ln_gamma_alpha = ln(Gamma(alpha)), is almost redundant. (1) series expansion if (alpha>x || x<=1) (2) continued fraction otherwise RATNEST FORTRAN by Bhattacharjee GP (1970) The incomplete gamma integral. Applied Statistics, 19: 285-287 (AS32)
x | the upper limit of the integration. |
alpha | the shape parameter. |
ln_gamma_alpha | ln(Gamma(alpha)). |
Definition at line 250 of file RandomTools.cpp.
Referenced by pGamma().
|
static |
Computes given
and
.
Returns ln(beta(alpha,beta)) for alpha>0 and beta>0.
alpha,beta | Alpha and Beta parameters. |
Definition at line 512 of file RandomTools.cpp.
Referenced by bpp::BetaDiscreteDistribution::BetaDiscreteDistribution(), and bpp::BetaDiscreteDistribution::fireParameterChanged().
|
static |
Computes given
.
Returns ln(gamma(alpha)) for alpha>0, accurate to 10 decimal places.
Stirling's formula is used for the central polynomial part of the procedure. Pike MC & Hill ID (1966) Algorithm 291: Logarithm of the gamma function. Communications of the Association for Computing Machinery, 9:684
alpha | Alpha parameter. |
Definition at line 232 of file RandomTools.cpp.
Referenced by bpp::GammaDiscreteDistribution::fireParameterChanged(), bpp::GammaDiscreteDistribution::GammaDiscreteDistribution(), and pGamma().
|
inlinestatic |
Definition at line 529 of file RandomTools.h.
References incompleteBeta().
Referenced by bpp::BetaDiscreteDistribution::Expectation(), and bpp::BetaDiscreteDistribution::pProb().
|
inlinestatic |
cumulative probability function.
x | The quantile for which the probability should be computed. |
v | number of degree of freedom. |
Definition at line 430 of file RandomTools.h.
References pGamma().
Referenced by bpp::ContingencyTableTest::ContingencyTableTest().
|
inlinestatic |
cumulative probability function.
x | The quantile for which the probability should be computed. |
alpha | Alpha parameter. |
beta | Beta parameter. |
Exception | If alpha or beta is invalid (<0). |
Definition at line 459 of file RandomTools.h.
References incompleteGamma(), and lnGamma().
Referenced by bpp::GammaDiscreteDistribution::Expectation(), pChisq(), and bpp::GammaDiscreteDistribution::pProb().
|
inlinestatic |
Pick one element in a vector.
Pick one element randomly in a vector and return it.
v | The vector of elements. |
replace | If set to yes, then elements are allowed to be picked more than once, and therefore can be re-"placed" in the final sample.(default: false) |
EmptyVectorException | if the vector is empty. |
Definition at line 183 of file RandomTools.h.
Referenced by getSample().
|
inlinestatic |
Definition at line 198 of file RandomTools.h.
|
inlinestatic |
Pick one element in a vector, with associated probability weights.
Pick one element randomly in a vector and return it. If you choose to make the picking without replacement the element is removed from the vector, and so is the corresponding weight
v | The vector of elements. |
w | The vector of weight associated to the v elements. |
replace | Should pick with replacement? (default: false) |
EmptyVectorException | if the vector is empty. |
Definition at line 250 of file RandomTools.h.
References bpp::VectorTools::cumSum(), and giveRandomNumberBetweenZeroAndEntry().
|
static |
Normal cumulative function.
Returns Prob{x<=z} where x ~ N(0,1)
z | the value. |
Definition at line 383 of file RandomTools.cpp.
Referenced by bpp::GaussianDiscreteDistribution::Expectation(), pNorm(), and bpp::GaussianDiscreteDistribution::pProb().
|
static |
Definition at line 377 of file RandomTools.cpp.
References pNorm().
|
static |
The Beta quantile function.
An adaptation from the C code of R Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka Copyright (C) 1998–2007 The R Development Core Team based on code (C) 1979 and later Royal Statistical Society
prob | The probability. |
alpha | Alpha parameter. |
beta | Beta parameter. |
Definition at line 523 of file RandomTools.cpp.
References bpp::NumConstants::VERY_TINY().
Referenced by bpp::BetaDiscreteDistribution::qProb(), and randBeta().
|
static |
quantile function.
returns z so that Prob{x<z}=prob where x is Chi2 distributed with df=v returns -1 if in error. 0.000002<prob<0.999998 RATNEST FORTRAN by Best DJ & Roberts DE (1975) The percentage points of the Chi2 distribution. Applied Statistics 24: 385-388. (AS91) Converted into C by Ziheng Yang, Oct. 1993.
prob | The probability. |
v | number of degree of freedom. |
Definition at line 317 of file RandomTools.cpp.
Referenced by qGamma().
|
inlinestatic |
The Gamma quantile function.
prob | The probability. |
alpha | Alpha parameter. |
beta | Beta parameter. |
Definition at line 444 of file RandomTools.h.
References qChisq().
Referenced by bpp::GammaDiscreteDistribution::qProb().
|
static |
Normal quantile function.
Returns z so that Prob{x<z}=prob where x ~ N(0,1) and (1e-12)<prob<1-(1e-12) returns (-9999) if in error Odeh RE & Evans JO (1974) The percentage points of the normal distribution. Applied Statistics 22: 96-97 (AS70)
Newer methods: Wichura MJ (1988) Algorithm AS 241: the percentage points of the normal distribution. 37: 477-484. Beasley JD & Springer SG (1977). Algorithm AS 111: the percentage points of the normal distribution. 26: 118-121.
prob | The probability. |
Definition at line 210 of file RandomTools.cpp.
Referenced by qNorm(), bpp::GaussianDiscreteDistribution::qProb(), and randGaussian().
|
static |
Normal quantile function.
Returns z so that Prob{x<z}=prob where x ~ N(mu,sigma^2) and (1e-12)<prob<1-(1e-12) returns (-9999) if in error Odeh RE & Evans JO (1974) The percentage points of the normal distribution. Applied Statistics 22: 96-97 (AS70)
Newer methods: Wichura MJ (1988) Algorithm AS 241: the percentage points of the normal distribution. 37: 477-484. Beasley JD & Springer SG (1977). Algorithm AS 111: the percentage points of the normal distribution. 26: 118-121.
prob | The probability. |
mu | The mean of the distribution |
sigma | The standard deviation of the distribution |
Definition at line 226 of file RandomTools.cpp.
References qNorm().
|
static |
alpha | The alpha parameter. |
beta | The beta parameter. |
generator | The uniform generator to use. |
Definition at line 517 of file RandomTools.cpp.
References bpp::RandomFactory::drawNumber(), and qBeta().
Referenced by bpp::BetaDiscreteDistribution::randC().
|
static |
mean | The mean of the distribution. |
generator | The uniform generator to use. |
Definition at line 96 of file RandomTools.cpp.
References giveRandomNumberBetweenZeroAndEntry().
Referenced by bpp::ExponentialDiscreteDistribution::randC(), and bpp::TruncatedExponentialDiscreteDistribution::randC().
|
static |
dblAlpha | The alpha parameter. |
generator | The uniform generator to use. |
Definition at line 80 of file RandomTools.cpp.
References DblGammaGreaterThanOne(), DblGammaLessThanOne(), and giveRandomNumberBetweenZeroAndEntry().
Referenced by bpp::GammaDiscreteDistribution::randC(), and randGamma().
|
static |
alpha | The alpha parameter. |
beta | The beta parameter. |
generator | The uniform generator to use. |
Definition at line 90 of file RandomTools.cpp.
References randGamma().
|
static |
mean | The mean of the law. |
variance | The variance of the law. |
generator | The uniform generator to use. |
Definition at line 75 of file RandomTools.cpp.
References bpp::RandomFactory::drawNumber(), and qNorm().
Referenced by bpp::GaussianDiscreteDistribution::randC().
|
static |
Get a random state from a set of probabilities/scores.
The input probabilities are scaled so that they sum to one. If 'x' probabilities are provided as input, the output vector will contain values between 0 and 'x-1'.
n | The sample size. |
probs | The set of intput probabilities. |
Definition at line 101 of file RandomTools.cpp.
References giveRandomNumberBetweenZeroAndEntry(), and bpp::VectorTools::sum().
|
static |
Set the default generator seed.
seed | New seed. |
Definition at line 55 of file RandomTools.cpp.
Referenced by bpp::BppApplication::BppApplication().
|
static |
Definition at line 95 of file RandomTools.h.