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

Code before:
            stats.binomtest(3, n=10, p=0.1, alternative='ekki')

    def test_alias(self):
        res = stats.binomtest(3, n=10, p=0.1)
        assert_equal(res.proportion_estimate, res.statistic)

    @pytest.mark.skipif(sys.maxsize <= 2**32, reason="32-bit does not overflow")
    def test_boost_overflow_raises(self):
        # Boost.Math error policy should raise exceptions in Python
        with pytest.raises(OverflowError, match='Error in function...'):
            stats.binomtest(5, 6, p=sys.float_info.min)


@make_xp_test_case(stats.fligner)
class TestFligner:

    def _perturb(self, g, rng=124987234782812):
        # g arrays have ties to which statistic is very sensitive; break them
        rng = np.random.default_rng(rng)
        return (np.asarray(g) + 1e-10 * rng.standard_normal(len(g))).tolist()

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_data(self, dtype, xp):
        # numbers from R: fligner.test in package stats
        dtype = xp_default_dtype(xp) if dtype is None else getattr(xp, dtype)
        x1 = xp.arange(5, dtype=dtype)
        res = stats.fligner(x1, x1**2)
        ref = (xp.asarray(3.2282229927203536, dtype=dtype),
               xp.asarray(0.072379187848207877, dtype=dtype))
        xp_assert_close(res[0], ref[0])
        xp_assert_close(res[1], ref[1])

    def test_trimmed1(self, xp):
        # Perturb input to break ties in the transformed data
        # See https://github.com/scipy/scipy/pull/8042 for more details
        rng = np.random.default_rng(9952379681)
        g1_ = xp.asarray(self._perturb(g1, rng=rng))
        g2_ = xp.asarray(self._perturb(g2, rng=rng))
        g3_ = xp.asarray(self._perturb(g3, rng=rng))
        # Test that center='trimmed' gives the same result as center='mean'
        # when proportiontocut=0.
        Xsq1, pval1 = stats.fligner(g1_, g2_, g3_, center='mean')
        Xsq2, pval2 = stats.fligner(g1_, g2_, g3_, center='trimmed',
                                    proportiontocut=0.0)
        xp_assert_close(Xsq1, Xsq2)
        xp_assert_close(pval1, pval2)

    @pytest.mark.skip_xp_backends(np_only=True,
                                  reason="inconsistent tie-breaking across backends")
    def test_trimmed_nonregression(self, xp):
        # This is a non-regression test

Review comment (suggestion):
```suggestion
        if is_numpy(xp) and dtype == 'float32' and xp.__version__ < "2":
            pytest.skip("Scalar dtypes only respected after NEP 50.")
        # numbers from R: fligner.test in package stats
```

Code after:
            stats.binomtest(3, n=10, p=0.1, alternative='ekki')

    def test_alias(self):
        res = stats.binomtest(3, n=10, p=0.1)
        assert_equal(res.proportion_estimate, res.statistic)

    @pytest.mark.skipif(sys.maxsize <= 2**32, reason="32-bit does not overflow")
    def test_boost_overflow_raises(self):
        # Boost.Math error policy should raise exceptions in Python
        with pytest.raises(OverflowError, match='Error in function...'):
            stats.binomtest(5, 6, p=sys.float_info.min)


@make_xp_test_case(stats.fligner)
class TestFligner:

    def _perturb(self, g, rng=124987234782812):
        # g arrays have ties to which statistic is very sensitive; break them
        rng = np.random.default_rng(rng)
        return (np.asarray(g) + 1e-10 * rng.standard_normal(len(g))).tolist()

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_data(self, dtype, xp):
        if is_numpy(xp) and dtype == 'float32' and xp.__version__ < "2":
            pytest.skip("Scalar dtypes only respected after NEP 50.")
        # numbers from R: fligner.test in package stats
        dtype = xp_default_dtype(xp) if dtype is None else getattr(xp, dtype)
        x1 = xp.arange(5, dtype=dtype)
        res = stats.fligner(x1, x1**2)
        ref = (xp.asarray(3.2282229927203536, dtype=dtype),
               xp.asarray(0.072379187848207877, dtype=dtype))
        xp_assert_close(res[0], ref[0])
        xp_assert_close(res[1], ref[1])

    def test_trimmed1(self, xp):
        # Perturb input to break ties in the transformed data
        # See https://github.com/scipy/scipy/pull/8042 for more details
        rng = np.random.default_rng(9952379681)
        g1_ = xp.asarray(self._perturb(g1, rng=rng))
        g2_ = xp.asarray(self._perturb(g2, rng=rng))
        g3_ = xp.asarray(self._perturb(g3, rng=rng))
        # Test that center='trimmed' gives the same result as center='mean'
        # when proportiontocut=0.
        Xsq1, pval1 = stats.fligner(g1_, g2_, g3_, center='mean')
        Xsq2, pval2 = stats.fligner(g1_, g2_, g3_, center='trimmed',
                                    proportiontocut=0.0)
        xp_assert_close(Xsq1, Xsq2)
        xp_assert_close(pval1, pval2)

    @pytest.mark.skip_xp_backends(np_only=True,
                                  reason="inconsistent tie-breaking across backends")