forked from stan-dev/posterior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pareto_smooth.R
637 lines (558 loc) · 18.1 KB
/
pareto_smooth.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#' Pareto khat diagnostic
#'
#' Estimate Pareto k value by fitting a Generalized Pareto
#' Distribution to one or two tails of x. This can be used to estimate
#' the number of fractional moments that is useful for convergence
#' diagnostics. For further details see Vehtari et al. (2022).
#'
#' @family diagnostics
#' @template args-pareto
#' @template args-methods-dots
#' @template ref-vehtari-paretosmooth-2022
#' @return `khat` estimated Generalized Pareto Distribution shape parameter k
#'
#' @seealso [`pareto_diags`] for additional related diagnostics, and
#' [`pareto_smooth`] for Pareto smoothed draws.
#' @examples
#' mu <- extract_variable_matrix(example_draws(), "mu")
#' pareto_khat(mu)
#'
#' d <- as_draws_rvars(example_draws("multi_normal"))
#' pareto_khat(d$Sigma)
#' @export
pareto_khat <- function(x, ...) UseMethod("pareto_khat")
#' @rdname pareto_khat
#' @export
pareto_khat.default <- function(x,
tail = c("both", "right", "left"),
r_eff = NULL,
ndraws_tail = NULL,
verbose = FALSE,
are_log_weights = FALSE,
...) {
smoothed <- pareto_smooth.default(
x,
tail = tail,
r_eff = r_eff,
ndraws_tail = ndraws_tail,
verbose = verbose,
return_k = TRUE,
smooth_draws = FALSE,
are_log_weights = are_log_weights,
...)
return(smoothed$diagnostics)
}
#' @rdname pareto_khat
#' @export
pareto_khat.rvar <- function(x, ...) {
draws_diags <- summarise_rvar_by_element_with_chains(
x,
pareto_smooth.default,
return_k = TRUE,
smooth_draws = FALSE,
...
)
dim(draws_diags) <- dim(draws_diags) %||% length(draws_diags)
margins <- seq_along(dim(draws_diags))
diags <- list(
khat = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$khat)
)
diags
}
#' Pareto smoothing diagnostics
#'
#' Compute diagnostics for Pareto smoothing the tail draws of x by
#' replacing tail draws by order statistics of a generalized Pareto
#' distribution fit to the tail(s).
#'
#' @family diagnostics
#' @template args-pareto
#' @template args-methods-dots
#' @template ref-vehtari-paretosmooth-2022
#' @return List of Pareto smoothing diagnostics:
#' * `khat`: estimated Pareto k shape parameter,
#' * `min_ss`: minimum sample size for reliable Pareto smoothed estimate,
#' * `khat_threshold`: khat-threshold for reliable Pareto smoothed estimate,
#' * `convergence_rate`: Pareto smoothed estimate RMSE convergence rate.
#'
#' @details When the fitted Generalized Pareto Distribution is used to
#' smooth the tail values and these smoothed values are used to
#' compute expectations, the following diagnostics can give further
#' information about the reliability of these estimates.
#'
#' * `min_ss`: Minimum sample size for reliable Pareto smoothed
#' estimate. If the actual sample size is greater than `min_ss`, then
#' Pareto smoothed estimates can be considered reliable. If the actual
#' sample size is lower than `min_ss`, increasing the sample size
#' might result in more reliable estimates. For further details, see
#' Section 3.2.3, Equation 11 in Vehtari et al. (2022).
#'
#' * `khat_threshold`: Threshold below which k-hat values result in
#' reliable Pareto smoothed estimates. The threshold is lower for
#' smaller effective sample sizes. If k-hat is larger than the
#' threshold, increasing the total sample size may improve reliability
#' of estimates. For further details, see Section 3.2.4, Equation 13
#' in Vehtari et al. (2022).
#'
#' * `convergence_rate`: Relative convergence rate compared to the
#' central limit theorem. Applicable only if the actual sample size
#' is sufficiently large (greater than `min_ss`). The convergence
#' rate tells the rate at which the variance of an estimate reduces
#' when the sample size is increased, compared to the central limit
#' theorem convergence rate. See Appendix B in Vehtari et al. (2022).
#'
#' @seealso [`pareto_khat`] for only calculating khat, and
#' [`pareto_smooth`] for Pareto smoothed draws.
#' @examples
#' mu <- extract_variable_matrix(example_draws(), "mu")
#' pareto_diags(mu)
#'
#' d <- as_draws_rvars(example_draws("multi_normal"))
#' pareto_diags(d$Sigma)
#' @export
pareto_diags <- function(x, ...) UseMethod("pareto_diags")
#' @rdname pareto_diags
#' @export
pareto_diags.default <- function(x,
tail = c("both", "right", "left"),
r_eff = NULL,
ndraws_tail = NULL,
verbose = FALSE,
are_log_weights = FALSE,
...) {
smoothed <- pareto_smooth.default(
x,
tail = tail,
r_eff = r_eff,
ndraws_tail = ndraws_tail,
return_k = TRUE,
extra_diags = TRUE,
verbose = verbose,
smooth_draws = FALSE,
are_log_weights = FALSE,
...)
return(smoothed$diagnostics)
}
#' @rdname pareto_diags
#' @export
pareto_diags.rvar <- function(x, ...) {
draws_diags <- summarise_rvar_by_element_with_chains(
x,
pareto_smooth.default,
return_k = TRUE,
smooth_draws = FALSE,
extra_diags = TRUE,
...
)
dim(draws_diags) <- dim(draws_diags) %||% length(draws_diags)
margins <- seq_along(dim(draws_diags))
diags <- list(
khat = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$khat),
min_ss = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$min_ss),
khat_threshold = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$khat_threshold),
convergence_rate = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$convergence_rate)
)
diags
}
#' Pareto smoothing
#'
#' Smooth the tail draws of x by replacing tail draws by order
#' statistics of a generalized Pareto distribution fit to the
#' tail(s). For further details see Vehtari et al. (2022).
#'
#' @template args-pareto
#' @param return_k (logical) Should the Pareto khat be included in
#' output? If `TRUE`, output will be a list containing smoothed
#' draws and diagnostics, otherwise it will be a numeric of the
#' smoothed draws. Default is `FALSE`.
#' @param extra_diags (logical) Should extra Pareto khat diagnostics
#' be included in output? If `TRUE`, `min_ss`, `khat_threshold` and
#' `convergence_rate` for the estimated k value will be
#' returned. Default is `FALSE`.
#' @template args-methods-dots
#' @template ref-vehtari-paretosmooth-2022
#' @return Either a vector `x` of smoothed values or a named list
#' containing the vector `x` and a named list `diagnostics`
#' containing Pareto smoothing diagnostics: * `khat`: estimated
#' Pareto k shape parameter, and optionally * `min_ss`: minimum
#' sample size for reliable Pareto smoothed estimate *
#' `khat_threshold`: khat-threshold for reliable Pareto smoothed
#' estimates * `convergence_rate`: Relative convergence rate for
#' Pareto smoothed estimates
#'
#' @seealso [`pareto_khat`] for only calculating khat, and
#' [`pareto_diags`] for additional diagnostics.
#' @examples
#' mu <- extract_variable_matrix(example_draws(), "mu")
#' pareto_smooth(mu)
#'
#' d <- as_draws_rvars(example_draws("multi_normal"))
#' pareto_smooth(d$Sigma)
#' @export
pareto_smooth <- function(x, ...) UseMethod("pareto_smooth")
#' @rdname pareto_smooth
#' @export
pareto_smooth.rvar <- function(x, return_k = FALSE, extra_diags = FALSE, ...) {
if (extra_diags) {
return_k <- TRUE
}
draws_diags <- summarise_rvar_by_element_with_chains(x, pareto_smooth.default, return_k = return_k, extra_diags = extra_diags, ...)
dim(draws_diags) <- dim(draws_diags) %||% length(draws_diags)
margins <- seq_along(dim(draws_diags))
if (return_k) {
if (extra_diags) {
diags <- list(
khat = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$khat),
min_ss = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$min_ss),
khat_threshold = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$khat_threshold),
convergence_rate = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$convergence_rate)
)
} else {
diags <- list(
khat = apply(draws_diags, margins, function(x) x[[1]]$diagnostics$khat)
)
}
out <- list(
x = rvar(apply(draws_diags, margins, function(x) x[[1]]$x), nchains = nchains(x)),
diagnostics = diags
)
} else {
out <- rvar(apply(draws_diags, margins, function(x) x[[1]]), nchains = nchains(x))
}
out
}
#' @rdname pareto_smooth
#' @export
pareto_smooth.default <- function(x,
tail = c("both", "right", "left"),
r_eff = 1,
ndraws_tail = NULL,
return_k = FALSE,
extra_diags = FALSE,
verbose = TRUE,
are_log_weights = FALSE,
...) {
checkmate::expect_numeric(ndraws_tail, null.ok = TRUE)
checkmate::expect_numeric(r_eff, null.ok = TRUE)
extra_diags <- as_one_logical(extra_diags)
return_k <- as_one_logical(return_k)
verbose <- as_one_logical(verbose)
are_log_weights <- as_one_logical(are_log_weights)
# check for infinite or na values
if (should_return_NA(x)) {
warning_no_call("Input contains infinite or NA values, or is constant. Fitting of generalized Pareto distribution not performed.")
if (!return_k) {
out <- x
} else {
out <- list(x = x, diagnostics = NA_real_)
}
return(out)
}
if (are_log_weights) {
tail <- "right"
}
tail <- match.arg(tail)
S <- length(x)
# automatically calculate relative efficiency
if (is.null(r_eff)) {
r_eff <- ess_tail(x) / S
}
# automatically calculate tail length
if (is.null(ndraws_tail)) {
ndraws_tail <- ps_tail_length(S, r_eff)
}
if (tail == "both") {
if (ndraws_tail > S / 2) {
warning("Number of tail draws cannot be more than half ",
"the total number of draws if both tails are fit, ",
"changing to ", S / 2, ".")
ndraws_tail <- S / 2
}
if (ndraws_tail < 5) {
warning("Number of tail draws cannot be less than 5. ",
"Changing to ", 5, ".")
ndraws_tail <- 5
}
# left tail
smoothed <- .pareto_smooth_tail(
x,
ndraws_tail = ndraws_tail,
tail = "left",
are_log_weights = are_log_weights,
...
)
left_k <- smoothed$k
# right tail
smoothed <-.pareto_smooth_tail(
x = smoothed$x,
ndraws_tail = ndraws_tail,
tail = "right",
are_log_weights = are_log_weights,
...
)
right_k <- smoothed$k
k <- max(left_k, right_k)
x <- smoothed$x
} else {
smoothed <- .pareto_smooth_tail(
x,
ndraws_tail = ndraws_tail,
tail = tail,
...
)
k <- smoothed$k
x <- smoothed$x
}
diags_list <- list(khat = k)
if (extra_diags) {
ext_diags <- .pareto_smooth_extra_diags(k, S)
diags_list <- c(diags_list, ext_diags)
}
if (verbose) {
if (!extra_diags) {
diags_list <- c(diags_list, .pareto_smooth_extra_diags(diags_list$khat, length(x)))
}
pareto_k_diagmsg(
diags = diags_list,
are_weights = are_log_weights
)
}
if (return_k) {
out <- list(x = x, diagnostics = diags_list)
} else {
out <- x
}
return(out)
}
#' @rdname pareto_diags
#' @export
pareto_khat_threshold <- function(x, ...) {
UseMethod("pareto_khat_threshold")
}
#' @rdname pareto_diags
#' @export
pareto_khat_threshold.default <- function(x, ...) {
c(khat_threshold = ps_khat_threshold(length(x)))
}
#' @rdname pareto_diags
#' @export
pareto_khat_threshold.rvar <- function(x, ...) {
c(khat_threshold = ps_khat_threshold(ndraws(x)))
}
#' @rdname pareto_diags
#' @export
pareto_min_ss <- function(x, ...) {
UseMethod("pareto_min_ss")
}
#' @rdname pareto_diags
#' @export
pareto_min_ss.default <- function(x, ...) {
k <- pareto_khat(x)$k
c(min_ss = ps_min_ss(k))
}
#' @rdname pareto_diags
#' @export
pareto_min_ss.rvar <- function(x, ...) {
k <- pareto_khat(x)$k
c(min_ss = ps_min_ss(k))
}
#' @rdname pareto_diags
#' @export
pareto_convergence_rate <- function(x, ...) {
UseMethod("pareto_convergence_rate")
}
#' @rdname pareto_diags
#' @export
pareto_convergence_rate.default <- function(x, ...) {
k <- pareto_khat(x)$khat
c(convergence_rate = ps_convergence_rate(k, length(x)))
}
#' @rdname pareto_diags
#' @export
pareto_convergence_rate.rvar <- function(x, ...) {
k <- pareto_khat(x)
c(convergence_rate = ps_convergence_rate(k, ndraws(x)))
}
#' Pareto smooth tail
#' internal function to pareto smooth the tail of a vector
#' @noRd
.pareto_smooth_tail <- function(x,
ndraws_tail,
smooth_draws = TRUE,
tail = c("right", "left"),
are_log_weights = FALSE,
...
) {
if (are_log_weights) {
# shift log values for safe exponentiation
x <- x - max(x)
}
tail <- match.arg(tail)
S <- length(x)
tail_ids <- seq(S - ndraws_tail + 1, S)
if (tail == "left") {
x <- -x
}
ord <- sort.int(x, index.return = TRUE)
draws_tail <- ord$x[tail_ids]
cutoff <- ord$x[min(tail_ids) - 1] # largest value smaller than tail values
max_tail <- max(draws_tail)
min_tail <- min(draws_tail)
if (ndraws_tail >= 5) {
ord <- sort.int(x, index.return = TRUE)
if (abs(max_tail - min_tail) < .Machine$double.eps / 100) {
warning_no_call(
"Can't fit generalized Pareto distribution ",
"because all tail values are the same."
)
smoothed <- NULL
k <- NA
} else {
# save time not sorting since x already sorted
if (are_log_weights) {
draws_tail <- exp(draws_tail)
cutoff <- exp(cutoff)
}
fit <- gpdfit(draws_tail - cutoff, sort_x = FALSE, ...)
k <- fit$k
sigma <- fit$sigma
if (is.finite(k) && smooth_draws) {
p <- (seq_len(ndraws_tail) - 0.5) / ndraws_tail
smoothed <- qgeneralized_pareto(p = p, mu = cutoff, k = k, sigma = sigma)
if (are_log_weights) {
smoothed <- log(smoothed)
}
} else {
smoothed <- NULL
}
}
} else {
warning_no_call(
"Can't fit generalized Pareto distribution ",
"because ndraws_tail is less than 5."
)
smoothed <- NULL
k <- NA
}
# truncate at max of raw draws
if (!is.null(smoothed)) {
smoothed[smoothed > max_tail] <- max_tail
x[ord$ix[tail_ids]] <- smoothed
}
if (tail == "left") {
x <- -x
}
out <- list(x = x, k = k)
return(out)
}
#' Extra pareto-k diagnostics
#'
#' internal function to calculate the extra diagnostics for a given
#' pareto k and sample size S
#' @noRd
.pareto_smooth_extra_diags <- function(k, S, ...) {
min_ss <- ps_min_ss(k)
khat_threshold <- ps_khat_threshold(S)
convergence_rate <- ps_convergence_rate(k, S)
other_diags <- list(
min_ss = min_ss,
khat_threshold = khat_threshold,
convergence_rate = convergence_rate
)
}
#' Pareto-smoothing minimum sample-size
#'
#' Given Pareto-k computes the minimum sample size for reliable Pareto
#' smoothed estimate (to have small probability of large error)
#' Equation (11) in PSIS paper
#' @param k pareto k value
#' @param ... unused
#' @return minimum sample size
#' @noRd
ps_min_ss <- function(k, ...) {
if (k < 1) {
out <- 10^(1 / (1 - max(0, k)))
} else {
out <- Inf
}
out
}
#' Pareto-smoothing k-hat threshold
#'
#' Given sample size S computes khat threshold for reliable Pareto
#' smoothed estimate (to have small probability of large error). See
#' section 3.2.4, equation (13).
#' @param S sample size
#' @param ... unused
#' @return threshold
#' @noRd
ps_khat_threshold <- function(S, ...) {
1 - 1 / log10(S)
}
#' Pareto-smoothing convergence rate
#'
#' Given S and scalar or array of k's, compute the relative
#' convergence rate of PSIS estimate RMSE
#' @param k pareto-k values
#' @param S sample size
#' @param ... unused
#' @return convergence rate
#' @noRd
ps_convergence_rate <- function(k, S, ...) {
# allow array of k's
rate <- numeric(length(k))
# k<0 bounded distribution
rate[k < 0] <- 1
# k>0 non-finite mean
rate[k > 1] <- 0
# limit value at k=1/2
rate[k == 0.5] <- 1 - 1 / log(S)
# smooth approximation for the rest (see Appendix of PSIS paper)
ki <- (k > 0 & k < 1 & k != 0.5)
kk <- k[ki]
rate[ki] <- pmax(
0,
(2 * (kk - 1) * S^(2 * kk + 1) + (1 - 2 * kk) * S^(2 * kk) + S^2) /
((S - 1) * (S - S^(2 * kk)))
)
rate
}
#' Calculate the tail length from S and r_eff
#' Appendix H in PSIS paper
#' @noRd
ps_tail_length <- function(S, r_eff, ...) {
ifelse(S > 225, ceiling(3 * sqrt(S / r_eff)), S / 5)
}
#' Pareto-k diagnostic message
#'
#' Given S and scalar and k, form a diagnostic message string
#' @param diags (numeric) named vector of diagnostic values
#' @param are_weights (logical) are the diagnostics for weights
#' @param ... unused
#' @return diagnostic message
#' @noRd
pareto_k_diagmsg <- function(diags, are_weights = FALSE, ...) {
khat <- diags$khat
min_ss <- diags$min_ss
khat_threshold <- diags$khat_threshold
convergence_rate <- diags$convergence_rate
msg <- NULL
if (!are_weights) {
if (khat > 1) {
msg <- paste0(msg, " Mean does not exist, making empirical mean estimate of the draws not applicable.")
} else {
if (khat > khat_threshold) {
msg <- paste0(msg, " Sample size is too small, for given Pareto k-hat. Sample size larger than ", round(min_ss, 0), " is needed for reliable results.\n")
}
if (khat > 0.7) {
msg <- paste0(msg, " Bias dominates when k-hat > 0.7, making empirical mean estimate of the Pareto-smoothed draws unreliable.\n")
}
}
} else {
if (khat > khat_threshold || khat > 0.7) {
msg <- paste0(msg, " Pareto khat for weights is high (", round(khat, 1) ,"). This indicates a single or few weights dominate.\n", "Inference based on weighted draws will be unreliable.\n")
}
}
message("Pareto k-hat = ", round(khat, 2), ".", msg)
invisible(diags)
}