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

Code before:
    ts2 = [
           (542, 0.9481266260876332, True, 'exact',
            [[10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
             [1, 8, 4, 7, 6, 5, 9, 3, 2, 10]]),
           (1322, 0.9993113928199309, True, 'exact',
            [[10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
             [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [9, 2, 8, 7, 6, 5, 4, 3, 10, 1],
             [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]),
           (2286, 0.9908688345484833, True, 'exact',
            [[8, 7, 6, 5, 4, 3, 2, 1], [8, 7, 6, 5, 4, 3, 2, 1],
             [8, 7, 6, 5, 4, 3, 2, 1], [8, 7, 6, 5, 4, 3, 2, 1],
             [8, 7, 6, 5, 4, 3, 2, 1], [8, 7, 6, 5, 4, 3, 2, 1],
             [8, 7, 6, 5, 4, 3, 2, 1], [8, 7, 6, 5, 4, 3, 2, 1],
             [8, 7, 6, 5, 4, 3, 2, 1], [1, 3, 5, 6, 4, 7, 2, 8],
             [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8],
             [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8],
             [1, 2, 3, 4, 5, 6, 7, 8]]),
          ]
    # only the first of these appears slow because intermediate data are
    # cached and used on the rest
    @pytest.mark.parametrize("L, p, ranked, method, data", ts2)
    @pytest.mark.slow()
    def test_accuracy2(self, L, p, ranked, method, data):
        res = stats.page_trend_test(data, ranked=ranked, method=method)
        assert_equal(L, res.statistic)
        assert_allclose(p, res.pvalue)
        assert_equal(method, res.method)

    def test_options(self):
        rng = np.random.default_rng(183973867)
        m, n = 10, 20
        predicted_ranks = np.arange(1, n+1)
        perm = rng.permutation(np.arange(n))
        data = rng.random((m, n))
        ranks = stats.rankdata(data, axis=1)
        res1 = stats.page_trend_test(ranks)
        res2 = stats.page_trend_test(ranks, ranked=True)
        res3 = stats.page_trend_test(data, ranked=False)
        res4 = stats.page_trend_test(ranks, predicted_ranks=predicted_ranks)
        res5 = stats.page_trend_test(ranks[:, perm],
                                     predicted_ranks=predicted_ranks[perm])
        assert_equal(res1.statistic, res2.statistic)
        assert_equal(res1.statistic, res3.statistic)
        assert_equal(res1.statistic, res4.statistic)
        assert_equal(res1.statistic, res5.statistic)

    def test_Ames_assay(self):
        # test from _page_trend_test.py [2] page 151; data on page 144
        data = [[101, 117, 111], [91, 90, 107], [103, 133, 121],
                [136, 140, 144], [190, 161, 201], [146, 120, 116]]
        data = np.array(data).T

Review comment (suggestion):
These and the test below are pretty verbose, but a lot of the bulk is due to the fact that there are five separate outputs with two different dtypes, so we can't very easily loop over them. Still, I'll work on condensing them where I can.

Code after:
    @pytest.mark.parametrize('alternative', ['less', 'greater', 'two-sided'])
    @pytest.mark.parametrize('axis', [0, 1])
    def test_multidimensional(self, alternative, axis):
        rng = np.random.default_rng(85468924398205602)
        x = rng.random(size=(2, 20))
        p = rng.random(size=(1, 3,))
        q = p + rng.random(size=(2, 1))*0.01

        ref = quantile_test_reference(x, q, p, alternative=alternative)
        ref_statistic, ref_pvalue, ref_low, ref_high = ref

        if axis == 0:
            x, p, q = x.T, p.T, q.T
        res = stats.quantile_test(x, q=q, p=p, alternative=alternative, axis=axis)
        res_statistic, res_pvalue = res.statistic, res.pvalue
        res_low, res_high = res.confidence_interval()
        if axis == 0:
            res_statistic, res_pvalue = res_statistic.T, res_pvalue.T
            res_low, res_high = res_low.T, res_high.T

        assert_allclose(res_statistic, ref_statistic)
        assert_allclose(res_pvalue, ref_pvalue)
        assert_allclose(res_low, res_low)
        assert_allclose(res_low, ref_low)

    def test_zero_size(self):
        rng = np.random.default_rng(883771738488451943)
        x_shape = (2, 50)
        qp_shape = (2, 10)
        x = rng.random(x_shape)
        q = rng.random(qp_shape)
        p = q + rng.random(qp_shape) * 1e-2

        # case 1: p/q is size zero.
        qp_zero = np.empty((0, *qp_shape))
        out = qp_zero
        res = stats.quantile_test(x, q=qp_zero, p=qp_zero, axis=-1)
        ci = res.confidence_interval()
        # different default int on different platforms
        xp_assert_equal(res.statistic, np.asarray(out), check_dtype=False)
        xp_assert_equal(res.statistic_type, np.asarray(out), check_dtype=False)
        assert np.isdtype(res.statistic.dtype, "integral")
        assert np.isdtype(res.statistic_type.dtype, "integral")
        xp_assert_equal(res.pvalue, out)
        xp_assert_equal(ci.low, out)
        xp_assert_equal(ci.high, out)

        # case 2: x is size zero with nonzero length along axis.
        x_zero = np.empty((0, *x_shape))
        out = np.empty((0, *qp_shape))
        res = stats.quantile_test(x_zero, q=q, p=p, axis=-1)