Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: kdensity with user defined kernel functions

From   "Ben Jann" <>
To   <>
Subject   st: RE: kdensity with user defined kernel functions
Date   Fri, 3 Feb 2006 23:31:26 +0100

The kernel functions used by -kdens- (see -ssc describe kdens-) 
are provided by Mata functions such as -kernel_biweight()- or 
-kernel_epan2()-. See

 . viewsource kernels.mata

for examples (after -kdens- has been installed). It would be 
relatively easy to define one's own Mata kernel functions. 
However, changes to the code of -kdens- would be required to be 
able to use them because the list of available kernel functions 
is hard-coded into -kdens-.

However, there is a backdoor (use with care): Simply re-define 
an existing kernel function. For example, the epan2 kernel is 
the standard kernel used by -kdens-. We could change epan2 to be 
the rectangular kernel as follows:

// the kernel
real matrix kernel_epan2(real matrix x)
// kernel "roughness"
real matrix kernel_I_epan2(| real scalar r, real matrix x)
	if (r==2 & args()==1) return(.5) 
	else _error("kernel integral not available")
// canonical bandwidth of kernel
real scalar kernel_del0_epan2() return((9/2)^.2)

sysuse auto
kdens price, ci

Note that actually three functions have been defined. This is 
because -kdens- also requires information on the kernel 
"roughness" and the canonical bandwidth of the kernel. The 
roughness is needed for approximate variance estimation and the 
canonical bandwidth is used for the automatic bandwidth scaling.
[Furthermore note that -kernel_I_...()- usually contains much 
more stuff required for kernel density estimation for bounded 

Let me know if you need help.


> -----Original Message-----
> From: [mailto:owner-
>] On Behalf Of D'Exelle Ben
> Sent: Friday, February 03, 2006 12:41 PM
> To:
> Subject: st: kdensity with user defined kernel functions
> Hi all,
> I need to make a kernel density estimation with a kernel function that
> not supported by the kdensity command. Is there a way to define your
> specific kernel function within the kdensity command? If this is not
> possible within the kdensity command, is there another way to make
> estimations with user-defined kernel functions?
> Thank you for any reactions,
> Ben D'Exelle
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index