25package umontreal.ssj.probdist;
27import umontreal.ssj.util.*;
28import umontreal.ssj.functions.MathFunction;
57 protected double sumLog;
59 public Function(
double s,
int n) {
64 public double evaluate(
double k) {
81 return Math.exp((n / 2.0 - 1) * Math.log(x) - x / 2.0 - C1);
84 public double cdf(
double x) {
85 return cdf(n, decPrec, x);
88 public double barF(
double x) {
89 return barF(n, decPrec, x);
112 public static double density(
int n,
double x) {
115 return Math.exp((n / 2.0 - 1) * Math.log(x) - x / 2.0 - (n / 2.0) *
Num.
LN2 -
Num.
lnGamma(n / 2.0));
123 public static double cdf(
int n,
int d,
double x) {
125 throw new IllegalArgumentException(
"n <= 0");
140 public static double barF(
int n,
int d,
double x) {
142 throw new IllegalArgumentException(
"n <= 0");
170 throw new IllegalArgumentException(
"n <= 0");
171 if (u < 0.0 || u > 1.0)
172 throw new IllegalArgumentException(
"u must be in [0,1]");
174 return Double.POSITIVE_INFINITY;
178 final double E = 0.5e-5;
179 final double AA = 0.6931471805;
180 double A, XX, X, C, G, CH, Q, P1, P2, T, B, S1, S2, S3, S4, S5, S6;
182 if (u < 0.00001 || u > 1.0 - 1.0e-5)
187 return (n + 4.0 * Math.sqrt(2.0 * n));
193 if (n >= -1.24 * Math.log(u)) {
196 Q = X * Math.sqrt(P1) + 1.0 - P1;
198 if (CH > 2.2 * n + 6.0)
199 CH = -2.0 * (Math.log1p(-u) - C * Math.log(0.5 * CH) + G);
202 CH = Math.pow(u * XX * Math.exp(G + XX * AA), 1.0 / XX);
211 throw new IllegalArgumentException(
"RESULT = -1");
213 T = P2 * Math.exp(XX * AA + G + P1 - C * Math.log(CH));
216 S1 = (210.0 + A * (140.0 + A * (105.0 + A * (84.0 + A * (70.0 + 60.0 * A))))) / 420.0;
217 S2 = (420.0 + A * (735.0 + A * (966.0 + A * (1141.0 + 1278.0 * A)))) / 2520.0;
218 S3 = (210.0 + A * (462.0 + A * (707.0 + 932.0 * A))) / 2520.0;
219 S4 = (252.0 + A * (672.0 + 1182.0 * A) + C * (294.0 + A * (889.0 + 1740.0 * A))) / 5040.0;
220 S5 = (84.0 + 264.0 * A + C * (175.0 + 606.0 * A)) / 2520.0;
221 S6 = (120.0 + C * (346.0 + 127.0 * C)) / 5040.0;
222 CH = CH + T * (1.0 + 0.5 * T * S1 - B * C * (S1 - B * (S2 - B * (S3 - B * (S4 - B * (S5 - B * S6))))));
225 while (Math.abs(Q / CH - 1.0) > E) {
234 T = P2 * Math.exp(XX * AA + G + P1 - C * Math.log(CH));
237 S1 = (210.0 + A * (140.0 + A * (105.0 + A * (84.0 + A * (70.0 + 60.0 * A))))) / 420.0;
238 S2 = (420.0 + A * (735.0 + A * (966.0 + A * (1141.0 + 1278.0 * A)))) / 2520.0;
239 S3 = (210.0 + A * (462.0 + A * (707.0 + 932.0 * A))) / 2520.0;
240 S4 = (252.0 + A * (672.0 + 1182.0 * A) + C * (294.0 + A * (889.0 + 1740.0 * A))) / 5040.0;
241 S5 = (84.0 + 264.0 * A + C * (175.0 + 606.0 * A)) / 2520.0;
242 S6 = (120.0 + C * (346.0 + 127.0 * C)) / 5040.0;
243 CH = CH + T * (1.0 + 0.5 * T * S1 - B * C * (S1 - B * (S2 - B * (S3 - B * (S4 - B * (S5 - B * S6))))));
258 public static double[]
getMLE(
double[] x,
int m) {
260 throw new IllegalArgumentException(
"m <= 0");
265 double k = Math.round(parameters[0]) - 5.0;
270 for (
int i = 0; i < m; i++) {
272 sum += 0.5 * Math.log(x[i]);
277 Function f =
new Function(sum, m);
278 while (f.evaluate(k) > 0.0)
294 double parameters[] =
getMLE(x, m);
306 throw new IllegalArgumentException(
"degrees of freedom " +
"must be non-null and positive.");
321 double[] parameters =
new double[1];
324 for (
int i = 0; i < m; i++)
326 parameters[0] = sum / (double) m;
339 throw new IllegalArgumentException(
"degrees of freedom " +
"must be non-null and positive.");
352 throw new IllegalArgumentException(
"degrees of freedom " +
"must be non-null and positive.");
354 return Math.sqrt(2 * n);
369 throw new IllegalArgumentException(
"degrees of freedom " +
"must be non-null and positive.");
380 double[] retour = { n };
388 return getClass().getSimpleName() +
" : n = " + n;
ChiSquareDist(int n)
Constructs a chi-square distribution with n degrees of freedom.
double[] getParams()
Return a table containing the parameters of the current distribution.
static double density(int n, double x)
Computes the density function ( Fchi2 ) for a chi-square distribution with degrees of freedom.
double cdf(double x)
Returns the distribution function .
String toString()
Returns a String containing information about the current distribution.
double getVariance()
Returns the variance.
static double getStandardDeviation(int n)
Returns the standard deviation of the chi-square distribution with parameter .
static ChiSquareDist getInstanceFromMLE(double[] x, int m)
Creates a new instance of a chi-square distribution with parameter.
double density(double x)
Returns , the density evaluated at .
static double getMean(int n)
Computes and returns the mean of the chi-square distribution with parameter .
double barF(double x)
Returns the complementary distribution function.
double getStandardDeviation()
Returns the standard deviation.
static double[] getMLE(double[] x, int m)
Estimates the parameter of the chi-square distribution using the maximum likelihood method,...
static double barF(int n, int d, double x)
Computes the complementary chi-square distribution function with.
double inverseF(double u)
Returns the inverse distribution function .
static double getVariance(int n)
Returns the variance of the chi-square distribution with parameter .
void setN(int n)
Sets the parameter of this object.
static double inverseF(int n, double u)
Computes an approximation of , where is the chi-square distribution with degrees of freedom.
double getMean()
Returns the mean.
int getN()
Returns the parameter of this object.
static double[] getMomentsEstimate(double[] x, int m)
Estimates and returns the parameter [ ] of the chi-square distribution using the moments method based...
static double cdf(int n, int d, double x)
Computes the chi-square distribution function with degrees of freedom, evaluated at .
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.
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a (page 80)).
static double inverseF01(double u)
Same as inverseF(0, 1, u).
This class provides various constants and methods to compute numerical quantities such as factorials,...
static double lnGamma(double x)
Returns the natural logarithm of the gamma function evaluated at x.
static final double LN2
The values of .
This interface should be implemented by classes which represent univariate mathematical functions.