Repository: scipy/scipy
File: scipy/special/tests/test_cdflib.py (Python)

Code before:
    @pytest.mark.parametrize(
        "x, df, nc, expected",
        [(1, 0, 1, np.nan),
         (1, 0, np.inf, np.nan),
         (1, np.inf, 1, np.nan),
         (1, np.inf, np.inf, 0),
         (1, 1, np.inf, 0),
         (np.inf, 0, 1, np.nan),
         (np.inf, 1, np.inf, np.nan),
         (np.inf, 1, 1, 1),
         (np.inf, np.inf, np.inf, np.nan)]
    )
    def test_chndtr_edge_cases(self, x, df, nc, expected):
        assert_allclose(sp.chndtr(x, df, nc), expected, rtol=1e-15)


@pytest.mark.parametrize("x", [0.1, 100])
def test_chdtriv_p_equals_1_returns_0(x):
    assert sp.chdtriv(1, x) == 0


class TestPdtrik:
    @pytest.mark.parametrize("p, m, expected",
                             [(0, 0.5, 0),
                              (0.5, 0, 0),
                              (0, 0, 0)])
    def test_edge_cases(self, p, m, expected):
        assert sp.pdtrik(p, m) == expected

    @pytest.mark.parametrize("m", (0.1, 1, 10))
    def test_p_equals_1_returns_nan(self, m):
        assert np.isnan(sp.pdtrik(1, m))

    def test_small_probabilities(self):
        # Edge case: m = 0 or very small.
        k = sp.pdtrik([[0], [0.25], [0.95]], [0, 1e-20, 1e-6])
        assert_array_equal(k, np.zeros((3, 3)))

    def test_roundtrip_against_pdtr(self):
        m = [10, 50, 500]
        k = 5
        p = sp.pdtr(k, m)
        assert_allclose(sp.pdtrik(p, m), k, atol=1.5e-7, rtol=0)

    @pytest.mark.parametrize("p, m, k",
                             [(1.8976107553682285e-40, 100, 2),
                              (0.48670120172085135, 100, 99),
                              (8.30383406699052e-69, 1000, 500),
                              (2.252837804125894e-227, 100_000, 90_000)]
                            )
    def test_accuracy(self, p, m, k):

Review comment (suggestion):
The case p=0, m=0 is covered in `test_small_probabilities()` below.
```suggestion
                              (0.5, 0, 0)])
```

Code after:
    @pytest.mark.parametrize(
        "x, df, nc, expected",
        [(1, 0, 1, np.nan),
         (1, 0, np.inf, np.nan),
         (1, np.inf, 1, np.nan),
         (1, np.inf, np.inf, 0),
         (1, 1, np.inf, 0),
         (np.inf, 0, 1, np.nan),
         (np.inf, 1, np.inf, np.nan),
         (np.inf, 1, 1, 1),
         (np.inf, np.inf, np.inf, np.nan)]
    )
    def test_chndtr_edge_cases(self, x, df, nc, expected):
        assert_allclose(sp.chndtr(x, df, nc), expected, rtol=1e-15)


@pytest.mark.parametrize("x", [0.1, 100])
def test_chdtriv_p_equals_1_returns_0(x):
    assert sp.chdtriv(1, x) == 0


class TestPdtrik:
    @pytest.mark.parametrize("p, m, expected",
                             [(0, 0.5, 0),
                              (0.5, 0, 0)])
    def test_edge_cases(self, p, m, expected):
        assert sp.pdtrik(p, m) == expected

    @pytest.mark.parametrize("m", (0.1, 1, 10))
    def test_p_equals_1_returns_nan(self, m):
        assert np.isnan(sp.pdtrik(1, m))

    def test_small_probabilities(self):
        # Edge case: m = 0 or very small.
        k = sp.pdtrik([[0], [0.25], [0.95]], [0, 1e-20, 1e-6])
        assert_array_equal(k, np.zeros((3, 3)))

    def test_roundtrip_against_pdtr(self):
        m = [10, 50, 500]
        k = 5
        p = sp.pdtr(k, m)
        assert_allclose(sp.pdtrik(p, m), k, rtol=1e-15)

    @pytest.mark.parametrize("p, m, k",
                             [(1.8976107553682285e-40, 100, 2),
                              (0.48670120172085135, 100, 99),
                              (8.30383406699052e-69, 1000, 500),
                              (2.252837804125894e-227, 100_000, 90_000)])
    def test_accuracy(self, p, m, k):
        # Reference values for p were computed with mpmath using
        # mp.gammainc(k+1, a=m, regularized=True)