Cross-Validation for Bandwidth Selection
cv_bandwidth.RdImplements least-squares cross-validation for bandwidth selection with any kernel function. Uses FFT-based algorithm for n >= 100 (fast, O(m log m) complexity) and exact computation for n < 100 (accurate). The FFT method bins data onto a regular grid and computes the LSCV objective via convolution in the frequency domain.
Arguments
- X
A numerical vector of sample data.
- h_grid
A numerical vector of bandwidth values to evaluate. If NULL (default), a grid is automatically generated based on the range and distribution of the data.
- kernel_func
The kernel function to use for cross-validation.
- kernel_type
A string identifying the kernel type, used only for reference bandwidth.
- grid_size
Number of grid points for FFT-based evaluation (used when n >= 100). Default is 512. Larger values increase accuracy but reduce speed. Automatically rounded up to the next power of 2.
Examples
# Generate sample data
X <- rnorm(100)
# Get optimal bandwidth using cross-validation with a normal kernel
kernel_functions <- create_kernel_functions("normal")
h_opt <- cv_bandwidth(X, kernel_func = kernel_functions$kernel,
kernel_type = kernel_functions$kernel_type)