示例#1
0
class TestInversion(with_metaclass(DecoratorMeta, object)):
    decorators = [(dec.slow, None), (check_version, (sympy, '0.7')),
                  (check_version, (mp, '0.19'))]

    def test_log(self):
        with mp.workdps(30):
            logcoeffs = mp.taylor(lambda x: mp.log(1 + x), 0, 10)
            expcoeffs = mp.taylor(lambda x: mp.exp(x) - 1, 0, 10)
            invlogcoeffs = lagrange_inversion(logcoeffs)
            mp_assert_allclose(invlogcoeffs, expcoeffs)

    def test_sin(self):
        with mp.workdps(30):
            sincoeffs = mp.taylor(mp.sin, 0, 10)
            asincoeffs = mp.taylor(mp.asin, 0, 10)
            invsincoeffs = lagrange_inversion(sincoeffs)
            mp_assert_allclose(invsincoeffs, asincoeffs, atol=1e-30)
示例#2
0
class TestInversion(with_metaclass(DecoratorMeta, object)):
    decorators = [(dec.slow, None),
                  (check_version, (sympy, '0.7')),
                  (check_version, (mp, '0.19'))]

    @dec.knownfailureif(_is_32bit_platform, "rtol only 2e-9, see gh-6938")
    def test_log(self):
        with mp.workdps(30):
            logcoeffs = mp.taylor(lambda x: mp.log(1 + x), 0, 10)
            expcoeffs = mp.taylor(lambda x: mp.exp(x) - 1, 0, 10)
            invlogcoeffs = lagrange_inversion(logcoeffs)
            mp_assert_allclose(invlogcoeffs, expcoeffs)

    @dec.knownfailureif(_is_32bit_platform, "rtol only 1e-15, see gh-6938")
    def test_sin(self):
        with mp.workdps(30):
            sincoeffs = mp.taylor(mp.sin, 0, 10)
            asincoeffs = mp.taylor(mp.asin, 0, 10)
            invsincoeffs = lagrange_inversion(sincoeffs)
            mp_assert_allclose(invsincoeffs, asincoeffs, atol=1e-30)
示例#3
0
class TestCDFlib(with_metaclass(DecoratorMeta, object)):
    decorators = [(dec.slow, None), (check_version, (mpmath, '0.19'))]

    @knownfailure_overridable()
    def test_bdtrik(self):
        _assert_inverts(
            sp.bdtrik,
            _binomial_cdf,
            0,
            [ProbArg(), IntArg(1, 1000), ProbArg()],
            rtol=1e-4)

    def test_bdtrin(self):
        _assert_inverts(
            sp.bdtrin,
            _binomial_cdf,
            1,
            [IntArg(1, 1000), ProbArg(), ProbArg()],
            rtol=1e-4,
            endpt_atol=[None, None, 1e-6])

    def test_btdtria(self):
        _assert_inverts(
            sp.btdtria,
            lambda a, b, x: mpmath.betainc(a, b, x2=x, regularized=True),
            0, [
                ProbArg(),
                Arg(0, 1e3, inclusive_a=False),
                Arg(0, 1, inclusive_a=False, inclusive_b=False)
            ],
            rtol=1e-6)

    def test_btdtrib(self):
        # Use small values of a or mpmath doesn't converge
        _assert_inverts(
            sp.btdtrib,
            lambda a, b, x: mpmath.betainc(a, b, x2=x, regularized=True),
            1, [
                Arg(0, 1e2, inclusive_a=False),
                ProbArg(),
                Arg(0, 1, inclusive_a=False, inclusive_b=False)
            ],
            rtol=1e-7,
            endpt_atol=[None, 1e-20, 1e-20])

    @knownfailure_overridable()
    def test_fdtridfd(self):
        _assert_inverts(
            sp.fdtridfd,
            _f_cdf,
            1, [IntArg(1, 100),
                ProbArg(),
                Arg(0, 100, inclusive_a=False)],
            rtol=1e-7)

    def test_gdtria(self):
        _assert_inverts(
            sp.gdtria,
            lambda a, b, x: mpmath.gammainc(b, b=a * x, regularized=True),
            0, [
                ProbArg(),
                Arg(0, 1e3, inclusive_a=False),
                Arg(0, 1e4, inclusive_a=False)
            ],
            rtol=1e-7,
            endpt_atol=[None, 1e-10, 1e-10])

    def test_gdtrib(self):
        # Use small values of a and x or mpmath doesn't converge
        _assert_inverts(
            sp.gdtrib,
            lambda a, b, x: mpmath.gammainc(b, b=a * x, regularized=True),
            1, [
                Arg(0, 1e2, inclusive_a=False),
                ProbArg(),
                Arg(0, 1e3, inclusive_a=False)
            ],
            rtol=1e-5)

    def test_gdtrix(self):
        _assert_inverts(
            sp.gdtrix,
            lambda a, b, x: mpmath.gammainc(b, b=a * x, regularized=True),
            2, [
                Arg(0, 1e3, inclusive_a=False),
                Arg(0, 1e3, inclusive_a=False),
                ProbArg()
            ],
            rtol=1e-7,
            endpt_atol=[None, 1e-10, 1e-10])

    def test_stdtr(self):
        # Ideally the left endpoint for Arg() should be 0.
        assert_mpmath_equal(
            sp.stdtr,
            _student_t_cdf,
            [IntArg(1, 100), Arg(1e-10, np.inf)],
            rtol=1e-7)

    @knownfailure_overridable()
    def test_stdtridf(self):
        _assert_inverts(sp.stdtridf,
                        _student_t_cdf,
                        0, [ProbArg(), Arg()],
                        rtol=1e-7)

    def test_stdtrit(self):
        _assert_inverts(sp.stdtrit,
                        _student_t_cdf,
                        1, [IntArg(1, 100), ProbArg()],
                        rtol=1e-7,
                        endpt_atol=[None, 1e-10])

    def test_chdtriv(self):
        _assert_inverts(
            sp.chdtriv,
            lambda v, x: mpmath.gammainc(v / 2, b=x / 2, regularized=True),
            0, [ProbArg(), IntArg(1, 100)],
            rtol=1e-4)

    @knownfailure_overridable()
    def test_chndtridf(self):
        # Use a larger atol since mpmath is doing numerical integration
        _assert_inverts(sp.chndtridf,
                        _noncentral_chi_cdf,
                        1, [
                            Arg(0, 100, inclusive_a=False),
                            ProbArg(),
                            Arg(0, 100, inclusive_a=False)
                        ],
                        n=1000,
                        rtol=1e-4,
                        atol=1e-15)

    @knownfailure_overridable()
    def test_chndtrinc(self):
        # Use a larger atol since mpmath is doing numerical integration
        _assert_inverts(
            sp.chndtrinc,
            _noncentral_chi_cdf,
            2, [Arg(0, 100, inclusive_a=False),
                IntArg(1, 100),
                ProbArg()],
            n=1000,
            rtol=1e-4,
            atol=1e-15)

    def test_chndtrix(self):
        # Use a larger atol since mpmath is doing numerical integration
        _assert_inverts(
            sp.chndtrix,
            _noncentral_chi_cdf,
            0, [ProbArg(),
                IntArg(1, 100),
                Arg(0, 100, inclusive_a=False)],
            n=1000,
            rtol=1e-4,
            atol=1e-15,
            endpt_atol=[1e-6, None, None])

    def test_tklmbda_zero_shape(self):
        # When lmbda = 0 the CDF has a simple closed form
        one = mpmath.mpf(1)
        assert_mpmath_equal(lambda x: sp.tklmbda(x, 0),
                            lambda x: one / (mpmath.exp(-x) + one), [Arg()],
                            rtol=1e-7)

    def test_tklmbda_neg_shape(self):
        _assert_inverts(
            sp.tklmbda,
            _tukey_lmbda_quantile,
            0, [ProbArg(), Arg(-np.inf, 0, inclusive_b=False)],
            spfunc_first=False,
            rtol=1e-5,
            endpt_atol=[1e-9, None])

    @knownfailure_overridable()
    def test_tklmbda_pos_shape(self):
        _assert_inverts(sp.tklmbda,
                        _tukey_lmbda_quantile,
                        0,
                        [ProbArg(), Arg(0, 100, inclusive_a=False)],
                        spfunc_first=False,
                        rtol=1e-5)