SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
UniformDist.java
1/*
2 * Class: UniformDist
3 * Description: uniform distribution over the reals
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
44public class UniformDist extends ContinuousDistribution {
45 private double a;
46 private double b;
47
51 public UniformDist() {
52 setParams(0.0, 1.0);
53 }
54
58 public UniformDist(double a, double b) {
59 setParams(a, b);
60 }
61
62 public double density(double x) {
63 return density(a, b, x);
64 }
65
66 public double cdf(double x) {
67 return cdf(a, b, x);
68 }
69
70 public double barF(double x) {
71 return barF(a, b, x);
72 }
73
74 public double inverseF(double u) {
75 return inverseF(a, b, u);
76 }
77
78 public double getMean() {
79 return UniformDist.getMean(a, b);
80 }
81
82 public double getVariance() {
83 return UniformDist.getVariance(a, b);
84 }
85
86 public double getStandardDeviation() {
87 return UniformDist.getStandardDeviation(a, b);
88 }
89
94 public static double density(double a, double b, double x) {
95 if (b <= a)
96 throw new IllegalArgumentException("b <= a");
97 if (x <= a || x >= b)
98 return 0.0;
99 return 1.0 / (b - a);
100 }
101
106 public static double cdf(double a, double b, double x) {
107 if (b <= a)
108 throw new IllegalArgumentException("b <= a");
109 if (x <= a)
110 return 0.0;
111 if (x >= b)
112 return 1.0;
113 return (x - a) / (b - a);
114 }
115
121 public static double barF(double a, double b, double x) {
122 if (b <= a)
123 throw new IllegalArgumentException("b <= a");
124 if (x <= a)
125 return 1.0;
126 if (x >= b)
127 return 0.0;
128 return (b - x) / (b - a);
129 }
130
135 public static double inverseF(double a, double b, double u) {
136 if (b <= a)
137 throw new IllegalArgumentException("b <= a");
138
139 if (u > 1.0 || u < 0.0)
140 throw new IllegalArgumentException("u not in [0, 1]");
141
142 if (u <= 0.0)
143 return a;
144 if (u >= 1.0)
145 return b;
146 return a + (b - a) * u;
147 }
148
163 public static double[] getMLE(double[] x, int n) {
164 if (n <= 0)
165 throw new IllegalArgumentException("n <= 0");
166
167 double parameters[] = new double[2];
168 parameters[0] = Double.POSITIVE_INFINITY;
169 parameters[1] = Double.NEGATIVE_INFINITY;
170 for (int i = 0; i < n; i++) {
171 if (x[i] < parameters[0])
172 parameters[0] = x[i];
173 if (x[i] > parameters[1])
174 parameters[1] = x[i];
175 }
176
177 return parameters;
178 }
179
188 public static UniformDist getInstanceFromMLE(double[] x, int n) {
189 double parameters[] = getMLE(x, n);
190 return new UniformDist(parameters[0], parameters[1]);
191 }
192
199 public static double getMean(double a, double b) {
200 if (b <= a)
201 throw new IllegalArgumentException("b <= a");
202
203 return ((a + b) / 2);
204 }
205
213 public static double getVariance(double a, double b) {
214 if (b <= a)
215 throw new IllegalArgumentException("b <= a");
216
217 return ((b - a) * (b - a) / 12);
218 }
219
226 public static double getStandardDeviation(double a, double b) {
227 return Math.sqrt(UniformDist.getVariance(a, b));
228 }
229
233 public double getA() {
234 return a;
235 }
236
240 public double getB() {
241 return b;
242 }
243
247 public void setParams(double a, double b) {
248 if (b <= a)
249 throw new IllegalArgumentException("b <= a");
250 this.a = a;
251 this.b = b;
252 supportA = a;
253 supportB = b;
254 }
255
260 public double[] getParams() {
261 double[] retour = { a, b };
262 return retour;
263 }
264
268 public String toString() {
269 return getClass().getSimpleName() + " : a = " + a + ", b = " + b;
270 }
271
272}
Classes implementing continuous distributions should inherit from this base class.
static double barF(double a, double b, double x)
Computes the uniform complementary distribution function.
double getVariance()
Returns the variance.
double inverseF(double u)
Returns the inverse distribution function .
static double getVariance(double a, double b)
Computes and returns the variance of the uniform distribution with parameters and .
UniformDist(double a, double b)
Constructs a uniform distribution over the interval .
static double getMean(double a, double b)
Computes and returns the mean of the uniform distribution with parameters and .
String toString()
Returns a String containing information about the current distribution.
double getMean()
Returns the mean.
double getStandardDeviation()
Returns the standard deviation.
static UniformDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a uniform distribution with parameters.
double getA()
Returns the parameter .
double[] getParams()
Return a table containing the parameters of the current distribution.
double getB()
Returns the parameter .
static double[] getMLE(double[] x, int n)
Estimates the parameter of the uniform distribution using the maximum likelihood method,...
UniformDist()
Constructs a uniform distribution over the interval .
void setParams(double a, double b)
Sets the parameters and for this object.
double barF(double x)
Returns the complementary distribution function.
double density(double x)
Returns , the density evaluated at .
double cdf(double x)
Returns the distribution function .
static double getStandardDeviation(double a, double b)
Computes and returns the standard deviation of the uniform distribution with parameters and .
static double inverseF(double a, double b, double u)
Computes the inverse of the uniform distribution function ( cdinvfuniform ).
static double density(double a, double b, double x)
Computes the uniform density function in ( funiform ).
static double cdf(double a, double b, double x)
Computes the uniform distribution function as in ( cdfuniform ).