130 | | //A copy of rgamma code from the R package!! |
131 | | // |
132 | | |
133 | | /* Constants : */ |
134 | | const static double sqrt32 = 5.656854; |
135 | | const static double exp_m1 = 0.36787944117144232159;/* exp(-1) = 1/e */ |
136 | | |
137 | | /* Coefficients q[k] - for q0 = sum(q[k]*a^(-k)) |
138 | | * Coefficients a[k] - for q = q0+(t*t/2)*sum(a[k]*v^k) |
139 | | * Coefficients e[k] - for exp(q)-1 = sum(e[k]*q^k) |
140 | | */ |
141 | | const static double q1 = 0.04166669; |
142 | | const static double q2 = 0.02083148; |
143 | | const static double q3 = 0.00801191; |
144 | | const static double q4 = 0.00144121; |
145 | | const static double q5 = -7.388e-5; |
146 | | const static double q6 = 2.4511e-4; |
147 | | const static double q7 = 2.424e-4; |
148 | | |
149 | | const static double a1 = 0.3333333; |
150 | | const static double a2 = -0.250003; |
151 | | const static double a3 = 0.2000062; |
152 | | const static double a4 = -0.1662921; |
153 | | const static double a5 = 0.1423657; |
154 | | const static double a6 = -0.1367177; |
155 | | const static double a7 = 0.1233795; |
156 | | |
157 | | /* State variables [FIXME for threading!] :*/ |
158 | | static double aa = 0.; |
159 | | static double aaa = 0.; |
160 | | static double s, s2, d; /* no. 1 (step 1) */ |
161 | | static double q0, b, si, c;/* no. 2 (step 4) */ |
162 | | |
163 | | double e, p, q, r, t, u, v, w, x, ret_val; |
164 | | double a = alpha; |
165 | | double scale = 1.0 / beta; |
166 | | |
167 | | if ( !R_FINITE ( a ) || !R_FINITE ( scale ) || a < 0.0 || scale <= 0.0 ) { |
168 | | it_error ( "Gamma_RNG wrong parameters" ); |
169 | | } |
170 | | |
171 | | if ( a < 1. ) { /* GS algorithm for parameters a < 1 */ |
172 | | if ( a == 0 ) |
173 | | return 0.; |
174 | | e = 1.0 + exp_m1 * a; |
175 | | for ( ;; ) { //VS repeat |
176 | | p = e * unif_rand(); |
177 | | if ( p >= 1.0 ) { |
178 | | x = -log ( ( e - p ) / a ); |
179 | | if ( exp_rand() >= ( 1.0 - a ) * log ( x ) ) |
180 | | break; |
181 | | } else { |
182 | | x = exp ( log ( p ) / a ); |
183 | | if ( exp_rand() >= x ) |
184 | | break; |
185 | | } |
186 | | } |
187 | | return scale * x; |
188 | | } |
189 | | |
190 | | /* --- a >= 1 : GD algorithm --- */ |
191 | | |
192 | | /* Step 1: Recalculations of s2, s, d if a has changed */ |
193 | | if ( a != aa ) { |
194 | | aa = a; |
195 | | s2 = a - 0.5; |
196 | | s = sqrt ( s2 ); |
197 | | d = sqrt32 - s * 12.0; |
198 | | } |
199 | | /* Step 2: t = standard normal deviate, |
200 | | x = (s,1/2) -normal deviate. */ |
201 | | |
202 | | /* immediate acceptance (i) */ |
203 | | t = norm_rand(); |
204 | | x = s + 0.5 * t; |
205 | | ret_val = x * x; |
206 | | if ( t >= 0.0 ) |
207 | | return scale * ret_val; |
208 | | |
209 | | /* Step 3: u = 0,1 - uniform sample. squeeze acceptance (s) */ |
210 | | u = unif_rand(); |
211 | | if ( ( d * u ) <= ( t * t * t ) ) |
212 | | return scale * ret_val; |
213 | | |
214 | | /* Step 4: recalculations of q0, b, si, c if necessary */ |
215 | | |
216 | | if ( a != aaa ) { |
217 | | aaa = a; |
218 | | r = 1.0 / a; |
219 | | q0 = ( ( ( ( ( ( q7 * r + q6 ) * r + q5 ) * r + q4 ) * r + q3 ) * r |
220 | | + q2 ) * r + q1 ) * r; |
221 | | |
222 | | /* Approximation depending on size of parameter a */ |
223 | | /* The constants in the expressions for b, si and c */ |
224 | | /* were established by numerical experiments */ |
225 | | |
226 | | if ( a <= 3.686 ) { |
227 | | b = 0.463 + s + 0.178 * s2; |
228 | | si = 1.235; |
229 | | c = 0.195 / s - 0.079 + 0.16 * s; |
230 | | } else if ( a <= 13.022 ) { |
231 | | b = 1.654 + 0.0076 * s2; |
232 | | si = 1.68 / s + 0.275; |
233 | | c = 0.062 / s + 0.024; |
234 | | } else { |
235 | | b = 1.77; |
236 | | si = 0.75; |
237 | | c = 0.1515 / s; |
238 | | } |
239 | | } |
240 | | /* Step 5: no quotient test if x not positive */ |
241 | | |
242 | | if ( x > 0.0 ) { |
243 | | /* Step 6: calculation of v and quotient q */ |
244 | | v = t / ( s + s ); |
245 | | if ( fabs ( v ) <= 0.25 ) |
246 | | q = q0 + 0.5 * t * t * ( ( ( ( ( ( a7 * v + a6 ) * v + a5 ) * v + a4 ) * v |
247 | | + a3 ) * v + a2 ) * v + a1 ) * v; |
248 | | else |
249 | | q = q0 - s * t + 0.25 * t * t + ( s2 + s2 ) * log ( 1.0 + v ); |
250 | | |
251 | | |
252 | | /* Step 7: quotient acceptance (q) */ |
253 | | if ( log ( 1.0 - u ) <= q ) |
254 | | return scale * ret_val; |
255 | | } |
256 | | |
257 | | for ( ;; ) { //VS repeat |
258 | | /* Step 8: e = standard exponential deviate |
259 | | * u = 0,1 -uniform deviate |
260 | | * t = (b,si)-double exponential (laplace) sample */ |
261 | | e = exp_rand(); |
262 | | u = unif_rand(); |
263 | | u = u + u - 1.0; |
264 | | if ( u < 0.0 ) |
265 | | t = b - si * e; |
266 | | else |
267 | | t = b + si * e; |
268 | | /* Step 9: rejection if t < tau(1) = -0.71874483771719 */ |
269 | | if ( t >= -0.71874483771719 ) { |
270 | | /* Step 10: calculation of v and quotient q */ |
271 | | v = t / ( s + s ); |
272 | | if ( fabs ( v ) <= 0.25 ) |
273 | | q = q0 + 0.5 * t * t * |
274 | | ( ( ( ( ( ( a7 * v + a6 ) * v + a5 ) * v + a4 ) * v + a3 ) * v |
275 | | + a2 ) * v + a1 ) * v; |
276 | | else |
277 | | q = q0 - s * t + 0.25 * t * t + ( s2 + s2 ) * log ( 1.0 + v ); |
278 | | /* Step 11: hat acceptance (h) */ |
279 | | /* (if q not positive go to step 8) */ |
280 | | if ( q > 0.0 ) { |
281 | | // TODO: w = expm1(q); |
282 | | w = exp ( q ) - 1; |
283 | | /* ^^^^^ original code had approximation with rel.err < 2e-7 */ |
284 | | /* if t is rejected sample again at step 8 */ |
285 | | if ( ( c * fabs ( u ) ) <= ( w * exp ( e - 0.5 * t * t ) ) ) |
286 | | break; |
287 | | } |
288 | | } |
289 | | } /* repeat .. until `t' is accepted */ |
290 | | x = s + 0.5 * t; |
291 | | return scale * x * x; |
| 130 | //A copy of rgamma code from the R package!! |
| 131 | // |
| 132 | |
| 133 | /* Constants : */ |
| 134 | const static double sqrt32 = 5.656854; |
| 135 | const static double exp_m1 = 0.36787944117144232159;/* exp(-1) = 1/e */ |
| 136 | |
| 137 | /* Coefficients q[k] - for q0 = sum(q[k]*a^(-k)) |
| 138 | * Coefficients a[k] - for q = q0+(t*t/2)*sum(a[k]*v^k) |
| 139 | * Coefficients e[k] - for exp(q)-1 = sum(e[k]*q^k) |
| 140 | */ |
| 141 | const static double q1 = 0.04166669; |
| 142 | const static double q2 = 0.02083148; |
| 143 | const static double q3 = 0.00801191; |
| 144 | const static double q4 = 0.00144121; |
| 145 | const static double q5 = -7.388e-5; |
| 146 | const static double q6 = 2.4511e-4; |
| 147 | const static double q7 = 2.424e-4; |
| 148 | |
| 149 | const static double a1 = 0.3333333; |
| 150 | const static double a2 = -0.250003; |
| 151 | const static double a3 = 0.2000062; |
| 152 | const static double a4 = -0.1662921; |
| 153 | const static double a5 = 0.1423657; |
| 154 | const static double a6 = -0.1367177; |
| 155 | const static double a7 = 0.1233795; |
| 156 | |
| 157 | /* State variables [FIXME for threading!] :*/ |
| 158 | static double aa = 0.; |
| 159 | static double aaa = 0.; |
| 160 | static double s, s2, d; /* no. 1 (step 1) */ |
| 161 | static double q0, b, si, c;/* no. 2 (step 4) */ |
| 162 | |
| 163 | double e, p, q, r, t, u, v, w, x, ret_val; |
| 164 | double a = alpha; |
| 165 | double scale = 1.0 / beta; |
| 166 | |
| 167 | if ( !R_FINITE ( a ) || !R_FINITE ( scale ) || a < 0.0 || scale <= 0.0 ) { |
| 168 | it_error ( "Gamma_RNG wrong parameters" ); |
| 169 | } |
| 170 | |
| 171 | if ( a < 1. ) { /* GS algorithm for parameters a < 1 */ |
| 172 | if ( a == 0 ) |
| 173 | return 0.; |
| 174 | e = 1.0 + exp_m1 * a; |
| 175 | for ( ;; ) { //VS repeat |
| 176 | p = e * unif_rand(); |
| 177 | if ( p >= 1.0 ) { |
| 178 | x = -log ( ( e - p ) / a ); |
| 179 | if ( exp_rand() >= ( 1.0 - a ) * log ( x ) ) |
| 180 | break; |
| 181 | } else { |
| 182 | x = exp ( log ( p ) / a ); |
| 183 | if ( exp_rand() >= x ) |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | return scale * x; |
| 188 | } |
| 189 | |
| 190 | /* --- a >= 1 : GD algorithm --- */ |
| 191 | |
| 192 | /* Step 1: Recalculations of s2, s, d if a has changed */ |
| 193 | if ( a != aa ) { |
| 194 | aa = a; |
| 195 | s2 = a - 0.5; |
| 196 | s = sqrt ( s2 ); |
| 197 | d = sqrt32 - s * 12.0; |
| 198 | } |
| 199 | /* Step 2: t = standard normal deviate, |
| 200 | x = (s,1/2) -normal deviate. */ |
| 201 | |
| 202 | /* immediate acceptance (i) */ |
| 203 | t = norm_rand(); |
| 204 | x = s + 0.5 * t; |
| 205 | ret_val = x * x; |
| 206 | if ( t >= 0.0 ) |
| 207 | return scale * ret_val; |
| 208 | |
| 209 | /* Step 3: u = 0,1 - uniform sample. squeeze acceptance (s) */ |
| 210 | u = unif_rand(); |
| 211 | if ( ( d * u ) <= ( t * t * t ) ) |
| 212 | return scale * ret_val; |
| 213 | |
| 214 | /* Step 4: recalculations of q0, b, si, c if necessary */ |
| 215 | |
| 216 | if ( a != aaa ) { |
| 217 | aaa = a; |
| 218 | r = 1.0 / a; |
| 219 | q0 = ( ( ( ( ( ( q7 * r + q6 ) * r + q5 ) * r + q4 ) * r + q3 ) * r |
| 220 | + q2 ) * r + q1 ) * r; |
| 221 | |
| 222 | /* Approximation depending on size of parameter a */ |
| 223 | /* The constants in the expressions for b, si and c */ |
| 224 | /* were established by numerical experiments */ |
| 225 | |
| 226 | if ( a <= 3.686 ) { |
| 227 | b = 0.463 + s + 0.178 * s2; |
| 228 | si = 1.235; |
| 229 | c = 0.195 / s - 0.079 + 0.16 * s; |
| 230 | } else if ( a <= 13.022 ) { |
| 231 | b = 1.654 + 0.0076 * s2; |
| 232 | si = 1.68 / s + 0.275; |
| 233 | c = 0.062 / s + 0.024; |
| 234 | } else { |
| 235 | b = 1.77; |
| 236 | si = 0.75; |
| 237 | c = 0.1515 / s; |
| 238 | } |
| 239 | } |
| 240 | /* Step 5: no quotient test if x not positive */ |
| 241 | |
| 242 | if ( x > 0.0 ) { |
| 243 | /* Step 6: calculation of v and quotient q */ |
| 244 | v = t / ( s + s ); |
| 245 | if ( fabs ( v ) <= 0.25 ) |
| 246 | q = q0 + 0.5 * t * t * ( ( ( ( ( ( a7 * v + a6 ) * v + a5 ) * v + a4 ) * v |
| 247 | + a3 ) * v + a2 ) * v + a1 ) * v; |
| 248 | else |
| 249 | q = q0 - s * t + 0.25 * t * t + ( s2 + s2 ) * log ( 1.0 + v ); |
| 250 | |
| 251 | |
| 252 | /* Step 7: quotient acceptance (q) */ |
| 253 | if ( log ( 1.0 - u ) <= q ) |
| 254 | return scale * ret_val; |
| 255 | } |
| 256 | |
| 257 | for ( ;; ) { //VS repeat |
| 258 | /* Step 8: e = standard exponential deviate |
| 259 | * u = 0,1 -uniform deviate |
| 260 | * t = (b,si)-double exponential (laplace) sample */ |
| 261 | e = exp_rand(); |
| 262 | u = unif_rand(); |
| 263 | u = u + u - 1.0; |
| 264 | if ( u < 0.0 ) |
| 265 | t = b - si * e; |
| 266 | else |
| 267 | t = b + si * e; |
| 268 | /* Step 9: rejection if t < tau(1) = -0.71874483771719 */ |
| 269 | if ( t >= -0.71874483771719 ) { |
| 270 | /* Step 10: calculation of v and quotient q */ |
| 271 | v = t / ( s + s ); |
| 272 | if ( fabs ( v ) <= 0.25 ) |
| 273 | q = q0 + 0.5 * t * t * |
| 274 | ( ( ( ( ( ( a7 * v + a6 ) * v + a5 ) * v + a4 ) * v + a3 ) * v |
| 275 | + a2 ) * v + a1 ) * v; |
| 276 | else |
| 277 | q = q0 - s * t + 0.25 * t * t + ( s2 + s2 ) * log ( 1.0 + v ); |
| 278 | /* Step 11: hat acceptance (h) */ |
| 279 | /* (if q not positive go to step 8) */ |
| 280 | if ( q > 0.0 ) { |
| 281 | // TODO: w = expm1(q); |
| 282 | w = exp ( q ) - 1; |
| 283 | /* ^^^^^ original code had approximation with rel.err < 2e-7 */ |
| 284 | /* if t is rejected sample again at step 8 */ |
| 285 | if ( ( c * fabs ( u ) ) <= ( w * exp ( e - 0.5 * t * t ) ) ) |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | } /* repeat .. until `t' is accepted */ |
| 290 | x = s + 0.5 * t; |
| 291 | return scale * x * x; |