SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
InverseGaussianDist.java
1/*
2 * Class: InverseGaussianDist
3 * Description: inverse Gaussian 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.probdist.NormalDist;
28import umontreal.ssj.util.*;
29import umontreal.ssj.functions.MathFunction;
30
55 protected double mu;
56 protected double lambda;
57
58 private static class Function implements MathFunction {
59 protected double mu;
60 protected double lambda;
61 protected double u;
62
63 public Function(double mu, double lambda, double u) {
64 this.mu = mu;
65 this.lambda = lambda;
66 this.u = u;
67 }
68
69 public double evaluate(double x) {
70 return u - cdf(mu, lambda, x);
71 }
72 }
73
79 public InverseGaussianDist(double mu, double lambda) {
80 setParams(mu, lambda);
81 }
82
83 public double density(double x) {
84 return density(mu, lambda, x);
85 }
86
87 public double cdf(double x) {
88 return cdf(mu, lambda, x);
89 }
90
91 public double barF(double x) {
92 return barF(mu, lambda, x);
93 }
94
95 public double inverseF(double u) {
96 return inverseF(mu, lambda, u);
97 }
98
99 public double getMean() {
100 return getMean(mu, lambda);
101 }
102
103 public double getVariance() {
104 return getVariance(mu, lambda);
105 }
106
107 public double getStandardDeviation() {
108 return getStandardDeviation(mu, lambda);
109 }
110
118 public static double density(double mu, double lambda, double x) {
119 if (mu <= 0.0)
120 throw new IllegalArgumentException("mu <= 0");
121 if (lambda <= 0.0)
122 throw new IllegalArgumentException("lambda <= 0");
123 if (x <= 0.0)
124 return 0.0;
125
126 double sqrtX = Math.sqrt(x);
127
128 return (Math.sqrt(lambda / (2 * Math.PI)) / (sqrtX * sqrtX * sqrtX)
129 * Math.exp(-lambda * (x - 2 * mu + (mu * mu / x)) / (2 * mu * mu)));
130 }
131
138 public static double cdf(double mu, double lambda, double x) {
139 if (mu <= 0.0)
140 throw new IllegalArgumentException("mu <= 0");
141 if (lambda <= 0.0)
142 throw new IllegalArgumentException("lambda <= 0");
143 if (x <= 0.0)
144 return 0.0;
145 double temp = Math.sqrt(lambda / x);
146 double z = temp * (x / mu - 1.0);
147 double w = temp * (x / mu + 1.0);
148
149 // C'est bien un + dans exp (2 * lambda / mu)
150 return (NormalDist.cdf01(z) + Math.exp(2 * lambda / mu) * NormalDist.cdf01(-w));
151 }
152
158 public static double barF(double mu, double lambda, double x) {
159 return 1.0 - cdf(mu, lambda, x);
160 }
161
166 public static double inverseF(double mu, double lambda, double u) {
167 if (mu <= 0.0)
168 throw new IllegalArgumentException("mu <= 0");
169 if (lambda <= 0.0)
170 throw new IllegalArgumentException("lambda <= 0");
171 if (u < 0.0 || u > 1.0)
172 throw new IllegalArgumentException("u must be in [0,1]");
173 if (u == 1.0)
174 return Double.POSITIVE_INFINITY;
175 if (u == 0.0)
176 return 0.0;
177
178 Function f = new Function(mu, lambda, u);
179
180 // Find interval containing root = x*
181 double sig = getStandardDeviation(mu, lambda);
182 double x0 = 0.0;
183 double x = mu;
184 double v = cdf(mu, lambda, x);
185 while (v < u) {
186 x0 = x;
187 x += 3.0 * sig;
188 v = cdf(mu, lambda, x);
189 }
190
191 return RootFinder.brentDekker(x0, x, f, 1e-12);
192 }
193
212 public static double[] getMLE(double[] x, int n) {
213 if (n <= 0)
214 throw new IllegalArgumentException("n <= 0");
215
216 double parameters[];
217 parameters = new double[2];
218 double sum = 0;
219 for (int i = 0; i < n; i++) {
220 sum += x[i];
221 }
222 parameters[0] = sum / (double) n;
223
224 sum = 0;
225 for (int i = 0; i < n; i++) {
226 sum += ((1.0 / (double) x[i]) - (1.0 / parameters[0]));
227 }
228 parameters[1] = (double) n / (double) sum;
229
230 return parameters;
231 }
232
242 public static InverseGaussianDist getInstanceFromMLE(double[] x, int n) {
243 double parameters[] = getMLE(x, n);
244 return new InverseGaussianDist(parameters[0], parameters[1]);
245 }
246
253 public static double getMean(double mu, double lambda) {
254 if (mu <= 0.0)
255 throw new IllegalArgumentException("mu <= 0");
256 if (lambda <= 0.0)
257 throw new IllegalArgumentException("lambda <= 0");
258
259 return mu;
260 }
261
269 public static double getVariance(double mu, double lambda) {
270 if (mu <= 0.0)
271 throw new IllegalArgumentException("mu <= 0");
272 if (lambda <= 0.0)
273 throw new IllegalArgumentException("lambda <= 0");
274
275 return (mu * mu * mu / lambda);
276 }
277
284 public static double getStandardDeviation(double mu, double lambda) {
285 return Math.sqrt(getVariance(mu, lambda));
286 }
287
291 public double getLambda() {
292 return lambda;
293 }
294
298 public double getMu() {
299 return mu;
300 }
301
305 public void setParams(double mu, double lambda) {
306 if (mu <= 0.0)
307 throw new IllegalArgumentException("mu <= 0");
308 if (lambda <= 0.0)
309 throw new IllegalArgumentException("lambda <= 0");
310
311 this.mu = mu;
312 this.lambda = lambda;
313 supportA = 0.0;
314 }
315
320 public double[] getParams() {
321 double[] retour = { mu, lambda };
322 return retour;
323 }
324
328 public String toString() {
329 return getClass().getSimpleName() + " : mu = " + mu + ", lambda = " + lambda;
330 }
331
332}
Classes implementing continuous distributions should inherit from this base class.
static double getMean(double mu, double lambda)
Returns the mean of the inverse gaussian distribution with parameters and .
double getLambda()
Returns the parameter of this object.
static InverseGaussianDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of an inverse gaussian distribution with parameters and estimated using the ...
static double getStandardDeviation(double mu, double lambda)
Computes and returns the standard deviation of the inverse gaussian distribution with parameters and...
String toString()
Returns a String containing information about the current distribution.
double density(double x)
Returns , the density evaluated at .
static double[] getMLE(double[] x, int n)
Estimates the parameters of the inverse gaussian distribution using the maximum likelihood method,...
static double getVariance(double mu, double lambda)
Computes and returns the variance of the inverse gaussian distribution with parameters and .
static double cdf(double mu, double lambda, double x)
Computes the distribution function ( FInverseGaussian ) of the inverse gaussian distribution with par...
double barF(double x)
Returns the complementary distribution function.
static double inverseF(double mu, double lambda, double u)
Computes the inverse of the inverse gaussian distribution with parameters and .
static double barF(double mu, double lambda, double x)
Computes the complementary distribution function of the inverse gaussian distribution with parameters...
double inverseF(double u)
Returns the inverse distribution function .
static double density(double mu, double lambda, double x)
Computes the density function ( fInverseGaussian ) for the inverse gaussian distribution with paramet...
double[] getParams()
Return a table containing the parameters of the current distribution.
double cdf(double x)
Returns the distribution function .
InverseGaussianDist(double mu, double lambda)
Constructs the inverse Gaussian distribution with parameters.
void setParams(double mu, double lambda)
Sets the parameters and of this object.
double getMu()
Returns the parameter of this object.
double getStandardDeviation()
Returns the standard deviation.
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
static double cdf01(double x)
Same as cdf(0, 1, x).
This class provides numerical methods to solve non-linear equations.
static double brentDekker(double a, double b, MathFunction f, double tol)
Computes a root of the function in f using the Brent-Dekker method.
This interface should be implemented by classes which represent univariate mathematical functions.