1
votes

I am using this form of the ksdensity function in MATLAB.

[f,xi] = ksdensity(x)

The documentation says that "f is the vector of density values evaluated at the points in xi ... The density is evaluated at 100 equally spaced points that cover the range of the data in x. "

Now, my xi values cover a much larger range than the data in x. Why is this?

For my data,

>> min(x)

ans =

   -2.2588

>> min(xi)

ans =

   -6.8010
>> max(x)

ans =

    6.5326

>> max(xi)

ans =

   11.0748

I know I can specify an xi range myself, but why is it not equally spaced between min and max of x by default?

It makes it hard to compare histogram estimators and kernel estimators when the bins in the histogram only cover the range of x, whereas the test points given from ksdensity exceed this range.

1

1 Answers

1
votes

ksdensity performs a smoothing of the histogram with a Gaussian kernel. As an illustration, check the output of the impulse response:

[f,xi] = ksdensity(0);
plot(xi,f)

enter image description here

The width of the Gaussian is 1, which means that you'll add about 3 on both sides of the data (which can make it appear as if there were negative values in a strictly positive histogram). Thus, if your data span a small range only, you need to shrink the kernel width in ksdensity.