-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomFuncs.h
80 lines (66 loc) · 2.12 KB
/
RandomFuncs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef RANDOMFUNCS_H_INCLUDED
#define RANDOMFUNCS_H_INCLUDED
typedef std::chrono::high_resolution_clock myclock;
myclock::time_point beginning = myclock::now();
myclock::duration d = myclock::now() - beginning;
unsigned seed1 = d.count();
std::mt19937_64 RandGenerator (seed1); // minstd_rand0 is a standard linear_congruential_engine
//Declaration of functions producing random variables with specified parameters and standard random variables
#include "Declarations.h"
const gsl_rng_type * T;
gsl_rng * r;
std::normal_distribution<double> RandN(0,1.0);
std::uniform_real_distribution<double> RandU(0.0,1.0);
int RandBinomial(const gsl_rng * r, int n, double p){
//~ std::binomial_distribution<int> distribution(n,p);
//~ return distribution(RandGenerator);
//~ int sum =0;
//~ for(int i = 0;i<n;i++){
//~ if(RandU(RandGenerator)<=p){sum++;}
//~ }
return gsl_ran_binomial ( r, p, n);
;
}
int RandPoisson(const gsl_rng * r, double lambda){
//~ std::poisson_distribution<int> distribution(lambda);
//~ return distribution(RandGenerator);
//~ int sum = 0;
//~ double X = (-1)*log(RandU(RandGenerator));
//~ while(X<lambda){
//~ sum++;
//~ X += (-1)*log(RandU(RandGenerator));
//~ }
return gsl_ran_poisson (r, lambda);
}
double PoissonPmf(int x, double lambda){
//~ if(lambda > 0){
//~ return exp((-1)*lambda + double(x*log(lambda)) )/tgamma(x+1);
//~ }
//~ else{
//~ if(x == 0){return 1;}
//~ else{return 0;}
//~ }
return gsl_ran_poisson_pdf (x, lambda);
}
double PoissonF(int x, double lambda){
//~ double sum = 0;
//~ for(int k = 0; k<=x ;k++){
//~ sum += PoissonPmf(k,lambda);
//~ }
return gsl_cdf_poisson_P (x, lambda);
}
double PoissonQ(int x, double lambda){
return gsl_cdf_poisson_Q (x, lambda);
}
double RandGamma(const gsl_rng * r, double shape, double scale){
//~ std::gamma_distribution<double> distribution(shape,scale);
//~ return distribution(RandGenerator);
return gsl_ran_gamma (r,shape,scale);
}
int RandNegBin(const gsl_rng * r, double k, double p){
//Uses Poisson gamma mixture
//Wiki version of the parametrisation
double G = RandGamma(r,k,p/(1-p));
return RandPoisson(r,G);
}
#endif