find_extremum: Find the Extremum of a Fitted lgspline

View source: R/methods.R

find_extremumR Documentation

Find the Extremum of a Fitted lgspline

Description

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).

Usage

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,
  ...
)

Arguments

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 generate_posterior. Default NULL uses object$B.

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 mu, sigma, and .... Default rnorm(length(mu), mu, sigma).

sigmasq_predict

Numeric; variance for stochastic draws. Default object$sigmasq_tilde.

custom_objective_function

Function; optional custom objective. Takes mu, sigma, y_best, .... Default NULL.

custom_objective_derivative

Function; optional gradient of custom_objective_function. Takes mu, sigma, y_best, d_mu, .... Default NULL.

...

Additional arguments passed to internal optimization routines.

Value

A list with elements:

t

Numeric vector; predictor values at the extremum.

y

Numeric; objective value at the extremum.

See Also

lgspline, generate_posterior

Examples


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')


lgspline documentation built on May 8, 2026, 5:07 p.m.