-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtune_inv_gamma.R
44 lines (42 loc) · 1.36 KB
/
tune_inv_gamma.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
#' Tune an Inverse Gamma to Achieve the Target Truncation
#'
#' @description `r lifecycle::badge("questioning")`
#' Allows an inverse gamma distribution to be. tuned so that less than 0.01 of its
#' probability mass function falls outside of the specified
#' bounds. This is required when using an inverse gamma prior, for example for a
#' Gaussian process. As no inverse gamma priors are currently in use and this function
#' has some stability issues it may be deprecated at a later date.
#' @param lower Numeric, defaults to 2. Lower truncation bound.
#' @param upper Numeric, defaults to 21. Upper truncation bound.
#'
#' @return A list of alpha and beta values that describe a inverse gamma
#' distribution that achieves the target truncation.
#' @export
#'
#' @examples
#'
#' tune_inv_gamma(lower = 2, upper = 21)
tune_inv_gamma <- function(lower = 2, upper = 21, model = NULL) {
if (!is.null(model)) {
model <- i2p_gp_tune_model()
}
# optimise for correct upper and lower probabilites
fit <- rstan::sampling(model,
data = list(
u = upper,
l = lower
),
iter = 1,
warmup = 0,
chains = 1,
algorithm = "Fixed_param",
refresh = 0
)
alpha <- rstan::extract(fit, "alpha")
beta <- rstan::extract(fit, "beta")
out <- list(
alpha = round(unlist(unname(alpha)), 1),
beta = round(unlist(unname(beta)), 1)
)
return(out)
}