@@ -276,7 +276,18 @@ all_counts <- function(x, ...) {
276
276
all_whole_number(x , ... ) && min(x ) > = 0
277
277
}
278
278
279
-
279
+ # ' Obtain the coverage parameter for simultaneous confidence bands for ECDFs
280
+ # '
281
+ # ' @param N Length of sample.
282
+ # ' @param L Number of chains. Used for MCMC, defaults to 1 for ppc.
283
+ # ' @param K Number of equally spaced evaluation points (1:K / K). Defaults to N.
284
+ # ' @param prob Desired simultaneous coverage (0,1).
285
+ # ' @param M number of simulations to run, if simulation method is used.
286
+ # ' @param adj_method String defining the desired adjustment method. By default
287
+ # ' "interpolate" is used. Other available options "optimize" and "simulate".
288
+ # ' @return The adjusted coverage parameter yielding the desired simultaneous
289
+ # ' coverage of the ECDF traces.
290
+ # ' @noRd
280
291
adjust_gamma <- function (N ,
281
292
L = 1 ,
282
293
K = N ,
@@ -298,11 +309,7 @@ adjust_gamma <- function(N,
298
309
gamma
299
310
}
300
311
301
- # Adjust coverage parameter to find simultaneous confidence intervals for the
302
- # ECDF of a sample from the uniform distribution.
303
- # N - length of samples
304
- # K - number of equally spaced evaluation points, i.e. the right ends of the
305
- # partition intervals.
312
+ # Adjust coverage parameter for single sample using the optimization method.
306
313
adjust_gamma_optimize <- function (N , K , prob = 0.99 ) {
307
314
target <- function (gamma , prob , N , K ) {
308
315
z <- 1 : (K - 1 ) / K
@@ -321,7 +328,7 @@ adjust_gamma_optimize <- function(N, K, prob=0.99) {
321
328
for (i in seq_along(z1 )) {
322
329
tmp <- p_interior(
323
330
p_int , x1 = x1 , x2 = x2_lower [i ]: x2_upper [i ],
324
- z1 = z1 [i ], z2 = z2 [i ], gamma = gamma , N = N
331
+ z1 = z1 [i ], z2 = z2 [i ], N = N
325
332
)
326
333
x1 <- tmp $ x1
327
334
p_int <- tmp $ p_int
@@ -331,21 +338,14 @@ adjust_gamma_optimize <- function(N, K, prob=0.99) {
331
338
optimize(target , c(0 , 1 - prob ), prob , N = N , K = K )$ minimum
332
339
}
333
340
334
- # Adjust coverage parameter to find silmultaneous confidence intervals for the
335
- # ECDFs of multiple samples (chains) from the uniform distribution.
336
- # N - length of samples (chains).
337
- # L - number of samples (chains).
338
- # K - number of equally spaced evaluation points, i.e. the right ends of the
339
- # partition intervals.
340
- # M - number of simulations used to determine the 'prob' middle quantile.
341
- # '
341
+ # Adjust coverage parameter for multiple chains using simulation method.
342
342
adjust_gamma_simulate <- function (N , L , K , prob = 0.99 , M = 1000 ) {
343
343
gamma <- numeric (M )
344
344
z <- (1 : (K - 1 )) / K
345
345
n <- N * (L - 1 )
346
346
k <- floor(z * N * L )
347
347
for (m in seq_len(M )) {
348
- u <- replicate(L , runif(N )) % > % u_scale
348
+ u <- u_scale( replicate(L , runif(N )))
349
349
scaled_ecdfs <- apply(outer(u , z , " <=" ), c(2 , 3 ), sum )
350
350
gamma [m ] <- 2 * min(
351
351
apply(
@@ -359,7 +359,10 @@ adjust_gamma_simulate <- function(N, L, K, prob = 0.99, M = 1000) {
359
359
alpha_quantile(gamma , 1 - prob )
360
360
}
361
361
362
- p_interior <- function (p_int , x1 , x2 , z1 , z2 , gamma , N ) {
362
+ # ' A helper function for 'adjust_gamma_optimize' defining the probability that
363
+ # ' an ECDF stays within the supplied bounds between z1 and z2.
364
+ # ' @noRd
365
+ p_interior <- function (p_int , x1 , x2 , z1 , z2 , N ) {
363
366
z_tilde <- (z2 - z1 ) / (1 - z1 )
364
367
N_tilde <- rep(N - x1 , each = length(x2 ))
365
368
p_int <- rep(p_int , each = length(x2 ))
@@ -369,9 +372,11 @@ p_interior <- function(p_int, x1, x2, z1, z2, gamma, N) {
369
372
list (p_int = rowSums(p_x2_int ), x1 = x2 )
370
373
}
371
374
372
- # 100 * `alpha` percent of the trials are allowed to be rejected.
373
- # In case of ties, return the largest value dominating at most
374
- # 100 * (alpha + tol) percent of the values.
375
+ # ' A helper function for 'adjust_alpha_simulate'
376
+ # ' 100 * `alpha` percent of the trials are allowed to be rejected.
377
+ # ' In case of ties, return the largest value dominating at most
378
+ # ' 100 * (alpha + tol) percent of the values.
379
+ # ' @noRd
375
380
alpha_quantile <- function (gamma , alpha , tol = 0.001 ) {
376
381
a <- unname(quantile(gamma , probs = alpha ))
377
382
a_tol <- unname(quantile(gamma , probs = alpha + tol ))
@@ -384,17 +389,17 @@ alpha_quantile <- function(gamma, alpha, tol = 0.001) {
384
389
a
385
390
}
386
391
387
- # Compute simultaneous confidence intervals for one or more samples from the
388
- # standard uniform distribution .
389
- # N - sample length
390
- # L - number of samples
391
- # K - size of uniform partition defining the ECDF evaluation points.
392
- # gamma - coverage parameter for the marginal distribution (binomial for
393
- # one sample and hypergeometric for multiple rank transformed chains).
392
+ # ' Compute simultaneous confidence intervals with the given adjusted coverage
393
+ # ' parameter gamma .
394
+ # ' @param N Sample length.
395
+ # ' @param L Number of MCMC-chains. (1 for ppc)
396
+ # ' @param K Number of uniformly spaced evaluation points.
397
+ # ' @param gamma Adjusted coverage parameter for the marginal distribution
398
+ # ' (binomial for PIT values and hypergeometric for rank transformed chains).
394
399
# ' @noRd
395
400
ecdf_intervals <- function (N , L = 1 , K , gamma ) {
396
401
lims <- list ()
397
- z <- seq(0 ,1 , length.out = K + 1 )
402
+ z <- seq(0 , 1 , length.out = K + 1 )
398
403
if (L == 1 ) {
399
404
lims $ lower <- qbinom(gamma / 2 , N , z )
400
405
lims $ upper <- qbinom(1 - gamma / 2 , N , z )
@@ -407,7 +412,8 @@ ecdf_intervals <- function(N, L=1, K, gamma) {
407
412
lims
408
413
}
409
414
410
- # ' Transform observations in 'x' into their corresponding fractional ranks.
415
+ # ' Helper for 'adjust_gamma_simulate`
416
+ # ' Transforms observations in 'x' into their corresponding fractional ranks.
411
417
# ' @noRd
412
418
u_scale <- function (x ) {
413
419
array (rank(x ) / length(x ), dim = dim(x ), dimnames = dimnames(x ))
0 commit comments