SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ChiSquareDistQuick.java
1/*
2 * Class: ChiSquareDistQuick
3 * Description: chi-square 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
39public class ChiSquareDistQuick extends ChiSquareDist {
40
44 public ChiSquareDistQuick(int n) {
45 super(n);
46 }
47
48 public double inverseF(double u) {
49 return inverseF(n, u);
50 }
51
74 public static double inverseF(int n, double u) {
75 /*
76 * Returns an approximation of the inverse of Chi square cdf with n degrees of
77 * freedom. As in Figure L.24 of P.Bratley, B.L.Fox, and L.E.Schrage. A Guide to
78 * Simulation Springer-Verlag, New York, second edition, 1987.
79 */
80
81 if (u < 0.0 || u > 1.0)
82 throw new IllegalArgumentException("u is not in [0,1]");
83 if (u <= 0.0)
84 return 0.0;
85 if (u >= 1.0)
86 return Double.POSITIVE_INFINITY;
87
88 final double SQP5 = 0.70710678118654752440;
89 final double DWARF = 0.1e-15;
90 final double ULOW = 0.02;
91 double Z, arg, v, ch, sqdf;
92
93 if (n == 1) {
94 Z = NormalDist.inverseF01((1.0 + u) / 2.0);
95 return Z * Z;
96
97 } else if (n == 2) {
98 arg = 1.0 - u;
99 if (arg < DWARF)
100 arg = DWARF;
101 return -Math.log(arg) * 2.0;
102
103 } else if ((u > ULOW) && (u < 1.0 - ULOW)) {
104 Z = NormalDist.inverseF01(u);
105 sqdf = Math.sqrt((double) n);
106 v = Z * Z;
107
108 ch = -(((3753.0 * v + 4353.0) * v - 289517.0) * v - 289717.0) * Z * SQP5 / 9185400;
109
110 ch = ch / sqdf + (((12.0 * v - 243.0) * v - 923.0) * v + 1472.0) / 25515.0;
111
112 ch = ch / sqdf + ((9.0 * v + 256.0) * v - 433.0) * Z * SQP5 / 4860;
113
114 ch = ch / sqdf - ((6.0 * v + 14.0) * v - 32.0) / 405.0;
115 ch = ch / sqdf + (v - 7.0) * Z * SQP5 / 9;
116 ch = ch / sqdf + 2.0 * (v - 1.0) / 3.0;
117 ch = ch / sqdf + Z / SQP5;
118 return n * (ch / sqdf + 1.0);
119
120 } else if (n >= 10) {
121 Z = NormalDist.inverseF01(u);
122 v = Z * Z;
123 double temp;
124 temp = 1.0 / 3.0 + (-v + 3.0) / (162.0 * n) - (3.0 * v * v + 40.0 * v + 45.0) / (5832.0 * n * n)
125 + (301.0 * v * v * v - 1519.0 * v * v - 32769.0 * v - 79349.0) / (7873200.0 * n * n * n);
126 temp *= Z * Math.sqrt(2.0 / n);
127
128 ch = 1.0 - 2.0 / (9.0 * n) + (4.0 * v * v + 16.0 * v - 28.0) / (1215.0 * n * n)
129 + (8.0 * v * v * v + 720.0 * v * v + 3216.0 * v + 2904.0) / (229635.0 * n * n * n) + temp;
130
131 return n * ch * ch * ch;
132
133 } else {
134 // Note: this implementation is quite slow.
135 // Since it is restricted to the tails, we could perhaps replace
136 // this with some other approximation (series expansion ?)
137 return 2.0 * GammaDist.inverseF(n / 2.0, 6, u);
138 }
139 }
140
141}
static double inverseF(int n, double u)
Computes a quick-and-dirty approximation of , where is the chi-square distribution with degrees of ...
double inverseF(double u)
Returns the inverse distribution function .
ChiSquareDistQuick(int n)
Constructs a chi-square distribution with n degrees of freedom.
ChiSquareDist(int n)
Constructs a chi-square distribution with n degrees of freedom.
Extends the class ContinuousDistribution for the gamma distribution tjoh95a  (page 337) with shape pa...
double inverseF(double u)
Returns the inverse distribution function .
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
static double inverseF01(double u)
Same as inverseF(0, 1, u).