| find_extremum | R Documentation |
Finds the global maximum or minimum of a fitted lgspline using L-BFGS-B, with options for partition-based heuristics, stochastic exploration, and custom objective functions (e.g., acquisition functions for Bayesian optimization).
find_extremum(
object,
vars = NULL,
quick_heuristic = TRUE,
initial = NULL,
B_predict = NULL,
minimize = FALSE,
stochastic = FALSE,
stochastic_draw = function(mu, sigma, ...) {
N <- length(mu)
rnorm(N, mu,
sigma)
},
sigmasq_predict = object$sigmasq_tilde,
custom_objective_function = NULL,
custom_objective_derivative = NULL,
...
)
object |
A fitted lgspline model object. |
vars |
Integer or character vector; indices or names of predictors to optimize over. Default NULL optimizes all predictors. |
quick_heuristic |
Logical; if TRUE (default) searches only the best-performing partition. If FALSE, initiates searches from all partition local maxima. |
initial |
Numeric vector; optional starting values. Useful for fixing binary predictors. Default NULL. |
B_predict |
List; optional coefficient list for prediction, e.g.
from |
minimize |
Logical; find minimum instead of maximum. Default FALSE. |
stochastic |
Logical; add noise during optimization for exploration. Default FALSE. |
stochastic_draw |
Function; generates noise for stochastic
optimization. Takes |
sigmasq_predict |
Numeric; variance for stochastic draws.
Default |
custom_objective_function |
Function; optional custom objective.
Takes |
custom_objective_derivative |
Function; optional gradient of
|
... |
Additional arguments passed to internal optimization routines. |
A list with elements:
Numeric vector; predictor values at the extremum.
Numeric; objective value at the extremum.
lgspline, generate_posterior
set.seed(1234)
t <- runif(1000, -10, 10)
y <- 2*sin(t) + -0.06*t^2 + rnorm(length(t))
model_fit <- lgspline(t, y)
plot(model_fit)
max_point <- find_extremum(model_fit)
min_point <- find_extremum(model_fit, minimize = TRUE)
abline(v = max_point$t, col = 'blue')
abline(v = min_point$t, col = 'red')
## Expected improvement acquisition function
ei_obj <- function(mu, sigma, y_best, ...) {
d <- y_best - mu
d * pnorm(d/sigma) + sigma * dnorm(d/sigma)
}
ei_deriv <- function(mu, sigma, y_best, d_mu, ...) {
d <- y_best - mu
z <- d/sigma
d_z <- -d_mu/sigma
pnorm(z)*d_mu - d*dnorm(z)*d_z + sigma*z*dnorm(z)*d_z
}
post_draw <- generate_posterior(model_fit)
acq <- find_extremum(model_fit,
stochastic = TRUE,
B_predict = post_draw$post_draw_coefficients,
sigmasq_predict = post_draw$post_draw_sigmasq,
custom_objective_function = ei_obj,
custom_objective_derivative = ei_deriv)
abline(v = acq$t, col = 'green')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.