SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BetaRejectionLoglogisticGen.java
1/*
2 * Class: BetaRejectionLoglogisticGen
3 * Description: beta random variate generators using the rejection method
4 with log-logistic envelopes
5 * Environment: Java
6 * Software: SSJ
7 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
8 * Organization: DIRO, Universite de Montreal
9 * @author
10 * @since
11 *
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 */
26package umontreal.ssj.randvar;
27
28import umontreal.ssj.util.Num;
29import umontreal.ssj.rng.*;
30import umontreal.ssj.probdist.*;
31
44
45 private RandomStream auxStream;
46 // Parameters for rejection with log-logistic envelopes
47 private static final int bb = 0;
48 private static final int bc = 1;
49 private double am;
50 private double bm;
51 private double al;
52 private double alnam;
53 private double be;
54 private double ga;
55 private double si;
56 private double rk1;
57 private double rk2;
58
67 public BetaRejectionLoglogisticGen(RandomStream s, RandomStream aux, double alpha, double beta) {
68 super(s, null);
69 auxStream = aux;
70 setParams(alpha, beta, 0.0, 1.0);
71 init();
72 }
73
80 public BetaRejectionLoglogisticGen(RandomStream s, double alpha, double beta) {
81 this(s, s, alpha, beta);
82 }
83
92 public BetaRejectionLoglogisticGen(RandomStream s, RandomStream aux, double alpha, double beta, double a, double b) {
93 super(s, null);
94 auxStream = aux;
95 setParams(alpha, beta, a, b);
96 init();
97 }
98
105 public BetaRejectionLoglogisticGen(RandomStream s, double alpha, double beta, double a, double b) {
106 this(s, s, alpha, beta, a, b);
107 }
108
116 super(s, dist);
117 auxStream = aux;
118 if (dist != null)
119 setParams(dist.getAlpha(), dist.getBeta(), dist.getA(), dist.getB());
120 init();
121 }
122
128 this(s, s, dist);
129 }
130
135 return auxStream;
136 }
137
138 private void init() {
139 // Code taken from UNURAN
140 if (p > 1.0 && q > 1.0) {
141 gen = bb;
142 am = (p < q) ? p : q;
143 bm = (p > q) ? p : q;
144 al = am + bm;
145 be = Math.sqrt((al - 2.0) / (2.0 * p * q - al));
146 ga = am + 1.0 / be;
147 } else {
148 gen = bc;
149 am = (p > q) ? p : q;
150 bm = (p < q) ? p : q;
151 al = am + bm;
152 alnam = al * Math.log(al / am) - 1.386294361;
153 be = 1.0 / bm;
154 si = 1.0 + am - bm;
155 rk1 = si * (0.013888889 + 0.041666667 * bm) / (am * be - 0.77777778);
156 rk2 = 0.25 + (0.5 + 0.25 / si) * bm;
157 }
158 }
159
160 public double nextDouble() {
161 /************************************
162 * Previously: return rejectionLogLogistic(); Now executes code directly
163 * (without calling anything)
164 ***********************************/
165
166 // The code was taken from UNURAN
167 double X = 0.0;
168 double u1, u2, v, w, y, z, r, s, t;
169 RandomStream stream = this.stream;
170 switch (gen) {
171 case bb:
172 /* -X- generator code -X- */
173 while (true) {
174 /* Step 1 */
175 u1 = stream.nextDouble();
176 u2 = stream.nextDouble();
177 stream = auxStream;
178 v = be * Math.log(u1 / (1.0 - u1));
179 w = am * Math.exp(v);
180 z = u1 * u1 * u2;
181 r = ga * v - 1.386294361;
182 s = am + r - w;
183
184 /* Step 2 */
185 if (s + 2.609437912 < 5.0 * z) {
186 /* Step 3 */
187 t = Math.log(z);
188 if (s < t)
189 /* Step 4 */
190 if (r + al * Math.log(al / (bm + w)) < t)
191 continue;
192 }
193
194 /* Step 5 */
195 X = equalsDouble(am, p) ? w / (bm + w) : bm / (bm + w);
196 break;
197 }
198 /* -X- end of generator code -X- */
199 break;
200 case bc:
201 while (true) {
202 /* Step 1 */
203 u1 = stream.nextDouble();
204 u2 = stream.nextDouble();
205 stream = auxStream;
206
207 if (u1 < 0.5) {
208 /* Step 2 */
209 y = u1 * u2;
210 z = u1 * y;
211
212 if ((0.25 * u2 - y + z) >= rk1)
213 continue; /* goto 1 */
214
215 /* Step 5 */
216 v = be * Math.log(u1 / (1.0 - u1));
217 if (v > 80.0) {
218 if (alnam < Math.log(z))
219 continue;
220 X = equalsDouble(am, p) ? 1.0 : 0.0;
221 break;
222 } else {
223 w = am * Math.exp(v);
224 if ((al * (Math.log(al / (bm + w)) + v) - 1.386294361) < Math.log(z))
225 continue; /* goto 1 */
226
227 /* Step 6_a */
228 X = !equalsDouble(am, p) ? bm / (bm + w) : w / (bm + w);
229 break;
230 }
231 } else {
232 /* Step 3 */
233 z = u1 * u1 * u2;
234 if (z < 0.25) {
235 /* Step 5 */
236 v = be * Math.log(u1 / (1.0 - u1));
237 if (v > 80.0) {
238 X = equalsDouble(am, p) ? 1.0 : 0.0;
239 break;
240 }
241
242 w = am * Math.exp(v);
243 X = !equalsDouble(am, p) ? bm / (bm + w) : w / (bm + w);
244 break;
245 } else {
246 if (z >= rk2)
247 continue;
248 v = be * Math.log(u1 / (1.0 - u1));
249 if (v > 80.0) {
250 if (alnam < Math.log(z))
251 continue;
252 X = equalsDouble(am, p) ? 1.0 : 0.0;
253 break;
254 }
255 w = am * Math.exp(v);
256 if ((al * (Math.log(al / (bm + w)) + v) - 1.386294361) < Math.log(z))
257 continue; /* goto 1 */
258
259 /* Step 6_b */
260 X = !equalsDouble(am, p) ? bm / (bm + w) : w / (bm + w);
261 break;
262 }
263 }
264 }
265 break;
266 default:
267 throw new IllegalStateException();
268 }
269
270 return a + (b - a) * X;
271 }
272
273 private static boolean equalsDouble(double a, double b) {
274 if (a == b)
275 return true;
276 double absa = Math.abs(a);
277 double absb = Math.abs(b);
278 return Math.abs(a - b) <= Math.min(absa, absb) * Num.DBL_EPSILON;
279 }
280
281}
Extends the class ContinuousDistribution for the beta distribution.
Definition BetaDist.java:54
BetaGen(RandomStream s, double alpha, double beta, double a, double b)
Creates a new beta generator with parameters alpha and beta, over the interval.
Definition BetaGen.java:64
BetaRejectionLoglogisticGen(RandomStream s, double alpha, double beta)
Creates a beta random variate generator with parameters.
BetaRejectionLoglogisticGen(RandomStream s, RandomStream aux, double alpha, double beta, double a, double b)
Creates a beta random variate generator with parameters.
BetaRejectionLoglogisticGen(RandomStream s, double alpha, double beta, double a, double b)
Creates a beta random variate generator with parameters.
BetaRejectionLoglogisticGen(RandomStream s, RandomStream aux, double alpha, double beta)
Creates a beta random variate generator with parameters.
BetaRejectionLoglogisticGen(RandomStream s, BetaDist dist)
Same as BetaRejectionLoglogisticGen(s,s, dist).
BetaRejectionLoglogisticGen(RandomStream s, RandomStream aux, BetaDist dist)
Creates a new generator for the distribution dist, using stream s and auxiliary stream aux.
RandomStream getAuxStream()
Returns the auxiliary stream associated with that object.
double nextDouble()
Generates a random number from the continuous distribution contained in this object.
This class provides various constants and methods to compute numerical quantities such as factorials,...
Definition Num.java:35
static final double DBL_EPSILON
Difference between 1.0 and the smallest double greater than 1.0.
Definition Num.java:90
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...