Skip to content

Commit

Permalink
CLN: Fixes for future SciPY and pandas
Browse files Browse the repository at this point in the history
Silence new warnings produced by pandas 1.0+
Ensure starting values respect builds in HoltWinters
  • Loading branch information
bashtage committed Jan 15, 2020
1 parent 76da0ea commit c0763f6
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion statsmodels/base/data.py
Expand Up @@ -587,7 +587,7 @@ def attach_dates(self, result):
squeezed = result.squeeze()
k_endog = np.array(self.ynames, ndmin=1).shape[0]
if k_endog > 1 and squeezed.shape == (k_endog,):
squeezed = squeezed[None, :]
squeezed = np.asarray(squeezed)[None, :]
# May be zero-dim, for example in the case of forecast one step in tsa
if squeezed.ndim < 2:
return Series(squeezed, index=self.predict_dates)
Expand Down
5 changes: 3 additions & 2 deletions statsmodels/base/tests/test_generic_methods.py
Expand Up @@ -13,6 +13,7 @@
from statsmodels.compat.pandas import assert_series_equal, assert_index_equal
from statsmodels.compat.platform import (PLATFORM_OSX, PLATFORM_LINUX32,
PLATFORM_WIN32)
from statsmodels.compat.scipy import SCIPY_GT_14

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -114,7 +115,7 @@ def test_zero_constrained(self):
assert_allclose(tvals1, res2.tvalues, rtol=tol, atol=tol)

# See gh5993
if PLATFORM_LINUX32:
if PLATFORM_LINUX32 or SCIPY_GT_14:
pvals1 = res1.pvalues[keep_index_p]
else:
with pytest.warns(RuntimeWarning,
Expand Down Expand Up @@ -264,7 +265,7 @@ def test_zero_collinear(self):
assert_allclose(tvals1, res2.tvalues, rtol=5e-8)

# See gh5993
if PLATFORM_LINUX32:
if PLATFORM_LINUX32 or SCIPY_GT_14:
pvals1 = res1.pvalues[keep_index_p]
else:
with pytest.warns(RuntimeWarning,
Expand Down
1 change: 1 addition & 0 deletions statsmodels/compat/scipy.py
Expand Up @@ -6,6 +6,7 @@
SCIPY_11 = (LooseVersion(scipy.__version__) < LooseVersion('1.2.0') and
LooseVersion(scipy.__version__) >= LooseVersion('1.1.0'))

SCIPY_GT_14 = LooseVersion(scipy.__version__) >= LooseVersion('1.5')

def _next_regular(target):
"""
Expand Down
12 changes: 7 additions & 5 deletions statsmodels/graphics/regressionplots.py
Expand Up @@ -406,6 +406,8 @@ def plot_partregress(endog, exog_i, exog_others, data=None,
# all arrays or pandas-like

if RHS_isemtpy:
endog = np.asarray(endog)
exog_i = np.asarray(exog_i)
ax.plot(endog, exog_i, 'o', **kwargs)
fitted_line = OLS(endog, exog_i).fit()
x_axis_endog_name = 'x' if isinstance(exog_i, np.ndarray) else exog_i.name
Expand All @@ -428,11 +430,11 @@ def plot_partregress(endog, exog_i, exog_others, data=None,
ax.set_ylabel("e(%s | X)" % y_axis_endog_name)
ax.set_title('Partial Regression Plot', **title_kwargs)

#NOTE: if we want to get super fancy, we could annotate if a point is
#clicked using this widget
#http://stackoverflow.com/questions/4652439/
#is-there-a-matplotlib-equivalent-of-matlabs-datacursormode/
#4674445#4674445
# NOTE: if we want to get super fancy, we could annotate if a point is
# clicked using this widget
# http://stackoverflow.com/questions/4652439/
# is-there-a-matplotlib-equivalent-of-matlabs-datacursormode/
# 4674445#4674445
if obs_labels is True:
if data is not None:
obs_labels = data.index
Expand Down
4 changes: 2 additions & 2 deletions statsmodels/graphics/tests/test_regressionplots.py
Expand Up @@ -140,9 +140,9 @@ class TestPlotFormula(TestPlotPandas):
def test_one_column_exog(self, close_figures):
from statsmodels.formula.api import ols
res = ols("y~var1-1", data=self.data).fit()
fig = plot_regress_exog(res, "var1")
plot_regress_exog(res, "var1")
res = ols("y~var1", data=self.data).fit()
fig = plot_regress_exog(res, "var1")
plot_regress_exog(res, "var1")


class TestABLine(object):
Expand Down
2 changes: 1 addition & 1 deletion statsmodels/sandbox/predict_functional.py
Expand Up @@ -421,7 +421,7 @@ def _glm_basic_scr(result, exog, alpha):

# The variance and SD of the linear predictor at each row of exog.
sigma2 = (np.dot(exog, cov) * exog).sum(1)
sigma = np.sqrt(sigma2)
sigma = np.asarray(np.sqrt(sigma2))

# Calculate kappa_0 (formula 42 from Sun et al)
bz = np.linalg.solve(B.T, exog.T).T
Expand Down
19 changes: 14 additions & 5 deletions statsmodels/tsa/holtwinters.py
Expand Up @@ -680,12 +680,13 @@ def fit(self, smoothing_level=None, smoothing_slope=None, smoothing_seasonal=Non
# using guesstimates for the levels
txi = xi & np.array([True, True, True, False, False, True] + [False] * m)
txi = txi.astype(np.bool)
bounds = np.array([(0.0, 1.0), (0.0, 1.0), (0.0, 1.0),
(0.0, None), (0.0, None), (0.0, 1.0)] + [(None, None), ] * m)
bounds = ([(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, None),
(0.0, None), (0.0, 1.0)] + [(None, None), ] * m)
args = (txi.astype(np.uint8), p, y, lvls, b, s, m, self.nobs,
max_seen)
if start_params is None and np.any(txi) and use_brute:
res = brute(func, bounds[txi], args, Ns=20,
_bounds = [b for b, flag in zip(bounds, txi) if flag]
res = brute(func, _bounds, args, Ns=20,
full_output=True, finish=None)
p[txi], max_seen, _, _ = res
else:
Expand All @@ -708,14 +709,22 @@ def fit(self, smoothing_level=None, smoothing_slope=None, smoothing_seasonal=Non
# Take a deeper look in the local minimum we are in to find the best
# solution to parameters, maybe hop around to try escape the local
# minimum we may be in.
_bounds = [b for b, flag in zip(bounds, xi) if flag]
res = basinhopping(func, p[xi],
minimizer_kwargs={'args': args, 'bounds': bounds[xi]},
minimizer_kwargs={'args': args, 'bounds': _bounds},
stepsize=0.01)
success = res.lowest_optimization_result.success
else:
# Take a deeper look in the local minimum we are in to find the best
# solution to parameters
res = minimize(func, p[xi], args=args, bounds=bounds[xi])
_bounds = [b for b, flag in zip(bounds, xi) if flag]
lb, ub = np.asarray(_bounds).T.astype(np.float)
initial_p = p[xi]
loc = p[xi] < lb
initial_p[loc] = lb[loc]
loc = p[xi] > ub
initial_p[loc] = ub[loc]
res = minimize(func, p[xi], args=args, bounds=_bounds)
success = res.success

if not success:
Expand Down
2 changes: 0 additions & 2 deletions statsmodels/tsa/tests/test_holtwinters.py
Expand Up @@ -323,8 +323,6 @@ def test_holt_damp_R(self):
447.2614880558126]
assert_allclose(fit.forecast(10), desired, atol=1e-4)



def test_hw_seasonal(self):
fit1 = ExponentialSmoothing(self.aust, seasonal_periods=4,
trend='additive',
Expand Down

0 comments on commit c0763f6

Please sign in to comment.