11
$\begingroup$

I am trying to produce a better distribution from a dataset that is bounded to be greater than 0. Here is an example distribution from the documentation that mimics the behavior of the actual dataset:

data = RandomVariate[ExponentialDistribution[2], 10^4];
dist = SmoothKernelDistribution[data];
\[ScriptCapitalD] = TruncatedDistribution[{0, Infinity}, dist];
Plot[Evaluate@PDF[\[ScriptCapitalD], x], {x, -1, 5}, PlotRange -> All,
  Filling -> Axis]

enter image description here

As you can see, the peak of the SmoothKernelDistribution is not at 0, but at some value slightly greater than zero. The more points there are in the dataset, the closer the peak is to zero, as there will be a point in the dataset that is closer and closer to zero with more points. In the real dataset, I don't have the liberty of drawing more points, but I do know that the real dataset is restricted to be > 0.

Using the standard histogram, the peak will be 0 given a large enough bin size, which I guess can be achieved in SmoothKernelDistribution by increasing the kernel width, but as far as I can tell SmoothKernelDistribution does not automatically set this width to avoid the above behavior. My question is: How do I generate a SmoothKernelDistribution which mimics the behavior of the underlying distribution?

$\endgroup$

1 Answer 1

15
$\begingroup$

As of version 9 there is an undocumented extension to the kernel functions which allows you to bound the domain. You do this by specifying the kernel function as {"Bounded", c, ker} where c is the left boundary (0 in your case) and ker is the usual kernel function. You can also allow for both the left and right to be bounded via {"Bounded", {c1, c2}, ker}. Bounding only on the right can be done using c1 = -Infinity.

This works by reflecting part of the data about the boundaries and subsequently truncating the resulting estimate. As this is undocumented I make no promises that it won't someday change or that it is 100% tested.

data = RandomVariate[ExponentialDistribution[2], 10^4];
dist = SmoothKernelDistribution[data, Automatic, {"Bounded", 0, "Gaussian"}];
Plot[Evaluate@PDF[dist, x], {x, -1, 5}, PlotRange -> All,Filling -> Axis]

enter image description here

Edit:

To do something similar without M9 (though not in full generality) you can try reflecting the data about the y-axis and truncate yourself.

data = RandomVariate[ExponentialDistribution[2], 10^4];
pseudodata = Join[-data, data];
dist = TruncatedDistribution[{0, \[Infinity]}, 
   SmoothKernelDistribution[pseudodata]];
Plot[PDF[dist, x], {x, -1, 5}, PlotRange -> All, Filling -> Axis]

enter image description here

$\endgroup$
5
  • $\begingroup$ Oh, that's exactly what I wanted. Unfortunately, I don't have the cajones to jump to 9 yet, so if there was a way to accomplish this in 8, that would be great. $\endgroup$
    – Guillochon
    Commented Dec 2, 2012 at 0:22
  • $\begingroup$ The edit to your post is exactly what I needed, thanks! $\endgroup$
    – Guillochon
    Commented Dec 2, 2012 at 0:52
  • $\begingroup$ I'm glad. You might want to use the estimate on the original data to get a better automatic bandwidth (so it isn't based on the pseudo-data). This can be done by SmoothKernelDistribution[data]["Bandwidth"]. $\endgroup$
    – Andy Ross
    Commented Dec 2, 2012 at 0:54
  • $\begingroup$ Andy, I don't quite understand your last comment, since I've doubled the width of the distribution by reflecting it, I should use half the automatic bandwidth. SmoothKernelDistribution[data]["Bandwidth"] doesn't seem to work properly... Edit: Nevermind, I see that I need to run the SmoothKernelDistribution first on the original data, and then use the bandwidth from that. $\endgroup$
    – Guillochon
    Commented Dec 2, 2012 at 1:06
  • $\begingroup$ This feature is documented (and appears to work as described) in V11 $\endgroup$
    – mikado
    Commented Sep 3, 2016 at 12:13

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.