Similarly to implemented probability distributions, implemented kernels all inherit from a single class ‘kernel’, which in turn inherits from Distribution
(see the uml diagram). But this is a much simpler class with reduced methods and many of these return the same result and are therefore defined in the kernel
parent class. For every kernel the following is true
kernel$mean()
== 0kernel$median()
== 0kernel$mode()
== 0kernel$type()
== Reals$new()kernel$valueSupport()
== continuouskernel$variateForm()
== univariateAnd for all kernels the sampling method uses inverse transform sampling, so this is also defined in the parent class. Hence the only methods required to add are
And then the d/p/q functions are given in the constructor just like probability distributions.
These are identical to the SDistribution public variables:
For the Epanechnikov kernel, the above all looks like
Epanechnikov <- R6::R6Class("Epanechnikov", inherit = Kernel, lock_objects = F) Epanechnikov$set("public","name","Epanechnikov") Epanechnikov$set("public","short_name","Epan") Epanechnikov$set("public","description","Epanechnikov Kernel")
Note:
lock_objects = F
which ensures correct usage with decorators.package
variable is omitted as it defaults to ‘distr6’As stated above, there are fewer methods that need to be implemented in kernels than probability distributions. These include
Again the d/p/q methods are implemented in the constructor. So for the Epanechnikov kernel,
Epanechnikov$set("public","squared2Norm",function(){ return(3/5) }) Epanechnikov$set("public","variance",function(){ return(1/5) })
Note:
The constructor for kernels is much more simple than that of probability distributions and only need include the d/p/q methods and properties. For the Epanechnikov kernel:
Epanechnikov$set("public","initialize",function(decorators = NULL){ pdf <- function(x1){ return(0.75 * (1-x1^2)) } cdf <- function(x1){ return(3/4*x1 - 1/4*x1^3 + 1/2) } super$initialize(decorators = decorators, pdf = pdf, cdf = cdf, support = Interval$new(-1, 1), symmetric = TRUE) invisible(self) })
Note:
Reals$new()
or Interval$new(-1,1)
decorators
which is passed to the parent-class constructorKernels are much simpler to extend than SDistributions. Just remember the following
decorators
, and the properties are generally identical