Gabriel
Gabriel

Reputation: 42439

Bad keyword in scipy.stats.gaussian_kde?

The documentation on scipy.stats.gaussian_kde says that the keyword bw_method should be used to try different methods but when I try to use it I get an error:

TypeError: __init__() got an unexpected keyword argument 'bw_method'

I'm running version 0.9.0 of Scipy, here's a MWE:

import numpy as np
from scipy import stats
import scipy

print scipy.__version__

## Generate some random two-dimensional data:
def measure(n):
    m1 = np.random.normal(size=n)
    m2 = np.random.normal(scale=0.5, size=n)
    return m1+m2, m1-m2

m1, m2 = measure(2000)
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()

# Perform a kernel density estimate on the data:
x, y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([x.ravel(), y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values, bw_method='silverman')

Upvotes: 1

Views: 1382

Answers (2)

unutbu
unutbu

Reputation: 880547

Here is a workaround for scipy versions (before 0.12.0?) that do not have the bw_method keyword:

density = kde.gaussian_kde(data)
density.covariance_factor = density.silverman_factor
density._compute_covariance()

Inspiration for this workaround comes from looking at the source.


This shows that the workaround above produces the same result as kde.gaussian_kde(data, bw_method='silverman'):

import numpy as np
import scipy.stats as stats

data = ([1.5] * 7 + [2.5] * 2 + [3.5] * 8
        + [4.5] * 3 + [5.5] * 1 + [6.5] * 8)
x = np.linspace(0., 8, 100)

density = stats.gaussian_kde(data)
density.covariance_factor = density.silverman_factor
density._compute_covariance()
workaround = density(x)

density = stats.gaussian_kde(data, bw_method='silverman')
answer = density(x)

assert np.allclose(workaround, answer)

The bw_method keyword was added some time after version 0.9.0:

>>> import scipy
>>> scipy.__version__
'0.9.0'
>>> import scipy.stats.kde as kde
>>> density = kde.gaussian_kde(data, bw_method='silverman')
TypeError: __init__() got an unexpected keyword argument 'bw_method'

Upvotes: 1

Ryan G
Ryan G

Reputation: 9570

I haven't tested the result, but from the docs it appears that there's a method called silverman_factor(self):

import numpy as np
from scipy import stats
import scipy

print scipy.__version__

## Generate some random two-dimensional data:
def measure(n):
    m1 = np.random.normal(size=n)
    m2 = np.random.normal(scale=0.5, size=n)
    return m1+m2, m1-m2

m1, m2 = measure(2000)
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()

# Perform a kernel density estimate on the data:
x, y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([x.ravel(), y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values)
kernel.silverman_factor()

Upvotes: 1

Related Questions