SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
UniformIntDist.java
1/*
2 * Class: UniformIntDist
3 * Description: discrete uniform 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.probdist;
26
47 protected int i;
48 protected int j;
49
53 public UniformIntDist(int i, int j) {
54 setParams(i, j);
55 }
56
57 public double prob(int x) {
58 return prob(i, j, x);
59 }
60
61 public double cdf(int x) {
62 return cdf(i, j, x);
63 }
64
65 public double barF(int x) {
66 return barF(i, j, x);
67 }
68
69 public int inverseFInt(double u) {
70 return inverseF(i, j, u);
71 }
72
73 public double getMean() {
74 return getMean(i, j);
75 }
76
77 public double getVariance() {
78 return getVariance(i, j);
79 }
80
81 public double getStandardDeviation() {
82 return getStandardDeviation(i, j);
83 }
84
89 public static double prob(int i, int j, int x) {
90 if (j < i)
91 throw new IllegalArgumentException("j < i");
92 if (x < i || x > j)
93 return 0.0;
94
95 return (1.0 / (j - i + 1.0));
96 }
97
102 public static double cdf(int i, int j, int x) {
103 if (j < i)
104 throw new IllegalArgumentException("j < i");
105 if (x < i)
106 return 0.0;
107 if (x >= j)
108 return 1.0;
109
110 return ((x - i + 1) / (j - i + 1.0));
111 }
112
119 public static double barF(int i, int j, int x) {
120 if (j < i)
121 throw new IllegalArgumentException("j < i");
122 if (x <= i)
123 return 1.0;
124 if (x > j)
125 return 0.0;
126
127 return ((j - x + 1.0) / (j - i + 1.0));
128 }
129
134 public static int inverseF(int i, int j, double u) {
135 if (j < i)
136 throw new IllegalArgumentException("j < i");
137
138 if (u > 1.0 || u < 0.0)
139 throw new IllegalArgumentException("u not in [0, 1]");
140
141 if (u <= 0.0)
142 return i;
143 if (u >= 1.0)
144 return j;
145
146 return i + (int) (u * (j - i + 1.0));
147 }
148
165 public static double[] getMLE(int[] x, int n) {
166 if (n <= 0)
167 throw new IllegalArgumentException("n <= 0");
168
169 double parameters[] = new double[2];
170 parameters[0] = (double) Integer.MAX_VALUE;
171 parameters[1] = (double) Integer.MIN_VALUE;
172 for (int i = 0; i < n; i++) {
173 if ((double) x[i] < parameters[0])
174 parameters[0] = (double) x[i];
175 if ((double) x[i] > parameters[1])
176 parameters[1] = (double) x[i];
177 }
178 return parameters;
179 }
180
190 public static UniformIntDist getInstanceFromMLE(int[] x, int n) {
191
192 double parameters[] = getMLE(x, n);
193
194 return new UniformIntDist((int) parameters[0], (int) parameters[1]);
195 }
196
203 public static double getMean(int i, int j) {
204 if (j < i)
205 throw new IllegalArgumentException("j < i");
206
207 return ((i + j) / 2.0);
208 }
209
216 public static double getVariance(int i, int j) {
217 if (j < i)
218 throw new IllegalArgumentException("j < i");
219
220 return (((j - i + 1.0) * (j - i + 1.0) - 1.0) / 12.0);
221 }
222
229 public static double getStandardDeviation(int i, int j) {
230 return Math.sqrt(UniformIntDist.getVariance(i, j));
231 }
232
236 public int getI() {
237 return i;
238 }
239
243 public int getJ() {
244 return j;
245 }
246
250 public void setParams(int i, int j) {
251 if (j < i)
252 throw new IllegalArgumentException("j < i");
253
254 supportA = this.i = i;
255 supportB = this.j = j;
256 }
257
262 public double[] getParams() {
263 double[] retour = { i, j };
264 return retour;
265 }
266
270 public String toString() {
271 return getClass().getSimpleName() + " : i = " + i + ", j = " + j;
272 }
273
274}
Classes implementing discrete distributions over the integers should inherit from this class.
int getJ()
Returns the parameter .
double getMean()
Returns the mean of the distribution function.
double cdf(int x)
Returns the distribution function evaluated at (see ( FDistDisc )).
static double cdf(int i, int j, int x)
Computes the discrete uniform distribution function defined in ( cdfuniformint ).
double getVariance()
Returns the variance of the distribution function.
double prob(int x)
Returns , the probability of .
static int inverseF(int i, int j, double u)
Computes the inverse of the discrete uniform distribution function ( invuniformint ).
static double getVariance(int i, int j)
Computes and returns the variance of the discrete uniform distribution.
void setParams(int i, int j)
Sets the parameters and for this object.
String toString()
Returns a String containing information about the current distribution.
int inverseFInt(double u)
Returns the inverse distribution function , where.
double getStandardDeviation()
Returns the standard deviation of the distribution function.
UniformIntDist(int i, int j)
Constructs a discrete uniform distribution over the interval .
static double[] getMLE(int[] x, int n)
Estimates the parameters of the uniform distribution over integers using the maximum likelihood meth...
int getI()
Returns the parameter .
double[] getParams()
Return a table containing the parameters of the current distribution.
static double getStandardDeviation(int i, int j)
Computes and returns the standard deviation of the discrete uniform distribution.
static double prob(int i, int j, int x)
Computes the discrete uniform probability defined in ( fmassuniformint ).
double barF(int x)
Returns , the complementary distribution function.
static UniformIntDist getInstanceFromMLE(int[] x, int n)
Creates a new instance of a discrete uniform distribution over integers with parameters and estimat...
static double barF(int i, int j, int x)
Computes the discrete uniform complementary distribution function.
static double getMean(int i, int j)
Computes and returns the mean of the discrete uniform distribution.