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

Code before:
        assert_raises(ValueError, stats.boxcox_normplot, self.x, 1, 0)
        # `x` can not contain negative values
        assert_raises(ValueError, stats.boxcox_normplot, [-1, 1], 0, 1)

    def test_empty(self):
        assert_(stats.boxcox_normplot([], 0, 1).size == 0)


@make_xp_test_case(stats.yeojohnson_llf)
class TestYeojohnson_llf:

    def test_array_like(self):
        # array_like not applicable with SCIPY_ARRAY_API=1
        x = stats.norm.rvs(size=100, loc=0, random_state=54321)
        lmbda = 1
        llf = stats.yeojohnson_llf(lmbda, x)
        llf2 = stats.yeojohnson_llf(lmbda, list(x))
        assert_allclose(llf, llf2, rtol=1e-12)

    def test_2d_input(self, xp):
        x = stats.norm.rvs(size=100, loc=10, random_state=54321)
        x = xp.asarray(x)
        lmbda = 1
        ref = stats.yeojohnson_llf(lmbda, x)
        res = stats.yeojohnson_llf(lmbda, xp.stack([x, x]).T)
        assert_allclose(res, ref, rtol=1e-12)

    def test_empty(self, xp):
        message = "One or more sample arguments is too small..."
        with eager_warns(SmallSampleWarning, match=message, xp=xp):
            assert xp.isnan(stats.yeojohnson_llf(1, xp.asarray([])))


class TestYeojohnson:

    def test_fixed_lmbda(self):
        rng = np.random.RandomState(12345)

        # Test positive input
        x = _old_loggamma_rvs(5, size=50, random_state=rng) + 5
        assert np.all(x > 0)
        xt = stats.yeojohnson(x, lmbda=1)
        assert_allclose(xt, x)
        xt = stats.yeojohnson(x, lmbda=-1)
        assert_allclose(xt, 1 - 1 / (x + 1))
        xt = stats.yeojohnson(x, lmbda=0)
        assert_allclose(xt, np.log(x + 1))
        xt = stats.yeojohnson(x, lmbda=1)
        assert_allclose(xt, x)

        # Test negative input

Review comment (suggestion):
```suggestion
        xp_assert_close(res, xp.stack((ref, ref)), rtol=1e-12)
```

Code after:
        assert_raises(ValueError, stats.boxcox_normplot, self.x, 1, 0)
        # `x` can not contain negative values
        assert_raises(ValueError, stats.boxcox_normplot, [-1, 1], 0, 1)

    def test_empty(self):
        assert_(stats.boxcox_normplot([], 0, 1).size == 0)


@make_xp_test_case(stats.yeojohnson_llf)
class TestYeojohnson_llf:

    def test_array_like(self):
        # array_like not applicable with SCIPY_ARRAY_API=1
        x = stats.norm.rvs(size=100, loc=0, random_state=54321)
        lmbda = 1
        llf = stats.yeojohnson_llf(lmbda, x)
        llf2 = stats.yeojohnson_llf(lmbda, list(x))
        assert_allclose(llf, llf2, rtol=1e-12)

    def test_2d_input(self, xp):
        x = stats.norm.rvs(size=100, loc=10, random_state=54321)
        x = xp.asarray(x)
        lmbda = 1
        ref = stats.yeojohnson_llf(lmbda, x)
        res = stats.yeojohnson_llf(lmbda, xp.stack([x, x]).T)
        xp_assert_close(res, xp.stack((ref, ref)), rtol=1e-12)

    def test_empty(self, xp):
        message = "One or more sample arguments is too small..."
        with eager_warns(SmallSampleWarning, match=message, xp=xp):
            assert xp.isnan(stats.yeojohnson_llf(1, xp.asarray([])))


class TestYeojohnson:

    def test_fixed_lmbda(self):
        rng = np.random.RandomState(12345)

        # Test positive input
        x = _old_loggamma_rvs(5, size=50, random_state=rng) + 5
        assert np.all(x > 0)
        xt = stats.yeojohnson(x, lmbda=1)
        assert_allclose(xt, x)
        xt = stats.yeojohnson(x, lmbda=-1)
        assert_allclose(xt, 1 - 1 / (x + 1))
        xt = stats.yeojohnson(x, lmbda=0)
        assert_allclose(xt, np.log(x + 1))
        xt = stats.yeojohnson(x, lmbda=1)
        assert_allclose(xt, x)

        # Test negative input