# [SciPy-user] KDE question

David Cournapeau david@ar.media.kyoto-u.ac...
Sat Nov 10 01:53:06 CST 2007

```Robert Kern wrote:
> Zachary Pincus wrote:
>> Hello all,
>>
>> I'm using scipy.stats.gaussian_kde to estimate the a probability
>> density based on some scattered 2D samples. What I would like to do
>> eventually is plot a line that surrounds (say) 90% of the probability
>> density.
>>
>> Clearly this line corresponds to some contour of the density function
>> (which I can extract out easily enough from KDE density estimates on
>> a grid), but I'm not sure how to figure out what exact contour value
>> it is that I want.
>>
>> Given the KDE density estimates on a grid, I could empirically figure
>> out which value is closest, but I was wondering if there's a simpler
>> closed-form solution... (And apologies if it's somehow obvious; I'm
>
> I don't think there is a closed-form solution for n>1-D. It shouldn't be too
> hard to evaluate the KDE on a grid, then search for the value between 0 and the
> peak where the integral is around 90%.
I am not sure I understand exactly the problem, but if the problem is to
find a contour level of a Gaussian density, it has a closed form for any
dimension. For the actual code, you can look at
scikits.learn.machine.em, function gauss_ell, which is used by my Em
toolbox for gaussian mixtures

http://projects.scipy.org/scipy/scikits/browser/trunk/learn/scikits/learn/machine/em/densities.py

The function can be used without any other code, I think (at least, I
remember I wanted it that way:) ). This is based on the fact that if X ~
N(mu, sigma), then (X-mu)' * sigma^-1 * (X-mu) is a one dimensional
random variable following a chi square.

cheers,

David
```