SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
StudentDist.java
1/*
2 * Class: StudentDistDist
3 * Description: Student-t 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 March 2009
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;
28import umontreal.ssj.functions.MathFunction;
29
47public class StudentDist extends ContinuousDistribution {
48 protected int n;
49 private double factor;
50 private static final int NLIM1 = 100000;
51 /*
52 * private static double cdfPeizer (int n, double x) { // Peizer-Pratt normal
53 * approximation for the cdf (n, u) // @cite tPEI68a double v =
54 * Math.log1p(x*x/n) / (n - 5.0/6.0); double z = -(n - 2.0/3.0 + 0.1/n) *
55 * Math.sqrt(v); double u = NormalDist.cdf01 (z); if (x >= 0.0) return 1.0 - u;
56 * return u; }
57 *
58 * private static double invPeizer (int n, double u) { // Peizer-Pratt normal
59 * approximation for the inverseF (n, u) // @cite tPEI68a double z =
60 * NormalDist.inverseF01 (u); double q = z / (n - 2.0/3.0 + 0.1/n); double v =
61 * q*q*(n - 5.0/6.0); double t = Math.sqrt(n * Math.expm1(v)); if (u >= 0.5)
62 * return t; else return -t; }
63 */
64
65 private static double cdfGaver(int n, double x) {
66 // Gaver-Kafadar normal approximation for the cdf
67 // @cite tGAV84a
68 double v = Math.log1p(x * x / n) / (n - 1.5);
69 double z = -(n - 1) * Math.sqrt(v);
70 double u = NormalDist.cdf01(z);
71 if (x >= 0.0)
72 return 1.0 - u;
73 return u;
74 }
75
76 private static double invGaver(int n, double u) {
77 // Gaver-Kafadar normal approximation for the inverse
78 // @cite tGAV84a
79 double z = NormalDist.inverseF01(u);
80 double q = z / (n - 1.0);
81 double v = q * q * (n - 1.5);
82 double t = Math.sqrt(n * Math.expm1(v));
83 if (u >= 0.5)
84 return t;
85 else
86 return -t;
87 }
88
89 private static class Function implements MathFunction {
90 private int n;
91 private double[] xi;
92
93 public Function(double[] x, int n) {
94 this.n = n;
95 this.xi = new double[n];
96 System.arraycopy(x, 0, this.xi, 0, n);
97 }
98
99 public double evaluate(double x) {
100 if (x <= 0.0)
101 return 1e200;
102 double sum = 0.0;
103 for (int i = 0; i < n; i++)
104 sum += Math.log(density((int) Math.round(x), xi[i]));
105 return sum;
106 }
107 }
108
112 public StudentDist(int n) {
113 setN(n);
114 }
115
116 public double density(double x) {
117 return factor * Math.pow(1.0 / (1.0 + x * x / n), (n + 1) / 2.0);
118 }
119
120 public double cdf(double x) {
121 return cdf(n, x);
122 }
123
124 public double barF(double x) {
125 return barF(n, x);
126 }
127
128 public double inverseF(double u) {
129 return inverseF(n, u);
130 }
131
132 public double getMean() {
133 return StudentDist.getMean(n);
134 }
135
136 public double getVariance() {
137 return StudentDist.getVariance(n);
138 }
139
140 public double getStandardDeviation() {
141 return StudentDist.getStandardDeviation(n);
142 }
143
149 public static double density(int n, double x) {
150 double factor = Num.gammaRatioHalf(n / 2.0) / Math.sqrt(n * Math.PI);
151 return factor * Math.pow(1.0 / (1.0 + x * x / n), (n + 1) / 2.0);
152 }
153
162 public static double cdf(int n, double x) {
163 if (n < 1)
164 throw new IllegalArgumentException("n < 1");
165 if (n == 1)
166 return CauchyDist.cdf(0, 1, x);
167
168 if (x > 1.0e10)
169 return 1.0;
170 if (n > NLIM1)
171 return cdfGaver(n, x);
172
173 double r = Math.abs(x);
174 if (r < 1.0e20)
175 r = Math.sqrt(n + x * x);
176
177 double z;
178 if (x >= 0.0)
179 z = 0.5 * (1.0 + x / r);
180 else
181 z = 0.5 * n / (r * (r - x));
182
183 if (n == 2)
184 return z;
185 return BetaSymmetricalDist.cdf(0.5 * n, 15, z);
186 }
187
191 @Deprecated
192 public static double cdf2(int n, int d, double x) {
193 if (d <= 0)
194 throw new IllegalArgumentException("student2: d <= 0");
195 return cdf(n, x);
196 }
197
205 public static double barF(int n, double x) {
206 if (n < 1)
207 throw new IllegalArgumentException("n < 1");
208 if (n == 1)
209 return CauchyDist.barF(0, 1, x);
210
211 if (n == 2) {
212 double z = Math.abs(x);
213 if (z < 1.0e20)
214 z = Math.sqrt(2.0 + x * x);
215 if (x <= 0.) {
216 if (x < -1.0e10)
217 return 1.0;
218 return 0.5 * (1.0 - x / z);
219 } else
220 return 1.0 / (z * (z + x));
221 }
222
223 return cdf(n, -x);
224 }
225
233 public static double inverseF(int n, double u) {
234 if (n < 1)
235 throw new IllegalArgumentException("Student: n < 1");
236 if (u > 1.0 || u < 0.0)
237 throw new IllegalArgumentException("Student: u not in [0, 1]");
238 if (u <= 0.0)
239 return Double.NEGATIVE_INFINITY;
240 if (u >= 1.0)
241 return Double.POSITIVE_INFINITY;
242
243 if (1 == n)
244 return CauchyDist.inverseF(0, 1, u);
245
246 if (2 == n)
247 return (2.0 * u - 1.0) / Math.sqrt(2.0 * u * (1.0 - u));
248
249 if (n > NLIM1)
250 return invGaver(n, u);
251 double z = BetaSymmetricalDist.inverseF(0.5 * n, u);
252 return (z - 0.5) * Math.sqrt(n / (z * (1.0 - z)));
253 }
254
265 public static double[] getMLE(double[] x, int m) {
266 double sum = 0.0;
267 double[] parameters = new double[1];
268
269 if (m <= 0)
270 throw new IllegalArgumentException("m <= 0");
271
272 double var = 0.0;
273 for (int i = 0; i < m; i++)
274 var += x[i] * x[i];
275 var /= (double) m;
276
277 Function f = new Function(x, m);
278
279 double n0 = Math.round((2.0 * var) / (var - 1.0));
280 double fn0 = f.evaluate(n0);
281 double min = fn0;
282 double fn1 = f.evaluate(n0 + 1.0);
283 double fn_1 = f.evaluate(n0 - 1.0);
284
285 parameters[0] = n0;
286
287 if (fn_1 > fn0) {
288 double n = n0 - 1.0;
289 double y;
290 while (((y = f.evaluate(n)) > min) && (n >= 1.0)) {
291 min = y;
292 parameters[0] = n;
293 n -= 1.0;
294 }
295
296 } else if (fn1 > fn0) {
297 double n = n0 + 1.0;
298 double y;
299 while ((y = f.evaluate(n)) > min) {
300 min = y;
301 parameters[0] = n;
302 n += 1.0;
303 }
304 }
305 return parameters;
306 }
307
316 public static StudentDist getInstanceFromMLE(double[] x, int m) {
317 double parameters[] = getMLE(x, m);
318 return new StudentDist((int) parameters[0]);
319 }
320
327 public static double getMean(int n) {
328 if (n < 2)
329 throw new IllegalArgumentException("n <= 1");
330 return 0;
331 }
332
340 public static double getVariance(int n) {
341 if (n < 3)
342 throw new IllegalArgumentException("n <= 2");
343 return (n / (n - 2.0));
344 }
345
352 public static double getStandardDeviation(int n) {
353 return Math.sqrt(StudentDist.getVariance(n));
354 }
355
359 public int getN() {
360 return n;
361 }
362
366 public void setN(int n) {
367 if (n <= 0)
368 throw new IllegalArgumentException("n <= 0");
369 this.n = n;
370 factor = Num.gammaRatioHalf(n / 2.0) / Math.sqrt(n * Math.PI);
371 }
372
376 public double[] getParams() {
377 double[] retour = { n };
378 return retour;
379 }
380
384 public String toString() {
385 return getClass().getSimpleName() + " : n = " + n;
386 }
387
388}
Specializes the class BetaDist to the case of a symmetrical beta distribution over the interval ,...
double inverseF(double u)
Returns the inverse distribution function .
double cdf(double x)
Returns the distribution function .
Extends the class ContinuousDistribution for the Cauchy distribution tjoh95a  (page 299) with locatio...
double inverseF(double u)
Returns the inverse distribution function .
double cdf(double x)
Returns the distribution function .
double barF(double x)
Returns the complementary distribution function.
Classes implementing continuous distributions should inherit from this base class.
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
static double inverseF01(double u)
Same as inverseF(0, 1, u).
static double cdf01(double x)
Same as cdf(0, 1, x).
static double density(int n, double x)
Computes the density function ( fstudent ) of a Student -distribution with degrees of freedom.
void setN(int n)
Sets the parameter associated with this object.
static double getVariance(int n)
Computes and returns the variance of the Student -distribution with parameter .
double getVariance()
Returns the variance.
static double getMean(int n)
Returns the mean of the Student -distribution with parameter .
static double cdf(int n, double x)
Computes the Student -distribution function with.
StudentDist(int n)
Constructs a StudentDist object with n degrees of freedom.
static double cdf2(int n, int d, double x)
Same as cdf(n, x).
static StudentDist getInstanceFromMLE(double[] x, int m)
Creates a new instance of a Student -distribution with parameter estimated using the maximum likelih...
double getMean()
Returns the mean.
double[] getParams()
Return a table containing the parameter of the current distribution.
static double[] getMLE(double[] x, int m)
Estimates the parameter of the Student -distribution using the maximum likelihood method,...
static double inverseF(int n, double u)
Returns the inverse of Student.
double density(double x)
Returns , the density evaluated at .
double cdf(double x)
Returns the distribution function .
String toString()
Returns a String containing information about the current distribution.
double barF(double x)
Returns the complementary distribution function.
double inverseF(double u)
Returns the inverse distribution function .
double getStandardDeviation()
Returns the standard deviation.
static double getStandardDeviation(int n)
Computes and returns the standard deviation of the Student.
static double barF(int n, double x)
Computes the complementary distribution function with degrees of freedom.
int getN()
Returns the parameter associated with this object.
This class provides various constants and methods to compute numerical quantities such as factorials,...
Definition Num.java:35
static double gammaRatioHalf(double x)
Returns the value of the ratio of two gamma functions.
Definition Num.java:635
This interface should be implemented by classes which represent univariate mathematical functions.