forked from jacyara/GenESyS-Reborn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SamplerDanielBoso.cpp
209 lines (159 loc) · 4.89 KB
/
SamplerDanielBoso.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: SamplerDanielBoso.cpp
* Author: Daniel Boso
*
* Created on October 18, 2018 at 09:23
*/
#include <stdio.h>
#include "SamplerDanielBoso.h"
SamplerDanielBoso::SamplerDanielBoso() { }
SamplerDanielBoso::SamplerDanielBoso(const SamplerDanielBoso& orig) { }
SamplerDanielBoso::~SamplerDanielBoso() { }
double SamplerDanielBoso::random() {
return ((double) rand() / (RAND_MAX));
}
double SamplerDanielBoso::sampleUniform(double min, double max) {
return (min + ((max - min) * random()));
}
double SamplerDanielBoso::sampleExponential(double mean) {
return (mean * (log(random())));
}
double SamplerDanielBoso::sampleErlang(double mean, int M) {
int i;
double p = 1;
for(i = 0; i < M; i++) {
p *= random();
}
return ((mean / M) * (-log(p)));
}
double SamplerDanielBoso::sampleNormal(double mean, double stddev) {
double u1, u2, w, y;
if(mean <= 0 || stddev <= 0) {
return 0;
}
if(_normalFlag) {
do {
u1 = 2 * random() - 1.0;
u2 = 2 * random() - 1.0;
w = u1 * u1 + u2 * u2;
} while(w >= 1.0);
y = sqrt((-2 * log(w)) / w);
_normalResult = mean + u2 * y * stddev;
_normalFlag = false;
return mean + u1 * y * stddev;
} else {
_normalFlag = true;
return _normalResult;
}
}
double SamplerDanielBoso::gammaJonk(double alpha) {
double r, r1, r2, x, y;
do {
do {
r1 = random();
r2 = random();
} while((r1 > 1e-30) && (r2 > 1e-30));
if (log10(r2) / alpha < -1e3) {
x = 0;
} else {
x = exp(log(r2) / alpha);
}
if (log(r1) / (1 - alpha) < -1e3) {
y = 0;
} else {
y = exp(log(r1) / (1 - alpha));
}
} while(x + y <= 1);
do {
r = random();
} while(r > 1e-20);
return ((-log(r) * x) / (y + x));
}
double SamplerDanielBoso::sampleGamma(double mean, double alpha) {
int i;
double p;
int intAlpha;
double ostAlpha;
if(alpha < 1.0) {
return ((mean / alpha) * gammaJonk(alpha));
} else {
if(alpha = 1.0) {
return (mean * log(random()));
} else {
intAlpha = round(int(alpha));
ostAlpha = alpha - intAlpha;
do {
p = 1;
for(i = 0; i < intAlpha; i++) {
p *= random();
}
} while(p > 0);
if(ostAlpha > 0) {
return (mean / alpha) * ((-log(p)) + gammaJonk(ostAlpha));
} else {
return (mean / alpha) * (-log(p));
}
}
}
}
double SamplerDanielBoso::sampleBeta(double alpha, double beta, double infLimit, double supLimit) {
double x, y1, y2;
do {
y1 = sampleGamma(alpha, alpha);
y2 = sampleGamma(beta, beta);
x = y1 / (y1 + y2);
} while((x >= 0) && (x <= 1.0));
return (infLimit + (supLimit - infLimit ) * x);
}
double SamplerDanielBoso::sampleWeibull(double alpha, double scale) {
return (exp(log(scale * (-log(random()))) / alpha));
}
double SamplerDanielBoso::sampleLogNormal(double mean, double stddev) {
double meanNormal, dispNormal;
dispNormal = log((stddev * stddev) / (mean * mean) + 1.0);
meanNormal = log(mean) - 0.5 * dispNormal;
return (exp(sampleNormal(meanNormal, sqrt(dispNormal))));
}
double SamplerDanielBoso::sampleTriangular(double min, double mode, double max) {
double part1, part2, full, r;
part1 = mode-min;
part2 = max-mode;
full = max-min;
r = random();
if(r <= part1/full) {
return(min + sqrt(part1 * full * r));
} else {
return(max - sqrt(part2 * full * (1.0-r)));
}
}
// Inspired by resolution of Bruno Bonotto, Fabíola Maria Kretzer and João Vicente Souto
double SamplerDanielBoso::sampleDiscrete(double value, double acumProb, ...) {
va_list args;
va_start(args, acumProb);
double lower_limit = 0;
double index_value = value;
double acumulated_probability = acumProb;
double random_value = random();
while(acumulated_probability < 1) {
if(lower_limit <= random_value && acumulated_probability >= random_value) {
va_end(args);
return index_value;
}
index_value = va_arg(args, double);
lower_limit = acumulated_probability;
acumulated_probability = va_arg(args, double);
}
va_end(args);
return 0;
}
void SamplerDanielBoso::setRNGparameters(SamplerDanielBoso::RNG_Parameters* param){
this->_param = param;
}
SamplerDanielBoso::RNG_Parameters* SamplerDanielBoso::getRNGparameters() const {
return this->_param;
}