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

Code before:

    # From Jerrorl H. Zar, "Biostatistical Analysis"(example 12.6),
    # Xf=10.68, 0.005 < p < 0.01:
    # Probability from this example is inexact
    # using Chisquare approximation of Friedman Chisquare.
    x3 = [array([7.0, 9.9, 8.5, 5.1, 10.3]),
          array([5.3, 5.7, 4.7, 3.5, 7.7]),
          array([4.9, 7.6, 5.5, 2.8, 8.4]),
          array([8.8, 8.9, 8.1, 3.3, 9.1])]
    ref3 = (10.68, 0.0135882729582176)

    @pytest.mark.parametrize("dtype", [None, "float32", "float64"])
    @pytest.mark.parametrize("data, ref", [(x1, ref1), (x2, ref2), (x3, ref3)])
    def test_against_references(self, dtype, data, ref, xp):
        if is_numpy(xp) and xp.__version__ < "2.0" and dtype=='float32':
            pytest.skip("NumPy doesn't preserve dtype pre-NEP 50.")
        dtype = dtype if dtype is None else getattr(xp, dtype)
        data = [xp.asarray(array.tolist(), dtype=dtype) for array in data]
        res = stats.friedmanchisquare(*data)
        xp_assert_close(res.statistic, xp.asarray(ref[0], dtype=dtype))
        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(*self.x3[:2])


class TestKSTest:
    """Tests kstest and ks_1samp agree with K-S various sizes, alternatives, modes."""

    def _testOne(self, x, alternative, expected_statistic, expected_prob,
                 mode='auto', decimal=14):
        result = stats.kstest(x, 'norm', alternative=alternative, mode=mode)
        expected = np.array([expected_statistic, expected_prob])
        assert_array_almost_equal(np.array(result), expected, decimal=decimal)

    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, stats.norm.cdf,
                                      alternative=alternative, mode=mode)
        assert_array_almost_equal(np.array(result), result_1samp, decimal=decimal)

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

    def test_agree_with_ks_1samp(self):

Review comment (suggestion):
Here the test has the `xp` fixture but the same array type is always used

Code after:

    # From Jerrorl H. Zar, "Biostatistical Analysis"(example 12.6),
    # Xf=10.68, 0.005 < p < 0.01:
    # Probability from this example is inexact
    # using Chisquare approximation of Friedman Chisquare.
    x3 = [[7.0, 9.9, 8.5, 5.1, 10.3],
          [5.3, 5.7, 4.7, 3.5, 7.7],
          [4.9, 7.6, 5.5, 2.8, 8.4],
          [8.8, 8.9, 8.1, 3.3, 9.1]]
    ref3 = (10.68, 0.0135882729582176)

    @pytest.mark.parametrize("dtype", [None, "float32", "float64"])
    @pytest.mark.parametrize("data, ref", [(x1, ref1), (x2, ref2), (x3, ref3)])
    def test_against_references(self, dtype, data, ref, xp):
        if is_numpy(xp) and xp.__version__ < "2.0" and dtype=='float32':
            pytest.skip("NumPy doesn't preserve dtype pre-NEP 50.")
        dtype = dtype if dtype is None else getattr(xp, dtype)
        data = [xp.asarray(array, dtype=dtype) for array in data]
        res = stats.friedmanchisquare(*data)
        xp_assert_close(res.statistic, xp.asarray(ref[0], dtype=dtype))
        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]))


class TestKSTest:
    """Tests kstest and ks_1samp agree with K-S various sizes, alternatives, modes."""

    def _testOne(self, x, alternative, expected_statistic, expected_prob,
                 mode='auto', decimal=14):
        result = stats.kstest(x, 'norm', alternative=alternative, mode=mode)
        expected = np.array([expected_statistic, expected_prob])
        assert_array_almost_equal(np.array(result), expected, decimal=decimal)

    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, stats.norm.cdf,
                                      alternative=alternative, mode=mode)
        assert_array_almost_equal(np.array(result), result_1samp, decimal=decimal)

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

    def test_agree_with_ks_1samp(self):