Skip to content

Commit ccb9893

Browse files
committed
Exported gpdfit, removed qgpd
1 parent ac74d1e commit ccb9893

4 files changed

Lines changed: 83 additions & 8 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export(example_loglik_array)
106106
export(example_loglik_matrix)
107107
export(extract_log_lik)
108108
export(find_model_names)
109+
export(gpdfit)
109110
export(is.kfold)
110111
export(is.loo)
111112
export(is.psis)

R/gpdfit.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#' Estimate parameters of the Generalized Pareto distribution
2+
#'
3+
#' Given a sample \eqn{x}, Estimate the parameters \eqn{k} and \eqn{\sigma} of
4+
#' the generalized Pareto distribution (GPD), assuming the location parameter is
5+
#' 0. By default the fit uses a prior for \eqn{k}, which will stabilize
6+
#' estimates for very small sample sizes (and low effective sample sizes in the
7+
#' case of MCMC samples). The weakly informative prior is a Gaussian prior
8+
#' centered at 0.5.
9+
#'
10+
#' @export
11+
#' @param x A numeric vector. The sample from which to estimate the parameters.
12+
#' @param wip Logical indicating whether to adjust \eqn{k} based on a weakly
13+
#' informative Gaussian prior centered on 0.5. Defaults to `TRUE`.
14+
#' @param min_grid_pts The minimum number of grid points used in the fitting
15+
#' algorithm. The actual number used is `min_grid_pts + floor(sqrt(length(x)))`.
16+
#' @param sort_x If `TRUE` (the default), the first step in the fitting
17+
#' algorithm is to sort the elements of `x`. If `x` is already
18+
#' sorted in ascending order then `sort_x` can be set to `FALSE` to
19+
#' skip the initial sorting step.
20+
#' @return A named list with components `k` and `sigma`.
21+
#'
22+
#' @details Here the parameter \eqn{k} is the negative of \eqn{k} in Zhang &
23+
#' Stephens (2009).
24+
#'
25+
#' @seealso [psis()], [pareto-k-diagnostic]
26+
#'
27+
#' @references
28+
#' Zhang, J., and Stephens, M. A. (2009). A new and efficient estimation method
29+
#' for the generalized Pareto distribution. *Technometrics* **51**, 316-325.
30+
#'
31+
gpdfit <- function(x, wip = TRUE, min_grid_pts = 30, sort_x = TRUE) {
32+
posterior::gpdfit(
33+
x = x,
34+
wip = wip,
35+
min_grid_pts = min_grid_pts,
36+
sort_x = sort_x
37+
)
38+
}

man/gpdfit.Rd

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_gpdfit.R

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,3 @@ test_that("gpdfit returns correct result", {
1111
expect_snapshot_value(gpdfit_val_wip_default_grid, style = "serialize")
1212
})
1313

14-
test_that("qgpd returns the correct result ", {
15-
probs <- seq(from = 0, to = 1, by = 0.25)
16-
q1 <- qgpd(probs, k = 1, sigma = 1)
17-
expect_equal(q1, c(0, 1 / 3, 1, 3, Inf))
18-
19-
q2 <- qgpd(probs, k = 1, sigma = 0)
20-
expect_true(all(is.nan(q2)))
21-
})

0 commit comments

Comments
 (0)