SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
DiscreteDistribution.java
1/*
2 * Class: DiscreteDistribution
3 * Description: discrete distributions over a set of real numbers
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
27import java.util.Formatter;
28import java.util.Locale;
29
45public class DiscreteDistribution implements Distribution {
46 /*
47 * For better precision in the tails, we keep the cumulative probabilities (F)
48 * in cdf[x] for x <= xmed (i.e. cdf[x] is the sum off all the probabi- lities
49 * pr[i] for i <= x), and the complementary cumulative probabilities (1 - F) in
50 * cdf[x] for x > xmed (i.e. cdf[x] is the sum off all the probabilities pr[i]
51 * for i >= x).
52 */
53
54 protected double cdf[] = null; // cumulative probabilities
55 protected double pr[] = null; // probability terms or mass distribution
56 protected int xmin = 0; // pr[x] = 0 for x < xmin
57 protected int xmax = 0; // pr[x] = 0 for x > xmax
58 protected int xmed = 0; // cdf[x] = F(x) for x <= xmed, and
59 // cdf[x] = bar_F(x) for x > xmed
60 protected int nVal; // number of different values
61 protected double sortedVal[];
62 protected double supportA = Double.NEGATIVE_INFINITY;
63 protected double supportB = Double.POSITIVE_INFINITY;
64
65 protected DiscreteDistribution() {
66 }
67 // Default constructor called by subclasses such as 'EmpiricalDist'
68
75 public DiscreteDistribution(double[] values, double[] prob, int n) {
76 init(n, values, prob);
77 }
78
83 public DiscreteDistribution(int[] values, double[] prob, int n) {
84 double[] A = new double[n];
85 for (int i = 0; i < n; i++)
86 A[i] = values[i];
87 init(n, A, prob);
88 }
89
90 private void init(int n, double[] val, double[] prob) {
91 int no = val.length;
92 int np = prob.length;
93 if (n <= 0)
94 throw new IllegalArgumentException("n <= 0");
95 if (no < n || np < n)
96 throw new IllegalArgumentException("Size of arrays 'values' or 'prob' less than 'n'");
97
98 nVal = n;
99 pr = prob;
100
101 // cdf
102 sortedVal = new double[nVal];
103 System.arraycopy(val, 0, sortedVal, 0, nVal);
104
105 supportA = sortedVal[0];
106 supportB = sortedVal[nVal - 1];
107 xmin = 0;
108 xmax = nVal - 1;
109
110 /*
111 * Compute the cumulative probabilities until F >= 0.5, and keep them in the
112 * lower part of cdf
113 */
114 cdf = new double[nVal];
115 cdf[0] = pr[0];
116 int i = 0;
117 while (i < xmax && cdf[i] < 0.5) {
118 i++;
119 cdf[i] = pr[i] + cdf[i - 1];
120 }
121 // This is the boundary between F and barF in the CDF
122 xmed = i;
123
124 /*
125 * Compute the cumulative probabilities of the complementary distribution and
126 * keep them in the upper part of cdf.
127 */
128 cdf[nVal - 1] = pr[nVal - 1];
129 i = nVal - 2;
130 while (i > xmed) {
131 cdf[i] = pr[i] + cdf[i + 1];
132 i--;
133 }
134 }
135
140 public double cdf(double x) {
141 if (x < sortedVal[0])
142 return 0.0;
143 if (x >= sortedVal[nVal - 1])
144 return 1.0;
145 if ((xmax == xmed) || (x < sortedVal[xmed + 1])) {
146 for (int i = 0; i <= xmed; i++)
147 if (x >= sortedVal[i] && x < sortedVal[i + 1])
148 return cdf[i];
149 } else {
150 for (int i = xmed + 1; i < nVal - 1; i++)
151 if (x >= sortedVal[i] && x < sortedVal[i + 1])
152 return 1.0 - cdf[i + 1];
153 }
154 throw new IllegalStateException();
155 }
156
161 public double barF(double x) {
162 if (x <= sortedVal[0])
163 return 1.0;
164 if (x > sortedVal[nVal - 1])
165 return 0.0;
166 if ((xmax == xmed) || (x <= sortedVal[xmed + 1])) {
167 for (int i = 0; i <= xmed; i++)
168 if (x > sortedVal[i] && x <= sortedVal[i + 1])
169 return 1.0 - cdf[i];
170 } else {
171 for (int i = xmed + 1; i < nVal - 1; i++)
172 if (x > sortedVal[i] && x <= sortedVal[i + 1])
173 return cdf[i + 1];
174 }
175 throw new IllegalStateException();
176 }
177
190 public double inverseF(double u) {
191 int i, j, k;
192
193 if (u < 0.0 || u > 1.0)
194 throw new IllegalArgumentException("u not in [0,1]");
195 if (u <= 0.0)
196 return supportA;
197 if (u >= 1.0)
198 return supportB;
199
200 // Remember: the upper part of cdf contains the complementary distribu-
201 // tion for xmed < s <= xmax, and the lower part of cdf the
202 // distribution for xmin <= s <= xmed
203
204 if (u <= cdf[xmed - xmin]) {
205 // In the lower part of cdf
206 if (u <= cdf[0])
207 return sortedVal[xmin];
208 i = 0;
209 j = xmed - xmin;
210 while (i < j) {
211 k = (i + j) / 2;
212 if (u > cdf[k])
213 i = k + 1;
214 else
215 j = k;
216 }
217 } else {
218 // In the upper part of cdf
219 u = 1 - u;
220 if (u < cdf[xmax - xmin])
221 return sortedVal[xmax];
222
223 i = xmed - xmin + 1;
224 j = xmax - xmin;
225 while (i < j) {
226 k = (i + j) / 2;
227 if (u < cdf[k])
228 i = k + 1;
229 else
230 j = k;
231 }
232 i--;
233 }
234
235 return sortedVal[i + xmin];
236 }
237
241 public double getMean() {
242 double mean = 0.0;
243 for (int i = 0; i < nVal; i++)
244 mean += sortedVal[i] * pr[i];
245 return mean;
246 }
247
252 public double getVariance() {
253 double mean = getMean();
254 double variance = 0.0;
255 for (int i = 0; i < nVal; i++)
256 variance += (sortedVal[i] - mean) * (sortedVal[i] - mean) * pr[i];
257 return (variance);
258 }
259
263 public double getStandardDeviation() {
264 return Math.sqrt(getVariance());
265 }
266
272 public double[] getParams() {
273 double[] retour = new double[1 + nVal * 2];
274 double sum = 0;
275 retour[0] = nVal;
276 System.arraycopy(sortedVal, 0, retour, 1, nVal);
277 for (int i = 0; i < nVal - 1; i++) {
278 retour[nVal + 1 + i] = cdf[i] - sum;
279 sum = cdf[i];
280 }
281 retour[2 * nVal] = 1.0 - sum;
282
283 return retour;
284 }
285
289 public int getN() {
290 return nVal;
291 }
292
300 public double prob(int i) {
301 if (i < 0 || i >= nVal)
302 return 0.;
303 return pr[i];
304 }
305
309 public double getValue(int i) {
310 return sortedVal[i];
311 }
312
318 public double getXinf() {
319 return supportA;
320 }
321
327 public double getXsup() {
328 return supportB;
329 }
330
334 public String toString() {
335 StringBuilder sb = new StringBuilder();
336 Formatter formatter = new Formatter(sb, Locale.US);
337 formatter.format("%s%n", getClass().getSimpleName());
338 formatter.format("%s : %s%n", "value", "cdf");
339 for (int i = 0; i < nVal - 1; i++)
340 formatter.format("%f : %f%n", sortedVal[i], cdf[i]);
341 formatter.format("%f : %f%n", sortedVal[nVal - 1], 1.0);
342 formatter.close();
343 return sb.toString();
344 }
345
346}
double getMean()
Computes the mean of the distribution.
int getN()
Returns the number of possible values .
double[] getParams()
Returns a table containing the parameters of the current distribution.
double getXsup()
Returns the upper limit of the support of the distribution.
double getXinf()
Returns the lower limit of the support of the distribution.
DiscreteDistribution(double[] values, double[] prob, int n)
Constructs a discrete distribution over the values contained in array values, with probabilities giv...
String toString()
Returns a String containing information about the current distribution.
double getVariance()
Computes the variance of the distribution.
DiscreteDistribution(int[] values, double[] prob, int n)
Similar to DiscreteDistribution(double[], double[], int).
double prob(int i)
Returns , the probability of the -th value, for.
double getStandardDeviation()
Computes the standard deviation of the distribution.
double getValue(int i)
Returns the -th value , for .
This interface should be implemented by all classes supporting discrete and continuous distributions.