Repository: scipy/scipy
File: scipy/stats/tests/test_hypotests.py (Python)

Code before:
            stats.bws_test(x, y, axis=3)

        message = '`x` and `y` must contain...'
        with pytest.raises(ValueError, match=message):
            stats.bws_test(x, xp.asarray([], dtype=x.dtype))

        message = 'alternative` must be one of...'
        with pytest.raises(ValueError, match=message):
            stats.bws_test(x, y, alternative='ekki-ekki')

        message = 'method` must be an instance of...'
        with pytest.raises(ValueError, match=message):
            stats.bws_test(x, y, method=42)

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_against_published_reference(self, dtype, xp):
        # Test against Example 2 in bws_test Reference [1], pg 9
        # https://link.springer.com/content/pdf/10.1007/BF02762032.pdf
        if is_numpy(xp) and xp.__version__ < "2.0" and dtype == 'float32':
            pytest.skip("Scalar dtypes only respected after NEP 50.")
        dtype = xp_default_dtype(xp) if dtype is None else getattr(xp, dtype)
        x = xp.asarray([1, 2, 3, 4, 6, 7, 8], dtype=dtype)
        y = xp.asarray([5, 9, 10, 11, 12, 13, 14], dtype=dtype)
        res = stats.bws_test(x, y, alternative='two-sided')
        xp_assert_close(res.statistic, xp.asarray(5.132, dtype=dtype), atol=1e-3)
        xp_assert_equal(res.pvalue, xp.asarray(10/3432, dtype=dtype))

    @pytest.mark.parametrize(('alternative', 'statistic', 'pvalue'),
                             [('two-sided', 1.7510204081633, 0.1264422777777),
                              ('less', -1.7510204081633, 0.05754662004662),
                              ('greater', -1.7510204081633, 0.9424533799534)])
    def test_against_R(self, alternative, statistic, pvalue, xp):
        # Test against R library BWStest function bws_test
        # library(BWStest)
        # options(digits=16)
        # x = c(...)
        # y = c(...)
        # bws_test(x, y, alternative='two.sided')
        rng = np.random.default_rng(4571775098104213308)
        x, y = rng.random(size=(2, 7))
        x, y = xp.asarray(x, dtype=xp.float64), xp.asarray(y, dtype=xp.float64)
        res = stats.bws_test(x, y, alternative=alternative)
        statistic = xp.asarray(statistic, dtype=xp.float64)
        pvalue = xp.asarray(pvalue, dtype=xp.float64)
        xp_assert_close(res.statistic, statistic, rtol=1e-13)
        xp_assert_close(res.pvalue, pvalue, atol=1e-2, rtol=1e-1)

    @pytest.mark.parametrize(('alternative', 'statistic', 'pvalue'),
                             [('two-sided', 1.142629265891, 0.2903950180801),
                              ('less', 0.99629665877411, 0.8545660222131),
                              ('greater', 0.99629665877411, 0.1454339777869)])

Review comment (suggestion):
```suggestion
        xp_assert_close(res.pvalue, xp.asarray(10/3432, dtype=dtype))
```

Code after:
        with pytest.raises(ValueError, match=message):
            stats.bws_test(x, y, axis=3)

        message = '`x` and `y` must contain...'
        with pytest.raises(ValueError, match=message):
            stats.bws_test(x, xp.asarray([], dtype=x.dtype))

        message = 'alternative` must be one of...'
        with pytest.raises(ValueError, match=message):
            stats.bws_test(x, y, alternative='ekki-ekki')

        message = 'method` must be an instance of...'
        with pytest.raises(ValueError, match=message):
            stats.bws_test(x, y, method=42)

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_against_published_reference(self, dtype, xp):
        # Test against Example 2 in bws_test Reference [1], pg 9
        # https://link.springer.com/content/pdf/10.1007/BF02762032.pdf
        dtype = xp_default_dtype(xp) if dtype is None else getattr(xp, dtype)
        x = xp.asarray([1, 2, 3, 4, 6, 7, 8], dtype=dtype)
        y = xp.asarray([5, 9, 10, 11, 12, 13, 14], dtype=dtype)
        res = stats.bws_test(x, y, alternative='two-sided')
        xp_assert_close(res.statistic, xp.asarray(5.132, dtype=dtype), atol=1e-3)
        xp_assert_close(res.pvalue, xp.asarray(10/3432, dtype=dtype))

    @pytest.mark.parametrize(('alternative', 'statistic', 'pvalue'),
                             [('two-sided', 1.7510204081633, 0.1264422777777),
                              ('less', -1.7510204081633, 0.05754662004662),
                              ('greater', -1.7510204081633, 0.9424533799534)])
    def test_against_R(self, alternative, statistic, pvalue, xp):
        # Test against R library BWStest function bws_test
        # library(BWStest)
        # options(digits=16)
        # x = c(...)
        # y = c(...)
        # bws_test(x, y, alternative='two.sided')
        rng = np.random.default_rng(4571775098104213308)
        x, y = rng.random(size=(2, 7))
        x, y = xp.asarray(x, dtype=xp.float64), xp.asarray(y, dtype=xp.float64)
        res = stats.bws_test(x, y, alternative=alternative)
        statistic = xp.asarray(statistic, dtype=xp.float64)
        pvalue = xp.asarray(pvalue, dtype=xp.float64)
        xp_assert_close(res.statistic, statistic, rtol=1e-13)
        xp_assert_close(res.pvalue, pvalue, atol=1e-2, rtol=1e-1)

    @pytest.mark.parametrize(('alternative', 'statistic', 'pvalue'),
                             [('two-sided', 1.142629265891, 0.2903950180801),
                              ('less', 0.99629665877411, 0.8545660222131),
                              ('greater', 0.99629665877411, 0.1454339777869)])
    def test_against_R_imbalanced(self, alternative, statistic, pvalue, xp):