Skip to content

Commit

Permalink
Merge 8668fa8 into 3b7082c
Browse files Browse the repository at this point in the history
  • Loading branch information
Padarn committed Oct 6, 2013
2 parents 3b7082c + 8668fa8 commit c3b0fc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions statsmodels/nonparametric/kde.py
Expand Up @@ -150,6 +150,7 @@ def fit(self, kernel="gau", bw="scott", fft=True, weights=None,
self.kernel = kernel_switch[kernel](h=bw) # we instantiate twice,
# should this passed to funcs?
# put here to ensure empty cache after re-fit with new options
self.kernel.weights = weights
self._cache = resettable_cache()

@cache_readonly
Expand Down
13 changes: 11 additions & 2 deletions statsmodels/sandbox/nonparametric/kernels.py
Expand Up @@ -48,6 +48,7 @@ def __init__(self, n, kernels = None, H = None):
kernels = Gaussian()

self._kernels = kernels
self.weights = None

if H is None:
H = np.matrix( np.identity(n))
Expand All @@ -66,13 +67,17 @@ def setH(self, value):
H = property(getH, setH, doc="Kernel bandwidth matrix")

def density(self, xs, x):

n = len(xs)
#xs = self.inDomain( xs, xs, x )[0]

if len(xs)>0: ## Need to do product of marginal distributions
#w = np.sum([self(self._Hrootinv * (xx-x).T ) for xx in xs])/n
#vectorized doesn't work:
w = np.mean(self((xs-x) * self._Hrootinv )) #transposed
if self.weights is not None:
w = np.sum(self((xs-x)/h).T * self_Hrootinv * self.weights, axis=1)
else:
w = np.mean(self((xs-x) * self._Hrootinv )) #transposed
#w = np.mean([self(xd) for xd in ((xs-x) * self._Hrootinv)] ) #transposed
return w
else:
Expand Down Expand Up @@ -137,6 +142,7 @@ def __init__(self, shape, h = 1.0, domain = None, norm = None):
norm = 1.0
self._normconst = norm
self.domain = domain
self.weights = None
if callable(shape):
self._shape = shape
else:
Expand Down Expand Up @@ -185,7 +191,10 @@ def density(self, xs, x):
xs = xs[:,None]
if len(xs)>0:
h = self.h
w = 1/h * np.mean(self((xs-x)/h), axis=0)
if self.weights is not None:
w = 1/h * np.sum(self((xs-x)/h).T * self.weights, axis=1)
else:
w = 1/h * np.mean(self((xs-x)/h), axis=0)
return w
else:
return np.nan
Expand Down

0 comments on commit c3b0fc2

Please sign in to comment.