fk_sum | R Documentation |
Computes exact (and binned approximations of) kernel and kernel derivative sums with arbitrary weights/coefficients. Computation is based on the method of Hofmeyr (2021).
fk_sum(x, omega, h, x_eval = NULL, beta = c(.25,.25),
nbin = NULL, type = "ksum")
x |
numeric vector of sample points. |
omega |
numeric vector of weights. |
h |
numeric bandwidth (must be strictly positive). |
x_eval |
vector of evaluation points. Default is to evaluate at the sample points themselves. |
beta |
numeric vector of kernel coefficients. Default is c(.25, .25); the smooth order 1 kernel. |
nbin |
integer number of bins for binning approximation. Default is to compute the exact sums. |
type |
one of "ksum": returns the kernel sums, "dksum": returns the kernel derivative sums and "both": returns a matrix cbind(ksum, dksum). |
A vector (if type%in%c("ksum","dksum")) of kernel sums, or kernel derivative sums. A matrix (if type == "both") with kernel sums in its first column and kernel derivative sums in its second column.
Hofmeyr, D.P. (2021) "Fast exact evaluation of univariate kernel sums", IEEE Transactions on Pattern Analysis and Machine Intelligence, 43(2), 447-458.
### Compute density estimates directly with
### kernel sums and constant normalising weights
set.seed(1)
n <- 150000
num_Gauss <- rbinom(1, n, 2 / 3)
x <- c(rnorm(num_Gauss), rexp(n - num_Gauss) + 1)
hs <- seq(.025, .1, length = 5)
xeval <- seq(-4, 8, length = 1000)
ftrue <- 2 / 3 * dnorm(xeval) + 1 / 3 * dexp(xeval - 1)
plot(xeval, ftrue, lwd = 6, col = rgb(.8, .8, .8), xlab = "x",
ylab = "f(x)", type = "l")
for(i in 1:5) lines(xeval, fk_sum(x, rep(1 / hs[i] / n, n), hs[i],
x_eval = xeval), lty = i)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.