SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ChiDist.java
1/*
2 * Class: ChiDist
3 * Description: chi 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.util.Num;
28import umontreal.ssj.functions.MathFunction;
29
49public class ChiDist extends ContinuousDistribution {
50 private int nu;
51 private double C1;
52
53 private static class Function implements MathFunction {
54 protected int n;
55 protected double sum;
56
57 public Function(double s, int n) {
58 this.n = n;
59 this.sum = s;
60 }
61
62 public double evaluate(double k) {
63 if (k < 1.0)
64 return 1.0e200;
65 return (sum + n * (Num.lnGamma(k / 2.0) - 0.5 * (Num.LN2) - Num.lnGamma((k + 1.0) / 2.0)));
66 }
67 }
68
72 public ChiDist(int nu) {
73 setNu(nu);
74 }
75
76 public double density(double x) {
77 if (x <= 0.0)
78 return 0.0;
79 return Math.exp((nu - 1) * Math.log(x) - x * x / 2.0 - C1);
80 }
81
82 public double cdf(double x) {
83 return cdf(nu, x);
84 }
85
86 public double barF(double x) {
87 return barF(nu, x);
88 }
89
90 public double inverseF(double u) {
91 return inverseF(nu, u);
92 }
93
94 public double getMean() {
95 return ChiDist.getMean(nu);
96 }
97
98 public double getVariance() {
99 return ChiDist.getVariance(nu);
100 }
101
102 public double getStandardDeviation() {
103 return ChiDist.getStandardDeviation(nu);
104 }
105
109 public static double density(int nu, double x) {
110 if (nu <= 0)
111 throw new IllegalArgumentException("nu <= 0");
112 if (x <= 0.0)
113 return 0.0;
114 return Math.exp((nu - 1) * Math.log(x) - x * x / 2.0 - (nu / 2.0 - 1.0) * Num.LN2 - Num.lnGamma(nu / 2.0));
115 }
116
120 public static double cdf(int nu, double x) {
121 if (x <= 0.0)
122 return 0.0;
123 return GammaDist.cdf(nu / 2.0, 15, x * x / 2.0);
124 }
125
129 public static double barF(int nu, double x) {
130 if (x <= 0.0)
131 return 1.0;
132 return GammaDist.barF(nu / 2.0, 15, x * x / 2.0);
133 }
134
138 public static double inverseF(int nu, double u) {
139 double res = GammaDist.inverseF(nu / 2.0, 15, u);
140 return Math.sqrt(2 * res);
141 }
142
153 public static double[] getMLE(double[] x, int n) {
154 double[] parameters = new double[1];
155
156 double mean = 0.0;
157 for (int i = 0; i < n; i++)
158 mean += x[i];
159 mean /= (double) n;
160
161 double var = 0.0;
162 for (int i = 0; i < n; i++)
163 var += ((x[i] - mean) * (x[i] - mean));
164 var /= (double) n;
165
166 double k = Math.round(var + mean * mean) - 5.0;
167 if (k < 1.0)
168 k = 1.0;
169
170 double sum = 0.0;
171 for (int i = 0; i < n; i++) {
172 if (x[i] > 0.0)
173 sum += Math.log(x[i]);
174 else
175 sum -= 709.0;
176 }
177
178 Function f = new Function(sum, n);
179 while (f.evaluate(k) > 0.0)
180 k++;
181 parameters[0] = k;
182
183 return parameters;
184 }
185
194 public static ChiDist getInstanceFromMLE(double[] x, int n) {
195 double parameters[] = getMLE(x, n);
196 return new ChiDist((int) parameters[0]);
197 }
198
207 public static double getMean(int nu) {
208 if (nu <= 0)
209 throw new IllegalArgumentException("nu <= 0");
210 return Num.RAC2 * Num.gammaRatioHalf(nu / 2.0);
211 }
212
223 public static double getVariance(int nu) {
224 if (nu <= 0)
225 throw new IllegalArgumentException("nu <= 0");
226 double mean = ChiDist.getMean(nu);
227 return (nu - (mean * mean));
228 }
229
236 public static double getStandardDeviation(int nu) {
237 return Math.sqrt(ChiDist.getVariance(nu));
238 }
239
243 public int getNu() {
244 return nu;
245 }
246
250 public void setNu(int nu) {
251 if (nu <= 0)
252 throw new IllegalArgumentException("nu <= 0");
253 this.nu = nu;
254 supportA = 0.0;
255 C1 = (nu / 2.0 - 1.0) * Num.LN2 + Num.lnGamma(nu / 2.0);
256 }
257
261 public double[] getParams() {
262 double[] retour = { nu };
263 return retour;
264 }
265
269 public String toString() {
270 return getClass().getSimpleName() + " : nu = " + nu;
271 }
272
273}
int getNu()
Returns the value of for this object.
Definition ChiDist.java:243
double getVariance()
Returns the variance.
Definition ChiDist.java:98
static double barF(int nu, double x)
Computes the complementary distribution.
Definition ChiDist.java:129
ChiDist(int nu)
Constructs a ChiDist object.
Definition ChiDist.java:72
static double cdf(int nu, double x)
Computes the distribution function by using the gamma distribution function.
Definition ChiDist.java:120
double density(double x)
Returns , the density evaluated at .
Definition ChiDist.java:76
static double density(int nu, double x)
Computes the density function.
Definition ChiDist.java:109
double getStandardDeviation()
Returns the standard deviation.
Definition ChiDist.java:102
static double inverseF(int nu, double u)
Returns the inverse distribution function computed using the gamma inversion.
Definition ChiDist.java:138
void setNu(int nu)
Sets the value of for this object.
Definition ChiDist.java:250
double inverseF(double u)
Returns the inverse distribution function .
Definition ChiDist.java:90
static double getStandardDeviation(int nu)
Computes and returns the standard deviation of the chi distribution with parameter .
Definition ChiDist.java:236
double getMean()
Returns the mean.
Definition ChiDist.java:94
double[] getParams()
Return a table containing parameters of the current distribution.
Definition ChiDist.java:261
static double[] getMLE(double[] x, int n)
Estimates the parameter of the chi distribution using the maximum likelihood method,...
Definition ChiDist.java:153
double barF(double x)
Returns the complementary distribution function.
Definition ChiDist.java:86
double cdf(double x)
Returns the distribution function .
Definition ChiDist.java:82
static double getVariance(int nu)
Computes and returns the variance.
Definition ChiDist.java:223
String toString()
Returns a String containing information about the current distribution.
Definition ChiDist.java:269
static ChiDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a chi distribution with parameter.
Definition ChiDist.java:194
static double getMean(int nu)
Computes and returns the mean.
Definition ChiDist.java:207
Classes implementing continuous distributions should inherit from this base class.
Extends the class ContinuousDistribution for the gamma distribution tjoh95a  (page 337) with shape pa...
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.
This class provides various constants and methods to compute numerical quantities such as factorials,...
Definition Num.java:35
static final double RAC2
The value of .
Definition Num.java:138
static double lnGamma(double x)
Returns the natural logarithm of the gamma function evaluated at x.
Definition Num.java:417
static double gammaRatioHalf(double x)
Returns the value of the ratio of two gamma functions.
Definition Num.java:635
static final double LN2
The values of .
Definition Num.java:148
This interface should be implemented by classes which represent univariate mathematical functions.