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

Code before:
        pytest.param(980, 38, 1.5, 1.1824454111413493e-291,
                     marks=pytest.mark.xfail(
                        reason="Bug in underlying Boost math implementation")),
        (980, 38, 15, 5.407535300713606e-105)
    ])
    def test_gh19896(self, df, nc, x, expected_cdf):
        # test that gh-19896 is resolved.
        # Originally this was a regression test that used the old Fortran results
        # as a reference. The Fortran results were not accurate, so the reference
        # values were recomputed with mpmath.
        nctdtr_result = sp.nctdtr(df, nc, x)
        assert_allclose(nctdtr_result, expected_cdf, rtol=1e-13, atol=1e-303)

    def test_nctdtr_gh8344(self):
        # test that gh-8344 is resolved.
        df, nc, x = 3000, 3, 0.1
        expected = 0.0018657780826323328
        assert_allclose(sp.nctdtr(df, nc, x), expected, rtol=1e-14)

    @pytest.mark.parametrize(
        "df, nc, x, expected, rtol",
        [[3., 5., -2., 1.5645373999149622e-09, 2e-8],
         [1000., 10., 1., 1.1493552133826623e-19, 1e-13],
         [1e-5, -6., 2., 0.9999999990135003, 1e-13],
         [10., 20., 0.15, 6.426530505957303e-88, 1e-13],
         [1., 1., np.inf, 1.0, 0.0],
         [1., 1., -np.inf, 0.0, 0.0]
        ]
    )
    def test_nctdtr_accuracy(self, df, nc, x, expected, rtol):
        assert_allclose(sp.nctdtr(df, nc, x), expected, rtol=rtol)

    @pytest.mark.parametrize("df, nc, x, expected_cdf", [
        (0.98, 38, 1.5, 2.591995360483094e-97),
        (3000, 3, 0.1, 0.0018657780826323328),
        (0.98, -3.8, 15, 0.9999990264591945),
        (9.8, 38, 15, 2.252076291604796e-09),

    ])
    def test_nctdtrit(self, df, nc, x, expected_cdf):
        assert_allclose(sp.nctdtrit(df, nc, expected_cdf), x, rtol=1e-10)


class TestNoncentralChiSquaredFunctions:

    def test_chndtr_and_inverses_against_wolfram_alpha(self):
        # Each row holds (x, nu, lam, expected_value)
        # These values were computed using Wolfram Alpha with
        #     CDF[NoncentralChiSquareDistribution[nu, lam], x]
        values = np.array([
            [25.00, 20.0, 400, 4.1210655112396197139e-57],

Review comment (suggestion):
It'd be nice to add a comment here saying that this value can be changed back to 5e-9 after we upgrade to boost-math 1.90.0

Code after:
        # revisit when boost1.90 is released,
        # see https://github.com/boostorg/math/issues/1308
        pytest.param(980, 38, 1.5, 1.1824454111413493e-291,
                     marks=pytest.mark.xfail(
                        reason="Bug in underlying Boost math implementation")),
        (980, 38, 15, 5.407535300713606e-105)
    ])
    def test_gh19896(self, df, nc, x, expected_cdf):
        # test that gh-19896 is resolved.
        # Originally this was a regression test that used the old Fortran results
        # as a reference. The Fortran results were not accurate, so the reference
        # values were recomputed with mpmath.
        nctdtr_result = sp.nctdtr(df, nc, x)
        assert_allclose(nctdtr_result, expected_cdf, rtol=1e-13, atol=1e-303)

    def test_nctdtr_gh8344(self):
        # test that gh-8344 is resolved.
        df, nc, x = 3000, 3, 0.1
        expected = 0.0018657780826323328
        assert_allclose(sp.nctdtr(df, nc, x), expected, rtol=1e-14)

    @pytest.mark.parametrize(
        "df, nc, x, expected, rtol",
        # revisit tolerances when boost1.90 is released,
        # see https://github.com/boostorg/math/issues/1308
        [[3., 5., -2., 1.5645373999149622e-09, 2e-8],
         [1000., 10., 1., 1.1493552133826623e-19, 1e-13],
         [1e-5, -6., 2., 0.9999999990135003, 1e-13],
         [10., 20., 0.15, 6.426530505957303e-88, 1e-13],
         [1., 1., np.inf, 1.0, 0.0],
         [1., 1., -np.inf, 0.0, 0.0]
        ]
    )
    def test_nctdtr_accuracy(self, df, nc, x, expected, rtol):
        assert_allclose(sp.nctdtr(df, nc, x), expected, rtol=rtol)

    @pytest.mark.parametrize("df, nc, x, expected_cdf", [
        (0.98, 38, 1.5, 2.591995360483094e-97),
        (3000, 3, 0.1, 0.0018657780826323328),
        (0.98, -3.8, 15, 0.9999990264591945),
        (9.8, 38, 15, 2.252076291604796e-09),

    ])
    def test_nctdtrit(self, df, nc, x, expected_cdf):
        assert_allclose(sp.nctdtrit(df, nc, expected_cdf), x, rtol=1e-10)


class TestNoncentralChiSquaredFunctions:

    def test_chndtr_and_inverses_against_wolfram_alpha(self):
        # Each row holds (x, nu, lam, expected_value)