5 #ifndef BPP_NUMERIC_RANDOM_RANDOMTOOLS_H 6 #define BPP_NUMERIC_RANDOM_RANDOMTOOLS_H 9 #include "../../Exceptions.h" 10 #include "../VectorExceptions.h" 11 #include "../VectorTools.h" 68 static void setSeed(std::mt19937::result_type seed)
70 DEFAULT_GENERATOR.seed(seed);
80 std::uniform_real_distribution<double> dis(0, entry);
81 return dis(DEFAULT_GENERATOR);
91 std::bernoulli_distribution d(prob);
92 return d(DEFAULT_GENERATOR);
102 template<
class intType>
106 throw Exception(
"RandomTools::giveIntRandomNumberBetweenZeroAndEntry. Entry must be at least 1.");
107 std::uniform_int_distribution<intType> dis(0, entry - 1);
108 return dis(DEFAULT_GENERATOR);
118 std::normal_distribution<double> dis(mean, sqrt(variance));
119 return dis(DEFAULT_GENERATOR);
128 std::gamma_distribution<double> dis(alpha, 1.);
129 return dis(DEFAULT_GENERATOR);
139 std::gamma_distribution<double> dis(alpha, beta);
140 return dis(DEFAULT_GENERATOR);
148 static double randBeta(
double alpha,
double beta);
156 std::exponential_distribution<double> dis(mean);
157 return dis(DEFAULT_GENERATOR);
175 static T
pickOne(std::vector<T>& v,
bool replace =
false)
179 size_t pos = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(v.size());
205 size_t pos = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(v.size());
227 static void getSample(
const std::vector<T>& vin, std::vector<T>& vout,
bool replace =
false)
229 if (vout.size() > vin.size() && !replace)
233 for (
size_t i = 0; i < vout.size(); ++i)
240 std::vector<size_t> hat(vin.size());
241 std::iota(hat.begin(), hat.end(), 0);
243 for (
size_t i = 0; i < vout.size(); i++)
245 vout[i] = vin[hat[i]];
266 static T
pickOne(std::vector<T>& v, std::vector<double>& w,
bool replace =
false)
276 size_t pos = v.size() - 1;
277 for (
size_t i = 0; i < v.size(); ++i)
312 static T
pickOne(
const std::vector<T>& v,
const std::vector<double>& w)
322 size_t pos = v.size() - 1;
323 for (
size_t i = 0; i < v.size(); ++i)
349 while (pos < w.size() - 1)
383 static void getSample(
const std::vector<T>& vin,
const std::vector<double>& w, std::vector<T>& vout,
bool replace =
false)
385 if (vout.size() > vin.size() && !replace)
386 throw IndexOutOfBoundsException(
"RandomTools::getSample (with weights): size exceeded v.size.", vout.size(), 0, vin.size());
387 std::vector<size_t> hat(vin.size());
388 std::iota(hat.begin(), hat.end(), 0);
391 for (
size_t i = 0; i < vout.size(); i++)
393 vout[i] = vin[
pickOne(hat, w)];
398 std::vector<double> w2(w);
399 for (
size_t i = 0; i < vout.size(); i++)
401 vout[i] = vin[
pickOne(hat, w2,
false)];
416 static std::vector<size_t>
randMultinomial(
size_t n,
const std::vector<double>& probs);
443 static double qNorm(
double prob);
464 static double qNorm(
double prob,
double mu,
double sigma);
472 static double lnGamma(
double alpha) {
return std::lgamma(alpha); }
490 static double incompleteGamma(
double x,
double alpha,
double ln_gamma_alpha);
507 static double qChisq(
double prob,
double v);
519 return pGamma(x, v / 2, 0.5);
530 static double qGamma(
double prob,
double alpha,
double beta)
532 return qChisq(prob, 2.0 * (alpha)) / (2.0 * (beta));
545 static double pGamma(
double x,
double alpha,
double beta)
547 if (alpha < 0)
throw Exception(
"RandomTools::pGamma. Negative alpha is not allowed.");
548 if (beta < 0)
throw Exception(
"RandomTools::pGamma. Negative beta is not allowed.");
549 if (alpha == 0.)
return 1.;
575 static double pNorm(
double z);
587 static double pNorm(
double z,
double mu,
double sigma);
599 static double lnBeta(
double alpha,
double beta);
614 static double incompleteBeta(
double x,
double alpha,
double beta);
615 static double pBeta(
double x,
double alpha,
double beta)
641 static double qBeta(
double prob,
double alpha,
double beta);
650 #endif // BPP_NUMERIC_RANDOM_RANDOMTOOLS_H
Exception thrown when an empty vector was found.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Index out of bounds exception class.