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

Code before:
        xp_assert_equal(res.statistic, NaN)
        xp_assert_equal(res.pvalue, NaN)

    def test_negative_pvalue_gh21152(self, xp):
        a = xp.asarray([10.1, 10.2, 10.3, 10.4], dtype=xp.float32)
        b = xp.asarray([10.15, 10.25, 10.35, 10.45], dtype=xp.float32)
        c = xp.asarray([10.05, 10.15, 10.25, 10.35], dtype=xp.float32)
        res = stats.bartlett(a, b, c)
        assert xp.all(res.statistic >= 0)


class TestLevene:

    def test_data(self):
        # https://www.itl.nist.gov/div898/handbook/eda/section3/eda35a.htm
        args = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10]
        W, pval = stats.levene(*args)
        assert_almost_equal(W, 1.7059176930008939, 7)
        assert_almost_equal(pval, 0.0990829755522, 7)

    def test_mean(self):
        # numbers from R: leveneTest in package car
        args = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10]
        W, pval = stats.levene(*args, center="mean")
        assert_almost_equal(W, 2.15945985647285, 7)
        assert_almost_equal(pval, 0.032236826559783, 7)

    def test_trimmed1(self):
        # Test that center='trimmed' gives the same result as center='mean'
        # when proportiontocut=0.
        W1, pval1 = stats.levene(g1, g2, g3, center='mean')
        W2, pval2 = stats.levene(g1, g2, g3, center='trimmed',
                                 proportiontocut=0.0)
        assert_almost_equal(W1, W2)
        assert_almost_equal(pval1, pval2)

    def test_trimmed2(self):
        # numbers from R: leveneTest in package car
        args = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10]
        W, pval = stats.levene(*args, center="trimmed", proportiontocut=0.25)
        assert_almost_equal(W, 2.07712845686874, 7)
        assert_almost_equal(pval, 0.0397269688035377, 7)

    def test_equal_mean_median(self):
        x = np.linspace(-1, 1, 21)
        np.random.seed(1234)
        x2 = np.random.permutation(x)
        y = x**3
        W1, pval1 = stats.levene(x, y, center='mean')
        W2, pval2 = stats.levene(x2, y, center='median')
        assert_almost_equal(W1, W2)

Review comment (suggestion):
```suggestion
        assert_allclose(W, 2.15945985647285, rtol=5e-14)
        assert_allclose(pval, 0.032236826559783, rtol=5e-14)
```

Code after:
        xp_assert_equal(res.statistic, NaN)
        xp_assert_equal(res.pvalue, NaN)

    def test_negative_pvalue_gh21152(self, xp):
        a = xp.asarray([10.1, 10.2, 10.3, 10.4], dtype=xp.float32)
        b = xp.asarray([10.15, 10.25, 10.35, 10.45], dtype=xp.float32)
        c = xp.asarray([10.05, 10.15, 10.25, 10.35], dtype=xp.float32)
        res = stats.bartlett(a, b, c)
        assert xp.all(res.statistic >= 0)


class TestLevene:

    def test_data(self):
        # https://www.itl.nist.gov/div898/handbook/eda/section3/eda35a.htm
        args = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10]
        W, pval = stats.levene(*args)
        assert_almost_equal(W, 1.7059176930008939, 7)
        assert_almost_equal(pval, 0.0990829755522, 7)

    def test_mean(self):
        # numbers from R: leveneTest in package car
        args = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10]
        W, pval = stats.levene(*args, center="mean")
        assert_allclose(W, 2.15945985647285, rtol=5e-14)
        assert_allclose(pval, 0.032236826559783, rtol=5e-14)

    def test_trimmed1(self):
        # Test that center='trimmed' gives the same result as center='mean'
        # when proportiontocut=0.
        W1, pval1 = stats.levene(g1, g2, g3, center='mean')
        W2, pval2 = stats.levene(g1, g2, g3, center='trimmed',
                                 proportiontocut=0.0)
        assert_almost_equal(W1, W2)
        assert_almost_equal(pval1, pval2)

    def test_trimmed2(self):
        # numbers from R: leveneTest in package car
        args = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10]
        W, pval = stats.levene(*args, center="trimmed", proportiontocut=0.25)
        assert_allclose(W, 2.07712845686874, rtol=5e-14)
        assert_allclose(pval, 0.0397269688035377, rtol=5e-14)

    def test_equal_mean_median(self):
        x = np.linspace(-1, 1, 21)
        np.random.seed(1234)
        x2 = np.random.permutation(x)
        y = x**3
        W1, pval1 = stats.levene(x, y, center='mean')
        W2, pval2 = stats.levene(x2, y, center='median')
        assert_almost_equal(W1, W2)