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

Code before:
        xp_assert_close(res.pvalue, xp.asarray(ref[1], dtype=dtype))

    def test_too_few_samples(self, xp):
        message = "At least 3 samples must be given"
        with pytest.raises(ValueError, match=message):
            stats.friedmanchisquare(xp.asarray(self.x3[0]), xp.asarray(self.x3[1]))


@make_xp_test_case(stats.kstest)
class TestKSTest:
    """Tests kstest and ks_1samp agree with K-S various sizes, alternatives, modes."""

    def _test_kstest_and_ks1samp(self, x, alternative, mode='auto', decimal=14):
        result = stats.kstest(x, 'norm', alternative=alternative, mode=mode)
        result_1samp = stats.ks_1samp(x, special.ndtr,
                                      alternative=alternative, mode=mode)
        xp_assert_close(result.statistic, result_1samp.statistic)
        xp_assert_close(result.pvalue, result_1samp.pvalue)

    def test_namedtuple_attributes(self, xp):
        x = xp.linspace(-1, 1, 9)
        # test for namedtuple attribute results
        attributes = ('statistic', 'pvalue')
        res = stats.kstest(x, special.ndtr)
        check_named_results(res, attributes, xp=xp)

    def test_agree_with_ks_1samp(self, xp):
        x = xp.linspace(-1, 1, 9)
        self._test_kstest_and_ks1samp(x, 'two-sided')

        x = xp.linspace(-15, 15, 9)
        self._test_kstest_and_ks1samp(x, 'two-sided')

        x = [-1.23, 0.06, -0.60, 0.17, 0.66, -0.17, -0.08, 0.27, -0.98, -0.99]
        x = xp.asarray(x)
        self._test_kstest_and_ks1samp(x, 'two-sided')
        self._test_kstest_and_ks1samp(x, 'greater', mode='exact')
        self._test_kstest_and_ks1samp(x, 'less', mode='exact')

    def test_pm_inf_gh20386(self, xp):
        # Check that gh-20386 is resolved - `kstest` does not
        # return NaNs when both -inf and inf are in sample.
        vals = xp.asarray([-xp.inf, 0, 1, xp.inf])
        def cdf(x):  # Cauchy CDF
            return xp.atan2(xp.ones_like(x), -x) / xp.pi
        res = stats.kstest(vals, cdf)
        ref = stats.kstest(vals, cdf, _no_deco=True)
        assert xp.isfinite(res.statistic)
        assert xp.isfinite(res.pvalue)
        assert xp.isfinite(res.statistic_location)
        assert xp.isfinite(res.statistic_sign)

Review comment (suggestion):
OK to change back.
```suggestion
        res = stats.kstest(x, 'norm')
```

Code after:
        xp_assert_close(res.pvalue, xp.asarray(ref[1], dtype=dtype))

    def test_too_few_samples(self, xp):
        message = "At least 3 samples must be given"
        with pytest.raises(ValueError, match=message):
            stats.friedmanchisquare(xp.asarray(self.x3[0]), xp.asarray(self.x3[1]))


@make_xp_test_case(stats.kstest)
class TestKSTest:
    """Tests kstest and ks_1samp agree with K-S various sizes, alternatives, modes."""

    def _test_kstest_and_ks1samp(self, x, alternative, mode='auto', decimal=14):
        result = stats.kstest(x, 'norm', alternative=alternative, mode=mode)
        result_1samp = stats.ks_1samp(x, special.ndtr,
                                      alternative=alternative, mode=mode)
        xp_assert_close(result.statistic, result_1samp.statistic)
        xp_assert_close(result.pvalue, result_1samp.pvalue)
        xp_assert_equal(result.statistic_location, result.statistic_location)
        xp_assert_equal(result.statistic_sign, result.statistic_sign)

    def test_namedtuple_attributes(self, xp):
        x = xp.linspace(-1, 1, 9)
        # test for namedtuple attribute results
        attributes = ('statistic', 'pvalue')
        res = stats.kstest(x, special.ndtr)
        check_named_results(res, attributes, xp=xp)

    def test_agree_with_ks_1samp(self, xp):
        x = xp.linspace(-1, 1, 9)
        self._test_kstest_and_ks1samp(x, 'two-sided')

        x = xp.linspace(-15, 15, 9)
        self._test_kstest_and_ks1samp(x, 'two-sided')

        x = [-1.23, 0.06, -0.60, 0.17, 0.66, -0.17, -0.08, 0.27, -0.98, -0.99]
        x = xp.asarray(x)
        self._test_kstest_and_ks1samp(x, 'two-sided')
        self._test_kstest_and_ks1samp(x, 'greater', mode='exact')
        self._test_kstest_and_ks1samp(x, 'less', mode='exact')

    # parametrize over function-specific options to make sure they're passed through;
    # `test_axis_nan_policy` takes care of the rest.
    @pytest.mark.parametrize("method", ["auto", "exact", "asymp"])
    @pytest.mark.parametrize("alternative", ['two-sided', 'less', 'greater'])
    def test_agree_with_ks_2samp(self, method, alternative, xp):
        rng = np.random.default_rng(236114087)
        x, y = rng.random((2, 9, 10))
        x, y = xp.asarray(x), xp.asarray(y)
        res = stats.kstest(x, y, alternative=alternative, method=method)
        ref = stats.ks_2samp(x, y, alternative=alternative, method=method)