SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
AndersonDarlingDistQuick.java
1/*
2 * Class: AndersonDarlingDistQuick
3 * Description: Anderson-Darling 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
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.*;
28import umontreal.ssj.functions.MathFunction;
29
41
42 private static class Function implements MathFunction {
43 protected int n;
44 protected double u;
45
46 public Function(int n, double u) {
47 this.n = n;
48 this.u = u;
49 }
50
51 public double evaluate(double x) {
52 return u - cdf(n, x);
53 }
54 }
55
56 // -------------------------------------------------------------------------
57
58 private static double lower_Marsaglia(int n, double x) {
59 // queue inférieure de l'algorithme de Marsaglia pour n = inf
60
61 double p0 = (2.00012 + (.247105 - (.0649821 - (.0347962 - (.011672 - .00168691 * x) * x) * x) * x) * x);
62 p0 *= Math.exp(-1.2337141 / x) / Math.sqrt(x);
63 return p0 >= 0 ? p0 : 0;
64 }
65
66 private static double diffcdf(int n, double x, double EPS) {
67 return (cdf(n, x + EPS) - cdf(n, x - EPS)) / (2.0 * EPS);
68 }
69
74 super(n);
75 }
76
77 public double density(double x) {
78 return density(n, x);
79 }
80
81 public double cdf(double x) {
82 return cdf(n, x);
83 }
84
85 public double barF(double x) {
86 return barF(n, x);
87 }
88
89 public double inverseF(double u) {
90 return inverseF(n, u);
91 }
92
97 public static double density(int n, double x) {
98 if (n <= 0)
99 throw new IllegalArgumentException("n <= 0");
100 if (n == 1)
101 return density_N_1(x);
102
103 if (x >= XBIG || x <= 0.0)
104 return 0.0;
105 final double EPS = 1.0 / 64.0;
106 final double D1 = diffcdf(n, x, EPS);
107 final double D2 = diffcdf(n, x, 2.0 * EPS);
108 double res = D1 + (D1 - D2) / 3.0;
109 return res >= 0. ? res : 0.;
110 }
111
112 // Tables for the approximation of the Anderson-Darling distribution
113 private static double[] F2AD = new double[103];
114 private static double[] CoAD = new double[103];
115
116 static {
117 F2AD[0] = 0.0;
118 F2AD[1] = 1.7315E-10;
119 F2AD[2] = 2.80781E-5;
120 F2AD[3] = 1.40856E-3;
121 F2AD[4] = 9.58772E-3;
122 F2AD[5] = 2.960552E-2;
123 F2AD[6] = 6.185146E-2;
124 F2AD[7] = 1.0357152E-1;
125 F2AD[8] = 1.5127241E-1;
126 F2AD[9] = 2.0190317E-1;
127 F2AD[10] = 2.5318023E-1;
128 F2AD[11] = 3.0354278E-1;
129 F2AD[12] = 3.5200015E-1;
130 F2AD[13] = 3.9797537E-1;
131 F2AD[14] = 4.4117692E-1;
132 F2AD[15] = 4.8150305E-1;
133 F2AD[16] = 5.1897375E-1;
134 F2AD[17] = 5.5368396E-1;
135 F2AD[18] = 5.8577199E-1;
136 F2AD[19] = 6.1539864E-1;
137 F2AD[20] = 6.4273362E-1;
138 F2AD[21] = 6.6794694E-1;
139 F2AD[22] = 6.9120359E-1;
140 F2AD[23] = 7.126605E-1;
141 F2AD[24] = 7.3246483E-1;
142 F2AD[25] = 7.507533E-1;
143 F2AD[26] = 7.6765207E-1;
144 F2AD[27] = 7.8327703E-1;
145 F2AD[28] = 7.9773426E-1;
146 F2AD[29] = 8.1112067E-1;
147 F2AD[30] = 8.2352466E-1;
148 F2AD[31] = 8.3502676E-1;
149 F2AD[32] = 8.4570037E-1;
150 F2AD[33] = 8.5561231E-1;
151 F2AD[34] = 8.6482346E-1;
152 F2AD[35] = 8.7338931E-1;
153 F2AD[36] = 8.8136046E-1;
154 F2AD[37] = 8.8878306E-1;
155 F2AD[38] = 8.9569925E-1;
156 F2AD[39] = 9.0214757E-1;
157 F2AD[40] = 9.081653E-1;
158 F2AD[41] = 9.1378043E-1;
159 F2AD[42] = 9.1902284E-1;
160 F2AD[43] = 9.2392345E-1;
161 F2AD[44] = 9.2850516E-1;
162 F2AD[45] = 9.3279084E-1;
163 F2AD[46] = 9.3680149E-1;
164 F2AD[47] = 9.4055647E-1;
165 F2AD[48] = 9.440736E-1;
166 F2AD[49] = 9.4736933E-1;
167 F2AD[50] = 9.5045883E-1;
168 F2AD[51] = 9.5335611E-1;
169 F2AD[52] = 9.5607414E-1;
170 F2AD[53] = 9.586249E-1;
171 F2AD[54] = 9.6101951E-1;
172 F2AD[55] = 9.6326825E-1;
173 F2AD[56] = 9.6538067E-1;
174 F2AD[57] = 9.6736563E-1;
175 F2AD[58] = 9.6923135E-1;
176 F2AD[59] = 9.7098548E-1;
177 F2AD[60] = 9.7263514E-1;
178 F2AD[61] = 9.7418694E-1;
179 F2AD[62] = 9.7564704E-1;
180 F2AD[63] = 9.7702119E-1;
181 F2AD[64] = 9.7831473E-1;
182 F2AD[65] = 9.7953267E-1;
183 F2AD[66] = 9.8067966E-1;
184 F2AD[67] = 9.8176005E-1;
185 F2AD[68] = 9.827779E-1;
186 F2AD[69] = 9.8373702E-1;
187 F2AD[70] = 9.8464096E-1;
188 F2AD[71] = 9.8549304E-1;
189 F2AD[72] = 9.8629637E-1;
190 F2AD[73] = 9.8705386E-1;
191 F2AD[74] = 9.8776824E-1;
192 F2AD[75] = 9.8844206E-1;
193 F2AD[76] = 9.8907773E-1;
194 F2AD[77] = 9.8967747E-1;
195 F2AD[78] = 9.9024341E-1;
196 F2AD[79] = 9.9077752E-1;
197 F2AD[80] = 9.9128164E-1;
198 F2AD[81] = 9.9175753E-1;
199 F2AD[82] = 9.9220682E-1;
200 F2AD[83] = 9.9263105E-1;
201 F2AD[84] = 9.9303165E-1;
202 F2AD[85] = 9.9340998E-1;
203 F2AD[86] = 9.9376733E-1;
204 F2AD[87] = 9.9410488E-1;
205 F2AD[88] = 9.9442377E-1;
206 F2AD[89] = 9.9472506E-1;
207 F2AD[90] = 9.9500974E-1;
208 F2AD[91] = 9.9527876E-1;
209 F2AD[92] = 9.95533E-1;
210 F2AD[93] = 9.9577329E-1;
211 F2AD[94] = 9.9600042E-1;
212 F2AD[95] = 9.9621513E-1;
213 F2AD[96] = 9.964181E-1;
214 F2AD[97] = 0.99661;
215 F2AD[98] = 9.9679145E-1;
216 F2AD[99] = 9.9696303E-1;
217 F2AD[100] = 9.9712528E-1;
218 F2AD[101] = 9.9727872E-1;
219 F2AD[102] = 9.9742384E-1;
220
221 CoAD[0] = 0.0;
222 CoAD[1] = 0.0;
223 CoAD[2] = 0.0;
224 CoAD[3] = 0.0;
225 CoAD[4] = 0.0;
226 CoAD[5] = -1.87E-3;
227 CoAD[6] = 0.00898;
228 CoAD[7] = 0.0209;
229 CoAD[8] = 0.03087;
230 CoAD[9] = 0.0377;
231 CoAD[10] = 0.0414;
232 CoAD[11] = 0.04386;
233 CoAD[12] = 0.043;
234 CoAD[13] = 0.0419;
235 CoAD[14] = 0.0403;
236 CoAD[15] = 0.038;
237 CoAD[16] = 3.54804E-2;
238 CoAD[17] = 0.032;
239 CoAD[18] = 0.0293;
240 CoAD[19] = 2.61949E-2;
241 CoAD[20] = 0.0228;
242 CoAD[21] = 0.0192;
243 CoAD[22] = 1.59865E-2;
244 CoAD[23] = 0.0129;
245 CoAD[24] = 0.0107;
246 CoAD[25] = 8.2464E-3;
247 CoAD[26] = 0.00611;
248 CoAD[27] = 0.00363;
249 CoAD[28] = 1.32272E-3;
250 CoAD[29] = -5.87E-4;
251 CoAD[30] = -2.75E-3;
252 CoAD[31] = -3.95248E-3;
253 CoAD[32] = -5.34E-3;
254 CoAD[33] = -6.892E-3;
255 CoAD[34] = -8.10208E-3;
256 CoAD[35] = -8.93E-3;
257 CoAD[36] = -9.552E-3;
258 CoAD[37] = -1.04605E-2;
259 CoAD[38] = -0.0112;
260 CoAD[39] = -1.175E-2;
261 CoAD[40] = -1.20216E-2;
262 CoAD[41] = -0.0124;
263 CoAD[42] = -1.253E-2;
264 CoAD[43] = -1.27076E-2;
265 CoAD[44] = -0.0129;
266 CoAD[45] = -1.267E-2;
267 CoAD[46] = -1.22015E-2;
268 CoAD[47] = -0.0122;
269 CoAD[48] = -1.186E-2;
270 CoAD[49] = -1.17218E-2;
271 CoAD[50] = -0.0114;
272 CoAD[51] = -1.113E-2;
273 CoAD[52] = -1.08459E-2;
274 CoAD[53] = -0.0104;
275 CoAD[54] = -9.93E-3;
276 CoAD[55] = -9.5252E-3;
277 CoAD[56] = -9.24E-3;
278 CoAD[57] = -9.16E-3;
279 CoAD[58] = -8.8004E-3;
280 CoAD[59] = -8.63E-3;
281 CoAD[60] = -8.336E-3;
282 CoAD[61] = -8.10512E-3;
283 CoAD[62] = -7.94E-3;
284 CoAD[63] = -7.71E-3;
285 CoAD[64] = -7.55064E-3;
286 CoAD[65] = -7.25E-3;
287 CoAD[66] = -7.11E-3;
288 CoAD[67] = -6.834E-3;
289 CoAD[68] = -0.0065;
290 CoAD[69] = -6.28E-3;
291 CoAD[70] = -6.11008E-3;
292 CoAD[71] = -5.86E-3;
293 CoAD[72] = -5.673E-3;
294 CoAD[73] = -5.35008E-3;
295 CoAD[74] = -5.11E-3;
296 CoAD[75] = -4.786E-3;
297 CoAD[76] = -4.59144E-3;
298 CoAD[77] = -4.38E-3;
299 CoAD[78] = -4.15E-3;
300 CoAD[79] = -4.07696E-3;
301 CoAD[80] = -3.93E-3;
302 CoAD[81] = -3.83E-3;
303 CoAD[82] = -3.74656E-3;
304 CoAD[83] = -3.49E-3;
305 CoAD[84] = -3.33E-3;
306 CoAD[85] = -3.20064E-3;
307 CoAD[86] = -3.09E-3;
308 CoAD[87] = -2.93E-3;
309 CoAD[88] = -2.78136E-3;
310 CoAD[89] = -2.72E-3;
311 CoAD[90] = -2.66E-3;
312 CoAD[91] = -2.56208E-3;
313 CoAD[92] = -2.43E-3;
314 CoAD[93] = -2.28E-3;
315 CoAD[94] = -2.13536E-3;
316 CoAD[95] = -2.083E-3;
317 CoAD[96] = -1.94E-3;
318 CoAD[97] = -1.82E-3;
319 CoAD[98] = -1.77E-3;
320 CoAD[99] = -1.72E-3;
321 CoAD[100] = -1.71104E-3;
322 CoAD[101] = -1.741E-3;
323 CoAD[102] = -0.0016;
324 }
325
343 public static double cdf(int n, double x) {
344 if (n <= 0)
345 throw new IllegalArgumentException(" n <= 0");
346 if (1 == n)
347 return cdf_N_1(x);
348 if (x <= 0.0)
349 return 0.0;
350 if (x >= XBIG)
351 return 1.0;
352 if (x <= 0.2)
353 return lower_Marsaglia(n, x);
354 return 1.0 - barF(n, x);
355 }
356
361 public static double barF(int n, double x) {
362 if (n <= 0)
363 throw new IllegalArgumentException("n <= 0");
364 if (n == 1)
365 return barF_N_1(x);
366
367 if (x <= 0.0)
368 return 1.0;
369 if (x >= XBIGM)
370 return 0.0;
371
372 double q;
373 if (x > 5.) {
374 // Grace-Wood approx. in upper tail
375 double nd = n;
376 q = (0.23945 * Math.pow(nd, -0.9379) - 0.1201 * Math.pow(nd, -0.96) - 1.0002816) * x
377 - 1.437 * Math.pow(nd, -0.9379) + 1.441 * Math.pow(nd, -0.96) - 0.0633101;
378 return Math.pow(x, -0.48897) * Math.exp(q);
379 }
380
381 if (x <= 0.2)
382 return 1.0 - cdf(n, x);
383
384 final double H = 0.05; // the step of the interpolation table
385 final int i = (int) (1 + x / H);
386 double res;
387 q = x / H - i;
388
389 // Newton backwards quadratic interpolation
390 res = (F2AD[i - 2] - 2.0 * F2AD[i - 1] + F2AD[i]) * q * (q + 1.0) / 2.0 + (F2AD[i] - F2AD[i - 1]) * q + F2AD[i];
391
392 // Empirical correction in 1/n
393 res += (CoAD[i] * (q + 1.0) - CoAD[i - 1] * q) / n;
394
395 res = 1.0 - res;
396 if (res >= 1.0)
397 return 1.0;
398 if (res <= 0.0)
399 return 0.0;
400 return res;
401 }
402
407 public static double inverseF(int n, double u) {
408 if (n <= 0)
409 throw new IllegalArgumentException("n <= 0");
410 if (u < 0.0 || u > 1.0)
411 throw new IllegalArgumentException("u must be in [0,1]");
412 if (n == 1)
413 return inverse_N_1(u);
414 if (u == 1.0)
415 return Double.POSITIVE_INFINITY;
416 if (u == 0.0)
417 return 0.0;
418 Function f = new Function(n, u);
419 return RootFinder.brentDekker(0.0, 50.0, f, 1.0e-5);
420 }
421
425 public String toString() {
426 return getClass().getSimpleName() + " : n = " + n;
427 }
428
429}
static double cdf(int n, double x)
Computes the Anderson–Darling distribution function at.
String toString()
Returns a String containing information about the current distribution.
double barF(double x)
Returns the complementary distribution function.
double density(double x)
Returns , the density evaluated at .
static double density(int n, double x)
Computes the density of the Anderson–Darling distribution with parameter .
double inverseF(double u)
Returns the inverse distribution function .
AndersonDarlingDistQuick(int n)
Constructs an Anderson–Darling distribution for a sample of size .
static double inverseF(int n, double u)
Computes the inverse of the Anderson–Darling distribution with parameter .
static double barF(int n, double x)
Computes the complementary distribution function with parameter .
double cdf(double x)
Returns the distribution function .
AndersonDarlingDist(int n)
Constructs an Anderson–Darling distribution for a sample of size .
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.