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

Code before:
        pl = boschloo_exact(tbl, alternative='less').pvalue
        pg = boschloo_exact(tbl, alternative='greater').pvalue
        assert 2*min(pl, pg) > 1
        pt = boschloo_exact(tbl, alternative='two-sided').pvalue
        assert pt == 1.0

    @pytest.mark.parametrize("alternative", ("less", "greater"))
    def test_against_fisher_exact(self, alternative):
        # Check that the statistic of `boschloo_exact` is the same as the
        # p-value of `fisher_exact` (for one-sided tests). See gh-15345.
        tbl = [[2, 7], [8, 2]]
        boschloo_stat = boschloo_exact(tbl, alternative=alternative).statistic
        fisher_p = stats.fisher_exact(tbl, alternative=alternative)[1]
        assert_allclose(boschloo_stat, fisher_p)


@make_xp_test_case(cramervonmises_2samp)
class TestCvm_2samp:
    @pytest.mark.parametrize('args', [([], np.arange(5)),
                                      (np.arange(5), [1])])
    def test_too_small_input(self, args, xp):
        args = (xp.asarray(arg, dtype=xp_default_dtype(xp)) for arg in args)
        with eager_warns(SmallSampleWarning, match=too_small_1d_not_omit, xp=xp):
            res = cramervonmises_2samp(*args)
            xp_assert_equal(res.statistic, xp.asarray(np.nan))
            xp_assert_equal(res.pvalue, xp.asarray(np.nan))

    def test_invalid_input(self, xp):
        y = xp.arange(5)
        msg = 'method must be either auto, exact or asymptotic'
        with pytest.raises(ValueError, match=msg):
            cramervonmises_2samp(y, y, 'xyz')

    def test_list_input(self):  # list input only relevant for NumPy
        x = [2, 3, 4, 7, 6]
        y = [0.2, 0.7, 12, 18]
        r1 = cramervonmises_2samp(x, y)
        r2 = cramervonmises_2samp(np.array(x), np.array(y))
        assert_equal((r1.statistic, r1.pvalue), (r2.statistic, r2.pvalue))

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_example_conover(self, dtype, xp):
        # Example 2 in Section 6.2 of W.J. Conover: Practical Nonparametric
        # Statistics, 1971.
        if is_numpy(xp) and xp.__version__ < "2.0" and dtype == 'float32':
            pytest.skip("Pre-NEP 50 doesn't respect dtypes")
        dtype = xp_default_dtype(xp) if dtype is None else getattr(xp, dtype)
        x = xp.asarray([7.6, 8.4, 8.6, 8.7, 9.3, 9.9, 10.1, 10.6, 11.2], dtype=dtype)
        y = xp.asarray([5.2, 5.7, 5.9, 6.5, 6.8, 8.2, 9.1, 9.8,
                        10.8, 11.3, 11.5, 12.3, 12.5, 13.4, 14.6], dtype=dtype)
        r = cramervonmises_2samp(x, y)

Review comment (suggestion):
Oops. Fortunately, this is safe to commit with `[skip ci]` if there is nothing else.
```suggestion
            xp_assert_equal(res.statistic, xp.asarray(xp.nan))
            xp_assert_equal(res.pvalue, xp.asarray(xp.nan))
```

Code after:
        pl = boschloo_exact(tbl, alternative='less').pvalue
        pg = boschloo_exact(tbl, alternative='greater').pvalue
        assert 2*min(pl, pg) > 1
        pt = boschloo_exact(tbl, alternative='two-sided').pvalue
        assert pt == 1.0

    @pytest.mark.parametrize("alternative", ("less", "greater"))
    def test_against_fisher_exact(self, alternative):
        # Check that the statistic of `boschloo_exact` is the same as the
        # p-value of `fisher_exact` (for one-sided tests). See gh-15345.
        tbl = [[2, 7], [8, 2]]
        boschloo_stat = boschloo_exact(tbl, alternative=alternative).statistic
        fisher_p = stats.fisher_exact(tbl, alternative=alternative)[1]
        assert_allclose(boschloo_stat, fisher_p)


@make_xp_test_case(cramervonmises_2samp)
class TestCvm_2samp:
    @pytest.mark.parametrize('args', [([], np.arange(5)),
                                      (np.arange(5), [1])])
    @pytest.mark.skip_xp_backends("jax.numpy", reason="lazy -> no axis_nan_policy")
    def test_too_small_input(self, args, xp):
        args = (xp.asarray(arg, dtype=xp_default_dtype(xp)) for arg in args)
        with eager_warns(SmallSampleWarning, match=too_small_1d_not_omit, xp=xp):
            res = cramervonmises_2samp(*args)
            xp_assert_equal(res.statistic, xp.asarray(xp.nan))
            xp_assert_equal(res.pvalue, xp.asarray(xp.nan))

    def test_invalid_input(self, xp):
        y = xp.arange(5)
        msg = 'method must be either auto, exact or asymptotic'
        with pytest.raises(ValueError, match=msg):
            cramervonmises_2samp(y, y, 'xyz')

    def test_list_input(self):  # list input only relevant for NumPy
        x = [2, 3, 4, 7, 6]
        y = [0.2, 0.7, 12, 18]
        r1 = cramervonmises_2samp(x, y)
        r2 = cramervonmises_2samp(np.array(x), np.array(y))
        assert_equal((r1.statistic, r1.pvalue), (r2.statistic, r2.pvalue))

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_example_conover(self, dtype, xp):
        # Example 2 in Section 6.2 of W.J. Conover: Practical Nonparametric
        # Statistics, 1971.
        if is_numpy(xp) and xp.__version__ < "2.0" and dtype == 'float32':
            pytest.skip("Pre-NEP 50 doesn't respect dtypes")
        dtype = xp_default_dtype(xp) if dtype is None else getattr(xp, dtype)
        x = xp.asarray([7.6, 8.4, 8.6, 8.7, 9.3, 9.9, 10.1, 10.6, 11.2], dtype=dtype)
        y = xp.asarray([5.2, 5.7, 5.9, 6.5, 6.8, 8.2, 9.1, 9.8,
                        10.8, 11.3, 11.5, 12.3, 12.5, 13.4, 14.6], dtype=dtype)