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);
95 stream->print_sprng();
100 #elif defined(HAS_GSL)
102 init_gsl( gsl_rng_mt19937, seed );
114 #if defined(HAS_SPRNG)
116 #elif defined(HAS_GSL)
130 return stream->sprng();
131 #elif defined(HAS_GSL)
132 return gsl_rng_uniform(r);
139 Seed1 = 40014 * (
Seed1 - w * 53668) - w * 12211;
145 Seed2 = 40692 * (
Seed2 - w * 52774) - w * 3791;
151 if (z < 1) z += 2147483562;
153 }
while (!((z * 4.656613e-10) < 1.0));
155 return (z * 4.656613e-10);
159 static inline unsigned int Uniform (
unsigned int max)
161 return (
unsigned int)(
Uniform() * max);
168 static int intrand = stream->isprng();
169 #elif defined(HAS_GSL)
170 static unsigned long int intrand = gsl_rng_get(r);
172 static int intrand = (int)(
Uniform()*(double)std::numeric_limits<int>::max());
175 static unsigned int num = 0;
181 intrand = stream->isprng();
182 #elif defined(HAS_GSL)
183 intrand = gsl_rng_get(r);
185 intrand = (int)(
Uniform()*(double)std::numeric_limits<int>::max());
189 return (intrand & (1 << num) );
195 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
196 return gsl_rng_get(r);
198 unsigned long rnd, limit = 0x10000000;
200 rnd = (
unsigned long)(
Uniform()*ULONG_MAX);
207 static inline double gammln (
double xx) {
208 double x,y,tmp,ser=1.000000000190015;
209 static double cof[6]={76.18009172947146,-86.50532032941677,
210 24.01409824083091,-1.231739572450155,
211 0.1208650973866179e-2,-0.5395239384953e-5};
215 tmp -= (x+0.5)*log(tmp);
216 for (j = 0; j < 6; ++j) ser += cof[j]/++y;
218 return -tmp+log(2.5066282746310005*ser/x);
224 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
225 return gsl_ran_poisson(r, mean);
227 static double sq,alxm,g,oldm=(-1.0);
249 g=mean*alxm-
gammln(mean+1.0);
257 t=0.9*(1.0+y*y)*exp(em*alxm-
gammln(em+1.0)-g);
266 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
268 return gsl_ran_gaussian_ziggurat (r, sigma);
291 double u, v, x, y, Q;
292 const double s = 0.449871;
293 const double t = -0.386595;
294 const double a = 0.19600;
295 const double b = 0.25472;
296 const double r1 = 0.27597;
297 const double r2 = 0.27846;
320 Q = x * x + y * (a * y - b * x);
327 while (Q >= r1 && (Q > r2 || v * v > -4 * u * u * log (u)));
329 return sigma * (v / u);
333 static inline void BivariateGaussian(
double sigma1,
double sigma2,
double rho,
double *out1,
double *out2) {
334 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
335 gsl_ran_bivariate_gaussian(r,sigma1,sigma2,rho,out1,out2);
338 double u, v, r2, scale;
350 while (r2 > 1.0 || r2 == 0);
352 scale = sqrt (-2.0 * log (r2) / r2);
354 *out1 = sigma1 * u * scale;
355 *out2 = sigma2 * (rho * u + sqrt(1 - rho*rho) * v) * scale;
360 static inline double LogNormal (
double zeta,
double sigma) {
361 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
362 return gsl_ran_lognormal(r,zeta,sigma);
365 double u, v, r2, normal, z;
377 while (r2 > 1.0 || r2 == 0);
379 normal = u * sqrt (-2.0 * log (r2) / r2);
381 z = exp (sigma * normal + zeta);
388 static inline double Gamma (
double a,
double b) {
389 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
390 return gsl_ran_gamma(r, a, b);
399 return Gamma(1.0 + a, b) * pow (u, 1.0 / a);
404 double d = a - 1.0 / 3.0;
405 double c = (1.0 / 3.0) / sqrt (d);
419 if (u < 1 - 0.0331 * x * x * x * x)
422 if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
434 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
435 return gsl_ran_bernoulli(r, p);
455 static inline void MultivariateGaussian(gsl_vector *eval, gsl_matrix *evec,
456 gsl_vector *workspace, gsl_vector *px)
460 for (i=0; i<eval->size; i++)
461 gsl_vector_set (workspace, i,
462 Gaussian(gsl_vector_get (eval, i)));
464 gsl_blas_dgemv (CblasNoTrans, 1.0,
465 evec, workspace, 0.0, px);
469 static inline void MultivariateGaussianCholesky(gsl_vector *sigma, gsl_matrix *M, gsl_vector *px)
471 for (
size_t i = 0; i < sigma->size; i++)
472 gsl_vector_set (px, i,
Gaussian(gsl_vector_get(sigma, i)));
474 gsl_blas_dtrmv (CblasLower, CblasNoTrans, CblasNonUnit, M, px);
477 static inline long double factoriel(
unsigned int x) {
479 for(
unsigned int i=1;i<=x;++i)
492 static inline double Binomial(
double p,
unsigned int n)
495 unsigned int i, a, b, k = 0;
514 p = (p - X) / (1 - X);
518 for (i = 0; i < n; i++)
529 static inline double Beta (
const double a,
const double b)
535 return x1 / (x1 + x2);
538 static inline unsigned int Binomial2 (
double p,
unsigned int n){
540 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
541 return gsl_ran_binomial (r, p, n);
547 static inline void Multinomial (
size_t K,
unsigned int N,
const double p[],
unsigned int n[])
549 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
550 return gsl_ran_multinomial (r, K, N, p, n);
557 unsigned int sum_n = 0;
564 for (k = 0; k < K; k++)
569 for (k = 0; k < K; k++)
573 n[k] =
Binomial (p[k] / (norm - sum_p), N - sum_n);
594 unsigned int sum_n = 0;
596 for (k = 0; k < K; k++)
606 const std::valarray<double> &p,
612 unsigned int _n, sum_n = 0;
613 unsigned int i = 0, pos = 0;
615 for (k = 0; k < K; k++) {
619 for(i = 0; i < _n; i++)
631 const std::valarray<double> &p,
637 unsigned int _n, sum_n = 0;
638 unsigned int i = 0, pos = 0;
640 for (k = 0; k < K; k++) {
644 for(i = 0; i < _n; i++)
660 const std::valarray<double> &p,
668 for (k = 0; k < N; k++) {
675 while( sum < rand ) {
694 unsigned int size = length, pos, last = length - 1, num = length -1, el;
698 for (
unsigned int i = 0; i < num; i++) {
701 array[last] = array[pos];
716 static inline void Sample (
const int from,
const int to,
const unsigned int num,
int* result,
bool replace)
720 unsigned int seq_length = to - from;
722 assert(num <= seq_length);
725 int *seq =
new int [seq_length];
729 for(
unsigned int i = 1; seq[i-1] < to && i < seq_length; ++i)
730 seq[i] = seq[i-1] + 1;
734 unsigned int size = seq_length, pos, last = seq_length - 1;
736 for (
unsigned int i = 0; i < num; i++) {
738 result[i] = seq[pos];
739 seq[pos] = seq[last];
745 for (
unsigned int i = 0; i < num; i++)
752 static inline void SampleSeq(
int from,
int to,
int by,
unsigned int num,
int* result,
bool replace =
false)
754 assert(from < to && by < to && by > 0);
756 unsigned int seq_length = (int)((to - from) / by);
758 assert(num <= seq_length);
760 int *seq =
new int [seq_length];
764 for(
unsigned int i = 1; seq[i-1] + by < to && i < seq_length; ++i) seq[i] = seq[i-1] + by;
768 unsigned int size = seq_length, pos, last = seq_length - 1;
770 for (
unsigned int i = 0; i < num; i++) {
772 result[i] = seq[pos];
773 seq[pos] = seq[last];
779 for (
unsigned int i = 0; i < num; i++) result[i] = seq[
RAND::Uniform(seq_length) ];
785 static inline void SampleSeqWithReciprocal(
int from,
int to,
int by,
unsigned int num1,
int* result1,
unsigned int num2,
int* result2)
787 assert(from < to && by < to && by > 0);
789 unsigned int seq_length = (int)((to - from) / by);
791 assert(num1 + num2 == seq_length);
793 int *seq =
new int [seq_length];
797 for(
unsigned int i = 1; seq[i-1] + by < to && i < seq_length; ++i) seq[i] = seq[i-1] + by;
799 unsigned int size = seq_length, pos, last = seq_length - 1;
801 for (
unsigned int i = 0; i < num1; i++) {
803 result1[i] = seq[pos];
804 seq[pos] = seq[last];
805 seq[last] = result1[i];
809 for (
unsigned int i = 0; i < num2 && i < size; i++)
816 static inline size_t Discrete (
const gsl_ran_discrete_t *g)
818 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
819 return gsl_ran_discrete(r, g);
836 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:450
static double Bernoulli(double p)
Definition: Uniform.h:432
static void SampleSeqWithReciprocal(int from, int to, int by, unsigned int num1, int *result1, unsigned int num2, int *result2)
Definition: Uniform.h:785
static double gammln(double xx)
From the Numerical Recieps.
Definition: Uniform.h:207
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:587
static void Multinomial(size_t K, unsigned int N, const double p[], unsigned int n[])
Definition: Uniform.h:547
static void BivariateGaussian(double sigma1, double sigma2, double rho, double *out1, double *out2)
Definition: Uniform.h:333
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:659
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:605
static double Binomial(double p, unsigned int n)
Definition: Uniform.h:492
static void free()
Memory de-allocation.
Definition: Uniform.h:111
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:692
static double Poisson(double mean)
From the Numerical Recieps.
Definition: Uniform.h:222
static unsigned int Binomial2(double p, unsigned int n)
Definition: Uniform.h:538
static size_t Discrete(const gsl_ran_discrete_t *g)
Calling the GSL ran_discrete function.
Definition: Uniform.h:816
static unsigned long RandULong()
Return a random unsigned long, from uniform distribution.
Definition: Uniform.h:194
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:630
static unsigned int Uniform(unsigned int max)
Returns a uniformly distributed random number from [0.0, max[.
Definition: Uniform.h:159
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:716
static double Beta(const double a, const double b)
Definition: Uniform.h:529
static double Gaussian(double sigma)
Definition: Uniform.h:264
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:127
static void SampleSeq(int from, int to, int by, unsigned int num, int *result, bool replace=false)
Definition: Uniform.h:752
static bool RandBool()
Returns a random boolean.
Definition: Uniform.h:165
static double LogNormal(double zeta, double sigma)
Definition: Uniform.h:360
static double Gamma(double a, double b)
Definition: Uniform.h:388
void message(const char *message,...)
Definition: output.cc:40