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

Code before:
        DATA myData;
        INPUT X Y;
        CARDS;
        1 0
        1 1
        1 2
        1 3
        1 4
        2 0
        2 1
        2 4
        2 9
        2 16
        ods graphics on;
        proc npar1way mood data=myData ;
           class X;
            ods output  MoodTest=mt;
        proc contents data=mt;
        proc print data=mt;
          format     Prob1 17.16 Prob2 17.16 Statistic 17.16 Z 17.16 ;
            title "Mood Two-Sample Test";
        proc print data=myData;
            title "Data for above results";
          run;
        """
        dtype = xp.float64
        x, y = xp.asarray(x, dtype=dtype), xp.asarray(y, dtype=dtype)
        statistic, pvalue = stats.mood(x, y, alternative=alternative)
        xp_assert_close(statistic, xp.asarray(stat_expect, dtype=dtype), atol=1e-16)
        xp_assert_close(pvalue, xp.asarray(p_expect, dtype=dtype), atol=1e-16)

    @pytest.mark.parametrize("dtype", [None, 'float32', 'float64'])
    @pytest.mark.parametrize("alternative, expected",
                             [('two-sided', (1.019938533549930,
                                             .3077576129778760)),
                              ('less', (1.019938533549930,
                                        1 - .1538788064889380)),
                              ('greater', (1.019938533549930,
                                           .1538788064889380))])
    def test_against_SAS_2(self, dtype, alternative, expected, xp):
        # Code to run in SAS in above function
        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)
        x = [111, 107, 100, 99, 102, 106, 109, 108, 104, 99,
             101, 96, 97, 102, 107, 113, 116, 113, 110, 98]
        y = [107, 108, 106, 98, 105, 103, 110, 105, 104, 100,
             96, 108, 103, 104, 114, 114, 113, 108, 106, 99]
        x, y = xp.asarray(x, dtype=dtype), xp.asarray(y, dtype=dtype)
        res = stats.mood(x, y, alternative=alternative)
        xp_assert_close(res.statistic, xp.asarray(expected[0], dtype=dtype))

Review comment (suggestion):
Since you test with "default dtypes" below, I was somewhat curious why this was being pinned to a single dtype here. Maybe this best reflects a match to what the original test did.

FWIW, the test seems to pass with array API locally even if you parametrize this to try "float32" and "float64".

Code after:
        DATA myData;
        INPUT X Y;
        CARDS;
        1 0
        1 1
        1 2
        1 3
        1 4
        2 0
        2 1
        2 4
        2 9
        2 16
        ods graphics on;
        proc npar1way mood data=myData ;
           class X;
            ods output  MoodTest=mt;
        proc contents data=mt;
        proc print data=mt;
          format     Prob1 17.16 Prob2 17.16 Statistic 17.16 Z 17.16 ;
            title "Mood Two-Sample Test";
        proc print data=myData;
            title "Data for above results";
          run;
        """
        x, y = xp.asarray(x.tolist()), xp.asarray(y.tolist())
        statistic, pvalue = stats.mood(x, y, alternative=alternative)
        xp_assert_close(statistic, xp.asarray(stat_expect), atol=1e-16)
        xp_assert_close(pvalue, xp.asarray(p_expect), atol=1e-16)

    @pytest.mark.parametrize("dtype", [None, 'float32', 'float64'])
    @pytest.mark.parametrize("alternative, expected",
                             [('two-sided', (1.019938533549930,
                                             .3077576129778760)),
                              ('less', (1.019938533549930,
                                        1 - .1538788064889380)),
                              ('greater', (1.019938533549930,
                                           .1538788064889380))])
    def test_against_SAS_2(self, dtype, alternative, expected, xp):
        # Code to run in SAS in above function
        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)
        x = [111, 107, 100, 99, 102, 106, 109, 108, 104, 99,
             101, 96, 97, 102, 107, 113, 116, 113, 110, 98]
        y = [107, 108, 106, 98, 105, 103, 110, 105, 104, 100,
             96, 108, 103, 104, 114, 114, 113, 108, 106, 99]
        x, y = xp.asarray(x, dtype=dtype), xp.asarray(y, dtype=dtype)
        res = stats.mood(x, y, alternative=alternative)
        xp_assert_close(res.statistic, xp.asarray(expected[0], dtype=dtype))
        xp_assert_close(res.pvalue, xp.asarray(expected[1], dtype=dtype))