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

Code before:
        xp_assert_close(stat, xp.asarray(n, dtype=xp.asarray(1.).dtype), rtol=1e-13)
        # check that attributes are identical to unpacked outputs - see gh-18368
        xp_assert_equal(res.statistic, stat)
        xp_assert_equal(res.pvalue, p)


@make_xp_test_case(stats.friedmanchisquare)
class TestFriedmanChiSquare:
    # verified with matlab and R
    # From Demsar "Statistical Comparisons of Classifiers over Multiple Data Sets"
    # 2006, Xf=9.28 (no tie handling, tie corrected Xf >=9.28)
    x1 = [array([0.763, 0.599, 0.954, 0.628, 0.882, 0.936, 0.661, 0.583,
                 0.775, 1.0, 0.94, 0.619, 0.972, 0.957]),
          array([0.768, 0.591, 0.971, 0.661, 0.888, 0.931, 0.668, 0.583,
                 0.838, 1.0, 0.962, 0.666, 0.981, 0.978]),
          array([0.771, 0.590, 0.968, 0.654, 0.886, 0.916, 0.609, 0.563,
                 0.866, 1.0, 0.965, 0.614, 0.9751, 0.946]),
          array([0.798, 0.569, 0.967, 0.657, 0.898, 0.931, 0.685, 0.625,
                 0.875, 1.0, 0.962, 0.669, 0.975, 0.970])]
    ref1 = (10.2283464566929, 0.0167215803284414)

    # From "Bioestadistica para las ciencias de la salud" Xf=18.95 p<0.001:
    x2 = [array([4, 3, 5, 3, 5, 3, 2, 5, 4, 4, 4, 3]),
          array([2, 2, 1, 2, 3, 1, 2, 3, 2, 1, 1, 3]),
          array([2, 4, 3, 3, 4, 3, 3, 4, 4, 1, 2, 1]),
          array([3, 5, 4, 3, 4, 4, 3, 3, 3, 4, 4, 4])]
    ref2 = (18.9428571428571, 0.000280938375189499)

    # 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"

Review comment (suggestion):
Nit, can we just make this a list of lists since the first thing that happens to the arrays is to call `.tolist()` on them

Code after:
        xp_assert_close(stat, xp.asarray(n, dtype=xp.asarray(1.).dtype), rtol=1e-13)
        # check that attributes are identical to unpacked outputs - see gh-18368
        xp_assert_equal(res.statistic, stat)
        xp_assert_equal(res.pvalue, p)


@make_xp_test_case(stats.friedmanchisquare)
class TestFriedmanChiSquare:
    # verified with matlab and R
    # From Demsar "Statistical Comparisons of Classifiers over Multiple Data Sets"
    # 2006, Xf=9.28 (no tie handling, tie corrected Xf >=9.28)
    x1 = [[0.763, 0.599, 0.954, 0.628, 0.882, 0.936, 0.661,
           0.583, 0.775, 1.0, 0.94, 0.619, 0.972, 0.957],
          [0.768, 0.591, 0.971, 0.661, 0.888, 0.931, 0.668,
           0.583, 0.838, 1.0, 0.962, 0.666, 0.981, 0.978],
          [0.771, 0.590, 0.968, 0.654, 0.886, 0.916, 0.609,
           0.563, 0.866, 1.0, 0.965, 0.614, 0.9751, 0.946],
          [0.798, 0.569, 0.967, 0.657, 0.898, 0.931, 0.685,
           0.625, 0.875, 1.0, 0.962, 0.669, 0.975, 0.970]]
    ref1 = (10.2283464566929, 0.0167215803284414)

    # From "Bioestadistica para las ciencias de la salud" Xf=18.95 p<0.001:
    x2 = [[4, 3, 5, 3, 5, 3, 2, 5, 4, 4, 4, 3],
          [2, 2, 1, 2, 3, 1, 2, 3, 2, 1, 1, 3],
          [2, 4, 3, 3, 4, 3, 3, 4, 4, 1, 2, 1],
          [3, 5, 4, 3, 4, 4, 3, 3, 3, 4, 4, 4]]
    ref2 = (18.9428571428571, 0.000280938375189499)

    # 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"