@@ -361,6 +361,10 @@ pareto_smooth.default <- function(x,
361361 x <- smoothed $ x
362362 }
363363
364+ if (is.na(k )) {
365+ return (pareto_diags_na(x , return_k , extra_diags ))
366+ }
367+
364368 diags_list <- list (khat = k )
365369
366370 if (extra_diags ) {
@@ -470,38 +474,42 @@ pareto_convergence_rate.rvar <- function(x, ...) {
470474# '
471475# ' @export
472476ps_tail <- function (x ,
473- ndraws_tail ,
474- smooth_draws = TRUE ,
475- tail = c(" right" , " left" ),
476- are_log_weights = FALSE ,
477- ...
478- ) {
477+ ndraws_tail ,
478+ smooth_draws = TRUE ,
479+ tail = c(" right" , " left" ),
480+ are_log_weights = FALSE ,
481+ ...
482+ ) {
483+
484+ if (ndraws_tail < 5 ) {
485+ warning_no_call(
486+ " Can't fit generalized Pareto distribution " ,
487+ " because ndraws_tail is less than 5."
488+ )
489+ return (list (x = x , k = NA ))
490+ }
479491
480492 if (are_log_weights ) {
481493 # shift log values for safe exponentiation
482494 x <- x - max(x )
483495 }
484496
485497 tail <- match.arg(tail )
486-
487- ndraws <- length(x )
488- tail_ids <- seq(ndraws - ndraws_tail + 1 , ndraws )
489-
490498 if (tail == " left" ) {
491499 x <- - x
492500 }
493501
502+ ndraws <- length(x )
503+ tail_ids <- seq(ndraws - ndraws_tail + 1 , ndraws )
504+
494505 ord <- sort.int(x , index.return = TRUE )
495506 draws_tail <- ord $ x [tail_ids ]
496507
497508 if (is_constant(draws_tail )) {
498-
499509 if (tail == " left" ) {
500510 x <- - x
501511 }
502-
503- out <- list (x = x , k = NA )
504- return (out )
512+ return (list (x = x , k = NA ))
505513 }
506514
507515 cutoff <- ord $ x [min(tail_ids ) - 1 ] # largest value smaller than tail values
@@ -511,43 +519,22 @@ ps_tail <- function(x,
511519 }
512520
513521 max_tail <- max(draws_tail )
514- min_tail <- min(draws_tail )
515-
516- if (ndraws_tail > = 5 ) {
517- ord <- sort.int(x , index.return = TRUE )
518- if (abs(max_tail - min_tail ) < .Machine $ double.eps / 100 ) {
519- warning_no_call(
520- " Can't fit generalized Pareto distribution " ,
521- " because all tail values are the same."
522- )
523- smoothed <- NULL
524- k <- NA
525- } else {
526- # save time not sorting since x already sorted
527- if (are_log_weights ) {
528- draws_tail <- exp(draws_tail )
529- cutoff <- exp(cutoff )
530- }
531- fit <- gpdfit(draws_tail - cutoff , sort_x = FALSE , ... )
532- k <- fit $ k
533- sigma <- fit $ sigma
534- if (is.finite(k ) && smooth_draws ) {
535- p <- (seq_len(ndraws_tail ) - 0.5 ) / ndraws_tail
536- smoothed <- qgeneralized_pareto(p = p , mu = cutoff , k = k , sigma = sigma )
537- if (are_log_weights ) {
538- smoothed <- log(smoothed )
539- }
540- } else {
541- smoothed <- NULL
542- }
522+
523+ if (are_log_weights ) {
524+ draws_tail <- exp(draws_tail )
525+ cutoff <- exp(cutoff )
526+ }
527+ fit <- gpdfit(draws_tail - cutoff , sort_x = FALSE , ... )
528+ k <- fit $ k
529+ sigma <- fit $ sigma
530+ if (is.finite(k ) && smooth_draws ) {
531+ p <- (seq_len(ndraws_tail ) - 0.5 ) / ndraws_tail
532+ smoothed <- qgeneralized_pareto(p = p , mu = cutoff , k = k , sigma = sigma )
533+ if (are_log_weights ) {
534+ smoothed <- log(smoothed )
543535 }
544536 } else {
545- warning_no_call(
546- " Can't fit generalized Pareto distribution " ,
547- " because ndraws_tail is less than 5."
548- )
549537 smoothed <- NULL
550- k <- NA
551538 }
552539
553540 # truncate at max of raw draws
@@ -598,7 +585,10 @@ ps_tail <- function(x,
598585# ' @return minimum sample size
599586# ' @export
600587ps_min_ss <- function (k , ... ) {
601- if (k < 1 ) {
588+ if (is.na(k )) {
589+ return (NA )
590+ }
591+ if (isTRUE(k < 1 )) {
602592 out <- 10 ^ (1 / (1 - max(0 , k )))
603593 } else {
604594 out <- Inf
0 commit comments