25package umontreal.ssj.probdist;
27import umontreal.ssj.util.*;
28import umontreal.ssj.functions.MathFunction;
58 public Function(
int n,
double u) {
63 public double evaluate(
double x) {
80 public double cdf(
double x) {
84 public double barF(
double x) {
92 private static double dclem(
int n,
double x,
double EPS) {
93 return (
cdf(n, x + EPS) -
cdf(n, x - EPS)) / (2.0 * EPS);
100 public static double density(
int n,
double x) {
102 throw new IllegalArgumentException(
"Calling kolmogorovSmirnovPlus with n < 1");
103 if (x <= 0.0 || x >= 1.0)
107 final double EPS = 1.0 / 100.0;
108 final double D1 = dclem(n, x, EPS);
109 final double D2 = dclem(n, x, 2.0 * EPS);
110 final double RES = D1 + (D1 - D2) / 3.0;
145 public static double cdf(
int n,
double x) {
147 throw new IllegalArgumentException(
"Calling kolmogorovSmirnovPlus with n < 1");
150 if ((x >= 1.0) || (n * x * x >= 25.0))
155 final double NXPARAM = 6.5;
156 final int NPARAM = 4000;
162 double LogCom = Math.log((
double) n);
170 if (n * x <= NXPARAM) {
172 jmax = (int) (n * x);
174 for (j = 1; j <= jmax; j++) {
180 term = LogCom + jreal * Math.log(-q) + (Njreal - 1.0) * Math.log1p(-q);
181 Sum += Sign * Math.exp(term);
184 LogCom += Math.log(Njreal / (j + 1));
187 Sum += Math.exp((n - 1) * Math.log1p(x));
197 jmax = (int) (n * (1.0 - x));
198 if (1.0 - x - (
double) jmax / n <= 0.0)
200 for (j = 1; j <= jmax; j++) {
204 term = LogCom + (jreal - 1.0) * Math.log(q) + Njreal * Math.log1p(-q);
205 Sum += Math.exp(term);
206 LogCom += Math.log(Njreal / (jreal + 1.0));
212 Sum += Math.exp(n * Math.log1p(-x));
222 Sum = 1.0 - Math.exp(-2.0 * q)
223 * (1.0 - term * x * (1.0 - x * (1.0 - term * q) - term / n * (0.2 - 19.0 / 15.0 * q + term * q * q)));
227 private static double KSPlusbarAsymp(
int n,
double x) {
232 double t = (6.0 * n * x + 1);
233 double z = t * t / (18.0 * n);
234 double v = 1.0 - (2.0 * z * z - 4.0 * z - 1.0) / (18.0 * n);
237 v = v * Math.exp(-z);
245 static double KSPlusbarUpper(
int n,
double x) {
252 return KSPlusbarAsymp(n, x);
254 int jmax = (int) (n * (1.0 - x));
256 if ((1.0 - x - (
double) jmax / n) <= 0.0)
264 int j = jmax / jdiv + 1;
267 final double LOGJM = LogCom;
269 final double EPSILON = 1.0E-12;
276 q = (double) j / n + x;
277 term = LogCom + (j - 1) * Math.log(q) + (n - j) * Math.log1p(-q);
280 LogCom += Math.log((
double) (n - j) / (j + 1));
281 if (t <= Sum * EPSILON)
287 LogCom = LOGJM + Math.log((
double) (j + 1) / (n - j));
290 q = (double) j / n + x;
291 term = LogCom + (j - 1) * Math.log(q) + (n - j) * Math.log1p(-q);
294 LogCom += Math.log((
double) j / (n - j + 1));
295 if (t <= Sum * EPSILON)
302 Sum += Math.exp(n * Math.log1p(-x));
310 public static double barF(
int n,
double x) {
312 throw new IllegalArgumentException(
"Calling kolmogorovSmirnovPlus with n < 1");
315 if ((x >= 1.0) || (n * x * x >= 365.0))
320 final double NXPARAM = 6.5;
321 final int NPARAM = 4000;
322 final int NASYMP = 200000;
325 if (n * x <= NXPARAM)
326 return 1.0 -
cdf(n, x);
329 return KSPlusbarAsymp(n, x);
331 if ((n <= NPARAM) || (n * x * x > 1.0))
332 return KSPlusbarUpper(n, x);
334 return KSPlusbarAsymp(n, x);
344 throw new IllegalArgumentException(
"n <= 0");
345 if (u < 0.0 || u > 1.0)
346 throw new IllegalArgumentException(
"u must be in [0,1]");
351 Function f =
new Function(n, u);
367 throw new IllegalArgumentException(
"n <= 0");
377 double[] retour = { n };
385 return getClass().getSimpleName() +
" : n = " + n;
Classes implementing continuous distributions should inherit from this base class.
KolmogorovSmirnovPlusDist(int n)
Constructs an Kolmogorov–Smirnov+ distribution for a sample of size .
double[] getParams()
Returns an array containing the parameter of this object.
static double cdf(int n, double x)
Computes the Kolmogorov–Smirnov+ distribution function.
static double inverseF(int n, double u)
Computes the inverse of the distribution with parameter .
int getN()
Returns the parameter of this object.
double inverseF(double u)
Returns the inverse distribution function .
static double barF(int n, double x)
Computes the complementary distribution function with parameter .
double density(double x)
Returns , the density evaluated at .
void setN(int n)
Sets the parameter of this object.
static double density(int n, double x)
Computes the density of the Kolmogorov–Smirnov+ distribution with parameter .
double barF(double x)
Returns the complementary distribution function.
String toString()
Returns a String containing information about the current distribution.
double cdf(double x)
Returns the distribution function .
This class provides various constants and methods to compute numerical quantities such as factorials,...
static final double DBL_MIN
Smallest normalized positive floating-point double.
static double lnFactorial(int n)
Returns the value of , the natural logarithm of factorial .
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.