bpp-core3
3.0.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 void | setSeed (std::mt19937::result_type seed) |
Set the default generator seed. More... | |
static double | giveRandomNumberBetweenZeroAndEntry (double entry) |
Get a double random value (between 0 and specified range). More... | |
static bool | flipCoin (double prob=0.5) |
Get a boolean random value. More... | |
template<class intType > | |
static intType | giveIntRandomNumberBetweenZeroAndEntry (intType entry) |
Get an integer random value (between 0 and specified range). More... | |
static double | randGaussian (double mean, double variance) |
static double | randGamma (double alpha) |
static double | randGamma (double alpha, double beta) |
static double | randBeta (double alpha, double beta) |
static double | randExponential (double mean) |
template<class T > | |
static T | pickOne (std::vector< T > &v, bool replace=false) |
Pick (and extract) one element randomly in a vector and return it. More... | |
template<class T > | |
static T | pickOne (const std::vector< T > &v) |
Pick one element randomly in a vector and return it. More... | |
template<class T > | |
static void | getSample (const std::vector< T > &vin, std::vector< T > &vout, bool replace=false) |
Sample a vector. More... | |
template<class T > | |
static T | pickOne (std::vector< T > &v, std::vector< double > &w, bool replace=false) |
Pick one element in a vector, with associated probability weights. More... | |
template<class T > | |
static T | pickOne (const std::vector< T > &v, const std::vector< double > &w) |
Pick one element in a vector, with associated probability weights. More... | |
static size_t | pickFromCumSum (const std::vector< double > &w) |
Pick one index from a cumsum vector of probabilities. More... | |
template<class T > | |
static void | getSample (const std::vector< T > &vin, const std::vector< double > &w, std::vector< T > &vout, bool replace=false) |
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 given . More... | |
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) |
quantile function. More... | |
static double | pChisq (double x, double v) |
cumulative probability function. More... | |
static double | qGamma (double prob, double alpha, double beta) |
The Gamma quantile function. More... | |
static double | pGamma (double x, double alpha, double beta) |
cumulative probability function. More... | |
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 given and . More... | |
static double | incompleteBeta (double x, double alpha, double beta) |
Returns the regularized incomplete beta function . More... | |
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 std::random_device | RANDOM_DEVICE |
static std::mt19937 | DEFAULT_GENERATOR |
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) |
static double | DblGammaLessThanOne (double dblAlpha) |
Utilitary function dealing with random numbers.
This class is adapted from Pupko's SEMPHY library. It also borrow some code from Yang's PAML package.
Most of these function are provided for convenience, directly using the "random" library might prove more efficient.
Definition at line 34 of file RandomTools.h.
|
inline |
Definition at line 37 of file RandomTools.h.
|
inlinevirtual |
Definition at line 38 of file RandomTools.h.
References incompletebetafe(), incompletebetafe2(), and incompletebetaps().
|
staticprivate |
Definition at line 43 of file RandomTools.cpp.
References giveRandomNumberBetweenZeroAndEntry().
Referenced by pBeta().
|
staticprivate |
Definition at line 87 of file RandomTools.cpp.
Referenced by pBeta().
|
inlinestatic |
Get a boolean random value.
prob | Probability of getting 'true'. |
Definition at line 89 of file RandomTools.h.
|
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 227 of file RandomTools.h.
References DEFAULT_GENERATOR, and 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 | [in] The vector to sample. |
w | [in] The vector of weights. |
vout | [out] The output vector to fill, with the appropriate size already set. |
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 383 of file RandomTools.h.
References pickOne(), qNorm(), and randMultinomial().
|
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. |
Definition at line 103 of file RandomTools.h.
|
inlinestatic |
Get a double random value (between 0 and specified range).
entry | Max number to reach. |
Definition at line 78 of file RandomTools.h.
Referenced by DblGammaGreaterThanOne(), pickFromCumSum(), pickOne(), bpp::AbstractDiscreteDistribution::rand(), bpp::UniformDiscreteDistribution::randC(), randMultinomial(), bpp::ContingencyTableGenerator::rcont2(), and bpp::AbstractHmmTransitionMatrix::sample().
|
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 549 of file RandomTools.cpp.
References bpp::NumConstants::VERY_BIG(), and bpp::NumConstants::VERY_TINY().
|
staticprivate |
functions for the computation of incompleteBeta
Definition at line 660 of file RandomTools.cpp.
References bpp::NumConstants::VERY_TINY().
Referenced by ~RandomTools().
|
staticprivate |
Definition at line 771 of file RandomTools.cpp.
References bpp::NumConstants::VERY_TINY().
Referenced by ~RandomTools().
|
staticprivate |
Definition at line 887 of file RandomTools.cpp.
References bpp::NumConstants::VERY_BIG(), and bpp::NumConstants::VERY_TINY().
Referenced by ~RandomTools().
|
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 143 of file RandomTools.cpp.
|
static |
Computes given and .
Returns ln(beta(alpha,beta)) for alpha>0 and beta>0.
alpha,beta | Alpha and Beta parameters. |
Definition at line 405 of file RandomTools.cpp.
Referenced by bpp::BetaDiscreteDistribution::BetaDiscreteDistribution(), bpp::BetaDiscreteDistribution::fireParameterChanged(), and pGamma().
|
inlinestatic |
Computes given .
alpha | Alpha parameter. |
Definition at line 472 of file RandomTools.h.
References incompleteGamma(), and qChisq().
Referenced by bpp::GammaDiscreteDistribution::fireParameterChanged(), bpp::GammaDiscreteDistribution::GammaDiscreteDistribution(), and pGamma().
|
inlinestatic |
Definition at line 615 of file RandomTools.h.
References DblGammaGreaterThanOne(), DblGammaLessThanOne(), incompleteBeta(), and qBeta().
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 516 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 545 of file RandomTools.h.
References incompleteBeta(), incompleteGamma(), lnBeta(), lnGamma(), and pNorm().
Referenced by bpp::GammaDiscreteDistribution::Expectation(), pChisq(), and bpp::GammaDiscreteDistribution::pProb().
|
inlinestatic |
Pick one index from a cumsum vector of probabilities.
Last probability of the vector is assumed to be one.
w | The vector of cumsumed weights. |
EmptyVectorException | if the vector is empty. |
Definition at line 345 of file RandomTools.h.
References giveRandomNumberBetweenZeroAndEntry().
|
inlinestatic |
Pick (and extract) 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, in which case the vector will lost one element). |
EmptyVectorException | if the vector is empty. |
Definition at line 175 of file RandomTools.h.
Referenced by getSample().
|
inlinestatic |
Pick one element randomly in a vector and return it.
v | The vector of elements. |
EmptyVectorException | if the vector is empty. |
Definition at line 201 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 266 of file RandomTools.h.
References bpp::VectorTools::cumSum(), and giveRandomNumberBetweenZeroAndEntry().
|
inlinestatic |
Pick one element in a vector, with associated probability weights.
Pick one element randomly in a vector and return it, with no change in the original vector.
v | The vector of elements. |
w | The vector of weight associated to the v elements. |
EmptyVectorException | if the vector is empty. |
Definition at line 312 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 276 of file RandomTools.cpp.
Referenced by bpp::GaussianDiscreteDistribution::Expectation(), pGamma(), pNorm(), and bpp::GaussianDiscreteDistribution::pProb().
|
static |
Definition at line 270 of file RandomTools.cpp.
References pNorm().
|
static |
The Beta quantile function.
Code adapted from Ziheng Yang's PAML. Original comment from the PAML package: Cran, G. W., K. J. Martin and G. E. Thomas (1977). Remark AS R19 and Algorithm AS 109, Applied Statistics, 26(1), 111-114. Remark AS R83 (v.39, 309-310) and correction (v.40(1) p.236).
My own implementation of the algorithm did not bracket the variable well. This version is Adpated from the pbeta and qbeta routines from "R : A Computer Language for Statistical Data Analysis". It fails for extreme values of p and q as well, although it seems better than my previous version. Ziheng Yang, May 2001
prob | The probability. |
alpha | Alpha parameter. |
beta | Beta parameter. |
Definition at line 416 of file RandomTools.cpp.
Referenced by pBeta(), 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 210 of file RandomTools.cpp.
|
inlinestatic |
The Gamma quantile function.
prob | The probability. |
alpha | Alpha parameter. |
beta | Beta parameter. |
Definition at line 530 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 122 of file RandomTools.cpp.
Referenced by getSample(), qNorm(), and bpp::GaussianDiscreteDistribution::qProb().
|
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 138 of file RandomTools.cpp.
References qNorm().
|
static |
alpha | The alpha parameter. |
beta | The beta parameter. |
Definition at line 410 of file RandomTools.cpp.
References qBeta().
Referenced by bpp::BetaDiscreteDistribution::randC(), and randGamma().
|
inlinestatic |
mean | The mean of the distribution. |
Definition at line 154 of file RandomTools.h.
Referenced by bpp::ExponentialDiscreteDistribution::randC(), and bpp::TruncatedExponentialDiscreteDistribution::randC().
|
inlinestatic |
alpha | The alpha parameter. |
Definition at line 126 of file RandomTools.h.
Referenced by bpp::GammaDiscreteDistribution::randC().
|
inlinestatic |
alpha | The alpha parameter. |
beta | The beta parameter. |
Definition at line 137 of file RandomTools.h.
References randBeta().
|
inlinestatic |
mean | The mean of the law. |
variance | The variance of the law. |
Definition at line 116 of file RandomTools.h.
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 13 of file RandomTools.cpp.
References giveRandomNumberBetweenZeroAndEntry(), and bpp::VectorTools::sum().
Referenced by getSample().
|
inlinestatic |
Set the default generator seed.
seed | New seed. |
Definition at line 68 of file RandomTools.h.
Referenced by bpp::BppApplication::BppApplication().
|
static |
Definition at line 61 of file RandomTools.h.
Referenced by getSample().
|
static |
Definition at line 60 of file RandomTools.h.