-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathp-r-random-tests.R
181 lines (152 loc) · 6.04 KB
/
p-r-random-tests.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
##
## RNG tests using DKW inequality for rate of convergence
##
## P(sup | F_n - F | > t) < 2 exp(-2nt^2)
##
## The 2 in front of exp() was derived by Massart. It is the best possible
## constant valid uniformly in t,n,F. For large n*t^2 this agrees with the
## large-sample approximation to the Kolmogorov-Smirnov statistic.
##
## When tryCatch()ing all seeds in 0:10000, the following 346 failed (Lnx 64b, R 3.5.0):
suppressWarnings(RNGversion("3.5.0"))
failingSeeds <- c(
16, 42, 51, 63, 79, 108, 143, 171, 208, 215,
230, 236, 254, 323, 327, 332, 333, 374, 386, 387,
438, 440, 450, 472, 547, 609, 673, 740, 784, 787,
792, 806, 846, 897, 938, 1017,1043,1062,1067,1076,
1090,1113,1115,1136,1142, 1148,1162,1193,1249,1259,
1299,1338,1347,1366,1407, 1428,1457,1461,1540,1609,
1613,1622,1629,1664,1712, 1760,1779,1786,1826,1852,
1868,1871,1880,1928,1930, 1978,1984,2025,2073,2081,
2082,2130,2148,2153,2172, 2175,2228,2298,2353,2368,
2430,2444,2462,2493,2528, 2631,2750,2752,2765,2774,
2794,2817,2873,2888,2905, 2906,2911,2936,2955,2989,
3029,3048,3053,3084,3100, 3148,3183,3192,3232,3256,
3266,3302,3311,3313,3319, 3325,3340,3344,3375,3477,
3506,3516,3518,3521,3553, 3601,3655,3717,3733,3810,
3814,3962,4043,4095,4119, 4174,4185,4192,4228,4240,
4261,4298,4335,4338,4349, 4402,4433,4461,4491,4496,
4508,4511,4530,4604,4622, 4640,4669,4677,4682,4683,
4705,4717,4725,4757,4816, 4899,4931,5014,5022,5063,
5082,5105,5107,5137,5155, 5160,5165,5169,5182,5186,
5197,5207,5210,5211,5263, 5281,5282,5288,5364,5529,
5568,5611,5651,5700,5740, 5796,5869,5874,5878,5920,
5954,5972,6034,6037,6073, 6086,6118,6120,6126,6234,
6235,6263,6287,6301,6360, 6364,6377,6416,6491,6493,
6524,6534,6568,6615,6679, 6682,6777,6782,6790,6808,
6885,6887,6936,6938,6961, 7011,7046,7047,7062,7111,
7181,7202,7206,7207,7227, 7261,7301,7311,7313,7324,
7364,7385,7394,7412,7486, 7504,7519,7536,7584,7665,
7692,7762,7787,7797,7865, 7916,7959,7967,8038,8047,
8048,8086,8123,8125,8160, 8213,8243,8254,8255,8307,
8335,8403,8453,8487,8541, 8549,8577,8587,8638,8640,
8651,8664,8703,8770,8781, 8793,8841,8888,8900,8962,
8963,8965,9028,9052,9054, 9061,9143,9198,9204,9232,
9238,9247,9308,9311,9321, 9342,9360,9430,9457,9564,
9572,9609,9657,9738,9743, 9750,9758,9779,9789,9848,
9881,9895,9903,9905,9947, 9982)
## randomly setting one of the valid 10001-346 = 9655 seeds:
iseed <- sample(setdiff(0:10000, failingSeeds), size=1)
dump("iseed", file="p-r-random-tests_seed") #(for reproducibility, not into *.Rout)
set.seed(iseed)
superror <- function(rfoo,pfoo,sample.size,...) {
x <- rfoo(sample.size,...)
tx <- table(signif(x, 12)) # such that xi will be sort(unique(x))
xi <- as.numeric(names(tx))
f <- pfoo(xi,...)
fhat <- cumsum(tx)/sample.size
max(abs(fhat-f))
}
pdkwbound <- function(n,t) 2*exp(-2*n*t*t)
qdkwbound <- function(n,p) sqrt(log(p/2)/(-2*n))
dkwtest <- function(stub = "norm", ...,
sample.size = 10000, pthreshold = 0.001,
print.result = TRUE, print.detail = FALSE,
stop.on.failure = TRUE)
{
rfoo <- eval(as.name(paste("r", stub, sep="")))
pfoo <- eval(as.name(paste("p", stub, sep="")))
s <- superror(rfoo, pfoo, sample.size, ...)
if (print.result || print.detail) {
printargs <- substitute(list(...))
printargs[[1]] <- as.name(stub)
cat(deparse(printargs))
if (print.detail)
cat("\nsupremum error = ",signif(s,2),
" with p-value=",min(1,round(pdkwbound(sample.size,s),4)),"\n")
}
rval <- (s < qdkwbound(sample.size,pthreshold))
if (print.result)
cat(c(" FAILED\n"," PASSED\n")[rval+1])
if (stop.on.failure && !rval)
stop("dkwtest failed")
rval
}
.proctime00 <- proc.time() # start timing
dkwtest("binom",size = 1,prob = 0.2)
dkwtest("binom",size = 2,prob = 0.2)
dkwtest("binom",size = 100,prob = 0.2)
dkwtest("binom",size = 1e4,prob = 0.2)
dkwtest("binom",size = 1,prob = 0.8)
dkwtest("binom",size = 100,prob = 0.8)
dkwtest("binom",size = 100,prob = 0.999)
dkwtest("pois",lambda = 0.095)
dkwtest("pois",lambda = 0.95)
dkwtest("pois",lambda = 9.5)
dkwtest("pois",lambda = 95)
dkwtest("nbinom",size = 1,prob = 0.2)
dkwtest("nbinom",size = 2,prob = 0.2)
dkwtest("nbinom",size = 100,prob = 0.2)
dkwtest("nbinom",size = 1e4,prob = 0.2)
dkwtest("nbinom",size = 1,prob = 0.8)
dkwtest("nbinom",size = 100,prob = 0.8)
dkwtest("nbinom",size = 100,prob = 0.999)
dkwtest("norm")
dkwtest("norm",mean = 5,sd = 3)
dkwtest("gamma",shape = 0.1)
dkwtest("gamma",shape = 0.2)
dkwtest("gamma",shape = 10)
dkwtest("gamma",shape = 20)
dkwtest("hyper",m = 40,n = 30,k = 20)
dkwtest("hyper",m = 40,n = 3,k = 20)
dkwtest("hyper",m = 6,n = 3,k = 2)
dkwtest("hyper",m = 5,n = 3,k = 2)
dkwtest("hyper",m = 4,n = 3,k = 2)
dkwtest("signrank",n = 1)
dkwtest("signrank",n = 2)
dkwtest("signrank",n = 10)
dkwtest("signrank",n = 30)
dkwtest("wilcox",m = 40,n = 30)
dkwtest("wilcox",m = 40,n = 10)
dkwtest("wilcox",m = 6,n = 3)
dkwtest("wilcox",m = 5,n = 3)
dkwtest("wilcox",m = 4,n = 3)
dkwtest("chisq",df = 1)
dkwtest("chisq",df = 10)
dkwtest("logis")
dkwtest("logis",location = 4,scale = 2)
dkwtest("t",df = 1)
dkwtest("t",df = 10)
dkwtest("t",df = 40)
dkwtest("beta",shape1 = 1, shape2 = 1)
dkwtest("beta",shape1 = 2, shape2 = 1)
dkwtest("beta",shape1 = 1, shape2 = 2)
dkwtest("beta",shape1 = 2, shape2 = 2)
dkwtest("beta",shape1 = .2,shape2 = .2)
dkwtest("cauchy")
dkwtest("cauchy",location = 4,scale = 2)
dkwtest("f",df1 = 1,df2 = 1)
dkwtest("f",df1 = 1,df2 = 10)
dkwtest("f",df1 = 10,df2 = 10)
dkwtest("f",df1 = 30,df2 = 3)
dkwtest("weibull",shape = 1)
dkwtest("weibull",shape = 4,scale = 4)
## regression test for PR#7314
dkwtest("hyper", m=60, n=100, k=50)
dkwtest("hyper", m=6, n=10, k=5)
dkwtest("hyper", m=600, n=1000, k=500)
## regression test for non-central t bug
dkwtest("t", df=20, ncp=3)
## regression test for non-central F bug
dkwtest("f", df1=10, df2=2, ncp=3)
cat('Time elapsed: ', proc.time() - .proctime00,'\n')