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

Code before:
        # Check that the parameterization of the frozen distribution is that of
        # the unfrozen distribution at the time of freezing
        rng = np.random.default_rng
        shapes = dict(alpha=1.9, beta=0.1, loc=0.0, scale=1.0)
        unfrozen = stats.levy_stable

        with levy_stable_lock:
            unfrozen.parameterization = "S0"
            frozen = stats.levy_stable(**shapes)
            unfrozen_a = unfrozen.rvs(**shapes, size=10, random_state=rng(329823498))
            frozen_a = frozen.rvs(size=10, random_state=rng(329823498))
            assert_equal(frozen_a, unfrozen_a)

            unfrozen.parameterization = "S1"
            frozen = stats.levy_stable(**shapes)
            unfrozen_b = unfrozen.rvs(**shapes, size=10, random_state=rng(329823498))
            frozen_b = frozen.rvs(size=10, random_state=rng(329823498))
            assert_equal(frozen_b, unfrozen_b)

    @pytest.mark.parametrize(
        "param, alpha, beta, xs, expected",
        [
            (0, 1.6, 1.0, [-15.0, -14.0, -13.0], [-187.1444263, -157.1411663, -130.3526688]),
            (1, 1.6, 1.0, [-15.0, -14.0, -13.0], [-165.0203129, -137.3671717, -112.8223545]),
            (0, 1.4, -1.0, [13.0, 14.0, 15.0], [-367.1943293, -464.7049565, -579.4399286]),
            (1, 1.4, -1.0, [13.0, 14.0, 15.0], [-258.1216497, -334.6139857, -426.0724094]),
        ],
    )
    @pytest.mark.xfail_on_32bit("`bisect` fails to recognize valid bracket")
    def test_logpdf_tail(self, param, alpha, beta, xs, expected):
        # gh-20636
        # Expected values computed from Mathematica. Mathematica command is:
        # Log[PDF[StableDistribution[0, 1.6, 1, 0, 1], -15.0]] // InputForm
        # Test levy_stable.logpdf accuracy in the tails when beta=1 or beta=-1
        # for both S0/S1 parameterizations.
        # Testing logpdf instead of pdf as more compact.
        stats.levy_stable.parameterization = f"S{param}"
        logpdfs = stats.levy_stable(alpha=alpha, beta=beta).logpdf(xs)
        assert_allclose(logpdfs, expected, rtol=1e-5)

class TestArrayArgument:  # test for ticket:992
    def setup_method(self):
        self.rng = np.random.default_rng(7556981556)

    def test_noexception(self):
        rvs = stats.norm.rvs(loc=(np.arange(5)), scale=np.ones(5),
                             size=(10, 5), random_state=self.rng)
        assert_equal(rvs.shape, (10, 5))


class TestDocstring:

Review comment (bug):
This should take care of the lint failure - but please check locally that it passes before committing.
```suggestion
            (0, 1.6, 1, [-15, -14, -13], [-187.1444263, -157.1411663, -130.3526688]),
            (1, 1.6, 1, [-15, -14, -13], [-165.0203129, -137.3671717, -112.8223545]),
            (0, 1.4, -1, [13, 14, 15], [-367.1943293, -464.7049565, -579.4399286]),
            (1, 1.4, -1, [13, 14, 15], [-258.1216497, -334.6139857, -426.0724094]),
```
Assuming the rest of CI has passed by then, it can be committed with `[lint only]` in the message.

Code after:
        # Check that the parameterization of the frozen distribution is that of
        # the unfrozen distribution at the time of freezing
        rng = np.random.default_rng
        shapes = dict(alpha=1.9, beta=0.1, loc=0.0, scale=1.0)
        unfrozen = stats.levy_stable

        with levy_stable_lock:
            unfrozen.parameterization = "S0"
            frozen = stats.levy_stable(**shapes)
            unfrozen_a = unfrozen.rvs(**shapes, size=10, random_state=rng(329823498))
            frozen_a = frozen.rvs(size=10, random_state=rng(329823498))
            assert_equal(frozen_a, unfrozen_a)

            unfrozen.parameterization = "S1"
            frozen = stats.levy_stable(**shapes)
            unfrozen_b = unfrozen.rvs(**shapes, size=10, random_state=rng(329823498))
            frozen_b = frozen.rvs(size=10, random_state=rng(329823498))
            assert_equal(frozen_b, unfrozen_b)

    @pytest.mark.parametrize(
        "param, alpha, beta, xs, expected",
        [
            (0, 1.6, 1, [-15, -14, -13], [-187.1444263, -157.1411663, -130.3526688]),
            (1, 1.6, 1, [-15, -14, -13], [-165.0203129, -137.3671717, -112.8223545]),
            (0, 1.4, -1, [13, 14, 15], [-367.1943293, -464.7049565, -579.4399286]),
            (1, 1.4, -1, [13, 14, 15], [-258.1216497, -334.6139857, -426.0724094]),
        ],
    )
    @pytest.mark.xfail_on_32bit("`bisect` fails to recognize valid bracket")
    def test_logpdf_tail(self, param, alpha, beta, xs, expected):
        # gh-20636
        # Expected values computed from Mathematica. Mathematica command is:
        # Log[PDF[StableDistribution[0, 1.6, 1, 0, 1], -15.0]] // InputForm
        # Test levy_stable.logpdf accuracy in the tails when beta=1 or beta=-1
        # for both S0/S1 parameterizations.
        # Testing logpdf instead of pdf as more compact.
        stats.levy_stable.parameterization = f"S{param}"
        logpdfs = stats.levy_stable(alpha=alpha, beta=beta).logpdf(xs)
        assert_allclose(logpdfs, expected, rtol=1e-5)

class TestArrayArgument:  # test for ticket:992
    def setup_method(self):
        self.rng = np.random.default_rng(7556981556)

    def test_noexception(self):
        rvs = stats.norm.rvs(loc=(np.arange(5)), scale=np.ones(5),
                             size=(10, 5), random_state=self.rng)
        assert_equal(rvs.shape, (10, 5))


class TestDocstring: