37 #include <sprng_cpp.h>
41 #include <gsl/gsl_rng.h>
42 #include <gsl/gsl_randist.h>
43 #include <gsl/gsl_blas.h>
44 #include <gsl/gsl_math.h>
45 #include <gsl/gsl_vector.h>
46 #include <gsl/gsl_matrix.h>
47 #include <gsl/gsl_permutation.h>
60 #if defined(HAS_SPRNG)
64 #elif defined(HAS_GSL)
68 static void init_gsl(
const gsl_rng_type* T,
unsigned long seed)
70 r = gsl_rng_alloc (T);
80 static void init (
unsigned long seed) {
82 #if defined(HAS_SPRNG)
85 stream = SelectType(4);
93 stream->print_sprng();
98 #elif defined(HAS_GSL)
100 init_gsl( gsl_rng_mt19937, seed );
112 #if defined(HAS_SPRNG)
114 #elif defined(HAS_GSL)
128 return stream->sprng();
129 #elif defined(HAS_GSL)
130 return gsl_rng_uniform(r);
137 Seed1 = 40014 * (
Seed1 - w * 53668) - w * 12211;
143 Seed2 = 40692 * (
Seed2 - w * 52774) - w * 3791;
149 if (z < 1) z += 2147483562;
151 }
while (!((z * 4.656613e-10) < 1.0));
153 return (z * 4.656613e-10);
157 static inline unsigned int Uniform (
unsigned int max)
159 return (
unsigned int)(
Uniform() * max);
173 static const unsigned int NBITS = 31;
174 static unsigned long intrand = (
unsigned long)stream->isprng();
176 static const unsigned int NBITS = (
unsigned int)(CHAR_BIT *
sizeof(
unsigned long));
177 static unsigned long intrand =
RandULong();
179 static unsigned int num = 0;
182 if ( num == NBITS ) {
186 intrand = (
unsigned long)stream->isprng();
192 return ( intrand >> num++ ) & 1UL;
201 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
202 return ((
unsigned long)(gsl_rng_get(r) & 0xFFFFFFFFUL) << 32)
203 | (
unsigned long)(gsl_rng_get(r) & 0xFFFFFFFFUL);
205 unsigned long rnd, limit = 0x10000000;
207 rnd = (
unsigned long)(
Uniform()*ULONG_MAX);
214 static inline double gammln (
double xx) {
215 double x,y,tmp,ser=1.000000000190015;
216 static double cof[6]={76.18009172947146,-86.50532032941677,
217 24.01409824083091,-1.231739572450155,
218 0.1208650973866179e-2,-0.5395239384953e-5};
222 tmp -= (x+0.5)*log(tmp);
223 for (j = 0; j < 6; ++j) ser += cof[j]/++y;
225 return -tmp+log(2.5066282746310005*ser/x);
231 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
232 return gsl_ran_poisson(r, mean);
234 static double sq,alxm,g,oldm=(-1.0);
256 g=mean*alxm-
gammln(mean+1.0);
264 t=0.9*(1.0+y*y)*exp(em*alxm-
gammln(em+1.0)-g);
273 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
275 return gsl_ran_gaussian_ziggurat (r, sigma);
298 double u, v, x, y, Q;
299 const double s = 0.449871;
300 const double t = -0.386595;
301 const double a = 0.19600;
302 const double b = 0.25472;
303 const double r1 = 0.27597;
304 const double r2 = 0.27846;
327 Q = x * x + y * (a * y - b * x);
334 while (Q >= r1 && (Q > r2 || v * v > -4 * u * u * log (u)));
336 return sigma * (v / u);
340 static inline void BivariateGaussian(
double sigma1,
double sigma2,
double rho,
double *out1,
double *out2) {
341 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
342 gsl_ran_bivariate_gaussian(r,sigma1,sigma2,rho,out1,out2);
345 double u, v, r2, scale;
357 while (r2 > 1.0 || r2 == 0);
359 scale = sqrt (-2.0 * log (r2) / r2);
361 *out1 = sigma1 * u * scale;
362 *out2 = sigma2 * (rho * u + sqrt(1 - rho*rho) * v) * scale;
367 static inline double LogNormal (
double zeta,
double sigma) {
368 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
369 return gsl_ran_lognormal(r,zeta,sigma);
372 double u, v, r2, normal, z;
384 while (r2 > 1.0 || r2 == 0);
386 normal = u * sqrt (-2.0 * log (r2) / r2);
388 z = exp (sigma * normal + zeta);
395 static inline double Gamma (
double a,
double b) {
396 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
397 return gsl_ran_gamma(r, a, b);
406 return Gamma(1.0 + a, b) * pow (u, 1.0 / a);
411 double d = a - 1.0 / 3.0;
412 double c = (1.0 / 3.0) / sqrt (d);
426 if (u < 1 - 0.0331 * x * x * x * x)
429 if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
441 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
442 return gsl_ran_bernoulli(r, p);
462 static inline void MultivariateGaussian(gsl_vector *eval, gsl_matrix *evec,
463 gsl_vector *workspace, gsl_vector *px)
467 for (i=0; i<eval->size; i++)
468 gsl_vector_set (workspace, i,
469 Gaussian(gsl_vector_get (eval, i)));
471 gsl_blas_dgemv (CblasNoTrans, 1.0,
472 evec, workspace, 0.0, px);
476 static inline void MultivariateGaussianCholesky(gsl_vector *sigma, gsl_matrix *M, gsl_vector *px)
478 for (
size_t i = 0; i < sigma->size; i++)
479 gsl_vector_set (px, i,
Gaussian(gsl_vector_get(sigma, i)));
481 gsl_blas_dtrmv (CblasLower, CblasNoTrans, CblasNonUnit, M, px);
484 static inline long double factoriel(
unsigned int x) {
486 for(
unsigned int i=1;i<=x;++i)
499 static inline double Binomial(
double p,
unsigned int n)
502 unsigned int i, a, b, k = 0;
521 p = (p - X) / (1 - X);
525 for (i = 0; i < n; i++)
536 static inline double Beta (
const double a,
const double b)
542 return x1 / (x1 + x2);
545 static inline unsigned int Binomial2 (
double p,
unsigned int n){
547 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
548 return gsl_ran_binomial (r, p, n);
554 static inline void Multinomial (
size_t K,
unsigned int N,
const double p[],
unsigned int n[])
556 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
557 return gsl_ran_multinomial (r, K, N, p, n);
564 unsigned int sum_n = 0;
571 for (k = 0; k < K; k++)
576 for (k = 0; k < K; k++)
580 n[k] =
Binomial (p[k] / (norm - sum_p), N - sum_n);
601 unsigned int sum_n = 0;
603 for (k = 0; k < K; k++)
613 const std::valarray<double> &p,
619 unsigned int _n, sum_n = 0;
620 unsigned int i = 0, pos = 0;
622 for (k = 0; k < K; k++) {
626 for(i = 0; i < _n; i++)
638 const std::valarray<double> &p,
644 unsigned int _n, sum_n = 0;
645 unsigned int i = 0, pos = 0;
647 for (k = 0; k < K; k++) {
651 for(i = 0; i < _n; i++)
667 const std::valarray<double> &p,
675 for (k = 0; k < N; k++) {
682 while( sum < rand ) {
701 unsigned int size = length, pos, last = length - 1, num = length -1, el;
705 for (
unsigned int i = 0; i < num; i++) {
708 array[last] = array[pos];
723 static inline void Sample (
const int from,
const int to,
const unsigned int num,
int* result,
bool replace)
727 unsigned int seq_length = to - from;
729 assert(num <= seq_length);
732 int *seq =
new int [seq_length];
736 for(
unsigned int i = 1; seq[i-1] < to && i < seq_length; ++i)
737 seq[i] = seq[i-1] + 1;
741 unsigned int size = seq_length, pos, last = seq_length - 1;
743 for (
unsigned int i = 0; i < num; i++) {
745 result[i] = seq[pos];
746 seq[pos] = seq[last];
752 for (
unsigned int i = 0; i < num; i++)
759 static inline void SampleSeq(
int from,
int to,
int by,
unsigned int num,
int* result,
bool replace =
false)
761 assert(from < to && by < to && by > 0);
763 unsigned int seq_length = (int)((to - from) / by);
765 assert(num <= seq_length);
767 int *seq =
new int [seq_length];
771 for(
unsigned int i = 1; seq[i-1] + by < to && i < seq_length; ++i) seq[i] = seq[i-1] + by;
775 unsigned int size = seq_length, pos, last = seq_length - 1;
777 for (
unsigned int i = 0; i < num; i++) {
779 result[i] = seq[pos];
780 seq[pos] = seq[last];
786 for (
unsigned int i = 0; i < num; i++) result[i] = seq[
RAND::Uniform(seq_length) ];
792 static inline void SampleSeqWithReciprocal(
int from,
int to,
int by,
unsigned int num1,
int* result1,
unsigned int num2,
int* result2)
794 assert(from < to && by < to && by > 0);
796 unsigned int seq_length = (int)((to - from) / by);
798 assert(num1 + num2 == seq_length);
800 int *seq =
new int [seq_length];
804 for(
unsigned int i = 1; seq[i-1] + by < to && i < seq_length; ++i) seq[i] = seq[i-1] + by;
806 unsigned int size = seq_length, pos, last = seq_length - 1;
808 for (
unsigned int i = 0; i < num1; i++) {
810 result1[i] = seq[pos];
811 seq[pos] = seq[last];
812 seq[last] = result1[i];
816 for (
unsigned int i = 0; i < num2 && i < size; i++)
823 static inline size_t Discrete (
const gsl_ran_discrete_t *g)
825 #if defined(HAS_GSL) && !defined(HAS_SPRNG)
826 return gsl_ran_discrete(r, g);
843 if (f == 1.0)
return c;
MPI environment setup.
Definition: MPImanager.h:120
int workerCount() const
Definition: MPImanager.h:127
int workerRank() const
Definition: MPImanager.h:128
Random number generation class, uses various types of random generators depending on the implementati...
Definition: Uniform.h:53
static double Exponential(double mu)
Definition: Uniform.h:457
static double Bernoulli(double p)
Definition: Uniform.h:439
static void SampleSeqWithReciprocal(int from, int to, int by, unsigned int num1, int *result1, unsigned int num2, int *result2)
Definition: Uniform.h:792
static double gammln(double xx)
From the Numerical Recieps.
Definition: Uniform.h:214
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:594
static void Multinomial(size_t K, unsigned int N, const double p[], unsigned int n[])
Definition: Uniform.h:554
static void BivariateGaussian(double sigma1, double sigma2, double rho, double *out1, double *out2)
Definition: Uniform.h:340
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:666
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:612
static double Binomial(double p, unsigned int n)
Definition: Uniform.h:499
static void free()
Memory de-allocation.
Definition: Uniform.h:109
static long Seed1
Definition: Uniform.h:76
static void ScrambleArrayUInt(const int length, unsigned int *array)
Randomize the elements within an array.
Definition: Uniform.h:699
static double Poisson(double mean)
From the Numerical Recieps.
Definition: Uniform.h:229
static unsigned int Binomial2(double p, unsigned int n)
Definition: Uniform.h:545
static size_t Discrete(const gsl_ran_discrete_t *g)
Calling the GSL ran_discrete function.
Definition: Uniform.h:823
static unsigned long RandULong()
Return a random unsigned long, from uniform distribution.
Definition: Uniform.h:200
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:637
static unsigned int Uniform(unsigned int max)
Returns a uniformly distributed random number from [0.0, max[.
Definition: Uniform.h:157
static void init(unsigned long seed)
Initialize the random generator's seed.
Definition: Uniform.h:80
static long Seed2
Definition: Uniform.h:76
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:723
static double Beta(const double a, const double b)
Definition: Uniform.h:536
static double Gaussian(double sigma)
Definition: Uniform.h:271
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:125
static void SampleSeq(int from, int to, int by, unsigned int num, int *result, bool replace=false)
Definition: Uniform.h:759
static bool RandBool()
Returns a random boolean.
Definition: Uniform.h:170
static double LogNormal(double zeta, double sigma)
Definition: Uniform.h:367
static double Gamma(double a, double b)
Definition: Uniform.h:395
void message(const char *message,...)
Definition: output.cc:38