Skip to content

Commit

Permalink
Merge pull request #5805 from jbrockmendel/E4
Browse files Browse the repository at this point in the history
REF/CLN: collect imports at top of file, de-duplicate imports
  • Loading branch information
bashtage committed May 29, 2019
2 parents 321619e + b65c971 commit 92885fd
Show file tree
Hide file tree
Showing 59 changed files with 158 additions and 169 deletions.
6 changes: 4 additions & 2 deletions setup.cfg
Expand Up @@ -65,8 +65,6 @@ select=
# E228: missing whitespace around modulo operator
E211,
# E211: whitespace before '['
E401,
# E401: multiple imports on one line
W601,
# W601: .has_key() is deprecated, use 'in'
W391,
Expand Down Expand Up @@ -159,6 +157,10 @@ select=
# E274: tab before keyword
# E275: missing whitespace after keyword

E4,
# E401: multiple imports on one line
# E402: module level import not at top of file

E9,
# E901: SyntaxError or IndentationError
# E902: IOError
Expand Down
5 changes: 3 additions & 2 deletions statsmodels/discrete/tests/test_constrained.py
Expand Up @@ -25,12 +25,13 @@
from statsmodels.tools.tools import add_constant
from statsmodels import datasets

from .results import results_poisson_constrained as results
from .results import results_glm_logit_constrained as reslogit


spector_data = datasets.spector.load(as_pandas=False)
spector_data.exog = add_constant(spector_data.exog, prepend=False)

from .results import results_poisson_constrained as results
from .results import results_glm_logit_constrained as reslogit

DEBUG = False

Expand Down
3 changes: 2 additions & 1 deletion statsmodels/examples/es_misc_poisson2.py
Expand Up @@ -5,6 +5,8 @@
import statsmodels.api as sm
from statsmodels.miscmodels.count import (PoissonGMLE, PoissonOffsetGMLE,
PoissonZiGMLE)
from statsmodels.discrete.discrete_model import Poisson


DEC = 3

Expand All @@ -23,7 +25,6 @@ class Dummy(object):
data_endog = np.random.poisson(np.exp(xbeta))

#estimate discretemod.Poisson as benchmark
from statsmodels.discrete.discrete_model import Poisson
res_discrete = Poisson(data_endog, data_exog).fit()

mod_glm = sm.GLM(data_endog, data_exog, family=sm.families.Poisson())
Expand Down
2 changes: 1 addition & 1 deletion statsmodels/examples/ex_generic_mle.py
Expand Up @@ -6,6 +6,7 @@
from scipy import stats
import statsmodels.api as sm
from statsmodels.base.model import GenericLikelihoodModel
from statsmodels.tools.numdiff import approx_fprime, approx_hess


data = sm.datasets.spector.load(as_pandas=False)
Expand Down Expand Up @@ -119,7 +120,6 @@ def loglikeobs(self, params):
res_bfgs = mod_norm2.fit(start_params=start_params, method="bfgs", fprime=None,
maxiter=500, retall=0)

from statsmodels.tools.numdiff import approx_fprime, approx_hess
hb=-approx_hess(res_norm3.params, mod_norm2.loglike, epsilon=-1e-4)
hf=-approx_hess(res_norm3.params, mod_norm2.loglike, epsilon=1e-4)
hh = (hf+hb)/2.
Expand Down
2 changes: 1 addition & 1 deletion statsmodels/examples/ex_generic_mle_t.py
Expand Up @@ -12,6 +12,7 @@
from scipy import special
import statsmodels.api as sm
from statsmodels.base.model import GenericLikelihoodModel
from statsmodels.tools.numdiff import approx_hess

#redefine some shortcuts
np_log = np.log
Expand Down Expand Up @@ -92,7 +93,6 @@ def nloglikeobs(self, params):
print(resp.params)
print(resp.bse)

from statsmodels.tools.numdiff import approx_hess

hb=-approx_hess(modp.start_value, modp.loglike, epsilon=-1e-4)
tmp = modp.loglike(modp.start_value)
Expand Down
18 changes: 8 additions & 10 deletions statsmodels/examples/ex_generic_mle_tdist.py
Expand Up @@ -13,6 +13,14 @@
from scipy import stats, special, optimize
import statsmodels.api as sm
from statsmodels.base.model import GenericLikelihoodModel
from statsmodels.tools.numdiff import approx_hess

#import for kstest based estimation
#should be replace
# FIXME: importing these patches scipy distribution classes in-place.
# Don't do this.
import statsmodels.sandbox.distributions.sppatch # noqa:F401


#redefine some shortcuts
np_log = np.log
Expand Down Expand Up @@ -137,7 +145,6 @@ def nloglikeobs(self, params):
print(resp2.params)
print(resp2.bse)

from statsmodels.tools.numdiff import approx_hess

hb=-approx_hess(modp.start_params, modp.loglike, epsilon=-1e-4)
tmp = modp.loglike(modp.start_params)
Expand All @@ -156,15 +163,6 @@ def nloglikeobs(self, params):
# fit_ks works well, but no bse or other result statistics yet





#import for kstest based estimation
#should be replace
# FIXME: importing these patches scipy distribution classes in-place.
# Don't do this.
import statsmodels.sandbox.distributions.sppatch # noqa:F401

class MyPareto(GenericLikelihoodModel):
'''Maximum Likelihood Estimation pareto distribution
Expand Down
2 changes: 1 addition & 1 deletion statsmodels/examples/ex_kernel_regression.py
Expand Up @@ -9,6 +9,7 @@
from __future__ import print_function
import numpy as np
import numpy.testing as npt
import matplotlib.pyplot as plt

import statsmodels.nonparametric.api as nparam
#import statsmodels.api as sm
Expand Down Expand Up @@ -54,7 +55,6 @@
npt.assert_allclose(sm_mean2, R_mean, atol=1e-2)
npt.assert_allclose(sm_R2, R_R2, atol=1e-2)

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
Expand Down
9 changes: 5 additions & 4 deletions statsmodels/examples/ex_lowess.py
Expand Up @@ -8,16 +8,19 @@
"""

from __future__ import print_function
import os

import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm

lowess = sm.nonparametric.lowess
import statsmodels.nonparametric.tests.results

# this is just to check direct import
import statsmodels.nonparametric.smoothers_lowess
statsmodels.nonparametric.smoothers_lowess.lowess

lowess = sm.nonparametric.lowess

x = np.arange(20.)

#standard normal noise
Expand Down Expand Up @@ -60,8 +63,6 @@
plt.plot(actual_lowess[:,1])
plt.plot(expected_lowess[:,1])

import os.path
import statsmodels.nonparametric.tests.results
rpath = os.path.split(statsmodels.nonparametric.tests.results.__file__)[0]
rfile = os.path.join(rpath, 'test_lowess_frac.csv')
test_data = np.genfromtxt(open(rfile, 'rb'),
Expand Down
4 changes: 2 additions & 2 deletions statsmodels/examples/ex_misc_tarma.py
Expand Up @@ -8,10 +8,12 @@

from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt

from statsmodels.tsa.arima_process import arma_generate_sample, ArmaProcess
from statsmodels.miscmodels.tmodel import TArma
from statsmodels.tsa.arima_model import ARMA
from statsmodels.tsa.arma_mle import Arma

nobs = 500
ar = [1, -0.6, -0.1]
Expand All @@ -36,7 +38,6 @@

print(proc.ar_roots(), proc.ma_roots())

from statsmodels.tsa.arma_mle import Arma
modn = Arma(x)
resn = modn.fit_mle(order=order)

Expand Down Expand Up @@ -69,7 +70,6 @@
resid = res2.model.geterrors(res2.params)
fv = res[2]['fvec'] #resid returned from leastsq?

import matplotlib.pyplot as plt
plt.plot(x, 'o', alpha=0.5)
plt.plot(x-resid)
plt.plot(x-fv)
Expand Down
2 changes: 1 addition & 1 deletion statsmodels/examples/ex_misc_tmodel.py
Expand Up @@ -5,6 +5,7 @@
from scipy import stats
import statsmodels.api as sm
from statsmodels.miscmodels import TLinearModel
from statsmodels.tools.numdiff import approx_hess

#Example:
#np.random.seed(98765678)
Expand Down Expand Up @@ -69,7 +70,6 @@
print(resp2.params)
print(resp2.bse)

from statsmodels.tools.numdiff import approx_hess

hb=-approx_hess(modp.start_params, modp.loglike, epsilon=-1e-4)
tmp = modp.loglike(modp.start_params)
Expand Down
10 changes: 3 additions & 7 deletions statsmodels/examples/ex_pandas.py
Expand Up @@ -10,11 +10,14 @@
from datetime import datetime

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

from pandas import DataFrame, Series

import statsmodels.api as sm
import statsmodels.tsa.api as tsa
from statsmodels.tsa.arima_process import arma_generate_sample


data = sm.datasets.stackloss.load(as_pandas=False)
Expand Down Expand Up @@ -43,9 +46,6 @@
print(hub_results.summary())


import matplotlib.pyplot as plt
import matplotlib as mpl

def plot_acf_multiple(ys, lags=20):
"""
Expand Down Expand Up @@ -109,12 +109,8 @@ def plot_acf_multiple(ys, lags=20):

#Example TSA ARMA

import numpy as np
import statsmodels.api as sm


# Generate some data from an ARMA process
from statsmodels.tsa.arima_process import arma_generate_sample
arparams = np.array([.75, -.25])
maparams = np.array([.65, .35])
# The conventions of the arma_generate function require that we specify a
Expand Down
9 changes: 4 additions & 5 deletions statsmodels/examples/ex_predict_results.py
Expand Up @@ -8,19 +8,22 @@
"""

import numpy as np
from numpy.testing import assert_allclose
from statsmodels.regression.linear_model import WLS

from statsmodels.tools.tools import add_constant
from statsmodels.sandbox.regression.predstd import wls_prediction_std
from statsmodels.regression._prediction import get_prediction
from statsmodels.genmod._prediction import params_transform_univariate
from statsmodels.genmod.generalized_linear_model import GLM
from statsmodels.genmod.families import links


# from example wls.py

nsample = 50
x = np.linspace(0, 20, nsample)
X = np.column_stack((x, (x - 5)**2))
from statsmodels.tools.tools import add_constant
X = add_constant(X)
beta = [5., 0.5, -0.01]
sig = 0.5
Expand All @@ -43,7 +46,6 @@
pred_res = get_prediction(res_wls)
ci = pred_res.conf_int(obs=True)

from numpy.testing import assert_allclose
assert_allclose(pred_res.se_obs, prstd, rtol=1e-13)
assert_allclose(ci, np.column_stack((iv_l, iv_u)), rtol=1e-13)

Expand All @@ -52,7 +54,6 @@
pred_res2 = res_wls.get_prediction()
ci2 = pred_res2.conf_int(obs=True)

from numpy.testing import assert_allclose
assert_allclose(pred_res2.se_obs, prstd, rtol=1e-13)
assert_allclose(ci2, np.column_stack((iv_l, iv_u)), rtol=1e-13)

Expand All @@ -62,7 +63,6 @@
pred_wls_n = res_wls_n.get_prediction()
print(pred_wls_n.summary_frame().head())

from statsmodels.genmod.generalized_linear_model import GLM

w_sqrt = np.sqrt(w)
mod_glm = GLM(y/w_sqrt, X/w_sqrt[:,None])
Expand All @@ -83,7 +83,6 @@
np.exp(res_glm.conf_int())))
assert_allclose(rates.summary_frame().values, rates2, rtol=1e-13)

from statsmodels.genmod.families import links

# with identity transform
pt = params_transform_univariate(res_glm.params, res_glm.cov_params(), link=links.identity())
Expand Down
3 changes: 2 additions & 1 deletion statsmodels/examples/ex_proportion.py
Expand Up @@ -9,6 +9,8 @@
from __future__ import print_function
from statsmodels.compat.python import lmap
import numpy as np
import matplotlib.pyplot as plt

import statsmodels.stats.proportion as sms
import statsmodels.stats.weightstats as smw

Expand Down Expand Up @@ -45,7 +47,6 @@
nn = np.arange(200, 351)
pow_z = sms.power_ztost_prop(0.5, 0.72, nn, 0.6, alpha=0.05)
pow_bin = sms.power_ztost_prop(0.5, 0.72, nn, 0.6, alpha=0.05, dist='binom')
import matplotlib.pyplot as plt
plt.plot(nn, pow_z[0], label='normal')
plt.plot(nn, pow_bin[0], label='binomial')
plt.legend(loc='lower right')
Expand Down
2 changes: 1 addition & 1 deletion statsmodels/examples/ex_regressionplots.py
Expand Up @@ -12,6 +12,7 @@

from statsmodels.sandbox.regression.predstd import wls_prediction_std
import statsmodels.graphics.regressionplots as smrp
from statsmodels.graphics.tests.test_regressionplots import TestPlot

#example from tut.ols with changes
#fix a seed for these examples
Expand Down Expand Up @@ -122,7 +123,6 @@
smrp.plot_ccpr(res, exog_idx=0)
smrp.plot_ccpr_grid(res, exog_idx=[0,1])

from statsmodels.graphics.tests.test_regressionplots import TestPlot
tp = TestPlot()
tp.test_plot_fit()

Expand Down
3 changes: 2 additions & 1 deletion statsmodels/examples/ex_wald_anova.py
Expand Up @@ -14,6 +14,8 @@
from statsmodels.discrete.discrete_model import Poisson

import statsmodels.stats.tests.test_anova as ttmod
from statsmodels.discrete.discrete_model import NegativeBinomial


test = ttmod.TestAnova3()
test.setup_class()
Expand All @@ -36,7 +38,6 @@
print('\nPoisson 2')
print(res_poi_2.wald_test_terms(skip_single=False))

from statsmodels.discrete.discrete_model import NegativeBinomial
res_nb2 = NegativeBinomial.from_formula("Days ~ C(Weight) * C(Duration)", data).fit()
print('\nNegative Binomial nb2')
print(res_nb2.wald_test_terms(skip_single=False))
Expand Down
7 changes: 3 additions & 4 deletions statsmodels/examples/example_discrete_mnl.py
Expand Up @@ -5,6 +5,9 @@
from statsmodels.compat.python import lrange
import numpy as np
import statsmodels.api as sm
from statsmodels.iolib.summary import (
table_extend, summary_params_2d, summary_params_2dflat)


anes_data = sm.datasets.anes96.load(as_pandas=False)
anes_exog = anes_data.exog
Expand All @@ -20,9 +23,6 @@
#mlogit_res = mlogit_mod.fit(method='ncg') # this takes forever


from statsmodels.iolib.summary import (
summary_params_2d, summary_params_2dflat)

exog_names = [anes_data.exog_name[i] for i in [0, 2]+lrange(5,8)] + ['const']
endog_names = [anes_data.endog_name+'_%d' % i for i in np.unique(mlogit_res.model.endog)[1:]]
print('\n\nMultinomial')
Expand All @@ -41,7 +41,6 @@
print('\n\n')
print('\n'.join((str(t) for t in tables)))

from statsmodels.iolib.summary import table_extend
at = table_extend(tables)
print(at)

Expand Down

0 comments on commit 92885fd

Please sign in to comment.