kdensity_grid: Evaluate a kdensity fit on a fast grid

View source: R/grid.R

kdensity_gridR Documentation

Evaluate a kdensity fit on a fast grid

Description

kdensity_grid() evaluates a fitted kdensity() object on a grid. The "fft" method uses weighted linear binning and an FFT convolution with the fitted kernel, the algorithm of stats::density(), for every built-in symmetric kernel. It also supports the Gaussian-copula kernel through its exact Gaussian-convolution representation on the probit scale.

Usage

kdensity_grid(
  object,
  from = NULL,
  to = NULL,
  n = 512L,
  method = c("auto", "exact", "fft"),
  fft_n = NULL,
  truncate = 8
)

Arguments

object

A fitted kdensity object.

from, to

Finite endpoints of the evaluation interval. By default, get_range() supplies a plotting interval.

n

Number of returned grid points.

method

One of "auto", "exact", or "fft". "auto" uses the FFT backend for Gaussian and Gaussian-copula kernels and otherwise uses the exact evaluator. Compact kernels retain their faster exact sorted evaluator. "fft" explicitly requests FFT evaluation and reports an error when the fitted kernel is unsupported.

fft_n

Size of the internal convolution grid. The default is at least four times n and at least 2048 points.

truncate

Number of Gaussian standard deviations used to pad an FFT convolution. The Laplace kernel is padded by truncate^2 / 2 bandwidths, where its tail is as small as the Gaussian tail at truncate standard deviations. Compact kernels use their exact support instead.

Details

The FFT method approximates only the binning and interpolation steps. The fitted parametric start and the normalization constant from object are retained. Calling the kdensity object itself always uses exact evaluation.

Value

An object of class "density". Its method element records whether exact or FFT evaluation was used.

See Also

kdensity()

Examples

set.seed(1)
fit <- kdensity(rbeta(1000, 2, 5), kernel = "gcopula", bw = 0.12)
fast <- kdensity_grid(fit, n = 512)
plot(fast)

kdensity documentation built on Oct. 28, 2026, 5:06 p.m.