Skip to content

Commit

Permalink
Merge pull request #8733 from WarrenWeckesser/statsmaint
Browse files Browse the repository at this point in the history
MAINT: stats: A bit of clean up.
  • Loading branch information
andyfaff committed Apr 15, 2018
2 parents 398f9a7 + 55bc2f7 commit 13d8ada
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
4 changes: 2 additions & 2 deletions scipy/stats/_continuous_distns.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
from scipy import integrate
import scipy.special as sc
from scipy._lib._numpy_compat import broadcast_to
from scipy._lib._util import _lazyselect, _lazywhere

from . import _stats
from ._tukeylambda_stats import (tukeylambda_variance as _tlvar,
tukeylambda_kurtosis as _tlkurt)
from ._distn_infrastructure import (get_distribution_names, _kurtosis,
_lazyselect, _lazywhere, _ncx2_cdf,
_ncx2_log_pdf, _ncx2_pdf,
_ncx2_cdf, _ncx2_log_pdf, _ncx2_pdf,
rv_continuous, _skew, valarray)
from ._constants import _XMIN, _EULER, _ZETA3, _XMAX, _LOGXMAX

Expand Down
3 changes: 2 additions & 1 deletion scipy/stats/_discrete_distns.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
from scipy import special
from scipy.special import entr, logsumexp, betaln, gammaln as gamln
from scipy._lib._numpy_compat import broadcast_to
from scipy._lib._util import _lazywhere

from numpy import floor, ceil, log, exp, sqrt, log1p, expm1, tanh, cosh, sinh

import numpy as np

from ._distn_infrastructure import (
rv_discrete, _lazywhere, _ncx2_pdf, _ncx2_cdf, get_distribution_names)
rv_discrete, _ncx2_pdf, _ncx2_cdf, get_distribution_names)


class binom_gen(rv_discrete):
Expand Down
40 changes: 18 additions & 22 deletions scipy/stats/_distn_infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

from scipy.misc import doccer
from ._distr_params import distcont, distdiscrete
from scipy._lib._util import check_random_state, _lazywhere, _lazyselect
from scipy._lib._util import check_random_state
from scipy._lib._util import _valarray as valarray

from scipy.special import (comb, chndtr, entr, rel_entr, kl_div, xlogy, ive)
from scipy.special import (comb, chndtr, entr, rel_entr, xlogy, ive)

# for root finding for discrete distribution ppf, and max likelihood estimation
from scipy import optimize
Expand All @@ -29,11 +29,9 @@
# to approximate the pdf of a continuous distribution given its cdf
from scipy.misc import derivative

from numpy import (arange, putmask, ravel, take, ones, shape, ndarray,
product, reshape, zeros, floor, logical_and, log, sqrt, exp)

from numpy import (place, argsort, argmax, vectorize,
asarray, nan, inf, isinf, NINF, empty)
from numpy import (arange, putmask, ravel, ones, shape, ndarray, zeros, floor,
logical_and, log, sqrt, place, argmax, vectorize, asarray,
nan, inf, isinf, NINF, empty)

import numpy as np

Expand Down Expand Up @@ -562,12 +560,12 @@ def _parse_args_stats(self, %(shape_arg_str)s %(locscale_in)s, moments='mv'):


# Both the continuous and discrete distributions depend on ncx2.
# I think the function name ncx2 is an abbreviation for noncentral chi squared.
# The function name ncx2 is an abbreviation for noncentral chi squared.

def _ncx2_log_pdf(x, df, nc):
# We use (xs**2 + ns**2)/2 = (xs - ns)**2/2 + xs*ns, and include the factor
# of exp(-xs*ns) into the ive function to improve numerical stability
# at large values of xs. See also `rice.pdf`.
# We use (xs**2 + ns**2)/2 = (xs - ns)**2/2 + xs*ns, and include the
# factor of exp(-xs*ns) into the ive function to improve numerical
# stability at large values of xs. See also `rice.pdf`.
df2 = df/2.0 - 1.0
xs, ns = np.sqrt(x), np.sqrt(nc)
res = xlogy(df2/2.0, x/nc) - 0.5*(xs - ns)**2
Expand Down Expand Up @@ -738,7 +736,7 @@ def _construct_doc(self, docdict, shapes_vals=None):
self.__doc__ = self.__doc__.replace('(, ', '(').replace(', )', ')')

def _construct_default_doc(self, longname=None, extradoc=None,
docdict=None, discrete='continuous'):
docdict=None, discrete='continuous'):
"""Construct instance docstring from the default template."""
if longname is None:
longname = 'A'
Expand Down Expand Up @@ -2247,7 +2245,8 @@ def _fit_loc_scale_support(self, data, *args):
if support_width <= 0:
return loc_hat, scale_hat

# Compute the proposed support according to the loc and scale estimates.
# Compute the proposed support according to the loc and scale
# estimates.
a_hat = loc_hat + a * scale_hat
b_hat = loc_hat + b * scale_hat

Expand Down Expand Up @@ -2449,10 +2448,6 @@ def _drv2_ppfsingle(self, q, *args): # Use basic bisection algorithm
if (qb == q):
return b
if b <= a+1:
# testcase: return wrong number at lower index
# python -c "from scipy.stats import zipf;print zipf.ppf(0.01, 2)" wrong
# python -c "from scipy.stats import zipf;print zipf.ppf([0.01, 0.61, 0.77, 0.83], 2)"
# python -c "from scipy.stats import logser;print logser.ppf([0.1, 0.66, 0.86, 0.93], 0.6)"
if qa > q:
return a
else:
Expand Down Expand Up @@ -2655,8 +2650,8 @@ class rv_discrete(rv_generic):
"""
def __new__(cls, a=0, b=inf, name=None, badvalue=None,
moment_tol=1e-8, values=None, inc=1, longname=None,
shapes=None, extradoc=None, seed=None):
moment_tol=1e-8, values=None, inc=1, longname=None,
shapes=None, extradoc=None, seed=None):

if values is not None:
# dispatch to a subclass
Expand Down Expand Up @@ -3178,8 +3173,8 @@ def expect(self, func=None, args=(), loc=0, lb=None, ub=None,
depending on the function, `func`. If it does exist, but the sum converges
slowly, the accuracy of the result may be rather low. For instance, for
``zipf(4)``, accuracy for mean, variance in example is only 1e-5.
increasing `maxcount` and/or `chunksize` may improve the result, but may also
make zipf very slow.
increasing `maxcount` and/or `chunksize` may improve the result, but may
also make zipf very slow.
The function is not vectorized.
Expand Down Expand Up @@ -3261,7 +3256,8 @@ def _expect(fun, lb, ub, x0, inc, maxcount=1000, tolerance=1e-10,
def _iter_chunked(x0, x1, chunksize=4, inc=1):
"""Iterate from x0 to x1 in chunks of chunksize and steps inc.
x0 must be finite, x1 need not be. In the latter case, the iterator is infinite.
x0 must be finite, x1 need not be. In the latter case, the iterator is
infinite.
Handles both x0 < x1 and x0 > x1. In the latter case, iterates downwards
(make sure to set inc < 0.)
Expand Down
2 changes: 1 addition & 1 deletion scipy/stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@

from scipy._lib.six import callable, string_types
from scipy._lib._version import NumpyVersion
from scipy._lib._util import _lazywhere
import scipy.special as special
import scipy.linalg as linalg
from . import distributions
from . import mstats_basic
from ._distn_infrastructure import _lazywhere
from ._stats_mstats_common import _find_repeats, linregress, theilslopes
from ._stats import _kendall_dis, _toint64, _weightedrankedtau

Expand Down

0 comments on commit 13d8ada

Please sign in to comment.