25package umontreal.ssj.gof;
27import cern.colt.list.*;
28import umontreal.ssj.util.PrintfFormat;
29import umontreal.ssj.probdist.*;
30import java.io.PrintWriter;
64public class GofFormat {
93 private static String formatMath2(
double x,
double y) {
96 StringBuffer sb =
new StringBuffer();
100 if ((x != 0.0) && (x < 0.1 || x > 1.0)) {
102 int exppos = S.indexOf(
'E');
104 S = S.substring(0, exppos) +
"*10^(" + S.substring(exppos + 1) +
")";
110 if (y != 0.0 && (y < 0.1 || y > 1.0)) {
112 int exppos = S.indexOf(
'E');
114 S = S.substring(0, exppos) +
"*10^(" + S.substring(exppos + 1) +
")";
119 return sb.toString();
122 private static String graphFunc(
ContinuousDistribution dist,
double a,
double b,
int m,
int mono, String desc) {
125 double yprec, y, x, h;
126 StringBuffer sb =
new StringBuffer();
127 String openComment =
"";
128 String closeComment =
"";
129 String openGraph =
"";
130 String closeGraph =
"";
131 if (mono != 1 && mono != -1)
132 throw new IllegalArgumentException(
"mono must be 1 or -1");
142 closeComment =
" *)";
148 sb.append(openComment +
"----------------------------------" + closeComment +
PrintfFormat.
NEWLINE);
151 sb.append(openGraph);
154 yprec = -Double.MAX_VALUE;
156 yprec = Double.MAX_VALUE;
160 for (i = 0; i <= m; i++) {
162 y = mono == 1 ? dist.
cdf(x) : dist.
barF(x);
165 sb.append(formatMath2(x, y));
176 sb.append(
" " + openComment +
" DECREASING" + closeComment);
180 sb.append(
" " + openComment +
" INCREASING" + closeComment);
188 sb.append(closeGraph);
189 return sb.toString();
211 return graphFunc(dist, a, b, m, 1, desc);
235 StringBuffer sb =
new StringBuffer();
236 String openComment =
"";
237 String closeComment =
"";
238 String openGraph =
"";
239 String closeGraph =
"";
250 closeComment =
" *)";
256 sb.append(openComment +
"----------------------------------" + closeComment +
PrintfFormat.
NEWLINE);
259 sb.append(openGraph);
262 for (i = 0; i <= m; i++) {
268 sb.append(formatMath2(x, y));
277 sb.append(closeGraph);
278 return sb.toString();
295 double[] u = data.elements();
298 double unSurN = 1.0 / n;
299 StringBuffer sb =
new StringBuffer();
306 for (i = 0; i < n; i++)
319 for (i = 0; i < n; i++)
324 throw new IllegalArgumentException(
"graphSoft unknown");
326 return sb.toString();
370 if ((p >= 0.01) && (p <= 0.99))
393 StringBuffer sb =
new StringBuffer();
394 sb.append(
"p-value of test :" +
formatp0(p));
395 if (p < SUSPECTP || p > 1.0 -
SUSPECTP)
399 return sb.toString();
409 public static String
formatp2(
double x,
double p) {
425 public static String
formatp3(String testName,
double x,
double p) {
426 final String SLT =
"p-value of test";
427 int l = Math.max(SLT.length(), testName.length());
431 if (p < SUSPECTP || p > 1.0 -
SUSPECTP)
449 StringBuffer sb =
new StringBuffer();
454 return sb.toString();
470 public static String
formatKS(
int n,
double dp,
double dm,
double d) {
492 double[] v = data.elements();
496 dataUnif.quickSortFromTo(0, dataUnif.size() - 1);
498 return formatKS(n, ret[0], ret[1], ret[2]);
533 double[] v = data.elements();
536 dataUnif.quickSortFromTo(0, dataUnif.size() - 1);
560 public static final int KSP = 0;
565 public static final int KSM = 1;
570 public static final int KS = 2;
575 public static final int AD = 3;
580 public static final int CM = 4;
585 public static final int WG = 5;
590 public static final int WU = 6;
595 public static final int MEAN = 7;
600 public static final int COR = 8;
611 public static final String[]
TESTNAMES = {
"KolmogorovSmirnovPlus",
"KolmogorovSmirnovMinus",
"KolmogorovSmirnov",
612 "Anderson-Darling",
"CramerVon-Mises",
"Watson G",
"Watson U",
"Mean",
"Correlation" };
623 private static void initActiveTests() {
652 public static void tests(DoubleArrayList sortedData,
double[] sVal) {
653 double[] u = sortedData.elements();
654 int n = sortedData.size();
656 double a2 = 0.0, w2, dm = 0.0, dp = 0.0, w;
657 double u1, ui, d2, d1;
662 throw new IllegalArgumentException(
"n <= 0");
664 throw new IllegalArgumentException(
"sVal must " +
"be of size NTESTTYPES.");
668 sVal[
KSP] = 1.0 - u[0];
675 for (i = 0; i < n; i++) {
677 d1 = u[i] - i * unSurN;
678 d2 = (i + 1) * unSurN - u[i];
685 w = u[i] - (i + 0.5) * unSurN;
694 a2 += (2 * i + 1) * Math.log(ui) + (1 + 2 * (n - i - 1)) * Math.log(u1);
702 sumZ = sumZ * unSurN - 0.5;
704 sVal[
WG] = Math.sqrt((
double) n) * (dp + sumZ);
705 sVal[
WU] = w2 - sumZ * sumZ * n;
706 sVal[
AD] = -n - a2 * unSurN;
707 sVal[
MEAN] = sumZ + 0.5;
722 double[] v = data.elements();
726 throw new IllegalArgumentException(
"n <= 0");
729 sortedData.quickSortFromTo(0, sortedData.size() - 1);
730 tests(sortedData, sVal);
749 public static void activeTests(DoubleArrayList sortedData,
double[] sVal,
double[] pVal) {
751 double[] u = sortedData.elements();
752 int n = sortedData.size();
755 throw new IllegalArgumentException(
"n <= 0");
758 throw new IllegalArgumentException(
"sVal and pVal must " +
"be of length NTESTTYPES.");
761 sVal[
KSP] = 1.0 - u[0];
762 pVal[
KSP] = 1.0 - u[0];
767 tests(sortedData, sVal);
771 System.err.println(
"activeTests was invalid, it was reinitialized.");
810 double[] v = data.elements();
814 throw new IllegalArgumentException(
"n <= 0");
817 sortedData.quickSortFromTo(0, sortedData.size() - 1);
841 System.err.println(
"activeTests was invalid, it was reinitialized.");
844 throw new IllegalArgumentException(
"The length of " +
"sVal and pVal must be NTESTTYPES.");
850 sb.append(
"Kolmogorov-Smirnov+ statistic = D+ :" +
formatp2(sVal[
KSP], pVal[
KSP]));
852 sb.append(
"Kolmogorov-Smirnov- statistic = D- :" +
formatp2(sVal[
KSM], pVal[
KSM]));
854 sb.append(
"Kolmogorov-Smirnov statistic = D :" +
formatp2(sVal[
KS], pVal[
KS]));
856 sb.append(
"Anderson-Darling statistic = A2 :" +
formatp2(sVal[
AD], pVal[
AD]));
858 sb.append(
"Cramer-von Mises statistic = W2 :" +
formatp2(sVal[
CM], pVal[
CM]));
860 sb.append(
"Watson statistic = G :" +
formatp2(sVal[
WG], pVal[
WG]));
862 sb.append(
"Watson statistic = U2 :" +
formatp2(sVal[
WU], pVal[
WU]));
864 return sb.toString();
887 public static String
iterSpacingsTests(DoubleArrayList sortedData,
int k,
boolean printval,
boolean graph,
890 int n = sortedData.size();
892 DoubleArrayList sortedDataCopy = (DoubleArrayList) sortedData.clone();
893 DoubleArrayList diffArrayList =
new DoubleArrayList(sortedData.size() + 2);
901 for (j = 1; j <= k; j++) {
905 GofStat.
diff(sortedDataCopy, diffArrayList, 0, n - 1, 0.0, 1.0);
907 sortedDataCopy.quickSortFromTo(0, sortedDataCopy.size() - 1);
911 String desc =
"Values of Uniforms after iterateSpacings, level " +
PrintfFormat.
d(2, j);
916 if (graph && f !=
null)
918 else if (graph && f ==
null)
921 return sb.toString();
937 public static String
iterPowRatioTests(DoubleArrayList sortedData,
int k,
boolean printval,
boolean graph,
940 int n = sortedData.size();
941 DoubleArrayList sortedDataCopy = (DoubleArrayList) sortedData.clone();
949 for (i = 1; i <= k; i++) {
954 sortedDataCopy.quickSortFromTo(0, sortedDataCopy.size() - 1);
958 String desc =
"Values of Uniforms after PowerRatios, level " +
PrintfFormat.
d(2, i);
963 if (graph && f !=
null)
965 else if (graph && f ==
null)
968 return sb.toString();
This class provides methods to compute (or approximate) the distribution functions of special types o...
static double kolmogorovSmirnovPlusJumpOne(int N, double a, double x)
Similar to umontreal.ssj.probdist.KolmogorovSmirnovPlusDist but for the case where the distribution f...
This class provides methods to compute several types of EDF goodness-of-fit test statistics and to ap...
static double[] kolmogorovSmirnov(double[] sortedData)
Computes the Kolmogorov-Smirnov (KS) test statistics ,.
static DoubleArrayList unifTransform(DoubleArrayList data, ContinuousDistribution dist)
Applies the probability integral transformation for.
static void iterateSpacings(DoubleArrayList data, DoubleArrayList spacings)
Applies one iteration of the iterated spacings transformation.
static double[] kolmogorovSmirnovJumpOne(DoubleArrayList sortedData, double a)
Compute the KS statistics and defined in the description of the method FDist.kolmogorovSmirnovPlusJ...
static void powerRatios(DoubleArrayList sortedData)
Applies the power ratios transformation described in section 8.4 of Stephens tste86a .
static double pDisc(double pL, double pR)
Computes a variant of the -value whenever a test statistic has a discrete probability distribution.
static double EPSILONAD
Used by andersonDarling(DoubleArrayList).
static void diff(IntArrayList sortedData, IntArrayList spacings, int n1, int n2, int a, int b)
Assumes that the real-valued observations contained in sortedData are already sorted in increasing o...
Extends the class AndersonDarlingDist for the Anderson–Darling distribution (see tand52a,...
double barF(double x)
Returns the complementary distribution function.
Extends the class ContinuousDistribution for the chi-square distribution with degrees of freedom,...
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.
abstract double density(double x)
Returns , the density evaluated at .
double barF(double x)
Returns the complementary distribution function.
Extends the class ContinuousDistribution for the Cramér-von Mises distribution (see tdur73a,...
double barF(double x)
Returns the complementary distribution function.
Extends the class KolmogorovSmirnovDist for the Kolmogorov–Smirnov distribution.
double barF(double x)
Returns the complementary distribution function.
Extends the class ContinuousDistribution for the Kolmogorov–Smirnov+ distribution (see tdar60a,...
double barF(double x)
Returns the complementary distribution function.
Extends the class ContinuousDistribution for the Watson distribution (see tdar83a,...
double barF(double x)
Returns the complementary distribution function.
Extends the class ContinuousDistribution for the Watson U distribution (see tdur73a,...
double barF(double x)
Returns the complementary distribution function.
double cdf(double x)
Returns the distribution function .