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

Code before:
        # 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(
            xp.asarray([1, 2, 5, 1.4, 0.14, 11, 13, 0.9, 7.5], dtype=dtype),
            lambda x: -xp.expm1(-x))
        xp_assert_close(res.statistic, xp.asarray(0.84218540922393, dtype=dtype))
        xp_assert_close(res.pvalue, xp.asarray(0.004433406063014, dtype=dtype))

    def test_str_cdf(self, xp):
        if not is_numpy(xp):
            message = "`cdf` must be a callable if `rvs` is a non-NumPy array."
            with pytest.raises(ValueError, match=message):
                cramervonmises(xp.asarray([1, 2, 3]), "beta")
            return

Review comment (bug):
To fix related CI failures:
```suggestion
        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)
```

Code after:
        # 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))

        # cvm.test(c(1, 2, 5, 1.4, 0.14, 11, 13, 0.9, 7.5), "pexp")
        res = cramervonmises(
            xp.asarray([1, 2, 5, 1.4, 0.14, 11, 13, 0.9, 7.5], dtype=dtype),
            lambda x: -xp.expm1(-x))
        xp_assert_close(res.statistic, xp.asarray(0.84218540922393, dtype=dtype))
        xp_assert_close(res.pvalue, xp.asarray(0.004433406063014, dtype=dtype))

    def test_str_cdf(self, xp):
        if not is_numpy(xp):
            message = "`cdf` must be a callable if `rvs` is a non-NumPy array."