R/internal-spselection.R

Defines functions duplicate_vector ML.opt GCV.opt smoothpar_selection

################################################################################
# ---------------------- SMOOTHING PARAMETER SELECTION ----------------------- #
################################################################################

#' Smoothing Parameter Selection
#'
#' @param y       vector with binned, variance stabilized and mirrored data
#' @param q       penalization order
#' @param evals   eigenvalues of the \code{q}-th order roughness penalty opera-
#'                tor in the Demmler-Reinsch basis.
#' @param method  to select the smoothing parameter of the smoothing spline
#' @param f.true  N-dim. vector with the true regression function evaluated at
#'                equi-spaced points. Only required if \code{method} is set
#'                to "\code{ML-oracle}" or "\code{GCV-oracle}".
#' @param sigma   variance of the regression data. Only required if
#'                \code{method} is set to "\code{ML-oracle}" or
#'                "\code{GCV-oracle}".
#'
#' @return lambda estimated smoothing parameter.
#'
#' @keywords internal
#' @noRd
smoothpar_selection <- function(y, q, evals, method, f.true=NULL, sigma=NULL) {
  
  switch (method,
          "GCV" = {
            lambda <- GCV.opt(y = y, q = q, evals = evals)
          },
          "GCV-oracle" = {
            if (is.null(f.true)){
              stop("True spectral density is missing.")
            } else {
              check_numeric_vector(f.true)
            }
            if (is.null(sigma)){
              stop("True variance is missing.")
            }
            f.true <- duplicate_vector(f.true)
            log.sdf.true <- VST(f.true, m=1)
            lambda <- GCV.opt(
              y = NULL, q = q,  evals = evals, 
              oracle = TRUE, f.true = log.sdf.true, sigma = sigma
            )
          },
          "ML" = {
            lambda <- ML.opt(y = y, q = q, evals = evals)
          },
          "ML-oracle" = {
            if (is.null(f.true)){
              stop("True spectral density is missing.")
            } else {
              check_numeric_vector(f.true)
            }
            if (is.null(sigma)){
              stop("True variance is missing.")
            }
            f.true <- duplicate_vector(f.true)
            log.sdf.true <- VST(f.true, m=1)
            lambda <- ML.opt(
              y = NULL, q = q, evals = evals, 
              oracle = TRUE, f.true = log.sdf.true, sigma = sigma
            )
          },
          stop("Unknown method.")
  )
  
  return(lambda)
}

#' Generalized Cross-Validation
#'
#' @param y       \code{N}-dim. vector with regression data points
#' @param q       penalization order
#' @param evals   eigenvalues of the \code{q}-th order roughness penalty opera-
#'                tor in the Demmler-Reinsch basis.
#' @param oracle  logical. If \code{TRUE}, then the \code{GCV-oracle} smoothing
#'                parameter is calculated.
#' @param f.true  \code{N}-dim. vector with the true regression function
#'                evaluated at equi-spaced points. Only required if
#'                \code{oracle=TRUE}.
#' @param sigma   variance of the regression data.
#'
#' @return lambda estimated smoothing parameter.
#'
#' @keywords internal
#' @noRd
GCV.opt <- function(y, q, evals, oracle = FALSE, f.true = NULL, sigma = NULL){
  
  if (!is.null(y)) {
    N_y <- length(y)
    fft_y_const <- stats::fft(y[N_y:1])[N_y:1]
  }
  
  GCV <- function(h,y,q) {
    N <- length(y)
    lam_w <- 1 / (1 + h^(2*q) * evals)
    term1 <- fft_y_const * (1 - lam_w)^2
    term1 <- c(y%*%Re(stats::fft(term1[N:1])[N:1])/N)
    trace <- sum(lam_w)
    score <- term1/(N*(1-trace/N)^2)
    return(score)
  }
  
  GCV.oracle <- function(h,f.true,q,sigma) {
    N <- length(f.true)
    lam_w <- 1 / (1 + h^(2*q) * evals)
    norm.term <- stats::fft(f.true[N:1])[N:1] * (1 - lam_w)
    norm.term <- Re(stats::fft(norm.term[N:1])[N:1])/N
    trace <- sum(lam_w^2)
    score <- (sum(norm.term^2) + trace * sigma) / N
    return(score)
  }
  
  if (oracle) {
    h <- stats::optimize(
      GCV.oracle,
      interval = c(.Machine$double.eps,1),
      f.true = f.true,
      q = q,
      sigma = sigma,
      tol = .Machine$double.eps
    )$minimum
  } else {
    h <- stats::optimize(
      GCV,
      interval = c(.Machine$double.eps, 1),
      y = y,
      q = q,
      tol = .Machine$double.eps
    )$minimum
  }
  
  lambda <- h^(2*q)
  
  return(lambda)
}

#' Maximum Likelihood method
#'
#' @param y       \code{N}-dim. vector with regression data points
#' @param q       penalization order
#' @param evals   eigenvalues of the \code{q}-th order roughness penalty opera-
#'                tor in the Demmler-Reinsch basis.
#' @param oracle  logical. If \code{TRUE}, then the \code{ML-oracle} smoothing
#'                parameter is calculated.
#' @param f.true  \code{N}-dim. vector with the true regression function
#'                evaluated at equi-spaced points. Only required if
#'                \code{oracle=TRUE}.
#' @param sigma   variance of the regression data.
#'
#' @return estimated smoothing parameter.
#'
#' @keywords internal
#' @noRd
ML.opt <- function(y, q, evals, oracle = FALSE, f.true = NULL, sigma = NULL){
  
  if (!is.null(y)) {
    N_y <- length(y)
    fft_y_const <- stats::fft(y[N_y:1])[N_y:1]
  }
  
  ML <- function(h, y, q) {
    N <- length(y)
    lam_w <- 1 / (1 + h^(2*q) * evals)
    term1 <- fft_y_const * (lam_w * (1 - lam_w))
    term1 <- c(y %*% Re(stats::fft(term1[N:1])[N:1]) / N)
    fac <- (sum(lam_w) - q) / (N - q)
    term2 <- fft_y_const * (1 - lam_w)
    term2 <- c(y %*% Re(stats::fft(term2[N:1])[N:1]) / N)
    score <- (term1 - term2 * fac) / N
    return(score)
  }
  
  ML.oracle <- function(h, q, f.true, sigma) {
    N <- length(f.true)
    lam_w <- 1 / (1 + h^(2*q) * evals)
    term1 <- stats::fft(f.true[N:1])[N:1] * (lam_w * (1 - lam_w))
    term1 <- c(f.true %*% Re(stats::fft(term1[N:1])[N:1]) / N)
    term2 <- sum(lam_w^2)
    score <- (term1 - sigma * (term2 - q)) / N
    return(score)
  }
  
  if (oracle) {
    h <- stats::uniroot(
      ML.oracle,
      interval = c(.Machine$double.eps,1),
      f.true = f.true,
      q = q,
      sigma = sigma,
      tol = .Machine$double.eps,
      extendInt = "yes"
    )$root
  } else {
    h <- stats::uniroot(
      ML,
      interval = c(.Machine$double.eps,1),
      y = y,
      q = q,
      tol = .Machine$double.eps,
      extendInt = "yes"
    )$root
  }
  
  lambda <- h^(2*q)
  
  return(lambda)
}

#' Duplicate vector
#'
#' @param x       \code{r}-dim. vector
#'
#' @return duplicated and mirrored vector.
#'
#' @keywords internal
#' @noRd
duplicate_vector <- function(x) {
  r <- length(x)
  return(c(x[2:(r-1)],x[r:1]))
}

Try the sdf.test package in your browser

Any scripts or data that you put into this service are public.

sdf.test documentation built on Dec. 2, 2026, 5:07 p.m.