25package umontreal.ssj.gof;
27import cern.colt.list.*;
28import umontreal.ssj.util.*;
29import umontreal.ssj.probdist.*;
30import java.util.Arrays;
55 private static double EPSILOND = 1.0E-15;
71 double[] v = data.elements();
74 double[] u =
new double[n];
75 for (
int i = 0; i < n; i++)
76 u[i] = dist.
cdf(v[i]);
77 return new DoubleArrayList(u);
96 double[] v = data.elements();
99 double[] u =
new double[n];
100 for (
int i = 0; i < n; i++)
101 u[i] = dist.cdf((
int) v[i]);
102 return new DoubleArrayList(u);
126 public static void diff(IntArrayList sortedData, IntArrayList spacings,
int n1,
int n2,
int a,
int b) {
127 if (n1 < 0 || n2 < 0 || n1 >= n2 || n2 >= sortedData.size())
128 throw new IllegalArgumentException(
"n1 and n2 not valid.");
129 int[] u = sortedData.elements();
130 int n = sortedData.size();
131 if (spacings.size() <= (n2 + 2))
132 spacings.setSize(n2 + 2);
133 int[] d = spacings.elements();
136 for (
int i = n1 + 1; i <= n2; i++)
137 d[i] = u[i] - u[i - 1];
138 d[n2 + 1] = b - u[n2];
155 public static void diff(DoubleArrayList sortedData, DoubleArrayList spacings,
int n1,
int n2,
double a,
double b) {
157 if (n1 < 0 || n2 < 0 || n1 >= n2 || n2 >= sortedData.size())
158 throw new IllegalArgumentException(
"n1 and n2 not valid.");
159 double[] u = sortedData.elements();
160 int n = sortedData.size();
161 if (spacings.size() <= (n2 + 2))
162 spacings.setSize(n2 + 2);
163 double[] d = spacings.elements();
166 for (
int i = n1 + 1; i <= n2; i++)
167 d[i] = u[i] - u[i - 1];
168 d[n2 + 1] = b - u[n2];
214 if (spacings.size() < (data.size() + 1))
215 throw new IllegalArgumentException(
"Invalid array sizes.");
216 double[] v = data.elements();
217 spacings.quickSortFromTo(0, data.size());
218 double[] s = spacings.elements();
221 for (
int i = 0; i < n; i++)
222 s[n - i] = (i + 1) * (s[n - i] - s[n - i - 1]);
223 s[0] = (n + 1) * s[0];
225 for (
int i = 1; i < n; i++)
226 v[i] = v[i - 1] + s[i];
254 double[] u = sortedData.elements();
255 int n = sortedData.size();
257 for (
int i = 0; i < (n - 1); i++) {
258 if (u[i + 1] == 0.0 || u[i + 1] == -0.0)
261 u[i] = Math.pow(u[i] / u[i + 1], (
double) i + 1);
264 u[n - 1] = Math.pow(u[n - 1], (
double) n);
265 sortedData.quickSortFromTo(0, sortedData.size() - 1);
337 smax = nbExp.length - 1;
340 for (
int i = 0; i <
nbExp.length; i++)
364 for (
int i = 0; i <
smin; i++)
368 for (
int i =
smax; i <
nbExp.length; i++)
386 this.nbCategories = nbCat;
408 if (
nbExp[s] < minExp) {
411 while (somme < minExp && s <
smax) {
417 for (j = s0; j <= s; j++)
435 for (j = s0 + 1; j <=
smax; j++)
440 throw new IllegalStateException(
"nbCategories < 2");
451 final double EPSILON = 5.0E-16;
452 StringBuffer sb =
new StringBuffer();
455 sb.append(
"Only expected numbers larger than " +
PrintfFormat.
g(6, 1, EPSILON) +
" are printed"
463 while (
nbExp[s] < EPSILON)
468 while (
nbExp[s] < EPSILON)
473 for (s = s1; s <= s2; s++)
482 if ((s ==
smin) && (s > 0))
484 else if ((s ==
smax) && (s <
loc.length - 1))
492 return sb.toString();
524 public static double chi2(
double[] nbExp,
int[] count,
int smin,
int smax) {
525 double diff, khi = 0.0;
527 for (
int s = smin; s <= smax; s++) {
528 if (nbExp[s] <= 0.0) {
530 throw new IllegalArgumentException(
"nbExp[s] = 0 and count[s] > 0");
532 diff = count[s] - nbExp[s];
552 int[] newcount =
new int[1 + cat.
smax];
553 for (
int s = cat.
smin; s <= cat.
smax; s++) {
554 newcount[cat.
loc[s]] += count[s];
557 double diff, khi = 0.0;
559 for (
int s = cat.
smin; s <= cat.
smax; s++) {
560 if (cat.
nbExp[s] > 0.0) {
627 double[] nbExp =
new double[smax + 1];
632 double[] newNbExp =
new double[smax + 1];
633 System.arraycopy(nbExp, smin, newNbExp, smin, nbExp.length - smin);
636 nbExp[i] = dist.
prob(i) * n;
648 int[] count =
new int[cat.smax + 1];
649 for (i = 0; i < count.length; i++)
651 for (i = 0; i < n; i++) {
653 while (cat.
loc[s] != s)
673 public static double chi2Equal(
double nbExp,
int[] count,
int smin,
int smax) {
675 double diff, khi = 0.0;
676 for (
int s = smin; s <= smax; s++) {
677 diff = count[s] - nbExp;
698 public static double chi2Equal(DoubleArrayList data,
double minExp) {
700 if (n < (
int) Math.ceil(minExp))
701 throw new IllegalArgumentException(
"Not enough observations");
702 double p = minExp / n;
703 int m = (int) Math.ceil(1.0 / p);
705 int[] count =
new int[m + 1];
706 for (
int i = 0; i < n; i++) {
707 int j = (int) Math.floor(data.get(i) / p);
711 count[m - 1] += count[m];
712 return chi2Equal(minExp, count, 0, m - 1);
738 public static int scan(DoubleArrayList sortedData,
double d) {
740 double[] u = sortedData.elements();
741 int n = sortedData.size();
743 int m = 1, j = 0, i = -1;
746 while (j < (n - 1) && High < 1.0) {
750 while (j < n && u[j] < High)
773 double[] u = sortedData.elements();
774 int n = sortedData.size();
777 System.err.println(
"cramerVonMises: n <= 0");
782 for (
int i = 0; i < n; i++) {
783 w = u[i] - (i + 0.5) / n;
805 public static double watsonG(DoubleArrayList sortedData) {
806 double[] u = sortedData.elements();
807 int n = sortedData.size();
811 double unSurN = 1.0 / n;
814 System.err.println(
"watsonG: n <= 0");
824 for (
int i = 0; i < n; i++) {
825 d2 = (i + 1) * unSurN - u[i];
830 sumZ = sumZ * unSurN - 0.5;
831 g = Math.sqrt((
double) n) * (dp + sumZ);
850 public static double watsonU(DoubleArrayList sortedData) {
851 double sumZ, w, w2, u2;
852 double[] u = sortedData.elements();
853 int n = sortedData.size();
856 System.err.println(
"watsonU: n <= 0");
866 for (
int i = 0; i < n; i++) {
868 w = u[i] - (i + 0.5) / n;
871 sumZ = sumZ / n - 0.5;
872 u2 = w2 - sumZ * sumZ * n;
891 double[] v = sortedData.elements();
916 int n = sortedData.length;
919 System.err.println(
"andersonDarling: n <= 0");
924 for (
int i = 0; i < n; i++) {
931 a2 += (2 * i + 1) * Math.log(u) + (1 + 2 * (n - i - 1)) * Math.log(u1);
949 double[] U =
new double[n];
950 for (
int i = 0; i < n; i++) {
951 U[i] = dist.
cdf(data[i]);
955 double x = GofStat.andersonDarling(U);
957 double[] res = { x, v };
971 DoubleArrayList v =
new DoubleArrayList(sortedData);
994 double[] ret =
new double[3];
995 int n = sortedData.size();
998 ret[0] = ret[1] = ret[2] = 0.0;
999 System.err.println(
"kolmogorovSmirnov: n <= 0");
1006 if (ret[1] > ret[0])
1030 int n = data.length;
1031 double[] T =
new double[n];
1032 for (
int i = 0; i < n; i++) {
1033 T[i] = dist.
cdf(data[i]);
1037 double[] statks = GofStat.kolmogorovSmirnov(T);
1038 for (
int i = 0; i < 3; i++) {
1039 sval[i] = statks[i];
1064 double[] u = sortedData.elements();
1065 int n = sortedData.size();
1067 double d2, d1, unSurN;
1068 double[] ret =
new double[2];
1071 ret[0] = ret[1] = 0.0;
1072 System.err.println(
"kolmogorovSmirnovJumpOne: n <= 0");
1081 while (j < n && u[j] <= a + EPSILOND)
1084 for (i = j - 1; i < n; i++) {
1086 d1 = (i + 1) * unSurN - u[i];
1091 d2 = u[i] - i * unSurN;
1116 public static double pDisc(
double pL,
double pR) {
This class helps managing the partitions of possible outcomes into categories for applying chi-square...
int smax
Maximum index for valid expected numbers in the array nbExp.
int smin
Minimum index for valid expected numbers in the array nbExp.
int nbCategories
Total number of categories.
void regroupCategories(double minExp)
Regroup categories as explained earlier, so that the expected number of observations in each category...
OutcomeCategoriesChi2(double[] nbExp, int[] loc, int smin, int smax, int nbCat)
Constructs an OutcomeCategoriesChi2 object.
OutcomeCategoriesChi2(double[] nbExp)
Constructs an OutcomeCategoriesChi2 object using the array nbExp for the number of expected observati...
double[] nbExp
Expected number of observations for each category.
int[] loc
loc[i] gives the relocation of the category i in the nbExp array.
OutcomeCategoriesChi2(double[] nbExp, int smin, int smax)
Constructs an OutcomeCategoriesChi2 object using the given nbExp expected observations array.
String toString()
Provides a report on the categories.
static double andersonDarling(DoubleArrayList sortedData)
Computes and returns the Anderson-Darling statistic (see method andersonDarling(double[]) ).
static double[] kolmogorovSmirnov(double[] sortedData)
Computes the Kolmogorov-Smirnov (KS) test statistics ,.
static double cramerVonMises(DoubleArrayList sortedData)
Computes and returns the Cramér-von Mises statistic (see.
static DoubleArrayList unifTransform(DoubleArrayList data, ContinuousDistribution dist)
Applies the probability integral transformation for.
static double chi2Equal(DoubleArrayList data)
Equivalent to chi2Equal (data, 10).
static DoubleArrayList unifTransform(DoubleArrayList data, DiscreteDistribution dist)
Applies the transformation for , where is a discrete distribution function, and returns the result ...
static double watsonG(DoubleArrayList sortedData)
Computes and returns the Watson statistic (see.
static void kolmogorovSmirnov(double[] data, ContinuousDistribution dist, double[] sval, double[] pval)
Computes the KolmogorovSmirnov (KS) test statistics and their.
static double chi2Equal(double nbExp, int[] count, int smin, int smax)
Similar to chi2(double[],int[],int,int), except that the expected number of observations per category...
static double[] andersonDarling(double[] data, ContinuousDistribution dist)
Computes the Anderson-Darling statistic and the corresponding -value .
static void diff(DoubleArrayList sortedData, DoubleArrayList spacings, int n1, int n2, double a, double b)
Same as method diff(IntArrayList,IntArrayList,int,int,int,int), but for the continuous case.
static double chi2Equal(DoubleArrayList data, double minExp)
Computes the chi-square statistic for a continuous distribution.
static void iterateSpacings(DoubleArrayList data, DoubleArrayList spacings)
Applies one iteration of the iterated spacings transformation.
static double andersonDarling(double[] sortedData)
Computes and returns the Anderson-Darling statistic (see.
static int scan(DoubleArrayList sortedData, double d)
Computes and returns the scan statistic , defined in ( scan ).
static double[] kolmogorovSmirnovJumpOne(DoubleArrayList sortedData, double a)
Compute the KS statistics and defined in the description of the method FDist.kolmogorovSmirnovPlusJ...
static double[] kolmogorovSmirnov(DoubleArrayList sortedData)
Computes the Kolmogorov-Smirnov (KS) test statistics ,.
static void powerRatios(DoubleArrayList sortedData)
Applies the power ratios transformation described in section 8.4 of Stephens tste86a .
static double watsonU(DoubleArrayList sortedData)
Computes and returns the Watson statistic (see.
static double chi2(double[] nbExp, int[] count, int smin, int smax)
Computes and returns the chi-square statistic for the observations.
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 double chi2(IntArrayList data, DiscreteDistributionInt dist, int smin, int smax, double minExp, int[] numCat)
Computes and returns the chi-square statistic for the observations stored in data,...
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...
static double chi2(OutcomeCategoriesChi2 cat, int[] count)
Computes and returns the chi-square statistic for the observations.
Extends the class AndersonDarlingDist for the Anderson–Darling distribution (see tand52a,...
double barF(double x)
Returns the complementary distribution function.
Classes implementing continuous distributions should inherit from this base class.
Classes implementing discrete distributions over the integers should inherit from this class.
abstract double prob(int x)
Returns , the probability of .
static double EPSILON
Environment variable that determines what probability terms can be considered as negligible when buil...
This class implements discrete distributions over a finite set of real numbers (also over integers as...
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.
static final double DBL_EPSILON
Difference between 1.0 and the smallest double greater than 1.0.
double cdf(double x)
Returns the distribution function .