SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
GeometricDist.java
1/*
2 * Class: GeometricDist
3 * Description: geometric 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
27import umontreal.ssj.util.Num;
28
47
48 private double p;
49 private double vp;
50
54 public GeometricDist(double p) {
55 setP(p);
56 }
57
58 public double prob(int x) {
59 return prob(p, x);
60 }
61
62 public double cdf(int x) {
63 return cdf(p, x);
64 }
65
66 public double barF(int x) {
67 return barF(p, x);
68 }
69
70 public int inverseFInt(double u) {
71 if (u > 1.0 || u < 0.0)
72 throw new IllegalArgumentException("u not in [0,1]");
73
74 if (p >= 1.0)
75 return 0;
76 if (u <= p)
77 return 0;
78 if (u >= 1.0 || p <= 0.0)
79 return Integer.MAX_VALUE;
80
81 return (int) Math.floor(Math.log1p(-u) / vp);
82 }
83
84 public double getMean() {
85 return GeometricDist.getMean(p);
86 }
87
88 public double getVariance() {
89 return GeometricDist.getVariance(p);
90 }
91
92 public double getStandardDeviation() {
93 return GeometricDist.getStandardDeviation(p);
94 }
95
100 public static double prob(double p, int x) {
101 if (p < 0 || p > 1)
102 throw new IllegalArgumentException("p not in range (0,1)");
103 if (p <= 0)
104 return 0;
105 if (p >= 1)
106 return 0;
107 if (x < 0)
108 return 0;
109 return p * Math.pow(1 - p, x);
110 }
111
115 public static double cdf(double p, int x) {
116 if (p < 0.0 || p > 1.0)
117 throw new IllegalArgumentException("p not in [0,1]");
118 if (x < 0)
119 return 0.0;
120 if (p >= 1.0) // In fact, p == 1
121 return 1.0;
122 if (p <= 0.0) // In fact, p == 0
123 return 0.0;
124 return 1.0 - Math.pow(1.0 - p, (double) x + 1.0);
125 }
126
132 public static double barF(double p, int x) {
133 if (p < 0.0 || p > 1.0)
134 throw new IllegalArgumentException("p not in [0,1]");
135 if (x < 0)
136 return 1.0;
137 if (p >= 1.0) // In fact, p == 1
138 return 0.0;
139 if (p <= 0.0) // In fact, p == 0
140 return 1.0;
141
142 return Math.pow(1.0 - p, x);
143 }
144
149 public static int inverseF(double p, double u) {
150 if (p > 1.0 || p < 0.0)
151 throw new IllegalArgumentException("p not in [0,1]");
152 if (u > 1.0 || u < 0.0)
153 throw new IllegalArgumentException("u not in [0,1]");
154 if (p >= 1.0)
155 return 0;
156 if (u <= p)
157 return 0;
158 if (u >= 1.0 || p <= 0.0)
159 return Integer.MAX_VALUE;
160
161 double v = Math.log1p(-p);
162 return (int) Math.floor(Math.log1p(-u) / v);
163 }
164
178 public static double[] getMLE(int[] x, int n) {
179 if (n <= 0)
180 throw new IllegalArgumentException("n <= 0");
181
182 double parameters[];
183 parameters = new double[1];
184 double sum = 0.0;
185 for (int i = 0; i < n; i++) {
186 sum += x[i];
187 }
188
189 parameters[0] = 1.0 / (((double) sum / (double) n) + 1.0);
190
191 return parameters;
192 }
193
202 public static GeometricDist getInstanceFromMLE(int[] x, int n) {
203
204 double parameters[] = getMLE(x, n);
205
206 return new GeometricDist(parameters[0]);
207 }
208
215 public static double getMean(double p) {
216 if (p < 0.0 || p > 1.0)
217 throw new IllegalArgumentException("p not in range (0,1)");
218
219 return (1 - p) / p;
220 }
221
229 public static double getVariance(double p) {
230 if (p < 0.0 || p > 1.0)
231 throw new IllegalArgumentException("p not in range (0,1)");
232
233 return ((1 - p) / (p * p));
234 }
235
242 public static double getStandardDeviation(double p) {
243 return Math.sqrt(GeometricDist.getVariance(p));
244 }
245
249 public double getP() {
250 return p;
251 }
252
256 public void setP(double p) {
257 if (p < 0 || p > 1)
258 throw new IllegalArgumentException("p not in range (0,1)");
259 vp = Math.log1p(-p);
260 this.p = p;
261 supportA = 0;
262 }
263
267 public double[] getParams() {
268 double[] retour = { p };
269 return retour;
270 }
271
275 public String toString() {
276 return getClass().getSimpleName() + " : p = " + p;
277 }
278
279}
Classes implementing discrete distributions over the integers should inherit from this class.
static double prob(double p, int x)
Computes the geometric probability given in ( fgeom ) .
int inverseFInt(double u)
Returns the inverse distribution function , where.
static GeometricDist getInstanceFromMLE(int[] x, int n)
Creates a new instance of a geometric distribution with parameter.
double prob(int x)
Returns , the probability of .
double cdf(int x)
Returns the distribution function evaluated at (see ( FDistDisc )).
double getP()
Returns the associated with this object.
GeometricDist(double p)
Constructs a geometric distribution with parameter .
static double getVariance(double p)
Computes and returns the variance of the geometric distribution with parameter .
double getStandardDeviation()
Returns the standard deviation of the distribution function.
double getMean()
Returns the mean of the distribution function.
static double barF(double p, int x)
Computes the complementary distribution function.
double[] getParams()
Return a table containing the parameters of the current distribution.
double barF(int x)
Returns , the complementary distribution function.
static double getStandardDeviation(double p)
Computes and returns the standard deviation of the geometric distribution with parameter .
void setP(double p)
Resets the value of associated with this object.
static int inverseF(double p, double u)
Computes the inverse of the geometric distribution, given by ( FInvgeom ).
String toString()
Returns a String containing information about the current distribution.
static double[] getMLE(int[] x, int n)
Estimates the parameter of the geometric distribution using the maximum likelihood method,...
static double getMean(double p)
Computes and returns the mean of the geometric distribution with parameter .
static double cdf(double p, int x)
Computes the distribution function .
double getVariance()
Returns the variance of the distribution function.