KDE
Description
KDE returns the kernel density estimate of the probability density at a specific value . It is not a dependency for any other KDE-related function and its presence in the CLS-Stats library is strictly for legacy purposes. If you want to estimate the probability density of a population given a sample, use Density instead of this function.
Like every other KDE-related function in CLS-Stats, this function uses a Gaussian kernel function.
Syntax
KDE(x_i, data, [bw=1]) where
x_iis the point at which to evaluate the KDEdatais the univariate data sample (any shape)bwis the smoothing factor; the default valuebw=1uses the Silverman rule-of-thumb bandwidth
The function returns a single value, the sum of the kernel functions at for the entire sample. It estimates the probability density at that value.
Example
Pretty straightforward to call, but you are encouraged to use Density instead which returns the same density estimate as this function but over a range of values, returned (much more quickly) as a dynamic array.
Note that this function (as with Density) does not estimate the probability density well near a boundary.
Source Code
KDE = LAMBDA(
xi, // point at which to calculate the density
data, // data (any shape) whose probability density is being estimated
[bw], // a smoothing factor, relative to the "rule-of-thumb" bandwidth (default 1)
LET(
// convert tables & rows to a column
Xc, TOCOL(data),
// filter for numbers
Xn, FILTER(Xc,ISNUMBER(Xc)),
// default bandwidth scale factor is 1
bw, IF(ISOMITTED(bw), 1, bw),
n, COUNT(Xn),
// estimate of std dev
s, MIN(STDEV.S(Xn), IQR(Xn, TRUE)),
// calculate "rule-of-thumb" value
H, 1.059 * s * n ^ (-0.2),
// bandwidth adjustment factor
W, H * bw,
KDE, SUM(NORM.S.DIST((xi - Xn) / W, FALSE)),
KDE / (n * W)
)
);