SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ChiRatioOfUniformsGen.java
1/*
2 * Class: ChiRatioOfUniformsGen
3 * Description: Chi random variate generators using the ratio of uniforms
4 method with shift
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.rng.*;
29import umontreal.ssj.probdist.*;
30
39public class ChiRatioOfUniformsGen extends ChiGen {
40
46 super(s, null);
47 setParams(nu);
48 }
49
54 super(s, dist);
55 if (dist != null)
56 setParams(dist.getNu());
57 }
58
59 public double nextDouble() {
60 return ratioOfUniforms(stream, nu);
61 }
62
63 public static double nextDouble(RandomStream s, int nu) {
64 if (nu <= 0)
65 throw new IllegalArgumentException("nu <= 0");
66 return ratioOfUniforms(s, nu);
67 }
68
69//>>>>>>>>>>>>>>>>>>>> P R I V A T E M E T H O D S <<<<<<<<<<<<<<<<<<<<
70
71 // ================================================================
72 // Method ratio of uniforms with shift.
73 // code taken and adapted from unuran
74 // file : /distributions/c_chi_gen.c
75 // ===============================================================
76
77 private static double ratioOfUniforms(RandomStream stream, int nu) {
78 double u, v, z, zz, r;
79 if (nu == 1) {
80 while (true) {
81 u = stream.nextDouble();
82 v = stream.nextDouble() * 0.857763884960707;
83 z = v / u;
84 if (z < 0)
85 continue;
86 zz = z * z;
87 r = 2.5 - zz;
88 if (z < 0.)
89 r = r + zz * z / (3. * z);
90 if (u < r * 0.3894003915)
91 break;
92 if (zz > (1.036961043 / u + 1.4))
93 continue;
94 if (2 * Math.log(u) < (-zz * 0.5))
95 break;
96 }
97 }
98
99 else { // nu > 1
100 final double b = Math.sqrt(nu - 1.);
101 final double vm1 = -0.6065306597 * (1. - 0.25 / (b * b + 1.));
102 final double vm = (-b > vm1) ? -b : vm1;
103 final double vp = 0.6065306597 * (0.7071067812 + b) / (0.5 + b);
104 final double vd = vp - vm;
105 while (true) {
106 u = stream.nextDouble();
107 v = stream.nextDouble() * vd + vm;
108 z = v / u;
109 if (z < -b)
110 continue;
111 zz = z * z;
112 r = 2.5 - zz;
113 if (z < 0.0)
114 r = r + zz * z / (3.0 * (z + b));
115 if (u < r * 0.3894003915) {
116 z += b;
117 break;
118 }
119 if (zz > (1.036961043 / u + 1.4))
120 continue;
121 if (2. * Math.log(u) < (Math.log(1.0 + z / b) * b * b - zz * 0.5 - z * b)) {
122 z += b;
123 break;
124 }
125 }
126 }
127 return z;
128 }
129}
Extends the class ContinuousDistribution for the chi distribution.
Definition ChiDist.java:49
ChiGen(RandomStream s, int nu)
Creates a chi random variate generator with nu degrees of freedom, using stream s.
Definition ChiGen.java:54
double nextDouble()
Generates a random number from the continuous distribution contained in this object.
ChiRatioOfUniformsGen(RandomStream s, ChiDist dist)
Create a new generator for the distribution dist, using stream s.
static double nextDouble(RandomStream s, int nu)
Generates a random variate from the chi distribution with &#160;nu degrees of freedom,...
ChiRatioOfUniformsGen(RandomStream s, int nu)
Creates a chi random variate generator with nu degrees of freedom, using stream s.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
double nextDouble()
Returns a (pseudo)random number from the uniform distribution over the interval , using this stream,...