Suppose that we have the vector $x = (11,10,5,14,10,10,16,9,13,10)$ we wish to adjust a kernel density $f$ to f where the Kernel(K) is a uniform(a,b) density.

I understand that we can write $f(x) = \frac{1}{nh}\sum_{i=1}^n K(\frac{x-x_i}{h})$ where the Kernel is a uniform(a,b) density. However I do not know how to proceed afterwards, I do not know how to construct the grid from where x comes from and where the $h$ is obtained from.

P.S. (I have done it in R though i wish to understand what R is doing)


1 Answer 1


For any fixed bandwidth $h$ the $x_i$ values come from the vector $x$ (i.e. $x_1=11$ and $x_2=10$). Typically the uniform kernel has values $a=-1$ and $b=1$, hence, the uniform density is 1/2 for $x\in[-1, 1]$ and 0 otherwise. As a consequence the uniform kernel averages values in a window of width $2h$. The value of $h$ controls how 'smooth' the density estimate is: the smaller the value the less smooth (overfitting) the larger the value the more smooth (underfitting). Since you do not want to over or under fit the data you need to select $h$ carefully. There are various heuristics or rules-of-thumb used to select the bandwidth. The default in the density() function in R is called the nrd0 rule. Some of the details of this rule can be found in the help page for the function.

To visualise how the density is constructed consider the following image. Each dash on the x-axis corresponds to a value $x_i$ in the vector $x$. For each value there is a Gaussian kernel with bandwidth $h$ centred at $x_i$. In your case, there there is a uniform kernel with bandwidth $h$ centred at each $x_i$. For any value $x$, the density $f(x)$ is obtained by averaging the Gaussian kernels evaluated at that point.

enter image description here

  • $\begingroup$ How do we propose the x? Is it correct to assume that any uniform works, even when the expectation is not necesarily zero for all uniforms? $\endgroup$ Commented Jul 3, 2020 at 0:45
  • $\begingroup$ I am not sure what you mean by propose? Is the question: how do you simulate an x from this density? Regarding the kernel, they are typically symmetric with zero mean so that they average points equally from both sides of each $x_i$. $\endgroup$
    – mtedwards
    Commented Jul 3, 2020 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.