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

Code before:

    def test_cdf_large_n(self, xp):
        # test that asymptotic cdf and cdf for large samples are close
        x = xp.asarray([0.02480, 0.03656, 0.11888, 1.16204, 100])
        xp_assert_close(_cdf_cvm(x, 10000), _cdf_cvm(x), atol=1e-4)

    def test_large_x(self, xp):
        # for large values of x and n, the series used to compute the cdf
        # converges slowly.
        # this leads to bug in R package goftest and MAPLE code that is
        # the basis of the implementation in scipy
        # note: cdf = 1 for x >= 1000/3 and n = 1000
        x = xp.asarray(333.3, dtype=xp.float64)
        assert (0.99999 < _cdf_cvm(x, 1000) < 1.0)
        assert (0.99999 < _cdf_cvm(x) < 1.0)

    def test_low_p(self, xp):
        # _cdf_cvm can return values larger than 1. In that case, we just
        # return a p-value of zero.
        n = 12
        res = cramervonmises(xp.ones(n)*0.8, special.ndtr)
        assert _cdf_cvm(res.statistic, n, xp=xp) > 1.0
        xp_assert_equal(res.pvalue, xp.asarray(0.))

    @pytest.mark.parametrize('x', [(), [1.5]])
    def test_invalid_input(self, x, xp):
        with pytest.warns(SmallSampleWarning, match=too_small_1d_not_omit):
            res = cramervonmises(xp.asarray(x), special.ndtr)
            xp_assert_equal(res.statistic, xp.asarray(xp.nan))
            xp_assert_equal(res.pvalue, xp.asarray(xp.nan))

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_values_R(self, dtype, xp):
        dtype = xp_default_dtype(xp) if dtype is None else getattr(xp, dtype)
        # compared against R package goftest, version 1.1.1
        # library(goftest)
        # options(digits=16)
        # cvm.test(c(-1.7, 2, 0, 1.3, 4, 0.1, 0.6), "pnorm")
        res = cramervonmises(xp.asarray([-1.7, 2, 0, 1.3, 4, 0.1, 0.6], dtype=dtype),
                             special.ndtr)
        xp_assert_close(res.statistic, xp.asarray(0.28815604599198, dtype=dtype))
        xp_assert_close(res.pvalue, xp.asarray( 0.1453465252039, dtype=dtype))

        # cvm.test(c(-1.7, 2, 0, 1.3, 4, 0.1, 0.6), "pnorm", mean = 3, sd = 1.5)
        res = cramervonmises(xp.asarray([-1.7, 2, 0, 1.3, 4, 0.1, 0.6], dtype=dtype),
                             lambda x: special.ndtr((x - 3)/1.5))
        xp_assert_close(res.statistic, xp.asarray(0.94266847977072, dtype=dtype))
        xp_assert_close(res.pvalue, xp.asarray(0.002026416728467, dtype=dtype))

        # cvm.test(c(1, 2, 5, 1.4, 0.14, 11, 13, 0.9, 7.5), "pexp")
        res = cramervonmises(

Review comment (suggestion):
```suggestion
    @pytest.mark.skip_xp_backends('jax.numpy', reason='lazy -> no _axis_nan_policy')
    @pytest.mark.parametrize('x', [(), [1.5]])
```

Code after:

    def test_cdf_large_n(self, xp):
        # test that asymptotic cdf and cdf for large samples are close
        x = xp.asarray([0.02480, 0.03656, 0.11888, 1.16204, 100])
        xp_assert_close(_cdf_cvm(x, 10000), _cdf_cvm(x), atol=1e-4)

    def test_large_x(self, xp):
        # for large values of x and n, the series used to compute the cdf
        # converges slowly.
        # this leads to bug in R package goftest and MAPLE code that is
        # the basis of the implementation in scipy
        # note: cdf = 1 for x >= 1000/3 and n = 1000
        x = xp.asarray(333.3, dtype=xp.float64)
        assert (0.99999 < _cdf_cvm(x, 1000) < 1.0)
        assert (0.99999 < _cdf_cvm(x) < 1.0)

    def test_low_p(self, xp):
        # _cdf_cvm can return values larger than 1. In that case, we just
        # return a p-value of zero.
        n = 12
        res = cramervonmises(xp.ones(n)*0.8, special.ndtr)
        assert _cdf_cvm(res.statistic, n, xp=xp) > 1.0
        xp_assert_equal(res.pvalue, xp.asarray(0.))

    @pytest.mark.skip_xp_backends('jax.numpy', reason='lazy -> no _axis_nan_policy')
    @pytest.mark.parametrize('x', [(), [1.5]])
    def test_invalid_input(self, x, xp):
        with pytest.warns(SmallSampleWarning, match=too_small_1d_not_omit):
            res = cramervonmises(xp.asarray(x), special.ndtr)
            xp_assert_equal(res.statistic, xp.asarray(xp.nan))
            xp_assert_equal(res.pvalue, xp.asarray(xp.nan))

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_values_R(self, dtype, xp):
        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)
        # compared against R package goftest, version 1.1.1
        # library(goftest)
        # options(digits=16)
        # cvm.test(c(-1.7, 2, 0, 1.3, 4, 0.1, 0.6), "pnorm")
        res = cramervonmises(xp.asarray([-1.7, 2, 0, 1.3, 4, 0.1, 0.6], dtype=dtype),
                             special.ndtr)
        xp_assert_close(res.statistic, xp.asarray(0.28815604599198, dtype=dtype))
        xp_assert_close(res.pvalue, xp.asarray( 0.1453465252039, dtype=dtype))

        # cvm.test(c(-1.7, 2, 0, 1.3, 4, 0.1, 0.6), "pnorm", mean = 3, sd = 1.5)
        res = cramervonmises(xp.asarray([-1.7, 2, 0, 1.3, 4, 0.1, 0.6], dtype=dtype),
                             lambda x: special.ndtr((x - 3)/1.5))
        xp_assert_close(res.statistic, xp.asarray(0.94266847977072, dtype=dtype))
        xp_assert_close(res.pvalue, xp.asarray(0.002026416728467, dtype=dtype))