Skip to content

Commit a1028b0

Browse files
Added adjustment value interpolation -> fast plots
1 parent 14d183c commit a1028b0

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

R/helpers-ppc.R

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -300,22 +300,16 @@ adjust_gamma <- function(N,
300300
if (prob >= 1 || prob <= 0) {
301301
abort("Value of 'prob' must be in (0,1).")
302302
}
303-
if (L == 1) {
304-
if (interpolate_adj == TRUE) {
305-
gamma <- 0
306-
} else {
307-
gamma <- adjust_gamma_optimize(N, K, prob)
308-
}
303+
if (interpolate_adj == TRUE) {
304+
gamma <- interpolate_gamma(N, K, prob, L)
305+
} else if (L == 1) {
306+
gamma <- adjust_gamma_optimize(N, K, prob)
309307
} else {
310-
if (interpolate_adj == TRUE) {
311-
gamma <- 0
312-
} else {
313-
gamma <- adjust_gamma_simulate(N,
314-
L,
315-
K,
316-
prob,
317-
M)
318-
}
308+
gamma <- adjust_gamma_simulate(N,
309+
L,
310+
K,
311+
prob,
312+
M)
319313
}
320314
gamma
321315
}
@@ -370,6 +364,55 @@ adjust_gamma_simulate <- function(N, L, K, prob, M) {
370364
alpha_quantile(gamma, 1 - prob)
371365
}
372366

367+
interpolate_gamma <- function(N, K, p, L) {
368+
vals <- get_interpolation_values(N, K, L, p)
369+
N_lb <- max(vals[vals$N <= N, ]$N)
370+
N_ub <- min(vals[vals$N >= N, ]$N)
371+
g_lb <- approx(
372+
x = log(vals[vals$N == N_lb, ]$K),
373+
y = log(vals[vals$N == N_lb, ]$val),
374+
xout = log(K)
375+
)$y
376+
g_ub <- approx(
377+
x = log(vals[vals$N == N_ub, ]$K),
378+
y = log(vals[vals$N == N_ub, ]$val),
379+
xout = log(K)
380+
)$y
381+
if (N_ub == N_lb) {
382+
g <- exp(g_lb)
383+
} else {
384+
g <- exp(approx(x = log(c(N_lb, N_ub)), y = c(g_lb, g_ub), xout = log(N))$y)
385+
}
386+
g
387+
}
388+
389+
get_interpolation_values <- function(N, K, L, p) {
390+
for (dim in c("L", "prob")) {
391+
if (all(get(if (dim == "L") dim else "p") != bayesplot:::gamma_adj[, dim])) {
392+
stop(paste(
393+
"No precomputed values to interpolate from for '", dim, "' = ",
394+
get(if (dim == "L") dim else "p"),
395+
".\n",
396+
"Values of '", dim, "' available for interpolation: ",
397+
unique(bayesplot:::gamma_adj[, dim]),
398+
".",
399+
sep = ""
400+
))
401+
}
402+
}
403+
vals <- bayesplot:::gamma_adj[bayesplot:::gamma_adj$L == L & bayesplot:::gamma_adj$prob == p, ]
404+
if (K > max(vals[vals$N <= N, ]$K)) {
405+
stop(paste(
406+
"No precomputed values available for interpolation for 'K' = ",
407+
K,
408+
".\n",
409+
"Try either setting a value of 'K' <= ",
410+
max(vals[vals$N <= N, ]$K),
411+
"or 'interpolate_adj' = FALSE."
412+
))
413+
}
414+
vals
415+
}
373416
#' A helper function for 'adjust_gamma_optimize' defining the probability that
374417
#' an ECDF stays within the supplied bounds between z1 and z2.
375418
#' @noRd

R/mcmc-traces.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ mcmc_rank_ecdf <-
453453
facet_args = list(),
454454
prob = 0.99,
455455
plot_diff = TRUE,
456-
interpolate_adj = FALSE) {
456+
interpolate_adj = TRUE) {
457457
check_ignored_arguments(...,
458458
ok_args = c("K", "pit", "prob", "plot_diff", "interpolate_adj", "M")
459459
)

R/ppc-distributions.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ ppc_pit_ecdf <- function(y,
558558
K = NULL,
559559
prob = .99,
560560
plot_diff = TRUE,
561-
interpolate_adj = FALSE) {
561+
interpolate_adj = TRUE) {
562562
check_ignored_arguments(...,
563563
ok_args = c("K", "pit", "prob", "plot_diff", "interpolate_adj"))
564564

@@ -617,7 +617,7 @@ ppc_pit_ecdf_grouped <-
617617
pit = NULL,
618618
prob = .99,
619619
plot_diff = TRUE,
620-
interpolate_adj = FALSE) {
620+
interpolate_adj = TRUE) {
621621
check_ignored_arguments(...,
622622
ok_args = c("K", "pit", "prob", "plot_diff", "interpolate_adj"))
623623

R/sysdata.rda

28.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)