SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
HypoExponentialDist.java
1/*
2 * Class: HypoExponentialDist
3 * Description: Hypo-exponential 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 Richard Simard
9 * @since January 2011
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;
29import cern.colt.matrix.*;
30import cern.colt.matrix.impl.*;
31import umontreal.ssj.util.*;
32import umontreal.ssj.functions.MathFunction;
33
75 protected double[] m_lambda;
76
77 protected static void testLambda(double[] lambda) {
78 int m = lambda.length;
79 for (int j = 0; j < m; ++j) {
80 if (lambda[j] <= 0)
81 throw new IllegalArgumentException("lambda_j <= 0");
82 }
83 }
84
85 // Builds the bidiagonal matrix A out of the lambda
86 private static DoubleMatrix2D buildMatrix(double[] lambda, double x) {
87 int m = lambda.length;
88 testLambda(lambda);
89 DoubleFactory2D F2 = DoubleFactory2D.dense;
90 DoubleMatrix2D A = F2.make(m, m);
91 for (int j = 0; j < m - 1; j++) {
92 A.setQuick(j, j, -lambda[j] * x);
93 A.setQuick(j, j + 1, lambda[j] * x);
94 }
95 A.setQuick(m - 1, m - 1, -lambda[m - 1] * x);
96 return A;
97 }
98
99 private static class myFunc implements MathFunction {
100 // For inverseF
101 private double[] m_lam;
102 private double m_u;
103
104 public myFunc(double[] lam, double u) {
105 m_lam = lam;
106 m_u = u;
107 }
108
109 public double evaluate(double x) {
110 return m_u - HypoExponentialDist.cdf(m_lam, x);
111 }
112 }
113
120 public HypoExponentialDist(double[] lambda) {
121 supportA = 0.0;
122 setLambda(lambda);
123 }
124
125 public double density(double x) {
126 return density(m_lambda, x);
127 }
128
129 public double cdf(double x) {
130 return cdf(m_lambda, x);
131 }
132
133 public double barF(double x) {
134 return barF(m_lambda, x);
135 }
136
137 public double inverseF(double u) {
138 return inverseF(m_lambda, u);
139 }
140
141 public double getMean() {
142 return getMean(m_lambda);
143 }
144
145 public double getVariance() {
146 return getVariance(m_lambda);
147 }
148
149 public double getStandardDeviation() {
150 return getStandardDeviation(m_lambda);
151 }
152
161 public static double density(double[] lambda, double x) {
162 if (x < 0)
163 return 0;
164 DoubleMatrix2D Ax = buildMatrix(lambda, x);
165 DoubleMatrix2D T = DMatrix.expBidiagonal(Ax);
166 int m = lambda.length;
167 return lambda[m - 1] * T.getQuick(0, m - 1);
168 }
169
178 public static double cdf(double[] lambda, double x) {
179 if (x <= 0.0)
180 return 0.0;
181 if (x >= Double.MAX_VALUE)
182 return 1.0;
183 double mean = HypoExponentialDist.getMean(lambda);
184 double std = HypoExponentialDist.getStandardDeviation(lambda);
185 double LOW = mean - 1.5 * std;
186 if (x > LOW) {
187 double p = 1.0 - HypoExponentialDist.barF(lambda, x);
188 double LIMIT = 1.0e-3;
189 if (p > LIMIT)
190 return p;
191 }
192
193 DoubleMatrix2D T = buildMatrix(lambda, x);
194 DoubleFactory1D fac1 = DoubleFactory1D.dense;
195 int m = lambda.length;
196 DoubleMatrix1D C = fac1.make(m, 1.0);
197 DoubleMatrix1D B = DMatrix.expmiBidiagonal(T, C);
198 return Math.abs(B.getQuick(0));
199 }
200
211 public static double cdf2(double[] lambda, double x) {
212 if (x <= 0.0)
213 return 0.0;
214 if (x >= Double.MAX_VALUE)
215 return 1.0;
216 return (1.0 - HypoExponentialDist.barF(lambda, x));
217 }
218
227 public static double barF(double[] lambda, double x) {
228 if (x <= 0.0)
229 return 1.0;
230 if (x >= Double.MAX_VALUE)
231 return 0.0;
232 DoubleMatrix2D T = buildMatrix(lambda, x);
233 DoubleFactory1D fac1 = DoubleFactory1D.dense;
234 int m = lambda.length;
235 DoubleMatrix1D C = fac1.make(m, 1.0);
236 DoubleMatrix1D B = DMatrix.expBidiagonal(T, C);
237 return B.getQuick(0);
238 }
239
248 public static double inverseF(double[] lambda, double u) {
249 if (u < 0.0 || u > 1.0)
250 throw new IllegalArgumentException("u not in [0,1]");
251 if (u >= 1.0)
252 return Double.POSITIVE_INFINITY;
253 if (u <= 0.0)
254 return 0.0;
255
256 final double EPS = 1.0e-12;
257 myFunc fonc = new myFunc(lambda, u);
258 double x1 = getMean(lambda);
259 double v = cdf(lambda, x1);
260 if (u <= v)
261 return RootFinder.brentDekker(0, x1, fonc, EPS);
262
263 // u > v
264 double x2 = 4.0 * x1 + 1.0;
265 v = cdf(lambda, x2);
266 while (v < u) {
267 x1 = x2;
268 x2 = 4.0 * x2;
269 v = cdf(lambda, x2);
270 }
271 return RootFinder.brentDekker(x1, x2, fonc, EPS);
272 }
273
282 public static double getMean(double[] lambda) {
283 testLambda(lambda);
284 int k = lambda.length;
285 double sum = 0;
286 for (int j = 0; j < k; j++)
287 sum += 1.0 / lambda[j];
288 return sum;
289 }
290
299 public static double getVariance(double[] lambda) {
300 testLambda(lambda);
301 int k = lambda.length;
302 double sum = 0;
303 for (int j = 0; j < k; j++)
304 sum += 1.0 / (lambda[j] * lambda[j]);
305 return sum;
306 }
307
316 public static double getStandardDeviation(double[] lambda) {
317 double s = getVariance(lambda);
318 return Math.sqrt(s);
319 }
320
324 public double[] getLambda() {
325 return m_lambda;
326 }
327
333 public void setLambda(double[] lambda) {
334 if (lambda == null)
335 return;
336 int k = lambda.length;
337 m_lambda = new double[k];
338 testLambda(lambda);
339 System.arraycopy(lambda, 0, m_lambda, 0, k);
340 }
341
345 public double[] getParams() {
346 return m_lambda;
347 }
348
352 public String toString() {
353 StringBuilder sb = new StringBuilder();
354 Formatter formatter = new Formatter(sb, Locale.US);
355 formatter.format(getClass().getSimpleName() + " : lambda = {" + PrintfFormat.NEWLINE);
356 int k = m_lambda.length;
357 for (int i = 0; i < k; i++) {
358 formatter.format(" %g%n", m_lambda[i]);
359 }
360 formatter.format("}%n");
361 return sb.toString();
362 }
363
364}
Classes implementing continuous distributions should inherit from this base class.
static double getMean(double[] lambda)
Returns the mean, , of the hypoexponential distribution with rates lambda[ ], .
static double density(double[] lambda, double x)
Computes the density function , with lambda[ ], .
void setLambda(double[] lambda)
Sets the values lambda[ ], for this object.
double cdf(double x)
Returns the distribution function .
static double getVariance(double[] lambda)
Returns the variance, , of the hypoexponential distribution with rates.
HypoExponentialDist(double[] lambda)
Constructs a HypoExponentialDist object, with rates lambda[ ], .
double barF(double x)
Returns the complementary distribution function.
static double barF(double[] lambda, double x)
Computes the complementary distribution , with.
String toString()
Returns a String containing information about the current distribution.
double density(double x)
Returns , the density evaluated at .
double getStandardDeviation()
Returns the standard deviation.
double[] getLambda()
Returns the values for this object.
static double cdf2(double[] lambda, double x)
Computes the distribution function , with lambda[ ], .
static double getStandardDeviation(double[] lambda)
Returns the standard deviation of the hypoexponential distribution with rates lambda[ ],...
static double cdf(double[] lambda, double x)
Computes the distribution function , with lambda[ ], .
static double inverseF(double[] lambda, double u)
Computes the inverse distribution function , with.
double inverseF(double u)
Returns the inverse distribution function .
This class implements a few methods for matrix calculations when the matrix entries are in double.
Definition DMatrix.java:38
static DoubleMatrix2D expmiBidiagonal(final DoubleMatrix2D A)
Computes , where is the exponential of the bidiagonal square matrix .
Definition DMatrix.java:663
static DoubleMatrix2D expBidiagonal(final DoubleMatrix2D A)
Returns , the exponential of the bidiagonal square matrix.
Definition DMatrix.java:554
This class acts like a StringBuffer which defines new types of append methods.
static final String NEWLINE
End-of-line symbol or line separator.
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.