SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
DiscreteDistributionInt.java
1/*
2 * Class: DiscreteDistributionInt
3 * Description: discrete distributions over the integers
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.probdist;
26
61
62public abstract class DiscreteDistributionInt implements Distribution {
63
73 public static double EPSILON = 1.0e-16;
74
75 /*
76 * For better precision in the tails, we keep the cumulative probabilities (F)
77 * in cdf[x] for x <= xmed (i.e. cdf[x] is the sum off all the probabi- lities
78 * pdf[i] for i <= x), and the complementary cumulative probabilities (1 - F) in
79 * cdf[x] for x > xmed (i.e. cdf[x] is the sum off all the probabilities pdf[i]
80 * for i >= x).
81 */
82 protected final static double EPS_EXTRA = 1.0e-6;
83 protected double cdf[] = null; // cumulative probabilities
84 protected double pdf[] = null; // probability terms or mass distribution
85 protected int xmin = 0; // pdf[x] < EPSILON for x < xmin
86 protected int xmax = 0; // pdf[x] < EPSILON for x > xmax
87
88 // xmed is such that cdf[xmed] >= 0.5 and cdf[xmed - 1] < 0.5.
89 protected int xmed = 0; /*
90 * cdf[x] = F(x) for x <= xmed, and cdf[x] = bar_F(x) for x > xmed
91 */
92 protected int supportA = Integer.MIN_VALUE;
93 protected int supportB = Integer.MAX_VALUE;
94
101 public abstract double prob(int x);
102
111 public double cdf(double x) {
112 return cdf((int) x);
113 }
114
122 public abstract double cdf(int x);
123
132 public double barF(double x) {
133 return barF((int) x);
134 }
135
145 public double barF(int x) {
146 return 1.0 - cdf(x - 1);
147 }
148
155 public int getXinf() {
156 return supportA;
157 }
158
165 public int getXsup() {
166 return supportB;
167 }
168
183 public double inverseF(double u) {
184 return inverseFInt(u);
185 }
186
201 public int inverseFInt(double u) {
202 int i, j, k;
203
204 if (u < 0.0 || u > 1.0)
205 throw new IllegalArgumentException("u is not in [0,1]");
206 if (u <= 0.0)
207 return supportA;
208 if (u >= 1.0)
209 return supportB;
210
211 // Remember: the upper part of cdf contains the complementary distribu-
212 // tion for xmed < s <= xmax, and the lower part of cdf the
213 // distribution for xmin <= x <= xmed
214
215 if (u <= cdf[xmed - xmin]) {
216 // In the lower part of cdf
217 if (u <= cdf[0])
218 return xmin;
219 i = 0;
220 j = xmed - xmin;
221 while (i < j) {
222 k = (i + j) / 2;
223 if (u > cdf[k])
224 i = k + 1;
225 else
226 j = k;
227 }
228 } else {
229 // In the upper part of cdf
230 u = 1 - u;
231 if (u < cdf[xmax - xmin])
232 return xmax;
233
234 i = xmed - xmin + 1;
235 j = xmax - xmin;
236 while (i < j) {
237 k = (i + j) / 2;
238 if (u < cdf[k])
239 i = k + 1;
240 else
241 j = k;
242 }
243 i--;
244 }
245
246 return i + xmin;
247 }
248
249}
Classes implementing discrete distributions over the integers should inherit from this class.
abstract double cdf(int x)
Returns the distribution function evaluated at (see ( FDistDisc )).
int getXsup()
Returns the upper limit of the support of the probability mass function.
abstract double prob(int x)
Returns , the probability of .
double barF(int x)
Returns , the complementary distribution function.
double barF(double x)
Returns , the complementary distribution function.
double inverseF(double u)
Returns the inverse distribution function , where.
static double EPSILON
Environment variable that determines what probability terms can be considered as negligible when buil...
double cdf(double x)
Returns the distribution function evaluated at (see ( FDistDisc )).
int inverseFInt(double u)
Returns the inverse distribution function , where.
int getXinf()
Returns the lower limit of the support of the probability mass function.
This interface should be implemented by all classes supporting discrete and continuous distributions.