39 #include <sprng_cpp.h>
43 #include <gsl/gsl_rng.h>
44 #include <gsl/gsl_randist.h>
45 #include <gsl/gsl_blas.h>
46 #include <gsl/gsl_math.h>
47 #include <gsl/gsl_vector.h>
48 #include <gsl/gsl_matrix.h>
49 #include <gsl/gsl_permutation.h>
62 #if defined(HAS_SPRNG)
66 #elif defined(HAS_GSL)
70 static void init_gsl(
const gsl_rng_type* T,
unsigned long seed)
72 r = gsl_rng_alloc (T);
82 static void init (
unsigned long seed) {
84 #if defined(HAS_SPRNG)
87 stream = SelectType(4);
97 #elif defined(HAS_GSL)
99 init_gsl( gsl_rng_mt19937, seed );
111 #if defined(HAS_SPRNG)
113 #elif defined(HAS_GSL)
127 return stream->sprng();
128 #elif defined(HAS_GSL)
129 return gsl_rng_uniform(r);
136 Seed1 = 40014 * (
Seed1 - w * 53668) - w * 12211;
142 Seed2 = 40692 * (
Seed2 - w * 52774) - w * 3791;
148 if (z < 1) z += 2147483562;
150 }
while (!((z * 4.656613e-10) < 1.0));
152 return (z * 4.656613e-10);
156 static inline unsigned int Uniform (
unsigned int max)
158 return (
unsigned int)(
Uniform() * max);
165 static int intrand = stream->isprng();
166 #elif defined(HAS_GSL)
167 static unsigned long int intrand = gsl_rng_get(r);
169 static int intrand = (int)(
Uniform()*(double)std::numeric_limits<int>::max());
172 static unsigned int num = 0;
178 intrand = stream->isprng();
179 #elif defined(HAS_GSL)
180 intrand = gsl_rng_get(r);
182 intrand = (int)(
Uniform()*(double)std::numeric_limits<int>::max());
186 return (intrand & (1 << num) );
192 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
193 return gsl_rng_get(r);
195 unsigned long rnd, limit = 0x10000000;
197 rnd = (
unsigned long)(
Uniform()*ULONG_MAX);
204 static inline double gammln (
double xx) {
205 double x,y,tmp,ser=1.000000000190015;
206 static double cof[6]={76.18009172947146,-86.50532032941677,
207 24.01409824083091,-1.231739572450155,
208 0.1208650973866179e-2,-0.5395239384953e-5};
212 tmp -= (x+0.5)*log(tmp);
213 for (j = 0; j < 6; ++j) ser += cof[j]/++y;
215 return -tmp+log(2.5066282746310005*ser/x);
221 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
222 return gsl_ran_poisson(r, mean);
224 static double sq,alxm,g,oldm=(-1.0);
246 g=mean*alxm-
gammln(mean+1.0);
254 t=0.9*(1.0+y*y)*exp(em*alxm-
gammln(em+1.0)-g);
263 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
265 return gsl_ran_gaussian_ziggurat (r, sigma);
288 double u, v, x, y, Q;
289 const double s = 0.449871;
290 const double t = -0.386595;
291 const double a = 0.19600;
292 const double b = 0.25472;
293 const double r1 = 0.27597;
294 const double r2 = 0.27846;
317 Q = x * x + y * (a * y - b * x);
324 while (Q >= r1 && (Q > r2 || v * v > -4 * u * u * log (u)));
326 return sigma * (v / u);
330 static inline void BivariateGaussian(
double sigma1,
double sigma2,
double rho,
double *out1,
double *out2) {
331 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
332 gsl_ran_bivariate_gaussian(r,sigma1,sigma2,rho,out1,out2);
335 double u, v, r2, scale;
347 while (r2 > 1.0 || r2 == 0);
349 scale = sqrt (-2.0 * log (r2) / r2);
351 *out1 = sigma1 * u * scale;
352 *out2 = sigma2 * (rho * u + sqrt(1 - rho*rho) * v) * scale;
357 static inline double LogNormal (
double zeta,
double sigma) {
358 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
359 return gsl_ran_lognormal(r,zeta,sigma);
362 double u, v, r2, normal, z;
374 while (r2 > 1.0 || r2 == 0);
376 normal = u * sqrt (-2.0 * log (r2) / r2);
378 z = exp (sigma * normal + zeta);
385 static inline double Gamma (
double a,
double b) {
386 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
387 return gsl_ran_gamma(r, a, b);
396 return Gamma(1.0 + a, b) * pow (u, 1.0 / a);
401 double d = a - 1.0 / 3.0;
402 double c = (1.0 / 3.0) / sqrt (d);
416 if (u < 1 - 0.0331 * x * x * x * x)
419 if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
431 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
432 return gsl_ran_bernoulli(r, p);
452 static inline void MultivariateGaussian(gsl_vector *eval, gsl_matrix *evec,
453 gsl_vector *workspace, gsl_vector *px)
457 for (i=0; i<eval->size; i++)
458 gsl_vector_set (workspace, i,
459 Gaussian(gsl_vector_get (eval, i)));
461 gsl_blas_dgemv (CblasNoTrans, 1.0,
462 evec, workspace, 0.0, px);
466 static inline void MultivariateGaussianCholesky(gsl_vector *sigma, gsl_matrix *M, gsl_vector *px)
468 for (
size_t i = 0; i < sigma->size; i++)
469 gsl_vector_set (px, i,
Gaussian(gsl_vector_get(sigma, i)));
471 gsl_blas_dtrmv (CblasLower, CblasNoTrans, CblasNonUnit, M, px);
474 static inline long double factoriel(
unsigned int x) {
476 for(
unsigned int i=1;i<=x;++i)
489 static inline double Binomial(
double p,
unsigned int n)
492 unsigned int i, a, b, k = 0;
511 p = (p - X) / (1 - X);
515 for (i = 0; i < n; i++)
526 static inline double Beta (
const double a,
const double b)
532 return x1 / (x1 + x2);
535 static inline unsigned int Binomial2 (
double p,
unsigned int n){
537 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
538 return gsl_ran_binomial (r, p, n);
544 static inline void Multinomial (
size_t K,
unsigned int N,
const double p[],
unsigned int n[])
546 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
547 return gsl_ran_multinomial (r, K, N, p, n);
554 unsigned int sum_n = 0;
561 for (k = 0; k < K; k++)
566 for (k = 0; k < K; k++)
570 n[k] =
Binomial (p[k] / (norm - sum_p), N - sum_n);
591 unsigned int sum_n = 0;
593 for (k = 0; k < K; k++)
603 const std::valarray<double> &p,
609 unsigned int _n, sum_n = 0;
610 unsigned int i = 0, pos = 0;
612 for (k = 0; k < K; k++) {
616 for(i = 0; i < _n; i++)
628 const std::valarray<double> &p,
634 unsigned int _n, sum_n = 0;
635 unsigned int i = 0, pos = 0;
637 for (k = 0; k < K; k++) {
641 for(i = 0; i < _n; i++)
657 const std::valarray<double> &p,
665 for (k = 0; k < N; k++) {
672 while( sum < rand ) {
691 unsigned int size = length, pos, last = length - 1, num = length -1, el;
695 for (
unsigned int i = 0; i < num; i++) {
698 array[last] = array[pos];
713 static inline void Sample (
const int from,
const int to,
const unsigned int num,
int* result,
bool replace)
717 unsigned int seq_length = to - from;
719 assert(num <= seq_length);
722 int *seq =
new int [seq_length];
726 for(
unsigned int i = 1; seq[i-1] < to && i < seq_length; ++i)
727 seq[i] = seq[i-1] + 1;
731 unsigned int size = seq_length, pos, last = seq_length - 1;
733 for (
unsigned int i = 0; i < num; i++) {
735 result[i] = seq[pos];
736 seq[pos] = seq[last];
742 for (
unsigned int i = 0; i < num; i++)
749 static inline void SampleSeq(
int from,
int to,
int by,
unsigned int num,
int* result,
bool replace =
false)
751 assert(from < to && by < to && by > 0);
753 unsigned int seq_length = (int)((to - from) / by);
755 assert(num <= seq_length);
757 int *seq =
new int [seq_length];
761 for(
unsigned int i = 1; seq[i-1] + by < to && i < seq_length; ++i) seq[i] = seq[i-1] + by;
765 unsigned int size = seq_length, pos, last = seq_length - 1;
767 for (
unsigned int i = 0; i < num; i++) {
769 result[i] = seq[pos];
770 seq[pos] = seq[last];
776 for (
unsigned int i = 0; i < num; i++) result[i] = seq[
RAND::Uniform(seq_length) ];
782 static inline void SampleSeqWithReciprocal(
int from,
int to,
int by,
unsigned int num1,
int* result1,
unsigned int num2,
int* result2)
784 assert(from < to && by < to && by > 0);
786 unsigned int seq_length = (int)((to - from) / by);
788 assert(num1 + num2 == seq_length);
790 int *seq =
new int [seq_length];
794 for(
unsigned int i = 1; seq[i-1] + by < to && i < seq_length; ++i) seq[i] = seq[i-1] + by;
796 unsigned int size = seq_length, pos, last = seq_length - 1;
798 for (
unsigned int i = 0; i < num1; i++) {
800 result1[i] = seq[pos];
801 seq[pos] = seq[last];
802 seq[last] = result1[i];
806 for (
unsigned int i = 0; i < num2 && i < size; i++)
813 static inline size_t Discrete (
const gsl_ran_discrete_t *g)
815 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
816 return gsl_ran_discrete(r, g);
833 if (f == 1.0)
return c;
MPI environment setup.
Definition: MPImanager.h:122
int workerCount() const
Definition: MPImanager.h:129
int workerRank() const
Definition: MPImanager.h:130
Random number generation class, uses various types of random generators depending on the implementati...
Definition: Uniform.h:55
static double Exponential(double mu)
Definition: Uniform.h:447
static double Bernoulli(double p)
Definition: Uniform.h:429
static void SampleSeqWithReciprocal(int from, int to, int by, unsigned int num1, int *result1, unsigned int num2, int *result2)
Definition: Uniform.h:782
static double gammln(double xx)
From the Numerical Recieps.
Definition: Uniform.h:204
static void MultinomialOnNormalizedValarray(size_t K, unsigned int N, const std::valarray< double > &p, unsigned int n[])
Multinomial draw assuming the probabilities sum to 1.0 and are all > 0.
Definition: Uniform.h:584
static void Multinomial(size_t K, unsigned int N, const double p[], unsigned int n[])
Definition: Uniform.h:544
static void BivariateGaussian(double sigma1, double sigma2, double rho, double *out1, double *out2)
Definition: Uniform.h:330
static void MultinomialOnNormalizedValarrayZipper_scrambleOut(size_t K, unsigned int N, const std::valarray< double > &p, unsigned int n[])
Multinomial draw assuming the probabilities sum to 1.0 and are all > 0, the output is an array of siz...
Definition: Uniform.h:656
static void MultinomialOnNormalizedValarray_expandedOut(size_t K, unsigned int N, const std::valarray< double > &p, unsigned int n[])
Multinomial draw assuming the probabilities sum to 1.0 and are all > 0, the output is an array of siz...
Definition: Uniform.h:602
static double Binomial(double p, unsigned int n)
Definition: Uniform.h:489
static void free()
Memory de-allocation.
Definition: Uniform.h:108
static long Seed1
Definition: Uniform.h:78
static void ScrambleArrayUInt(const int length, unsigned int *array)
Randomize the elements within an array.
Definition: Uniform.h:689
static double Poisson(double mean)
From the Numerical Recieps.
Definition: Uniform.h:219
static unsigned int Binomial2(double p, unsigned int n)
Definition: Uniform.h:535
static size_t Discrete(const gsl_ran_discrete_t *g)
Calling the GSL ran_discrete function.
Definition: Uniform.h:813
static unsigned long RandULong()
Return a random unsigned long, from uniform distribution.
Definition: Uniform.h:191
static void MultinomialOnNormalizedValarray_scrambleOut(size_t K, unsigned int N, const std::valarray< double > &p, unsigned int n[])
Multinomial draw assuming the probabilities sum to 1.0 and are all > 0, the output is an array of siz...
Definition: Uniform.h:627
static unsigned int Uniform(unsigned int max)
Returns a uniformly distributed random number from [0.0, max[.
Definition: Uniform.h:156
static void init(unsigned long seed)
Initialize the random generator's seed.
Definition: Uniform.h:82
static long Seed2
Definition: Uniform.h:78
static void Sample(const int from, const int to, const unsigned int num, int *result, bool replace)
Creates a sample of integers within range [from, to), with or without replacement.
Definition: Uniform.h:713
static double Beta(const double a, const double b)
Definition: Uniform.h:526
static double Gaussian(double sigma)
Definition: Uniform.h:261
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:124
static void SampleSeq(int from, int to, int by, unsigned int num, int *result, bool replace=false)
Definition: Uniform.h:749
static bool RandBool()
Returns a random boolean.
Definition: Uniform.h:162
static double LogNormal(double zeta, double sigma)
Definition: Uniform.h:357
static double Gamma(double a, double b)
Definition: Uniform.h:385