SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
LogarithmicGen.java
1/*
2 * Class: LogarithmicGen
3 * Description: random variate generators for the (discrete) logarithmic distribution
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.randvar;
26
27import umontreal.ssj.rng.*;
28import umontreal.ssj.probdist.*;
29import umontreal.ssj.util.Num;
30
51public class LogarithmicGen extends RandomVariateGenInt {
52 private static final double default_theta_limit = 0.96;
53
54 private double theta_limit = default_theta_limit;
55 private double theta;
56 private double t; // = log (1.0-theta).
57 private double h; // = -theta/log (1.0-theta).
58
65 public LogarithmicGen(RandomStream s, double theta) {
66 this(s, theta, default_theta_limit);
67 }
68
75 public LogarithmicGen(RandomStream s, double theta, double theta0) {
76 super(s, null);
77 this.theta = theta;
78 theta_limit = theta0;
79 init();
80 }
81
87 this(s, dist, default_theta_limit);
88 }
89
94 public LogarithmicGen(RandomStream s, LogarithmicDist dist, double theta0) {
95 super(s, dist);
96 theta_limit = theta0;
97 if (dist != null)
98 theta = dist.getTheta();
99 init();
100 }
101
102 private void init() {
103 if (theta <= 0.0 || theta >= 1.0)
104 throw new IllegalArgumentException("theta not in (0, 1)");
105 if (theta >= theta_limit)
106 h = Math.log1p(-theta);
107 else
108 t = -theta / Math.log1p(-theta);
109 }
110
111 public int nextInt() {
112 if (theta < theta_limit)
113 return ls(stream, theta, t);
114 else // Transformation
115 return lk(stream, theta, h);
116 }
117
122 public static int nextInt(RandomStream s, double theta) {
123 if (theta < default_theta_limit)
124 return ls(s, theta, -theta / Math.log1p(-theta));
125 else // Transformation
126 return lk(s, theta, Math.log1p(-theta));
127 }
128
129//>>>>>>>>>>>>>>>>>>>> P R I V A T E M E T H O D S <<<<<<<<<<<<<<<<<<<<
130
131 private static int ls(RandomStream stream, double theta, double t) {
132 double u = stream.nextDouble();
133 int x = 1;
134
135 double p = t;
136
137 while (u > p) {
138 u -= p;
139 x++;
140 p *= theta * ((double) x - 1.0) / ((double) x);
141 }
142 return x;
143 }
144
145 private static int lk(RandomStream stream, double theta, double h) {
146 double u, v, p, q;
147 int x;
148
149 u = stream.nextDouble();
150 if (u > theta)
151 return 1;
152 v = stream.nextDouble();
153 q = 1.0 - Math.exp(v * h);
154 if (u <= q * q) {
155 x = (int) (1. + (Math.log(u) / Math.log(q)));
156 return x;
157 }
158 return ((u > q) ? 1 : 2);
159 }
160
164 public double getTheta() {
165 return theta;
166 }
167
171 public double getTheta0() {
172 return theta_limit;
173 }
174
175}
Extends the class DiscreteDistributionInt for the logarithmic distribution.
double getTheta()
Returns the associated with this object.
double getTheta0()
Returns the associated with this object.
LogarithmicGen(RandomStream s, double theta, double theta0)
Creates a logarithmic random variate generator with parameters.
LogarithmicGen(RandomStream s, LogarithmicDist dist, double theta0)
Creates a new generator with distribution dist and stream s, with .
LogarithmicGen(RandomStream s, LogarithmicDist dist)
Creates a new generator with distribution dist and stream s, with default value .
LogarithmicGen(RandomStream s, double theta)
Creates a logarithmic random variate generator with parameters.
int nextInt()
Generates a random number (an integer) from the discrete distribution contained in this object.
static int nextInt(RandomStream s, double theta)
Uses stream s to generate a new variate from the logarithmic distribution with parameter theta.
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,...