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

Code before:
        x, y = rng.random(15), rng.random(15)
        x, y = xp.asarray(x), xp.asarray(y)

        res = stats.wilcoxon(x, y, method="asymptotic")
        ref = special.ndtri(res.pvalue/2)
        xp_assert_close(res.zstatistic, ref)

        res = stats.wilcoxon(x, y, method="exact")
        assert not hasattr(res, 'zstatistic')

        res = stats.wilcoxon(x, y)
        assert not hasattr(res, 'zstatistic')

    def test_wilcoxon_tie(self, xp):
        # Regression test for gh-2391.
        # Corresponding R code is:
        #   > result = wilcox.test(rep(0.1, 10), exact=FALSE, correct=FALSE)
        #   > result$p.value
        #   [1] 0.001565402258002551
        #   > result = wilcox.test(rep(0.1, 10), exact=FALSE, correct=TRUE)
        #   > result$p.value
        #   [1] 0.00190419504300439
        d = xp.asarray([0.1] * 10)
        expected_stat = xp.asarray(0.0)

        stat, p = stats.wilcoxon(d, method="asymptotic", correction=False, )
        expected_p = xp.asarray(0.001565402258002551)
        xp_assert_equal(stat, expected_stat)
        xp_assert_close(p, expected_p)

        stat, p = stats.wilcoxon(d, method="asymptotic", correction=True)
        expected_p = xp.asarray(0.00190419504300439)
        xp_assert_equal(stat, expected_stat)
        xp_assert_close(p, expected_p)

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_onesided(self, dtype, xp):
        # tested against "R version 4.0.3 (2020-10-10)"
        #
        # x <- c(125, 115, 130, 140, 140, 115, 140, 125, 140, 135)
        # y <- c(110, 122, 125, 120, 140, 124, 123, 137, 135, 145)
        # cfg <- list(x = x, y = y, paired = TRUE, exact = FALSE)
        # do.call(wilcox.test, c(cfg, list(alternative = "less", correct = FALSE)))
        # do.call(wilcox.test, c(cfg, list(alternative = "less", correct = TRUE)))
        # do.call(wilcox.test, c(cfg, list(alternative = "greater", correct = FALSE)))
        # do.call(wilcox.test, c(cfg, list(alternative = "greater", correct = TRUE)))
        if is_numpy(xp) and xp.__version__ < "2.0" and dtype == 'float32':
            pytest.skip("dtypes not preserved with pre-NEP 50 rules")

        dtype = dtype if dtype is None else getattr(xp, dtype)
        x = xp.asarray([125, 115, 130, 140, 140, 115, 140, 125, 140, 135], dtype=dtype)

Review comment (suggestion):
```suggestion
        stat, p = stats.wilcoxon(d, method="asymptotic", correction=False)
```

Code after:
        x, y = rng.random(15), rng.random(15)
        x, y = xp.asarray(x), xp.asarray(y)

        res = stats.wilcoxon(x, y, method="asymptotic")
        ref = special.ndtri(res.pvalue/2)
        xp_assert_close(res.zstatistic, ref)

        res = stats.wilcoxon(x, y, method="exact")
        assert not hasattr(res, 'zstatistic')

        res = stats.wilcoxon(x, y)
        assert not hasattr(res, 'zstatistic')

    def test_wilcoxon_tie(self, xp):
        # Regression test for gh-2391.
        # Corresponding R code is:
        #   > result = wilcox.test(rep(0.1, 10), exact=FALSE, correct=FALSE)
        #   > result$p.value
        #   [1] 0.001565402258002551
        #   > result = wilcox.test(rep(0.1, 10), exact=FALSE, correct=TRUE)
        #   > result$p.value
        #   [1] 0.00190419504300439
        d = xp.asarray([0.1] * 10)
        expected_stat = xp.asarray(0.0)

        stat, p = stats.wilcoxon(d, method="asymptotic", correction=False)
        expected_p = xp.asarray(0.001565402258002551)
        xp_assert_equal(stat, expected_stat)
        xp_assert_close(p, expected_p)

        stat, p = stats.wilcoxon(d, method="asymptotic", correction=True)
        expected_p = xp.asarray(0.00190419504300439)
        xp_assert_equal(stat, expected_stat)
        xp_assert_close(p, expected_p)

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_onesided(self, dtype, xp):
        # tested against "R version 4.0.3 (2020-10-10)"
        #
        # x <- c(125, 115, 130, 140, 140, 115, 140, 125, 140, 135)
        # y <- c(110, 122, 125, 120, 140, 124, 123, 137, 135, 145)
        # cfg <- list(x = x, y = y, paired = TRUE, exact = FALSE)
        # do.call(wilcox.test, c(cfg, list(alternative = "less", correct = FALSE)))
        # do.call(wilcox.test, c(cfg, list(alternative = "less", correct = TRUE)))
        # do.call(wilcox.test, c(cfg, list(alternative = "greater", correct = FALSE)))
        # do.call(wilcox.test, c(cfg, list(alternative = "greater", correct = TRUE)))
        if is_numpy(xp) and xp.__version__ < "2.0" and dtype == 'float32':
            pytest.skip("dtypes not preserved with pre-NEP 50 rules")

        dtype = dtype if dtype is None else getattr(xp, dtype)
        x = xp.asarray([125, 115, 130, 140, 140, 115, 140, 125, 140, 135], dtype=dtype)