Skip to content

Commit

Permalink
ENH: Return self from KDEUnivariate fit
Browse files Browse the repository at this point in the history
Return the fitted instance to simplify chaining
  • Loading branch information
bashtage committed Aug 25, 2020
1 parent 26d901a commit dd432e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions statsmodels/nonparametric/kde.py
Expand Up @@ -120,6 +120,11 @@ def fit(self, kernel="gau", bw="normal_reference", fft=True, weights=None,
-/+ cut*bw*{min(X) or max(X)}
adjust : float
An adjustment factor for the bw. Bandwidth becomes bw * adjust.
Returns
-------
KDEUnivariate
The instance fit,
"""
try:
bw = float(bw)
Expand Down Expand Up @@ -152,6 +157,7 @@ def fit(self, kernel="gau", bw="normal_reference", fft=True, weights=None,
if weights is not None:
self.kernel.weights /= weights.sum()
self._cache = {}
return self

@cache_readonly
def cdf(self):
Expand Down
7 changes: 7 additions & 0 deletions statsmodels/nonparametric/tests/test_kde.py
Expand Up @@ -341,3 +341,10 @@ def test_kde_bw_positive():
kde = KDE(x)
kde.fit()
assert kde.bw > 0


def test_fit_self(reset_randomstate):
x = np.random.standard_normal(100)
kde = KDE(x)
assert isinstance(kde, KDE)
assert isinstance(kde.fit(), KDE)

0 comments on commit dd432e8

Please sign in to comment.