Skip to content

Commit

Permalink
Merge pull request #14181 from tylerjereddy/treddy_170_more_backports
Browse files Browse the repository at this point in the history
MAINT: (more) backports for SciPy 1.7.0
  • Loading branch information
tylerjereddy committed Jun 6, 2021
2 parents 3914732 + a9dc086 commit 9b04f4d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 58 deletions.
4 changes: 4 additions & 0 deletions doc/release/1.7.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ Issues closed for 1.7.0
* `#14055 <https://github.com/scipy/scipy/issues/14055>`__: linalg.solve: Unclear error when using assume_a='her' with real...
* `#14093 <https://github.com/scipy/scipy/issues/14093>`__: DOC: Inconsistency in the definition of default values in the...
* `#14158 <https://github.com/scipy/scipy/issues/14158>`__: TST, BUG: test_rbfinterp.py -- test_interpolation_misfit_1d fails...
* `#14170 <https://github.com/scipy/scipy/issues/14170>`__: TST: signal submodule test_filtfilt_gust failing on 32-bit amd64...


***********************
Expand Down Expand Up @@ -1025,3 +1026,6 @@ Pull requests for 1.7.0
* `#14146 <https://github.com/scipy/scipy/pull/14146>`__: MAINT: commit count if no tag
* `#14164 <https://github.com/scipy/scipy/pull/14164>`__: TST, BUG: fix rbf matrix value
* `#14166 <https://github.com/scipy/scipy/pull/14166>`__: CI, MAINT: restrictions on pre-release CI
* `#14171 <https://github.com/scipy/scipy/pull/14171>`__: TST: signal: Bump tolerances for a test of Gustafsson's...
* `#14175 <https://github.com/scipy/scipy/pull/14175>`__: TST: stats: Loosen tolerance in some binomtest tests.
* `#14182 <https://github.com/scipy/scipy/pull/14182>`__: MAINT: stats: Update ppcc_plot and ppcc_max docstring.
8 changes: 4 additions & 4 deletions scipy/signal/tests/test_signaltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2373,10 +2373,10 @@ def check_filtfilt_gust(b, a, shape, axis, irlen=None):
zo1 = np.swapaxes(zo1, -1, axis)
zo2 = np.swapaxes(zo2, -1, axis)

assert_allclose(y, yo, rtol=1e-9, atol=1e-10)
assert_allclose(yg, yo, rtol=1e-9, atol=1e-10)
assert_allclose(zg1, zo1, rtol=1e-9, atol=1e-10)
assert_allclose(zg2, zo2, rtol=1e-9, atol=1e-10)
assert_allclose(y, yo, rtol=1e-8, atol=1e-9)
assert_allclose(yg, yo, rtol=1e-8, atol=1e-9)
assert_allclose(zg1, zo1, rtol=1e-8, atol=1e-9)
assert_allclose(zg2, zo2, rtol=1e-8, atol=1e-9)


def test_choose_conv_method():
Expand Down
83 changes: 43 additions & 40 deletions scipy/stats/morestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,11 @@ def probplot(x, sparams=(), dist='norm', fit=True, plot=None, rvalue=False):
def ppcc_max(x, brack=(0.0, 1.0), dist='tukeylambda'):
"""Calculate the shape parameter that maximizes the PPCC.
The probability plot correlation coefficient (PPCC) plot can be used to
determine the optimal shape parameter for a one-parameter family of
distributions. ppcc_max returns the shape parameter that would maximize the
probability plot correlation coefficient for the given data to a
one-parameter family of distributions.
The probability plot correlation coefficient (PPCC) plot can be used
to determine the optimal shape parameter for a one-parameter family
of distributions. ``ppcc_max`` returns the shape parameter that would
maximize the probability plot correlation coefficient for the given
data to a one-parameter family of distributions.
Parameters
----------
Expand Down Expand Up @@ -667,37 +667,33 @@ def ppcc_max(x, brack=(0.0, 1.0), dist='tukeylambda'):
References
----------
.. [1] J.J. Filliben, "The Probability Plot Correlation Coefficient Test for
Normality", Technometrics, Vol. 17, pp. 111-117, 1975.
.. [2] https://www.itl.nist.gov/div898/handbook/eda/section3/ppccplot.htm
.. [1] J.J. Filliben, "The Probability Plot Correlation Coefficient Test
for Normality", Technometrics, Vol. 17, pp. 111-117, 1975.
.. [2] Engineering Statistics Handbook, NIST/SEMATEC,
https://www.itl.nist.gov/div898/handbook/eda/section3/ppccplot.htm
Examples
--------
First we generate some random data from a Tukey-Lambda distribution,
with shape parameter -0.7:
First we generate some random data from a Weibull distribution
with shape parameter 2.5:
>>> from scipy import stats
>>> x = stats.tukeylambda.rvs(-0.7, loc=2, scale=0.5, size=10000,
... random_state=1234567) + 1e4
Now we explore this data with a PPCC plot as well as the related
probability plot and Box-Cox normplot. A red line is drawn where we
expect the PPCC value to be maximal (at the shape parameter -0.7 used
above):
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure(figsize=(8, 6))
>>> ax = fig.add_subplot(111)
>>> res = stats.ppcc_plot(x, -5, 5, plot=ax)
>>> rng = np.random.default_rng()
>>> c = 2.5
>>> x = stats.weibull_min.rvs(c, scale=4, size=2000, random_state=rng)
Generate the PPCC plot for this data with the Weibull distribution.
We calculate the value where the shape should reach its maximum and a red
line is drawn there. The line should coincide with the highest point in the
ppcc_plot.
>>> fig, ax = plt.subplots(figsize=(8, 6))
>>> res = stats.ppcc_plot(x, c/2, 2*c, dist='weibull_min', plot=ax)
>>> max = stats.ppcc_max(x)
>>> ax.vlines(max, 0, 1, colors='r', label='Expected shape value')
We calculate the value where the shape should reach its maximum and a
red line is drawn there. The line should coincide with the highest
point in the PPCC graph.
>>> cmax = stats.ppcc_max(x, brack=(c/2, 2*c), dist='weibull_min')
>>> ax.axvline(cmax, color='r')
>>> plt.show()
"""
Expand Down Expand Up @@ -770,28 +766,35 @@ def ppcc_plot(x, a, b, dist='tukeylambda', plot=None, N=80):
Examples
--------
First we generate some random data from a Tukey-Lambda distribution,
with shape parameter -0.7:
First we generate some random data from a Weibull distribution
with shape parameter 2.5, and plot the histogram of the data:
>>> from scipy import stats
>>> import matplotlib.pyplot as plt
>>> rng = np.random.default_rng()
>>> x = stats.tukeylambda.rvs(-0.7, loc=2, scale=0.5,
... size=10000, random_state=rng) + 1e4
>>> c = 2.5
>>> x = stats.weibull_min.rvs(c, scale=4, size=2000, random_state=rng)
Take a look at the histogram of the data.
>>> fig1, ax = plt.subplots(figsize=(9, 4))
>>> ax.hist(x, bins=50)
>>> ax.set_title('Histogram of x')
>>> plt.show()
Now we explore this data with a PPCC plot as well as the related
probability plot and Box-Cox normplot. A red line is drawn where we
expect the PPCC value to be maximal (at the shape parameter -0.7 used
above):
expect the PPCC value to be maximal (at the shape parameter ``c``
used above):
>>> fig = plt.figure(figsize=(12, 4))
>>> ax1 = fig.add_subplot(131)
>>> ax2 = fig.add_subplot(132)
>>> ax3 = fig.add_subplot(133)
>>> fig2 = plt.figure(figsize=(12, 4))
>>> ax1 = fig2.add_subplot(1, 3, 1)
>>> ax2 = fig2.add_subplot(1, 3, 2)
>>> ax3 = fig2.add_subplot(1, 3, 3)
>>> res = stats.probplot(x, plot=ax1)
>>> res = stats.boxcox_normplot(x, -5, 5, plot=ax2)
>>> res = stats.ppcc_plot(x, -5, 5, plot=ax3)
>>> ax3.vlines(-0.7, 0, 1, colors='r', label='Expected shape value')
>>> res = stats.boxcox_normplot(x, -4, 4, plot=ax2)
>>> res = stats.ppcc_plot(x, c/2, 2*c, dist='weibull_min', plot=ax3)
>>> ax3.axvline(c, color='r')
>>> plt.show()
"""
Expand Down
23 changes: 9 additions & 14 deletions scipy/stats/tests/test_morestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,13 +778,12 @@ class TestBinomTest:
# Expected results here are from R binom.test, e.g.
# options(digits=16)
# binom.test(484, 967, p=0.48)
@pytest.mark.xfail_on_32bit("The large inputs make these tests "
"sensitive to machine epsilon level")
#
def test_two_sided_pvalues1(self):
# These tests work on all OS's but fail on
# Linux_Python_37_32bit_full due to numerical issues caused
# by large inputs.
rtol = 1e-13 # aarch64 observed rtol: 3.5e-13
# `tol` could be stricter on most architectures, but the value
# here is limited by accuracy of `binom.cdf` for large inputs on
# Linux_Python_37_32bit_full and aarch64
rtol = 1e-10 # aarch64 observed rtol: 1.5e-11
res = stats.binomtest(10079999, 21000000, 0.48)
assert_allclose(res.pvalue, 1.0, rtol=rtol)
res = stats.binomtest(10079990, 21000000, 0.48)
Expand All @@ -794,29 +793,25 @@ def test_two_sided_pvalues1(self):
res = stats.binomtest(10080017, 21000000, 0.48)
assert_allclose(res.pvalue, 0.9940754817328, rtol=1e-9)

@pytest.mark.xfail_on_32bit("The large inputs make these tests "
"sensitive to machine epsilon level")
def test_two_sided_pvalues2(self):
rtol = 1e-13 # no aarch64 failure with 1e-15, preemptive bump
rtol = 1e-10 # no aarch64 failure with 1e-15, preemptive bump
res = stats.binomtest(9, n=21, p=0.48)
assert_allclose(res.pvalue, 0.6689672431939, rtol=rtol)
res = stats.binomtest(4, 21, 0.48)
assert_allclose(res.pvalue, 0.008139563452106, rtol=rtol)
res = stats.binomtest(11, 21, 0.48)
assert_allclose(res.pvalue, 0.8278629664608, rtol=rtol)
res = stats.binomtest(7, 21, 0.48)
assert_allclose(res.pvalue, 0.1966772901718, rtol=1e-12)
assert_allclose(res.pvalue, 0.1966772901718, rtol=rtol)
res = stats.binomtest(3, 10, .5)
assert_allclose(res.pvalue, 0.34375, rtol=rtol)
res = stats.binomtest(2, 2, .4)
assert_allclose(res.pvalue, 0.16, rtol=rtol)
res = stats.binomtest(2, 4, .3)
assert_allclose(res.pvalue, 0.5884, rtol=rtol)

@pytest.mark.xfail_on_32bit("The large inputs make these tests "
"sensitive to machine epsilon level")
def test_edge_cases(self):
rtol = 1e-14 # aarch64 observed rtol: 1.33e-15
rtol = 1e-10 # aarch64 observed rtol: 1.33e-15
res = stats.binomtest(484, 967, 0.5)
assert_allclose(res.pvalue, 1, rtol=rtol)
res = stats.binomtest(3, 47, 3/47)
Expand Down Expand Up @@ -863,7 +858,7 @@ def test_binary_srch_for_binom_tst(self):

# Expected results here are from R 3.6.2 binom.test
@pytest.mark.parametrize('alternative, pval, ci_low, ci_high',
[('less', 0.148831050443,
[('less', 0.148831050443,
0.0, 0.2772002496709138),
('greater', 0.9004695898947,
0.1366613252458672, 1.0),
Expand Down

0 comments on commit 9b04f4d

Please sign in to comment.