forked from edrosten/threeB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoisson.cc
81 lines (65 loc) · 1.16 KB
/
poisson.cc
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
80
81
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "mt19937.h"
#include "poisson.h"
#include <vector>
using namespace std;
namespace {
void set_mt19937(void* r, unsigned long int seed)
{
MT19937& rng = *((MT19937*)r);
rng.simple_seed(seed);
}
unsigned long int sample_int_mt19937(void* r)
{
MT19937& rng = *((MT19937*)r);
return rng.rand_int();
}
double sample_double_mt19937(void* r)
{
MT19937& rng = *((MT19937*)r);
return rng();
}
static const gsl_rng_type rng_local_19937 =
{
"local MT19937",
0xffffffff,
0,
0,
set_mt19937,
sample_int_mt19937,
sample_double_mt19937
};
static double fact(int n)
{
double f=1;
for(int i=2; i <= n; i++)
f*=i;
return f;
}
}
unsigned int poisson(double mu, MT19937& rng)
{
gsl_rng grng = { &rng_local_19937, &rng};
return gsl_ran_poisson (&grng, mu);
}
/*int main()
{
MT19937 rng;
vector<int> hist(200);
int sum=0;
double mu=100.5;
for(int i=0; i < 10000000; i++)
{
unsigned int n = fish(mu, rng);
if(n < hist.size())
{
hist[n]++;
sum++;
}
}
for(unsigned int i=0; i < hist.size(); i++)
{
cout << i << " " << hist[i]*1.0/sum << " " << (pow(mu, i)/fact(i) * exp(-mu)) << endl;
}
}*/